重绘重排回流(重绘和回流是什么,如何避免)

重绘重排回流(重绘和回流是什么,如何避免)p 在 CSS 中 盒模型的浮动是一种用于控制元素布局的属性 br br 一 浮动的作用 br 实现多栏布局 可以让元素向左或向右浮动 从而使多个元素在同一行显示 实现类似报纸排版的多栏布局效果 p 环绕效果

大家好,我是讯享网,很高兴认识大家。



 <p>在 CSS 中&#xff0c;盒模型的浮动是一种用于控制元素布局的属性。<br />  <br /> 一、浮动的作用<br /> 实现多栏布局&#xff1a;可以让元素向左或向右浮动&#xff0c;从而使多个元素在同一行显示&#xff0c;实现类似报纸排版的多栏布局效果。</p> 

讯享网

环绕效果:浮动元素会脱离正常的文档流,使得周围的非浮动元素围绕它进行布局。

打破常规布局顺序:通常情况下,HTML 元素在文档流中是按照先后顺序依次排列的。但通过设置浮动,可以让元素脱离原来的排列顺序,出现在它原本不应该出现的位置上,从而实现一些特殊的布局需求。
二、语法
使用  float  属性来设置元素的浮动方向,可选值有  left (向左浮动)、 right (向右浮动)和  none (不浮动,默认值)。

例子: 

&lt;!DOCTYPE html&gt;

&lt;html lang=“en”&gt;

&lt;head&gt;

    &lt;meta charset=“UTF-8”&gt;

    &lt;meta name=“viewport” content=“width=device-width, initial-scale=1.0”&gt;

    &lt;title&gt;Document&lt;/title&gt;

    &lt;style&gt;

        .box {

  width: 100px;

  height: 100px;

  background-color: blue;

}

.float-left {

  float: left;

}

.float-right {

  float: right;

}

    &lt;/style&gt;

&lt;/head&gt;

&lt;body&gt;

      &lt;div class=“box float-left”&gt;左浮动&lt;/div&gt;

      &lt;div class=“box float-right”&gt;右浮动&lt;/div&gt;

&lt;/body&gt;

   

&lt;/html&gt;

运行结果:


讯享网

在 CSS 中,当使用浮动属性时,可能会导致父元素高度塌陷等问题,这时就需要进行清除浮动。以下是几种常见的清除浮动的方法: 
一、使用额外元素并设置 clear 属性
在浮动元素之后添加一个空的  &lt;div&gt;  元素,并设置其  clear  属性为  both ,以清除浮动的影响。

&lt;head&gt;
  &lt;style&gt;
  .box {
      width: 100px;
      height: 100px;
      background-color: blue;
      float: left;
    }
  &lt;/style&gt;
&lt;/head&gt;








&lt;body&gt;
  &lt;div class=“box”&gt;&lt;/div&gt;
  &lt;div style=“ clear: both;”&gt;&lt;/div&gt;
&lt;/body&gt;


&lt;/html&gt;

.parent {
  overflow: auto; /* 或者 hidden、scroll /
}

.parent {
  / 其他样式 */
}

.parent::after {
  content: “”;
  display: block;
  clear: both;
}



例子:

这是最原本的代码,未做任何设置

&lt;!DOCTYPE html&gt;

&lt;html&gt;

&lt;head&gt;

  &lt;style&gt;

    body{

        margin: 15px;

        font-family: Arial;

        font-size: 12px;

    }

    .father{

        background-color: aliceblue;

        border: 2px red solid;

        padding: 5px;

    }

    .father div{

        padding: 10px;

        margin: 15px;

        border: 2px red solid;

        background-color: aqua;

    }

   .father p {

      border: 2px lightblue solid;

      background-color: lightblue;

    }

    #float1 {

      width: 100px;

      height: 100px;

      background-color: lightblue;

    }

    #float2{    

         width: 100px;

      height: 100px;

      background-color: yellow;

    }

    #float3 {          

        width: 100px;

      height: 100px;

      background-color: blue;

    }

  &lt;/style&gt;

&lt;/head&gt;

