Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does inner DIV spill out of outer DIV?

Tags:

html

css

I have been long away from HTML and CSS, can't find the solution to this simple problem.

I have one div inside the other. Outer Black and inner orange.

enter image description here

My HTML and CSS is :

#outer {
  position: fixed;
  width: 30%;
  height: 30%;
  background-color: black;
}
#inner {
  width: 100%;
  height: 100%;
  margin: 5px;
  background-color: orange;
}
<div id="outer">
  <div id="inner">
  </div>
</div>

Why is my inner DIV spilling out of the outer ? How do I fix it without giving fixed dimensions?

like image 999
user3769778 Avatar asked May 27 '16 16:05

user3769778


People also ask

What is the difference between the outer Div and content Div?

The outer div has a width and height of 100%, the content div in which all of this is placed, on the other hand, has a width of some 770px. Maybe it has to do with that?

Why does my Div push outside of the Div?

As you can see, its too big for the DIV, and so pushes outside it (this is the same for both Firefox and IE). This is a relevant example bit of HTML:

Does the Div display when there’s no image?

The DIV displays fine when there’s no image, but as soon as there is I get the following: As you can see, its too big for the DIV, and so pushes outside it (this is the same for both Firefox and IE).


2 Answers

Because of the margin - the width is 100% PLUS the margin. To avoid that, write width: calc(100% - 10px); (= twice the margin.) Same for height.

#outer {
  position: fixed;
  width: 30%;
  height: 30%;
  background-color: black;
}
#inner {

  width: calc(100% - 10px);
  height: calc(100% - 10px);
  margin: 5px;
  background-color: orange;
}
<div id="outer">
  <div id="inner">
  </div>
</div>
like image 163
Johannes Avatar answered Sep 29 '22 14:09

Johannes


The answers above assume that you do not want the margin. If you, in fact, want the margins, then you can add overflow: hidden; to the #outer.

like image 20
Rob Allen Avatar answered Sep 29 '22 15:09

Rob Allen