美文网首页
盒子模型

盒子模型

作者: Khada | 来源:发表于2018-06-19 20:25 被阅读0次
  • 一个盒子我们会分成几个部分:
    内边区(content)
    内边距(padding)
    边框(border)
    外边距(margin)

  • 内容区
    内容区指的是盒子中放置内容的区域,也就是元素中的文本内容,子元素都是存在于内容区中的。
    如果没有为元素设置内边距和边框,则内容区大小默认和盒子大小是一致的。
    通过width和height两个属性可以设置内容区的大小。
    width和height属性只适用于块元素。

  • 内边距
    内边距指的是元素内容区与边框以内的空间。
    默认情况下width和height不包含padding的大小。
    使用padding属性来设置元素的内边距
    padding:10px 20px 30px 40px
    设置元素的上、右、下、左四个方向的内边距。
    padding:10px 20px 30px;
    设置元素的上、左右、下四个方向的内边距。
    padding:10px 20px;
    设置元素的上下、左右四个方向的内边距。
    padding:10px
    设置元素上下左右四个方向的内边距。
    同时在css中还提供了padding-top、padding-right、padding-left、padding-bottom分别用来指定四个方向的内边距。

    .box1{
      width: 200px;
      height: 200px;
      background-color: red;
      /*设置边框*/
      border: 10px black solid;
      padding: 100px 100px 100px 100px;
    }
    /*创建一个子元素box2占满box1*/
    .box2{
      width: 100%;
      height: 80%;
      background-color: yellow;
    }
    
  • 边框
    可以在元素周围创建边框,边框是元素可见框的最外部。
    可以使用border属性来设置盒子的边框。
    和padding一样,默认width和height并包括边框的高度。
    border:1px red solid
    分别指定了边框的宽度、颜色、样式。
    使用border-top/left/right/bottom分别指定上左右下四个边框的方向。
    边框的多种样式

--none 没有边框
--dotted 点线
--dashed 虚线
--solid 实线
--double 双线
--groove 槽线
--ridge 脊线
--inset 凹边
--outset 凸边
  • 外边距
    外边距是元素边框与周围元素相距的空间。
    使用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>
    

相关文章

  • 面试题(DAT)

    1.什么是盒子模型?盒子模型有两种,标准盒子模型和IE盒子模型。标准盒子模型:width = content + ...

  • 【IMWeb秋招训练营】【Day1】面试题总结

    1.什么是盒子模型? 盒子模型有两种,标准盒子模型和IE盒子模型。 标准盒子模型:width = content ...

  • CSS盒子模型

    标准盒子模型 IE盒子模型 区别 在我看来标准盒子模型也就是: IE盒子模型:

  • 前端面试之HTML+CSS(八股文)

    1. 盒子模型,普通盒子模型和怪异盒子模型有什么区别? 答:普通盒子模型又称标准W3C盒子模型,标准盒子模型的Wi...

  • css基础问题

    介绍css的盒子模型 css的盒子模型有两种:IE盒子模型,W3C盒子模型盒子模型:内容(content),内边距...

  • CSS盒模型概述

    盒子模型: 盒子模型,又称框模型 (Box Model) 盒子模型主要的属性:width、height、paddi...

  • CSS 盒子模型

    CSS盒子模型 盒子模型边框 CSS盒子模型的宽度和高度 CSS模型的填充 CSS盒子模型的边界 填充和边界的区别

  • CSS

    1. 盒子模型 1.1 标准盒子模型 标准盒子模型包括margin、border、padding、content,...

  • js笔记五十一之js盒子模型

    js盒子模型 CSS盒子模型 ,margin - border - padding - content JS盒子...

  • 笔记:两种盒子模型——ie 盒子模型和标准 w3c 盒子模型。

    其实盒子模型有两种,分别是ie 盒子模型和标准 w3c 盒子模型。 W3C 盒子模型: W3C 盒子模型的范围包括...

网友评论

      本文标题:盒子模型

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