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 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)

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It's out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That's like this.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I'd say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Let's use (X, X, X, X) for talking about specificity Let's use (X, X, X, X) for talking about specificity Mar 24, 2025 am 10:37 AM

I was just chatting with Eric Meyer the other day and I remembered an Eric Meyer story from my formative years. I wrote a blog post about CSS specificity, and

See all articles