What does the CSS rule “clear: both” do?

前端 未结 5 1532
轮回少年
轮回少年 2020-11-21 07:42

What does the following CSS rule do:

.clear { clear: both; }

And why do we need to use it?

相关标签:
5条回答
  • 2020-11-21 07:57

    CSS float and clear

    Sample Fiddle

    Just try to remove clear:both property from the div with class sample and see how it follows floating divs.

    0 讨论(0)
  • 2020-11-21 08:01

    Mr. Alien's answer is perfect, but anyway I don't recommend to use <div class="clear"></div> because it just a hack which makes your markup dirty. This is useless empty div in terms of bad structure and semantic, this also makes your code not flexible. In some browsers this div causes additional height and you have to add height: 0; which even worse. But real troubles begin when you want to add background or border around your floated elements - it just will collapse because web was designed badly. I do recommend to wrap floated elements into container which has clearfix CSS rule. This is hack as well, but beautiful, more flexible to use and readable for human and SEO robots.

    0 讨论(0)
  • 2020-11-21 08:03

    When you want one element placed at the bottom other element you use this code in CSS. It is used for floats.

    If you float content you can float left or right... so in a common layout you might have a left nav, a content div and a footer.

    To ensure the footer stays below both of these floats (if you have floated left and right) then you put the footer as clear: both.

    This way it will stay below both floats.

    (If you are only clearing left then you only really need to clear: left;.)

    Go through this tutorial:

    0 讨论(0)
  • 2020-11-21 08:05

    The clear property indicates that the left, right or both sides of an element can not be adjacent to earlier floated elements within the same block formatting context. Cleared elements are pushed below the corresponding floated elements. Examples:

    clear: none; Element remains adjacent to floated elements

    body {
      font-family: monospace;
      background: #EEE;
    }
    .float-left {
      float: left;
      width: 60px;
      height: 60px;
      background: #CEF;
    }
    .float-right {
      float: right;
      width: 60px;
      height: 60px;
      background: #CEF;
    }
    .clear-none {
      clear: none;
      background: #FFF;
    }
    <div class="float-left">float: left;</div>
    <div class="float-right">float: right;</div>
    <div class="clear-none">clear: none;</div>

    clear: left; Element pushed below left floated elements

    body {
      font-family: monospace;
      background: #EEE;
    }
    .float-left {
      float: left;
      width: 60px;
      height: 60px;
      background: #CEF;
    }
    .float-right {
      float: right;
      width: 60px;
      height: 120px;
      background: #CEF;
    }
    .clear-left {
      clear: left;
      background: #FFF;
    }
    <div class="float-left">float: left;</div>
    <div class="float-right">float: right;</div>
    <div class="clear-left">clear: left;</div>

    clear: right; Element pushed below right floated elements

    body {
      font-family: monospace;
      background: #EEE;
    }
    .float-left {
      float: left;
      width: 60px;
      height: 120px;
      background: #CEF;
    }
    .float-right {
      float: right;
      width: 60px;
      height: 60px;
      background: #CEF;
    }
    .clear-right {
      clear: right;
      background: #FFF;
    }
    <div class="float-left">float: left;</div>
    <div class="float-right">float: right;</div>
    <div class="clear-right">clear: right;</div>

    clear: both; Element pushed below all floated elements

    body {
      font-family: monospace;
      background: #EEE;
    }
    .float-left {
      float: left;
      width: 60px;
      height: 60px;
      background: #CEF;
    }
    .float-right {
      float: right;
      width: 60px;
      height: 60px;
      background: #CEF;
    }
    .clear-both {
      clear: both;
      background: #FFF;
    }
    <div class="float-left">float: left;</div>
    <div class="float-right">float: right;</div>
    <div class="clear-both">clear: both;</div>

    clear does not affect floats outside the current block formatting context

    body {
      font-family: monospace;
      background: #EEE;
    }
    .float-left {
      float: left;
      width: 60px;
      height: 120px;
      background: #CEF;
    }
    .inline-block {
      display: inline-block;
      background: #BDF;
    }
    .inline-block .float-left {
      height: 60px;
    }
    .clear-both {
      clear: both;
      background: #FFF;
    }
    <div class="float-left">float: left;</div>
    <div class="inline-block">
      <div>display: inline-block;</div>
      <div class="float-left">float: left;</div>
      <div class="clear-both">clear: both;</div>
    </div>

    0 讨论(0)
  • 2020-11-21 08:06

    I won't be explaining how the floats work here (in detail), as this question generally focuses on Why use clear: both; OR what does clear: both; exactly do...

    I'll keep this answer simple, and to the point, and will explain to you graphically why clear: both; is required or what it does...

    Generally designers float the elements, left or to the right, which creates an empty space on the other side which allows other elements to take up the remaining space.

    Why do they float elements?

    Elements are floated when the designer needs 2 block level elements side by side. For example say we want to design a basic website which has a layout like below...

    enter image description here

    Live Example of the demo image.

    Code For Demo

    /*  CSS:  */
    
    * { /* Not related to floats / clear both, used it for demo purpose only */
        box-sizing: border-box;
        -moz-box-sizing: border-box;
        -webkit-box-sizing: border-box;
    }
    
    header, footer {
        border: 5px solid #000;
        height: 100px;
    }
    
    aside {
        float: left;
        width: 30%;
        border: 5px solid #000;
        height: 300px;
    }
    
    section {
        float: left;
        width: 70%;
        border: 5px solid #000;
        height: 300px;
    }
    
    .clear {
        clear: both;
    }
    <!-- HTML -->
    <header>
        Header
    </header>
    <aside>
        Aside (Floated Left)
    </aside>
    <section>
        Content (Floated Left, Can Be Floated To Right As Well)
    </section>
    <!-- Clearing Floating Elements-->
    <div class="clear"></div>
    <footer>
        Footer
    </footer>

    Note: You might have to add header, footer, aside, section (and other HTML5 elements) as display: block; in your stylesheet for explicitly mentioning that the elements are block level elements.

    Explanation:

    I have a basic layout, 1 header, 1 side bar, 1 content area and 1 footer.

    No floats for header, next comes the aside tag which I'll be using for my website sidebar, so I'll be floating the element to left.

    Note: By default, block level element takes up document 100% width, but when floated left or right, it will resize according to the content it holds.

    1. Normal Behavior Of Block Level Element
    2. Floated Behavior Of Block Level Element

    So as you note, the left floated div leaves the space to its right unused, which will allow the div after it to shift in the remaining space.

    1. div's will render one after the other if they are NOT floated
    2. div will shift beside each other if floated left or right

    Ok, so this is how block level elements behave when floated left or right, so now why is clear: both; required and why?

    So if you note in the layout demo - in case you forgot, here it is..

    I am using a class called .clear and it holds a property called clear with a value of both. So lets see why it needs both.

    I've floated aside and section elements to the left, so assume a scenario, where we have a pool, where header is solid land, aside and section are floating in the pool and footer is solid land again, something like this..

    Floated View

    So the blue water has no idea what the area of the floated elements are, they can be bigger than the pool or smaller, so here comes a common issue which troubles 90% of CSS beginners: why the background of a container element is not stretched when it holds floated elements. It's because the container element is a POOL here and the POOL has no idea how many objects are floating, or what the length or breadth of the floated elements are, so it simply won't stretch.

    1. Normal Flow Of The Document
    2. Sections Floated To Left
    3. Cleared Floated Elements To Stretch Background Color Of The Container

    (Refer [Clearfix] section of this answer for neat way to do this. I am using an empty div example intentionally for explanation purpose)

    I've provided 3 examples above, 1st is the normal document flow where red background will just render as expected since the container doesn't hold any floated objects.

    In the second example, when the object is floated to left, the container element (POOL) won't know the dimensions of the floated elements and hence it won't stretch to the floated elements height.

    enter image description here

    After using clear: both;, the container element will be stretched to its floated element dimensions.

    enter image description here

    Another reason the clear: both; is used is to prevent the element to shift up in the remaining space.

    Say you want 2 elements side by side and another element below them... So you will float 2 elements to left and you want the other below them.

    1. div Floated left resulting in section moving into remaining space
    2. Floated div cleared so that the section tag will render below the floated divs

    1st Example

    enter image description here


    2nd Example

    enter image description here

    Last but not the least, the footer tag will be rendered after floated elements as I've used the clear class before declaring my footer tags, which ensures that all the floated elements (left/right) are cleared up to that point.


    Clearfix

    Coming to clearfix which is related to floats. As already specified by @Elky, the way we are clearing these floats is not a clean way to do it as we are using an empty div element which is not a div element is meant for. Hence here comes the clearfix.

    Think of it as a virtual element which will create an empty element for you before your parent element ends. This will self clear your wrapper element holding floated elements. This element won't exist in your DOM literally but will do the job.

    To self clear any wrapper element having floated elements, we can use

    .wrapper_having_floated_elements:after {  /* Imaginary class name */
      content: "";
      clear: both;
      display: table;
    }
    

    Note the :after pseudo element used by me for that class. That will create a virtual element for the wrapper element just before it closes itself. If we look in the dom you can see how it shows up in the Document tree.

    So if you see, it is rendered after the floated child div where we clear the floats which is nothing but equivalent to have an empty div element with clear: both; property which we are using for this too. Now why display: table; and content is out of this answers scope but you can learn more about pseudo element here.

    Note that this will also work in IE8 as IE8 supports :after pseudo.


    Original Answer:

    Most of the developers float their content left or right on their pages, probably divs holding logo, sidebar, content etc., these divs are floated left or right, leaving the rest of the space unused and hence if you place other containers, it will float too in the remaining space, so in order to prevent that clear: both; is used, it clears all the elements floated left or right.

    Demonstration:

    ------------------ ----------------------------------
    div1(Floated Left) Other div takes up the space here
    ------------------ ----------------------------------
    

    Now what if you want to make the other div render below div1, so you'll use clear: both; so it will ensure you clear all floats, left or right

    ------------------
    div1(Floated Left)
    ------------------
    <div style="clear: both;"><!--This <div> acts as a separator--></div>
    ----------------------------------
    Other div renders here now
    ----------------------------------
    
    0 讨论(0)
提交回复
热议问题