1

I have this code

.outer {
  background-color: #ff00ff;
  width: 100%;
}

.outer div {
  float: left;
  background-color: #0000ff;

}
<div class="outer">
  <div>
    test1    <br/> test1
  </div>
  <div>
    test2    <br/> test2
  </div> 
</div>

https://jsfiddle.net/jcb5mdqa/

It looks like

enter image description here

but I want the outer to have a width of 100% like in .css written.

Expected result

enter image description here

What do I need to change?

Toshi
  • 2,532
  • 4
  • 17
  • 45
  • Because all the content of .outer is floating, they are removed from the document flow. As a result, .outer is empty and not shown. – Gerard Jun 08 '17 at 09:16
  • @Gerard thank you for explaination did not know that – Toshi Jun 08 '17 at 09:16

5 Answers5

2

.outer has width 100% already because it's a block element. It lacks height because the content is floated. To give .outer the height of the content, add overflow: auto to the class definition:

.outer {
  background-color: #ff00ff;
  overflow: auto;
}

.outer div {
  float: left;
  background-color: #0000ff;
}
<div class="outer">
  <div>
    test1    <br/> test1
  </div>
  <div>
    test2    <br/> test2
  </div> 
</div>
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
1

Use clearfix

.outer {
  background-color: #ff00ff;
  width: 100%;
}

.outer div {
  float: left;
  background-color: #0000ff;

}
.clearfix:after {
  content: "";
  display: table;
  clear: both;
}
<div class="outer clearfix">
  <div>
    test1    <br/> test1
  </div>
  <div>
    test2    <br/> test2
  </div> 
</div>
Abhishek Pandey
  • 13,302
  • 8
  • 38
  • 68
1

There are two aspects to resolve this question:

  1. use clear properties, whether add :after to .outer class or add a real div as the last .outer container element can do;

.outer {
  background-color: #ff00ff;
  width: 100%;
}
.outer:after {
  display: block;
  content: "";
  clear: both;
}
.outer div {
  float: left;
  background-color: #0000ff;
}
<div class="outer">
  <div>
    test1
    <br/> test1
    <br/> test1
  </div>
  <div>
    test2
    <br/> test2
    <br/> test2
  </div> 
</div>
  1. Establish a new block formatting context. According to CSS2.1 specification, the way to create a new BFC is below:

Floats, absolutely positioned elements, block containers (such as inline-blocks, table-cells, and table-captions) that are not block boxes, and block boxes with 'overflow' other than 'visible' (except when that value has been propagated to the viewport) establish new block formatting contexts for their contents.

.outer {
  background-color: #ff00ff;
  width: 100%;
  /* float and below overflow peoperties can both establish a new BFC */
  // float: left; 
  overflow: auto;
}

.outer div {
  float: left;
  background-color: #0000ff;
}
<div class="outer">
  <div>
    test1
    <br/> test1
    <br/> test1
  </div>
  <div>
    test2
    <br/> test2
    <br/> test2
  </div> 
</div>
Luckness
  • 481
  • 3
  • 4
0

Use overflow:auto

.outer {
  background-color: #ff00ff;
  width: 100%;
  overflow:auto;
}

.outer div {
  float: left;
  background-color: #0000ff;

}
<div class="outer">
  <div>
    test1    <br/> test1
  </div>
  <div>
    test2    <br/> test2
  </div> 
</div>
LKG
  • 4,152
  • 1
  • 11
  • 21
0

Method 1: use <br style="clear: both">

.outer {
  background-color: #ff00ff;
  width: 100%;
}

.outer div {
  float: left;
  background-color: #0000ff;
}
<div class="outer">
  <div>
    test1    <br/> test1
  </div>
  <div>
    test2    <br/> test2
  </div> 
  <br style="clear: both">
</div> 

Method 2 :use overflow:hidden

.outer {
  background-color: #ff00ff;
  width: 100%;
  overflow: hidden;/*/<=========new/*/
}

Mehod 3: use of :after

.outer:after {
    content: '';
    clear: both;
    display: block;
}
Ehsan
  • 12,655
  • 3
  • 25
  • 44