Home Web Front-end CSS Tutorial Solution to div+css floating

Solution to div+css floating

Mar 31, 2017 am 11:15 AM
css float

如何清楚浮动(一)

  已知一个大的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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1657
14
PHP Tutorial
1257
29
C# Tutorial
1230
24
How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

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.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

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

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

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.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

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.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

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.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

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 bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

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

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

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-

See all articles