盒子模型

  • 一个盒子我们会分成几个部分:
    内边区(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>
    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 161,326评论 4 369
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,228评论 1 304
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 110,979评论 0 252
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,489评论 0 217
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,894评论 3 294
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,900评论 1 224
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,075评论 2 317
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,803评论 0 205
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,565评论 1 249
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,778评论 2 253
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,255评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,582评论 3 261
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,254评论 3 241
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,151评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,952评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,035评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,839评论 2 277