CSS布局

单列布局

实现方式: 定宽+水平居中

width: 1000px; //或 max-width: 1000px;
margin-left: auto;
margin-right: auto;

max-width和width的区别:固定宽度当浏览器缩小时页面出现滚动条;最大宽度当页面缩小时页面自动伸缩 以真实宽度作为浏览器计算的基准
1.一栏布局

<style>
  .layout{
  /*   width: 960px; */
    max-width: 960px;
    margin: 0 auto;
  }
  #header{
    height: 60px;
    background: red;
  }
  #content{
    height: 400px;
    background: blue;
  }
  #footer{
    height: 50px;
    background: yellow;
  }
</style>
<div class="layout">
  <div id="header">头部</div>
  <div id="content">内容</div>
  <div id="footer">尾部</div>
</div>

也可有如下写法,省标签,便于控制局部:

<style>
  .layout{
    width: 960px;
    margin: 0 auto;
  }
  #header{
    height: 60px;
    background: red;
  }
  #content{
    height: 400px;
    background: blue;
  }
  #footer{
    height: 50px;
    background: yellow;
  }
</style>
<div id="header"  class="layout">头部</div>
<div id="content" class="layout">内容</div>
<div id="footer" class="layout">尾部</div>

2.通栏布局

<style>
  .layout{
    width: 960px;
    margin: 0 auto;
  }
  body{
    min-width: 960px;
  }
  #header{
    height: 60px;
    background: red;
  }
  #content{
    height: 400px;
    background: blue;
  }
  #footer{
    height: 50px;
    background: yellow;
  }
</style>
<div id="header">
  <div class="layout">头部</div>
</div>
<div id="content" class="layout">内容</div>
<div id="footer">
  <div class="layout">尾部</div>
</div>

双列布局

浮动元素+普通元素marmgin

<style>
    #content:after{
      content: '';
      display: block;
      clear: both;
    }
    .aside{
      width: 200px;
      height: 500px;
      background: yellow;
      float: left;
    }
    .main{
      margin-left: 210px;
      height: 400px;
      background: red;
    }

    #footer{
      background: #ccc;
    }

  </style>
  <div id="content">
    <div class="aside">aside</div>
    <div class="main">content</div>
  </div>
  <div id="footer">footer</div>

三列布局

两侧两列固定宽度,中间列自适应宽度

#content:after{
      content: '';
      display: block;
      clear: both;
    }
    .menu{
      width: 100px;
      height: 500px;
      background: pink;
      float: left;
    }
    .aside{
      width: 200px;
      height: 500px;
      background: yellow;
      float: right;
    }
    .main{
      margin-left: 110px; /*为什么要加margin-left*/
      margin-right: 210px;
      height: 400px;
      background: red;
    }

    #footer{
      background: #ccc;
    }

  <div id="content">
    <!-- 为什么不是main在前面 -->
    <div class="menu">aside</div>
    <div class="aside">aside</div>
    <div class="main">content</div>
  </div>
  <div id="footer">footer</div>

水平等距排列

<style>
ul,li{
  margin: 0;
  padding: 0;
  list-style: none;
}
.ct{
    overflow:hidden;
    width: 640px;
    border:dashed 1px orange;
    margin: 0 auto;
}

.ct .item{
    float:left;
    margin-left: 20px;
    margin-top: 20px;
    width:200px;
    height:200px;
    background: red;
}
.ct>ul{
  margin-left: -20px;
}

</style>
<div class="ct">
  <ul>
    <li class="item">1</li>
    <li class="item">2</li>
    <li class="item">3</li>
    <li class="item">4</li>
    <li class="item">5</li>
    <li class="item">6</li>
    <li class="item">7</li>
    <li class="item">8</li>
  </ul>
</div>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 贫穷的人吃不起饭,做不起好车,治不起病,用不起好药。所以死亡的更快。
    雨中的狮子阅读 1,234评论 0 0
  • stackoverflow的答案 保存变量: 保存变量很简单,只需定义saver对象然后调用save函数就可以 结...
    vincehxb阅读 4,659评论 0 0
  • 站在雪域之巅,俯瞰瞭望之原; 千年的睡梦,拉进了唐古拉山之谷, 任佛光普照,梵语轻鸣, 寻着莲花盛开的地方; 桑耶...
    不羁尘子阅读 1,342评论 0 0
  • 大约在半年前吧,我第一次做饭。 那天清晨,我要求自己做一顿饭。于是,他们同意了,说是给我一次煅炼的机会...
    暖暖小时光阅读 2,047评论 0 3
  • 江南的雨总是那么让人向往,不在于它的漂亮,不在于它的诗意画境,而在于它那让人流连忘返的让人念之不忘的“曼妙身姿...
    宋冬有野阅读 1,117评论 0 0

友情链接更多精彩内容