Solution to div+css floating

高洛峰
Release: 2017-03-31 11:15:30
Original
1689 people have browsed it

如何清楚浮动(一)

  已知一个大的div容器,这个容器包含了两个子div容器,然后在这两个子div容器的后面再添加一个div(这个div表示清除浮动的div容器),清楚浮动的div容器设置css样式为clear:both,此时,大的div标签的内部(左右两边/*css5*/)浮动就清除了。

  如果有一个大的div容器

,这个大的div包含了一个子div容器
, the sub-div container sets a width, height, background color, and does not set the inner, margins and floating. At this time, the sub-div container defaults to the left, and the sub-div to the left is floating (float)Inherits the properties of the browser and does not have the function of layout. Then we set the float of the sub-div container to float:left; I found that after setting the float:left, the sub-div container floated, and the sub-div container was a certain distance from the top of the large div container.

<body>
        <div class="divcss5">
            <div class="clear"></div>
        <!--<div class="clear eft"></div>
                    <div class="clear ight"></div>-->
        </div> 
    </body>
Copy after login

Float has margin attribute

Then I margin-top:10px, the sub-div container moved down 10px on the original basis distance, that is to say, floating will not affect the offset of the margin-top attribute, and similarly will not affect margin-left, margin-right, margin-bottomThe offset that the attribute should have.

Floating does not have top, left, right, bottom attributes

Then, now I delete margin-top:10px;, set top:10px for the sub-div container, and find that the vertical coordinate does not occur. Offset, that is to say, floating does not have a top attribute. In the same way, there are no left, right, and bottom attributes.

So, under what circumstances can the properties of top, left, right, and bottom be set to have an effect on floating?

Floating has top, left, right, and bottom attributes (conditional setting relative position position)

So, I added relative position: relative to the sub-div container, Then set top:10px, and suddenly found that the child div container was offset downward by 10px.

Continue in the state of the child div floating, set it to position: relative;, and then set margin-left: 10px; to it, and see that the position of the child div container moves horizontally to the right If the offset of 10px is changed, margin-top, margin-right, and margin-bottom will all change in the same way.

In other words, when setting float:left for a child div container, and then setting margin-left, it will work, but setting top will have no effect. Top can only work in a floating state when the relative position is set to the child div container. So does it mean that left and top can only work when they are in relative positions? So now we will do an experiment. I deleted float:left;position:relative; in the child div container. There will be no floating or relative position, and then write top:10px;left:10px; The corresponding css code is as follows. clear{width: 200px;background: #f2e;color:#030617;height: 20px;top:50px;left: 10px;};, the result is that the sub-div container has not moved at all on the horizontal and vertical axes, so it turns out that left and top only work when the relative position is set. Margin will work whether you set position or not.

Continue the float floating above. If you now set inherit for float; inherit the browser's floating attribute, at this time we see that the child div does not float and is on the left by default. Similarly, setting the floating style none initial to the child div will not have a floating effect. Only when the left and right floats are set for the sub-div container, the sub-div container will appear floating.

How to clear floats (2)

Below, we use another method to clear floats, the HTML code is still the above code, the css code of the div of the large container: .divcss5{padding:10px 0;width: 100%; left: 50%;right: 50%;background: #007CB5;}, css code of the child div container: .clear{width: 200px;background: #f2e ;color:#030617;height: 20px;float:left;}, what you see at this time is the floating effect, the sub-div container floats on top of the large container. For the same principle, I still use clear:both; we can Use css pseudo-element :after, the following is a very simple css code, write css pseudo-class to the parent container, code: .divcss5:after{content: "1";clear : both;display: block;}, at this time, what we see is that the floating of the large container is cleared, and on the browser, we can see that the sub-div container is included inside the large div container . Viewing the element on the browser, you can see that after is included in the parent container. Therefore, after is equivalent to adding a clear floating div inside the parent container, except that one div layer is missing. This layer is implemented by .divcss5:after .

The above is the detailed content of Solution to div+css floating. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template