如何清楚浮动(一)
已知一个大的div容器,这个容器包含了两个子div容器,然后在这两个子div容器的后面再添加一个div(这个div表示清除浮动的div容器),清楚浮动的div容器设置css样式为clear:both,此时,大的div标签的内部(左右两边/*css5*/)浮动就清除了。
如果有一个大的div容器
<body> <div class="divcss5"> <div class="clear"></div> <!--<div class="clear eft"></div> <div class="clear ight"></div>--> </div> </body>
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!

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.

Article discusses CSS Flexbox, a layout method for efficient alignment and distribution of space in responsive designs. It explains Flexbox usage, compares it with CSS Grid, and details browser support.

The article discusses techniques for creating responsive websites using CSS, including viewport meta tags, flexible grids, fluid media, media queries, and relative units. It also covers using CSS Grid and Flexbox together and recommends CSS framework

The article discusses the CSS box-sizing property, which controls how element dimensions are calculated. It explains values like content-box, border-box, and padding-box, and their impact on layout design and form alignment.

Article discusses creating animations using CSS, key properties, and combining with JavaScript. Main issue is browser compatibility.

Article discusses using CSS for 3D transformations, key properties, browser compatibility, and performance considerations for web projects.(Character count: 159)

The article discusses using CSS gradients (linear, radial, repeating) to enhance website visuals, adding depth, focus, and modern aesthetics.

Article discusses pseudo-elements in CSS, their use in enhancing HTML styling, and differences from pseudo-classes. Provides practical examples.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver CS6
Visual web development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
