Understanding Container Height with Floated Children
When working with CSS, you may encounter a situation where a parent container with floated children ends up with a height of zero. This behavior can be confusing, given that the children are occupying space within the container.
The Issue
Consider the following CSS:
If you use this CSS with the following HTML:
The page will render correctly. However, if you inspect the elements, you'll notice that the div#wrapper is shown as 0px high.
Why It Happens
Content that is floated does not influence the height of its container. In this case, both the .content and .lbar elements are floating, so they are removed from the normal flow of the document. As a result, the container contains no content that isn't floating. This allows the height of the container to collapse to zero, just as if it were empty.
Solutions
To address this issue, you can use the following methods:
- overflow: hidden: Setting this property on the container will prevent the content from overflowing outside its bounds. This establishes a new block formatting context, which forces the container to expand to accommodate its floated children.
- Containing Floats: Other techniques for containing floats include using the clear property or adding an empty div with clear: both; at the end of the container.
Understanding this behavior of floats is essential for creating layouts with CSS. By employing the appropriate techniques, you can ensure that your containers have the desired height and that your page elements are correctly positioned.
The above is the detailed content of Why Does a Container with Floated Children Have a Zero Height?. For more information, please follow other related articles on the PHP Chinese website!

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

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.

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

@keyframesispopularduetoitsversatilityandpowerincreatingsmoothCSSanimations.Keytricksinclude:1)Definingsmoothtransitionsbetweenstates,2)Animatingmultiplepropertiessimultaneously,3)Usingvendorprefixesforbrowsercompatibility,4)CombiningwithJavaScriptfo

CSSCountersareusedtomanageautomaticnumberinginwebdesigns.1)Theycanbeusedfortablesofcontents,listitems,andcustomnumbering.2)Advancedusesincludenestednumberingsystems.3)Challengesincludebrowsercompatibilityandperformanceissues.4)Creativeusesinvolvecust

Using scroll shadows, especially for mobile devices, is a subtle bit of UX that Chris has covered before. Geoff covered a newer approach that uses the animation-timeline property. Here’s yet another way.

Let’s run through a quick refresher. Image maps date all the way back to HTML 3.2, where, first, server-side maps and then client-side maps defined clickable regions over an image using map and area elements.

The State of Devs survey is now open to participation, and unlike previous surveys it covers everything except code: career, workplace, but also health, hobbies, and more.


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

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

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.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use
