盒子模型简单布局

  • 外边距
    外边距是元素边框与周围元素相距的空间。
    使用margin属性可以设置外边距。
    使用margin-top/right/left/bottom为元素设置四个方向。
    使用margin:0 auto 可以使元素居中。

    .box1{
      width: 200px;
      height: 200px;
      background-color: #bfa;
      border: 10px solid red;
      margin:10px 20px 30px 40px;
     }
    .box2{
      width: 200px;
      height: 200px;
      background-color: yellow;
     }
    
  • display
    通过display来修改元素的性质。
    --block:设置元素为块元素
    --inline:设置元素为行内元素
    --inline-block:设置元素为行内块元素
    --none: 隐藏元素
    visibility
    visibility属性主要用于元素是否可见
    -visible:可见的
    -hidden: 隐藏的
    overflow
    当相关标签里面的内容超出了样式的宽度和高度使,就会发生一些奇怪的事情,浏览器会让内容溢出盒子。
    可以通过overflow来控制内容溢出的情况。
    可选值:
    *--visible:默认值
    --scroll:添加滚动条
    --auto:根据需要添加滚动条
    --hidden:隐藏超出盒子的内容
    文档流
    文档流指的是文档中可实现的对象在排列时所占用的位置,
    将窗体自上而下分为一行行,并在每行中按从左至右的顺序排放元素,即为文档流。
    也就是说在文档流中元素默认会紧贴到上一个元素的右边,如果右边不足以放下元素,元素则会另起一行,在新的一行中继续从左至右摆放。
    这样一来每一个块元素都会另起一行,那么我们如果想在文档流中进行布局就会变得比较麻烦。
    浮动
    所谓浮动指的就是使元素脱离原来的文本流,在父元素中浮动起来。

    --none: 不浮动
    --left:向左浮动
    --right: 向右浮动
    clear: 清除浮动

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>浮动</title>
        <style type="text/css">
        .box1{
            width: 600px;
            height: 200px;
            background-color: red;
            float: left;
        }
        .box2{
          width: 200px;
          height: 200px;
          background-color: yellow;
          float: left;
        }
        .box3{
          width: 200px;
          height: 200px;
          background-color: green;
          float: left;
        }
        </style>
    </head>
    <body>
        <div class="box1"></div>
        <div class="box2"></div>
        <div class="box3"></div>
    </body>
    </html>
    
  • 内联素浮动

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>内联素的浮动</title>
    <style type="text/css">
    .box1{
      background-color: #bfa;
    }
    .s1{
      float: left;
      width: 100px;
      height: 100px;
      background-color: yellow;
    }
    </style>
    </head>
    <body>
    <div class="box1">a</div>
    <span class="s1">hello</span>
    </body>
    </html>
    
  • 简单布局

      <!DOCTYPE html>
      <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>简单布局</title>
    <style type="text/css">
    *{
      margin: 0;
      padding: 0;
    }
    .header{
      width: 1000px;
      height: 150px;
      background-color: yellowgreen;
      margin: 0 auto;
    }
    .content{
      width: 1000px;
      height: 400px;
      background-color: orange;
      margin: 10px auto;
    }
    .left{
      width: 200px;
      height: 100%;
      background-color: skyblue;
      float: left;
    }
    .center{
      width: 580px;
      height: 100%;
      background-color: yellow;
      float: left;
      margin: 0 10px;
    }
    .right{
      width: 200px;
      height: 100%;
      background-color: pink;
      float: left;
    }
    .footer{
      width: 1000px;
      height: 150px;
      background-color: silver;
      margin: 0 auto;
    }
    </style>
    </head>
    <body>
    <div class="header"></div>
    <div class="content">
    <div class="left"></div>
    <div class="center"></div>
    <div class="right"></div>
     </div>
     <div class="footer"></div>
    </body>
    </html>
    
  • 相对定位

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>相对定位</title>
    <style type="text/css">
    .box1{
      height: 200px;
      background-color: red;
      position: relative;
      }
    .box2{
      width: 200px;
      height: 200px;
      background-color: yellow;
      position: relative;
      left: 100px;
      top: 200px;
     }
      .box3{
       width: 200px;
      height: 200px;
      background-color: yellowgreen;
    }
    .s1{
      position: relative;
      width: 200px;
      height: 200px;
      background-color: yellow;
    }
     </style>
    </head>
    <body>
    <div class="box1"></div>
    <div class="box2"></div>
    <div class="box3"></div>
    <span class="s1">我是一个span</span>
    </body>
    </html>
    
  • 绝对定位

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>绝对定位</title>
    <style type="text/css">
      .box1{
      width: 200px;
      height: 200px;
      background-color: red;
      }
      .box2{
      width: 200px;
      height: 200px;
      background-color: yellow;
      position: absolute;
      }
      .box3{
      width: 300px;
      height: 300px;
      background-color: yellowgreen;
      }
      .box4{
      width: 300px;
      height: 300px;
      background-color: orange;
      }
      .s1{
      width: 100px;
      height: 100px;
      background-color: yellow;
      position: absolute;
    }
    </style>
    </head>
    <body>
    <div class="box1"></div>
    <div class="box5">
    <div class="box4">
    <div class="box2"></div>
    </div>
    </div>
    <div class="box3"></div>
    
    <span class="s1">我是一个span</span>
    </body>
    </html>
    
  • 固定定位

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>固定定位</title>
    <style type="text/css">
    .box1{
      width: 200px;
      height: 200px;
      background-color: red;
    }
    .box2{
      width: 200px;
      height: 200px;
      background-color: yellow;
      position: fixed;
      left: 0px;
      top: 0px;
    }
    .box3{
      width: 200px;
      height: 200px;
      background-color: yellowgreen;
    }
    </style>
    </head>
    <body style="height: 5000px;">
    <div class="box1"></div>
    
    <div class="box4" style="width: 300px; height: 300px; background-      color: orange; position: relative;">
    <div class="box2"></div>
        </div>
    
    <div class="box3"></div>
    </body>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一个盒子我们会分成几个部分:内边区(content)内边距(padding)边框(border)外边距(margi...
    Khada阅读 254评论 0 0
  • 1. tab列表折叠效果 html: 能源系统事业部 岗位名称: 工作地点 岗位名...
    lilyping阅读 1,909评论 0 1
  • 本文是针对刚学编程的小白,都是一些基础知识,如果想了解更多深层一点的东西,欢迎移步本人博客!! 博客地址 点击跳转...
    西巴撸阅读 557评论 0 0
  • 一、在什么场景下会出现外边距合并?如何合并?如何不让相邻元素外边距合并?给个父子外边距合并的范例 在CSS当中,相...
    dengpan阅读 603评论 0 0
  • 7c2764878307阅读 224评论 0 0