zoukankan      html  css  js  c++  java
  • 页面布局之一边固定一边自适应

    项目开发过程中我们常用的布局一边固定一边自适应。常见的是左边固定,右边自适应。

    主体内容在右边

    <div class="container">
      <div class="left"></div>
      <div class="right"></div>
    </div>
    

     

    1.浮动 

      <style>
        .left{
          width: 100px;
          background-color: red;
          float: left;
        }
        .right{
          background-color: green;
          margin-left: 100px;
        }
        .container:after {
          clear: both;/*清除浮动*/
        }
    
      </style>

    2.定位

     

      <style>
        .left {
          width: 100px;
          background-color: red;
          position: absolute;
        }
        .right {
          background-color: green;
          margin-left: 100px;
        }
    
    
      </style>

     这种布局方式有一定的缺陷,当内容的高度大于父元素时,会影响下面的布局,出现下面的情况。这种方式不建议在不知道内容高度的情况下使用.

    3.flex

     flex 是css3的特性,有兼容性.

      <style>
        .container {
          display: flex;
        }
        .left {
          width: 100px;
          background-color: red;
        }
        .right {
          background-color: green;
          flex: 1;
        }
        
      </style>

     4.calc

    calc()不是所有的都兼容.

      <style>
        .container {
          font-size: 0; /*消除间距*/
        }
        .left,.right {
          display: inline-block;
        }
        .left {
          width: 100px;
          background-color: red;
        }
        .right {
          background-color: green;
          width: calc(100% - 100px);
        }
    
    
      </style>

    主体内容在右边

    <div class="container">
      <div class="main">
        <div class="main_container">主体内容显示区域</div>
      </div>
      <div class="sidebar">导航区域</div>
    </div>
    
    <style>
      .main {
        float: left;
        width: 100%;
        margin-left: -320px;
      }
    
      .main_container {
        margin-left: 320px;
        background-color: red;
      }
    
      .sidebar {
        float: right;
        width: 320px;
        background-color: green;
      }
    </style>
  • 相关阅读:
    CH6201走廊泼水节
    P3366 (模板)最小生成树
    linux 基础语法
    django 3.1 序列化讲述
    django 的基础设计
    django 的初始项目结构
    http
    mysql(一)
    反射型xss
    html(四)
  • 原文地址:https://www.cnblogs.com/xiaoxueer/p/11843164.html
Copyright © 2011-2022 走看看