美文网首页
3.如何用CSS进行网页布局

3.如何用CSS进行网页布局

作者: 单纯的土豆 | 来源:发表于2016-05-03 20:19 被阅读123次

最常见的是混合布局。

制作布局案例:

1.一列布局

<!DOCTYPE>
<html>
<head>
  <meta charset="UTF-8">
  <title></title>
  <style type="text/css">
    body{margin:0;padding-left: 0;}
    .top{height: 100px;background:blue;}
    .main{width:800px;height:300px;background:#CCC;margin:0 auto;}  /* margin:0 auto;居中 */
    .foot{width:800px;height:100px;background:#900;margin: 0 auto;}
  </style>
</head>
<body>
   <div class="top"></div>
   <div class="main"></div>
   <div class="foot"></div>
</body>
</html>

效果如下:

2.两列布局

<!DOCTYPE>
<html>
<head>
  <meta charset="UTF-8">
  <title></title>
  <style type="text/css">
    body{margin:0;padding: 0;}
    .left{width:20%;height:500px;float:left;background: #CCC;}
    .right{width:80%;height:500px;float:right;background: #ddd;}
  </style>
</head>
<body>
   <div class="left"></div>
   <div class="right"></div>
</body>
</html>

效果如下:

该效果是两列自适应,会随着浏览器的缩放自动调整页面的宽带

自适应宽度的两列布局用的很少,用得比较多的是固定宽度的两列布局。

增加一个父级,代码如下:

<!DOCTYPE>
<html>
<head>
   <meta charset="UTF-8">
   <title></title>
   <style type="text/css">
     body{margin:0;padding: 0;}
     .main{width:800px;margin:0 auto;}
     .left{width:20%;height:500px;float:left;background: #CCC;}
     .right{width:80%;height:500px;float:right;background: #ddd;}
   </style>
</head>
<body>
   <div class="main">
      <div class="left"></div>
      <div class="right"></div>
   </div>
</body>
</html>

效果如下:

浮动(float)和 绝对定位(position:absolute)两个css设置,可以让元素脱离文档流。

3.三列布局

<!DOCTYPE>
<html>
<head>
  <meta charset="UTF-8">
  <title></title>
  <style type="text/css">
     .left{width:33.33%;height: 500px;float:left;background: #CCC;}
     .middle{width:33.33%;height: 500px;float:left;background: #999;}
     .right{width:33.33%;height: 500px;float:right;background: #ddd;}
   </style>
 </head>
 <body>
      <div class="left"></div>
      <div class="middle"></div>
      <div class="right"></div>
 </body>
 </html>

效果如下(自适应的三列布局):

修改代码后利用浮动看三个板块是否能排列在一起

 <!DOCTYPE>
 <html>
 <head>
   <meta charset="UTF-8">
   <title></title>
   <style type="text/css">
     .left{width:200px;height: 500px;float:left;background: #CCC;}
     .middle{height: 500px;float:left;background: #999;}
     .right{width:300px;height: 500px;float:right;background: #ddd;}
   </style>
 </head>
 <body>
     <div class="left">200px</div>
     <div class="middle">qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq</div>
     <div class="right">300px</div>
 </body>
 </html>

实际效果如下:利用浮动后,排列都乱了

如何实现他们都在一排中呢?去掉浮动,使用定位

  <!DOCTYPE>
  <html>
  <head>
   <meta charset="UTF-8">
   <title></title>
   <style type="text/css">
      .left{width:200px;height: 500px;float:left;background: #CCC;position: absolute;left:0;top:0;}
      .middle{height: 500px;background: #999;margin: 0 300px 0 200px;}
      .right{width:300px;height: 500px;float:right;background: #ddd;position: absolute;right:0;top:0;}
   </style>
 </head>
 <body>
     <div class="left">200px</div>
     <div class="middle">qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq</div>
     <div class="right">300px</div>
 </body>
 </html>

实际效果:

4.混合布局

 <!DOCTYPE>
 <html>
 <head>
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
         body{margin:0;padding-left: 0;}
         .top{height: 100px;background:blue;}
         .main{width:800px;height:300px;background:#CCC;margin:0 auto;}  
         .left{width:200px;height: 600px;background: yellow;float:left;}
         .right{width:600px;height: 600px;background: #369;float:right;}
         .foot{width:800px;height:100px;background:#900;margin:0 auto;}
    </style>
 </head>
 <body>
    <div class="top"></div>
    <div class="main">
          <div class="left"></div>
          <div class="right"></div>
    </div>
    <div class="foot"></div>
 </body>
 </html>

实际效果:

分割小模块:

代码如下:

 <!DOCTYPE>
 <html>
 <head>
   <meta charset="UTF-8">
   <title></title>
   <style type="text/css">
      body{margin:0;padding-left: 0;}
      .top{height: 100px;background:blue;}
      .head{height: 100px;width:800px;background:#f60;margin:0 auto;}
      .main{width:800px;height:300px;background:#CCC;margin:0 auto;}  
      .left{width:200px;height: 600px;background:yellow;float:left;}
      .right{width:600px;height: 600px;background:#369;float:right;}
      .sub_l{width:400px;height: 600px;background:green;float:left;}
      .sub_r{width:200px;height: 600px;background:#09F;float:right;}
      .foot{width:800px;height:100px;background:#900;margin:0 auto;}
   </style>
 </head>
 <body>
    <div class="top">
        <div class="head"></div>
    </div>
    <div class="main">
        <div class="left"></div>
        <div class="right">
            <div class="sub_l"></div>
            <div class="sub_r"></div>
        </div>
    </div>
    <div class="foot"></div>
  </body>
  </html>

实际效果如下:

相关文章

  • 3.如何用CSS进行网页布局

    最常见的是混合布局。 制作布局案例: 1.一列布局 效果如下: 2.两列布局 效果如下: 该效果是两列自适应,会随...

  • 如何用CSS进行网页布局

    1-1, 内容简介 使用 % 可以令网页自适应宽度 一般网页分为头部、 主体和底部~ 单列布局Div{width:...

  • DIV+CSS对SEO网站优化好处有哪些?

    div+css布局是一种网页的布局方法,是目前应用最广泛的网页布局方法。div css布局是把网页用div+css...

  • 1.网页布局基础

    网页布局基础 什么是网页布局?网页布局是网页制作的基础(DIV+CSS网页布局) 分类:流式布局,浮动布局,绝对定...

  • 2018-07-25

    ## 1.网页布局HTML+CSS **技能要求** * 学会用工具(*如PS*)切图,将设计稿还原成网页布局 *...

  • css基本知识

    认识css css作用:给网页中的每个元素进行化妆,排版布局,让网页更精美 完全没有使用css的页面:基本就是一堆...

  • 2018-06-24

    css 网页布局学习心得 1.左右布局 2.左中右布局 3.水平布局 4.垂直布局 5.其他小技巧 在浮动布局时,...

  • PHP从入门到精通,024第三章CSS之DIV+CSS标准化布局

    四、DIV+CSS标准化布局 (一)、DIV+CSS布局 说明:在网页开发制作中,需要对页面内容进行“模块化标准布...

  • CSS浮动

    CSS浮动 传统网页布局的三种方式 网页布局的本质——用 CSS 来摆放盒子。 把盒子摆放到相应位置. CSS 提...

  • 6、盒子模型 边框、圆角、阴影、内外边距、外边距塌陷

    1、网页布局的本质 网页布局的核心本质: 就是利用 CSS 摆盒子。 网页布局过程: 先准备好相关的网页元素,网页...

网友评论

      本文标题:3.如何用CSS进行网页布局

      本文链接:https://www.haomeiwen.com/subject/ctkrrttx.html