Learning experience of css float_html/css_WEB-ITnose
Detailed explanation of css float
@(css float)[hasLayout|clear float|Miaotong]
The definition and usage of css float
float attribute defines in which direction the element floats. Historically this property has always been applied to images, causing the text to wrap around the image, but in CSS, any element can be floated. A floated element creates a block-level box, regardless of what type of element it is.If floating non-replaced elements, specify an explicit width; otherwise, they are made as narrow as possible.
Note: If there is very little space for a floating element on a row, then the element will jump to the next row, and this process will continue until a certain row has enough space.
默认值:none;继承性:no;版本:CSS1JavaScript 语法:object.style.cssFloat="left";
Possible values
值 描述left 元素向左浮动。right 元素向右浮动。none 默认值。元素不浮动,并显示在其在文本中出现的位置。inherit 规定应该从父元素继承 float 属性的值。
Simple example
float:left is used here ;We float the ul element and a element to the left.
What is clear float? Many people are used to translating clear float as "clear float". I think that in view of the problems caused by float, what we need to do is to understand the impact of float, not just clear float, but also Include closed floats so that floats do not affect other elements.
Why do we need to clear float?The reason why we need to clear float is not that we are unhappy with float, but that in some cases, float causes Problems with our layout forced us to clear the impact of float. Listed below are some layout problems caused by float and the clear float method:
The code and renderings are as follows:
What if we set the height of the parent container?
The code and renderings are as follows:
We found that when setting the height of the parent container, the height is not 0, but 200px set for us;
The code and renderings are as follows:
In this case, the clear float method is as follows:
Method 1: Add a new element tag and apply clear: both; The code and renderings are as follows:
Method 2: Parent div defines overflow:auto/hidden; (the parent div here refers to div#wrap)The code and renderings are as follows:
The use of BFC:
Non-BFC elements will ignore the height value of their child elements that add float; their top and bottom margins will be the same as those of the child elements. The margins are collapsed; its inner and outer float elements will affect the layout of itself and its sub-elements.Triggering BFC is an effective way to solve these three problems. This is why overflow:hidden/auto can be used to clear problems such as floating.
Conditions for triggering BFC :
The value of "overflow" is not The value of "visible"
"display" is "table-cell"
"table-caption", or the value of "inline-block"
"position" is neither "static" nor "relative"
Therefore, when using the overflow attribute to clear floats, please note that the overflow attribute has three attribute values: hidden, auto, visible, scroll, and inherit. We can use hidden and auto values to clear floats, but we cannot use visible values. This value will not achieve the effect of clearing floats. Both atuo and hidden values are ok, and hasLayout needs to be triggered in IE6, such as zoom: 1;
The advantages and disadvantages of using the overflow:auto; method are as follows:
The advantages and disadvantages of using the overflow:hidden; method are as follows:
Method 3: The parent element is also set to float
The code and effect are as follows:
Method 4: Use the br tag (clear="all" in br)
This method is somewhat niche, br has the clear="all | left | right | none" attribute;
The code and effect are as follows:
Method 5: Set display:table on the parent element; or set display:inline-block;
The code and effect are as follows:
Method 5: Use:after pseudo-element
It should be noted that:after is a pseudo-element (Pseudo-Element) , not a pseudo class (called a "pseudo object" in some CSS manuals).
Since IE6-7 does not support :after, use zoom:1 to trigger hasLayout.
This method is derived from: How To Clear Floats Without Structural Markup http://snipplr.com/view/86/clear-floats-without-structural-markup/
The code and results are as follows:
style can also be written like this:
.clearfix:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
.clearfix {display: inline-block;} /* for IE/Mac /
Due to the low market share of IE/Mac, it can be simplified to:
.clearfix:after {content:"."; display:block; height:0; visibility:hidden; clear:both; }
.clearfix { zoom:1; }
The code and renderings are as follows :
where clear:both; refers to clearing all floats; content: '.'; display:block; is indispensable for FF/chrome/opera/IE8, and content() can be taken The value can also be empty. The function of visibility:hidden; is to allow the browser to render it but not display it, so that the float can be cleared.
When writing HTML code, I found that in Firefox and other browsers that comply with W3C standards, if there is a DIV as an external container, and the internal DIV has a float style set, the external container DIV will not clear internally, resulting in Can't be stretched open.
This clearfix CSS uses the after pseudo-object, which will add the content in content at the end of the element to which clearfix is applied. A period "." is added here, and its display is set to block; the height is set to 0; clear is set to both; and visibility is set to hidden. This achieves the purpose of opening the container. Explain here:
CSS content attribute
Definition and usage
content attribute and :before and :after pseudo-elements Used together to insert generated content.
Description
This attribute is used to define generated content placed before or after the element. By default, this is often inline content, but the type of box this content creates can be controlled using the display attribute.
Default: normal
Inheritance: no
Version: CSS2
JavaScript Syntax: object.style.content="url(beep.wav)"
Value
none
normal
content specifications
inherit specifies that the value of the content attribute should be inherited from the parent element.
For details, please refer to the link: http://www.w3school.com.cn/cssref/pr_gen_content.asp
Conclusion
This article involves hasLayout, BFC, etc. Due to the limited level, I will not talk about them one by one here. I will discuss them in another article after research.
That’s it for today. If there are any mistakes, please point them out. Thank you! -----Miaotong

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

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.

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

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.
