Home Web Front-end CSS Tutorial CSS floating, positioning, and parent container collapse issues

CSS floating, positioning, and parent container collapse issues

Feb 27, 2017 pm 03:00 PM

How to put it, it has been three months since I came into contact with the front-end. After such a long period of study, my level is still average. I participated in IFE2017 a few days ago. Here is a summary of my understanding of floating, positioning, and Some thoughts on column layout and parent container collapse issues.

First of all, floating and positioning are the basis of layout in CSS. Through floating and positioning, each box model can be controlled accurately to the pixel level, which shows its importance.

Let’s talk about floating first:

In the document object model of HTML, a fluid layout is adopted, that is to say, block-level elements occupy a line alone, and block-level elements are arranged side by side. , there are two main methods, one is to set the display of block-level elements to inline-block in CSS. But many times it is not suitable to use this method. More often we will use the floating method.

Float, there are two main types: float: left; and float: right; floating can make block-level elements break away from the standard document flow. It can be understood that elements with float defined must move in the defined direction. Until it is blocked or hits the parent container boundary. If the remaining width of the line is insufficient, the floating box will float to the next line. Floating provides a solution for implementing page layout.

However, what cannot be ignored is that sometimes simple floating cannot meet our needs for interface layout. At this time, the importance of positioning is reflected. Positioning can be divided into four types: relative (relative positioning), absolute (absolute positioning), fixed (fixed positioning), and static. When we do not apply positioning attributes to an element, it is equivalent to static.

So, how to understand relative positioning? The element (box model) with relative positioning applied does not break away from the standard document flow. You can set the top, left, right, and bottom values ​​​​for it to achieve fine-tuning of the element (box model) relative to the original position. Top means the element relative to the original position. The position moves downward (you can set a negative value, which is equivalent to setting a positive value bottom), and left means that the element moves to the right relative to its original position. Similarly, right moves left and bottom moves up.

Absolute positioning: The element with absolute positioning applied will be separated from the document flow, as if it has never existed. At this time, its positioning is relative to its ancestor element with relative positioning applied. And it also has a very important feature: it will "cross" according to the set displacement value. What does it mean? That is to say, the top, left, right, and bottom settings are relative to the boundaries of its ancestor element (box). If a displacement direction is set for it, the element (box) will first move to the boundary in that direction, and then move relative to the boundary.

Fixed positioning: fixed positioning is also separated from the standard document flow, but it is relative to the browser window and will not change with the movement of the scroll bar or interface. It can also be set top, left, right, bottom values.

As for the column layout, I personally use the following methods:

1. If it is divided into two columns, you can apply float to the two boxes at the same time for layout. You can set Set the width or width percentage of the left and right boxes.

 2. It is also laid out in two columns. You can also apply left floating layout to the box on the left, apply positioning to the box on the right or set its margin value to position.

 3. For a three-column layout, it is best to use the method of floating and positioning. Float the boxes on the left and right sides, and set the left and right margins of the middle element (box) to achieve positioning.

It must be understood that the great initiative of floating may cause the parent container to collapse. That is to say, when all elements in the container float (which will cause the height of the parent container to be zero) or the internal elements do not float. When the elements are not enough to support the parent container, the height of the parent container will be 0 or not enough to meet our page layout requirements. Then, we must think of some ways to solve this problem. I have a few ways:

1. Set a height for the parent container

2. Set the parent container overflow: hidden or overflow: auto;

overflow:hidden;
overflow:auto;
Copy after login

 3. Set the parent element to float (not recommended)

 4. Set the empty element to it (clearfix: both)

 5. Apply the following style to the parent element:

.clearfix:before,
.clearfix:after
{
    content:"";
    display:table;
}
.clearfix:after
{
    clear:both;
}
Copy after login

In summary, for the layout of elements in web pages, floating and positioning often need to be used together. Only when combined can we achieve the effect we need.

The above are some of my little experiences about CSS positioning and floating during this period. There may be errors in it. I hope you can point them out to me so that we can all make progress together. Let me summarize these I read a lot of documents from big guys, so I borrowed a lot of their opinions, expressed some of my own understandings, and at the same time deepened my understanding of the knowledge involved. I hope everyone will work together on the front-end road and make progress every day!

For more CSS floating, positioning, and parent container collapse issues, please pay attention to the PHP Chinese website for related articles!


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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
A Comparison of Static Form Providers A Comparison of Static Form Providers Apr 16, 2025 am 11:20 AM

Let’s attempt to coin a term here: "Static Form Provider." You bring your HTML

A Proof of Concept for Making Sass Faster A Proof of Concept for Making Sass Faster Apr 16, 2025 am 10:38 AM

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

Weekly Platform News: HTML Loading Attribute, the Main ARIA Specifications, and Moving from iFrame to Shadow DOM Weekly Platform News: HTML Loading Attribute, the Main ARIA Specifications, and Moving from iFrame to Shadow DOM Apr 17, 2025 am 10:55 AM

In this week's roundup of platform news, Chrome introduces a new attribute for loading, accessibility specifications for web developers, and the BBC moves

Some Hands-On with the HTML Dialog Element Some Hands-On with the HTML Dialog Element Apr 16, 2025 am 11:33 AM

This is me looking at the HTML element for the first time. I've been aware of it for a while, but haven't taken it for a spin yet. It has some pretty cool and

Paperform Paperform Apr 16, 2025 am 11:24 AM

Buy or build is a classic debate in technology. Building things yourself might feel less expensive because there is no line item on your credit card bill, but

Where should 'Subscribe to Podcast' link to? Where should 'Subscribe to Podcast' link to? Apr 16, 2025 pm 12:04 PM

For a while, iTunes was the big dog in podcasting, so if you linked "Subscribe to Podcast" to like:

Weekly Platform News: Text Spacing Bookmarklet, Top-Level Await, New AMP Loading Indicator Weekly Platform News: Text Spacing Bookmarklet, Top-Level Await, New AMP Loading Indicator Apr 17, 2025 am 11:26 AM

In this week's roundup, a handy bookmarklet for inspecting typography, using await to tinker with how JavaScript modules import one another, plus Facebook's

Options for Hosting Your Own Non-JavaScript-Based Analytics Options for Hosting Your Own Non-JavaScript-Based Analytics Apr 15, 2025 am 11:09 AM

There are loads of analytics platforms to help you track visitor and usage data on your sites. Perhaps most notably Google Analytics, which is widely used

See all articles