-3

I have a parent and child div, and when I add margin to the child div, it expands the parent div. How can I stop this from happening. You can see it happen here because the scroll bars appear.

I had a look at this similar question but I it doesn't seem like that answer does anything.

html, body {
  height: 100%;
  width: 100%;
  margin: 0px;
}

.container {
  background-color: orange;
  height: 100%;
  width: 100%;
}

.child {
  background-color: green;
  height: 100%;
  width: 200px;
  margin: 10px;
}
<div class="container">
  <div class="child"></div>
</div>

enter image description here

I would like this to be the final output. With the Orange div being 100% height

Frank
  • 2,109
  • 7
  • 25
  • 48

1 Answers1

1

When you want the margins to collapse on the child div, give overflow: hidden to the parent div.

* {
  box-sizing: border-box;
}

html, body {
  height: 100%;
  width: 100%;
  margin: 0px;
}

.container {
  background-color: orange;
  height: 100%;
  width: 100%;
  overflow: hidden;
  padding: 10px;
}

.child {
  background-color: green;
  height: 100%;
  width: 200px;
}
<div class="container">
  <div class="child"></div>
</div>

Now there's no scrollbars. I have used padding on the parent instead of margin. Please don't use calc() - not a good idea.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252