CSS完成两栏合理布局,左侧固定不动,右侧自融

日期:2021-02-27 类型:科技新闻 

关键词:h5小游戏,h5如何制作,免费h5在线制作,h5页面在线制作,测一测h5

1. float+overflow:hidden

这类方法关键根据 overflow 开启 BFC,而 BFC 不容易重合波动元素。因为设定 overflow:hidden 其实不会开启 IE6-访问器的 haslayout 特性,因此必须设定 zoom:1 来适配 IE6-访问器。实际编码以下:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF⑻">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    .parent {
      margin: 0 auto; // 使父器皿水平垂直居中显示信息
      overflow: hidden;
      zoom: 1;
      max-width: 1000px;
    }
    .left {
      float: left;
      margin-right: 20px;
      width: 200px;
      background-color: green;
    }

    .right {
      overflow: hidden;
      zoom: 1;
      background-color: yellow;
    }
  </style>
</head>
<body>
  <div class="parent"> 
    <div class="left"> 
    <p>left left left left</p> 
    </div> 
    <div class="right"> 
    <p>right</p> 
    <p>right</p> 
    </div> 
  </div> 
</body>
</html>

2. float: left+ margin-left

float使左侧的元素摆脱文本文档流,右侧的元素能够和左侧的元素显示信息在同1行,设定margin-left让右侧的元素不遮盖掉左侧的元素, 编码以下:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF⑻">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    .parent {
      margin: 0 auto;
      max-width: 1000px;
    }
    .parent::after {
      content: '';
      display: table;
      clear: both;
    }
    .left {
      float: left;
      width: 200px;
      background-color: green;
    }

    .right {
      margin-left: 200px;
      background-color: yellow;
    }
  </style>
</head>
<body>
  <div class="parent"> 
    <div class="left"> 
    <p>left left left left</p> 
    </div> 
    <div class="right"> 
    <p>right</p> 
    <p>right</p>
    <p>right</p> 
    </div> 
   </div> 
</body>
</html>

3. position: absolute + margin-left

左侧肯定精准定位,右侧设定margin-left,编码以下:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF⑻">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    .parent {
      position: relative;
      margin: 0 auto;
      max-width: 1000px;
    }

    .left {
      position: absolute;
      width: 200px;
      background-color: green;
    }

    .right {
      margin-left: 200px;
      background-color: yellow;
    }
  </style>
</head>
<body>
  <div class="parent"> 
    <div class="left"> 
    <p>left left left left</p> 
    </div> 
    <div class="right"> 
    <p>right</p> 
    <p>right</p>
    <p>right</p> 
    </div> 
   </div> 
</body>
</html>

4. flex合理布局

flex合理布局可使两个子元素显示信息在同1行,要是左侧的宽度固定不动,便可以完成实际效果, 编码以下:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF⑻">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    .parent {
      display: flex;
      margin: 0 auto;
      max-width: 1000px;
    }
    .left {
      width: 200px;
      background-color: green;
    }

    .right {
      margin-left: 20px;
      flex: 1;
      background-color: yellow;
    }
  </style>
</head>

<body>
  <div class="parent">
    <div class="left">
      <p>left left left left</p>
    </div>
    <div class="right">
      <p>right</p>
      <p>right</p>
      <p>right</p>
    </div>
  </div>
</body>
</html>

到此这篇有关CSS完成两栏合理布局,左侧固定不动,右侧自融入的4种方式的文章内容就详细介绍到这了,更多有关CSS完成两栏合理布局內容请检索脚本制作之家之前的文章内容或再次访问下面的有关文章内容,期待大伙儿之后多多适用脚本制作之家!