Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Input field is not fitting well within a div

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;
}

input with 100% width and margin but not fitting well

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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/

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading