Home >Web Front-end >Front-end Q&A >Which item in css language is floating syntax?
The floating syntax in the CSS language is "float: attribute value;". The float attribute is used to define the direction in which the element floats. It will make the box (element) float on the standard flow, and the elements around it will also be rearranged until its outer edge touches the border of the containing box or another floating box. . This attribute has three attribute values: 1. "left", which defines the element to float to the left; 2. "right", which defines the element to float to the right; 3. "none", which defines the element not to float.
The operating environment of this tutorial: Windows 7 system, CSS3&&HTML5 version, Dell G3 computer.
In the CSS language, if you want an element to float, you need to use the float attribute; This attribute specifies whether a box (element) should float. In the past, this property was always applied to images to make the text surround 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, the element will jump to the next row, and this process will continue until a certain row has enough space.
The three attribute values of the float floating attribute:
left The element floats to the left.
#right The element floats to the right.
#none Default value. The element is not floated and appears where it appears in the text.
1. Three mechanisms of CSS layout
css provides 3 mechanisms to set The placement positions of the boxes are: normal flow (standard flow), floating and positioning, among which:
1. Normal flow (standard flow: "Block-level elements" will occupy one line, "from top to bottom" "Arrangement; "Inline elements" will be arranged in order, "from left to right", and will automatically wrap when touching the edge of the parent element;
2. Floating: Let the box "float" from the normal flow, the main function Let multiple block-level boxes be displayed in one line.
3. Positioning: "Position" the box at a certain position - CSS is inseparable from positioning, especially the subsequent js special effects.
2. Why is floating required?
**Concept: **Element floating means**an element with a floating attribute set will:**
Function:
nbsp;html> <meta> <meta> <title>Document</title> <style> .box1{ width: 200px; height: 200px; background-color: rgba(255,0,0,0.5); float: left; } .box2{ width: 300px; height: 150px; background-color: skyblue; } </style> <div></div> <div></div>
## The float<span style="background-color: rgb(255, 192, 0);"></span>
attribute will make the box float on top of the standard stream, so the second standard stream box runs to the bottom of the floating box.
nbsp;html> <meta> <meta> <title>Document</title> <style> .box1{ width: 200px; height: 200px; background-color: rgba(255,0,0,0.5); /* 让第一个盒子浮动起来,不占位置 */ float: left; } .box2{ width: 300px; height: 150px; background-color: skyblue; } </style> <div></div> <div></div>So, the thing under box2 actually ran under box1, and was suppressed by box1, blocking it
div { width: 200px; height: 200px; background-color: pink; /* 转换为行内块元素,可以水平显示,不过 div 之间有间隙,不方便处理 */ /* display: inline-block; */ /* 设置浮动属性,可以让 div 水平排列,并且没有间隙 */ float: left; } .two { background-color: hotpink; }The floating elements are close to each other, but if the parent width is installed If these floating boxes are not included, the extra boxes will be aligned on a new lineFloating expansion
1. The relationship between floating elements and parent boxes
2. The relationship between floating elements and sibling boxes
结论: 如果一个盒子里面有多个子盒子,如果其中一个盒子浮动了,其他兄弟也应该浮动。防止引起问题
ps:浮动只会影响当前的或者后面的标准流的盒子,不会影响前面的标准流
建议:如果一个盒子里面有多个盒子,如果其中的一个盒子浮动了,其他兄弟也应该浮动。防止引起问题
三、为什么要清除浮动
因为父级盒子很多情况下,不方便给高度,但是子盒子浮动就不占有位置,最后父级盒子高度为0,就影响了下面的标准流盒子。 !
结论:
四、清除浮动本质
清除浮动主要为了解决父级元素因为子级浮动引起内部高度为0 的问题。 清除浮动之后, 父级就会根据浮动的子盒子自动检测高度。 父级有了高度,就不会影响下面的标准流了
五、清除浮动的四种方式
在CSS中,clear属性用于清除浮动
语法:
选择器{clear:属性值;} //clear 清除
属性值 | 右描述 |
---|---|
left | 不允许左侧有浮动元素(清除左侧浮动的影响) |
right | 不允许右侧有浮动元素(清除右侧浮动的影响) |
both | 同时清除左右俩侧浮动的影响 |
但是我们实际工作中, 几乎只用 clear: both;
1.额外标签法(隔墙法)
<!-- 是W3C推荐的做法是通过在浮动元素末尾添加一个空的标签: 1.添加在浮动元素最后 2.该元素必须是块元素,行内元素无效 --> <div></div>
2.父级添加overflow属性方法
可以给父级添加: overflow为 hidden| auto| scroll 都可以实现。
3.使用after伪元素清除浮动
after 方式为空元素额外标签法的升级版,好处是不用单独加标签了
.clearfix:after { content: ""; display: block; height: 0; clear: both; visibility: hidden; } .clearfix { /* IE6、7 专有 */ *zoom: 1; }
4.使用双伪元素清除浮动
.clearfix:before,.clearfix:after { content:""; display:table; } .clearfix:after { clear:both; } .clearfix { *zoom:1; }
标准流(普通流)在布局中 块级元素会独占一行,从上向下排列;行内元素会按照顺序,从左到右排列,碰到父元素边缘则自动换行。
浮动的应用场景大部分用于让盒子水平排列成一行和控制图片。
清除浮动主要为了解决父级元素因为子级浮动引起内部高度为0 的问题。
清除浮动一共有4中方式:
额外标签法(隔墙法)
父级添加overflow属性方法
使用after伪元素清除浮动
使用双伪元素清除浮动
(学习视频分享:web前端)
The above is the detailed content of Which item in css language is floating syntax?. For more information, please follow other related articles on the PHP Chinese website!