Solution to div+css floating
如何清楚浮动(一)
已知一个大的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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-