&lt;body&gt;

    &lt;div class=“father”&gt;

  &lt;div id=“float1”&gt;盒子1&lt;/div&gt;

  &lt;div id=“float2”&gt;盒子2&lt;/div&gt;

  &lt;div id=“float3”&gt;盒子3&lt;/div&gt;

  &lt;p&gt;清除浮动&lt;br&gt;

    清除浮动&lt;br&gt;

    清除浮动&lt;br&gt;

    清除浮动&lt;br&gt;

    清除浮动&lt;br&gt;

    清除浮动&lt;br&gt;

    清除浮动&lt;br&gt;

    清除浮动&lt;br&gt;

&lt;/p&gt;

  &lt;/div&gt;

&lt;/body&gt;

&lt;/html&gt;

运行结果:

 下面将把盒子1、2设置为向左浮动,盒子3设置为向右浮动

代码如下:

&lt;!DOCTYPE html&gt;

&lt;html&gt;

&lt;head&gt;

  &lt;style&gt;

    body{

        margin: 15px;

        font-family: Arial;

        font-size: 12px;

    }

    .father{

        background-color: aliceblue;

        border: 2px red solid;

        padding: 5px;

    }

    .father div{

        padding: 10px;

        margin: 15px;

        border: 2px red solid;

        background-color: aqua;

    }

   .father p {

      border: 2px lightblue solid;

      background-color: lightblue;

    }

    #float1 {

        float: left;

      width: 100px;

      height: 100px;

      background-color: lightblue;

    }

    #float2{

        float: left;    

         width: 100px;

      height: 100px;

      background-color: yellow;

    }

    #float3 {

        float: right;          

        width: 100px;

      height: 100px;

      background-color: blue;

    }

  &lt;/style&gt;

&lt;/head&gt;

&lt;body&gt;

    &lt;div class=“father”&gt;

  &lt;div id=“float1”&gt;盒子1&lt;/div&gt;

  &lt;div id=“float2”&gt;盒子2&lt;/div&gt;

  &lt;div id=“float3”&gt;盒子3&lt;/div&gt;

  &lt;p&gt;清除浮动&lt;br&gt;

    清除浮动&lt;br&gt;

    清除浮动&lt;br&gt;

    清除浮动&lt;br&gt;

    清除浮动&lt;br&gt;

    清除浮动&lt;br&gt;

    清除浮动&lt;br&gt;

    清除浮动&lt;br&gt;

  &lt;/p&gt;

&lt;/body&gt;

&lt;/html&gt;

运行结果:

下面将清除浮动

代码如下: 

&lt;!DOCTYPE html&gt;

&lt;html&gt;

&lt;head&gt;

  &lt;style&gt;

    body{

        margin: 15px;

        font-family: Arial;

        font-size: 12px;

    }

    .father{

        background-color: aliceblue;

        border: 2px red solid;

        padding: 5px;

    }

    .father div{

        padding: 10px;

        margin: 15px;

        border: 2px red solid;

        background-color: aqua;

    }

   .father p {

      border: 2px lightblue solid;

      background-color: lightblue;

      clear: both;

    }

    #float1 {

        float: left;

      width: 100px;

      height: 100px;

      background-color: lightblue;

    }

    #float2{

        float: left;    

         width: 100px;

      height: 100px;

      background-color: yellow;

    }

    #float3 {

        float: right;          

        width: 100px;

      height: 100px;

      background-color: blue;

    }

  &lt;/style&gt;

&lt;/head&gt;

&lt;body&gt;

    &lt;div class=“father”&gt;

  &lt;div id=“float1”&gt;盒子1&lt;/div&gt;

  &lt;div id=“float2”&gt;盒子2&lt;/div&gt;

  &lt;div id=“float3”&gt;盒子3&lt;/div&gt;

  &lt;p&gt;清除浮动&lt;br&gt;

    清除浮动&lt;br&gt;

    清除浮动&lt;br&gt;

    清除浮动&lt;br&gt;

    清除浮动&lt;br&gt;

    清除浮动&lt;br&gt;

    清除浮动&lt;br&gt;

    清除浮动&lt;br&gt;

  &lt;/p&gt;

  &lt;!– &lt;div style=“clear:both;”&gt;&lt;/div&gt; –&gt;

&lt;/body&gt;

&lt;/html&gt;

运行结果:


小讯
上一篇 2025-06-06 11:17
下一篇 2025-05-03 20:12

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/175369.html