search
HomeWeb Front-endCSS TutorialCSS floating, positioning, and parent container collapse issues

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;

 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;
}

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
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
@keyframes vs CSS Transitions: What is the difference?@keyframes vs CSS Transitions: What is the difference?May 14, 2025 am 12:01 AM

@keyframesandCSSTransitionsdifferincomplexity:@keyframesallowsfordetailedanimationsequences,whileCSSTransitionshandlesimplestatechanges.UseCSSTransitionsforhovereffectslikebuttoncolorchanges,and@keyframesforintricateanimationslikerotatingspinners.

Using Pages CMS for Static Site Content ManagementUsing Pages CMS for Static Site Content ManagementMay 13, 2025 am 09:24 AM

I know, I know: there are a ton of content management system options available, and while I've tested several, none have really been the one, y'know? Weird pricing models, difficult customization, some even end up becoming a whole &

The Ultimate Guide to Linking CSS Files in HTMLThe Ultimate Guide to Linking CSS Files in HTMLMay 13, 2025 am 12:02 AM

Linking CSS files to HTML can be achieved by using elements in part of HTML. 1) Use tags to link local CSS files. 2) Multiple CSS files can be implemented by adding multiple tags. 3) External CSS files use absolute URL links, such as. 4) Ensure the correct use of file paths and CSS file loading order, and optimize performance can use CSS preprocessor to merge files.

CSS Flexbox vs Grid: a comprehensive reviewCSS Flexbox vs Grid: a comprehensive reviewMay 12, 2025 am 12:01 AM

Choosing Flexbox or Grid depends on the layout requirements: 1) Flexbox is suitable for one-dimensional layouts, such as navigation bar; 2) Grid is suitable for two-dimensional layouts, such as magazine layouts. The two can be used in the project to improve the layout effect.

How to Include CSS Files: Methods and Best PracticesHow to Include CSS Files: Methods and Best PracticesMay 11, 2025 am 12:02 AM

The best way to include CSS files is to use tags to introduce external CSS files in the HTML part. 1. Use tags to introduce external CSS files, such as. 2. For small adjustments, inline CSS can be used, but should be used with caution. 3. Large projects can use CSS preprocessors such as Sass or Less to import other CSS files through @import. 4. For performance, CSS files should be merged and CDN should be used, and compressed using tools such as CSSNano.

Flexbox vs Grid: should I learn them both?Flexbox vs Grid: should I learn them both?May 10, 2025 am 12:01 AM

Yes,youshouldlearnbothFlexboxandGrid.1)Flexboxisidealforone-dimensional,flexiblelayoutslikenavigationmenus.2)Gridexcelsintwo-dimensional,complexdesignssuchasmagazinelayouts.3)Combiningbothenhanceslayoutflexibilityandresponsiveness,allowingforstructur

Orbital Mechanics (or How I Optimized a CSS Keyframes Animation)Orbital Mechanics (or How I Optimized a CSS Keyframes Animation)May 09, 2025 am 09:57 AM

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.

CSS Animations: Is it hard to create them?CSS Animations: Is it hard to create them?May 09, 2025 am 12:03 AM

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

See all articles

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

Hot Tools

Safe Exam Browser

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.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

DVWA

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