I can’t understand why this input field with margin (at all sides) is not fitting well within that div. I was hoping to see the input with the right margin being shown.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="index.css">
<title>CSS Tests</title>
</head>
<body>
<div class="div2">
<input type="Foobar">
</div>
</body>
</html>
body {
margin: 0;
}
.div2 {
width: 100%;
height: 100px;
background-color:indianred;
}
.div2 input {
width: 100%;
margin: 10px;
box-sizing: border-box;
}
>Solution :
Width 100% will set the width to the available space, which in this case is the width of the parent div. Try setting the width to width: calc(100% - 20px); (subtract the margin out).
here is a fiddle link to demonstrate https://jsfiddle.net/2vcokm6x/
