zoukankan      html  css  js  c++  java
  • CSS: 实现两栏布局,左边固定,右边自适应的4种方法

    1. float+overflow:hidden 

    这种办法主要通过 overflow 触发 BFC,而 BFC 不会重叠浮动元素。由于设置 overflow:hidden 并不会触发 IE6-浏览器的 haslayout 属性,所以需要设置 zoom:1 来兼容 IE6-浏览器。具体代码如下:

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <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使左边的元素脱离文档流,右边的元素可以和左边的元素显示在同一行,设置margin-left让右边的元素不覆盖掉左边的元素, 代码如下:

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <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-8">
      <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布局可以使两个子元素显示在同一行,只要左边的宽度固定,就可以实现效果, 代码如下:

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <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>
  • 相关阅读:
    ubuntu 16.04下源码安装opencv3.4
    机器学习库--dlib
    ubuntu查看内存占用和查看cpu使用情况的简单方法(ubuntu内存管理)
    语音开放平台简介
    语音开源代码简介
    语音开源代码与平台对比
    source insight 添加 python 支持
    Taglist: Exuberant ctags (http://ctags.sf.net) not found in PATH. Plugin is not loaded
    人脸检测----Adaboost学习方法
    人脸检测---特征的提取
  • 原文地址:https://www.cnblogs.com/ycherry/p/12396658.html
Copyright © 2011-2022 走看看