CSS盒模型

文章详细探讨了CSS中的盒子模型,包括宽高、边框、内边距和外边距的概念,以及如何通过`box-sizing`属性改变元素尺寸计算方式。同时,介绍了内层盒子居中和外层盒子固定宽高的布局技巧。此外,还讨论了定位(绝对定位和相对定位)以及浮动元素的影响,以及BFC(块级格式化上下文)在解决margin合并和高度塌陷问题中的应用。最后,提到了一些其他CSS特性,如伪类、阴影、圆角和背景图像的使用。
摘要由CSDN通过智能技术生成

盒子模型
1、宽高所划分的区域
2、边框
3、内边距
4、外边距
内边距和边框是不占据盒子本身的宽高的

内层盒子居中
外部盒子宽高固定,内层盒子宽高继承外部盒子

在这里插入代码片<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style type="text/css">
        /* .out-box{
            width: 200px;
            height: 200px;
            border: 10px solid green;
            padding: 50px;
            margin: 50px;
        }
        .inner-box{
            width: 100px;
            height: 100px;
            background-color: orange;
        } */
        .box{
            width: 100px;
            height: 100px;
            padding: 30px;
            border: 1px solid #000;
        }
        .box .box1{
            /* width: 100px;
            height: 100px; 或者*/
            width: 100%;
            height: 100%;
            background-color:orange ;

        }
    </style>
</head>
<body>
    <!-- <div class="out-box">
        <div class="inner-box"></div>
    </div> -->
    <div class="box">
        <div class="box1"></div>
    </div>
</body>
</html>

使用box-sizing: border-box; 把边框和内边距收到盒子内部

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style type="text/css">
        .box {
            width: 200px;
            height: 200px;
            padding: 30px;
            background-color: green;
            border: 10px solid #000;
            /* 把边框和内边距收到盒子内部 */
            box-sizing: border-box;
            /* firefox */
            -moz-box-sizing: border-box;
            /* chrome safair */
            -webkit-box-sizing: border-box;
            /* IE8及以下 */
            -ms-box-sizing: border-box;
            /* presto opera */
            -o-box-sizing: border-box;

        }

        .box1 {
            width: 100%;
            height: 100%;
            background-color: orange;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="box1"></div>
    </div>
</body>

</html>

body默认的外边距
ie 8 上下16px 左右 8px
ie7 上下16px 左右 11px

盒子居中
水平居中 margin:0 auto;

定位
绝对定位 :设置了定位后,会在当前的文档层之上在重新建一个文档
层,它就到新建的文档层上去了,所以就会导致下边的元素会上去

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style type="text/css">
        .box1{
            /* 
            * 设置了定位后,会在当前的文档层之上在重新建一个文档
            *层,它就到新建的文档层上去了,所以就会导致下边的元素会上去
             */
            position: absolute;
            width: 100px;
            height: 100px;
            background-color: rebeccapurple;
        }
        .box2{
            width: 200px;
            height: 200px;
            background-color: green;
        }
      
    </style>
</head>

<body>
    <!-- 绝对定位 
        position: absolute;
        left/right top/bottom
    -->
    <div class="box1">
    </div>
    <div class="box2"></div>
</body>

</html>

在这里插入图片描述

在这里插入图片描述
相对定位: 设置相对定位后,依然会新建文档层,但是之前文档层的位置还会保留

绝对定位中的两栏设计

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style type="text/css">
        html,body{
            height: 100%;
            margin: 0;
            overflow: hidden;
        }
        .left{
            height: 100%;
            margin-right: 300px;
            background-color: #bfa;
        }
        .right{
            position: absolute;
            top: 0;
            right: 0;
            width: 300px;
            height: 100%;
            background-color: orange;
        }
    </style>
</head>

<body>
    <!-- 绝对定位 
        position: absolute;
        left/right top/bottom
    -->
    <div class="left">
    left
    </div>
    <div class="right">
    right
    </div>
</body>

</html>

在这里插入图片描述
浮动
浮动流并没有新建一个层,块级元素无法识别浮动流的位置
内联、内联块、浮动、溢出隐藏、纯文本都可以识别浮动元素的位置
float以后元素就变成内联块了

伪类、伪元素
:after
:before
伪元素
::before
::after

<style>
p:before{
content:attr(data-username)
}
</style>
<body>
<p data-username="动态数据">内容</p>
</body>

盒子阴影
box-shadow:水平位置(必填) 垂直位置(必填) 模糊距离 阴影的尺寸 阴影颜色 阴影的种类(inset outset)

.box1{
            width: 300px;
            height: 150px;
            background-color: orange;
            margin: 100px;
            /* 
            box-shadow:水平位置(必填) 垂直位置(必填)
                模糊距离 阴影的尺寸 阴影颜色 阴影的种类(inset outset)
             */
             /* box-shadow: 20px 10px; */
             -webkit-box-shadow: 0px 10px;
             -moz-box-shadow: 20px 0px;
             -o-box-shadow: -20px -10px;
             box-shadow: 20px 10px 20px;
             box-shadow: 20px 10px 5px 10px;

        }

边框圆角
border-radius: 纯圆 设置为50% 但是宽高要一样
半圆角 height/2 px
圆角被非圆角元素隐藏 解决方法:设置圆角元素的overflow:hidden

 -webkit-border-radius:20px;
 -moz-border-radius: 20px ;
 -o-border-radius: 20px ;

bakckground-image: url(图片地址不用写引号)

 .box1{
            width: 300px;
            height: 300px;
            margin: 100px;
            border: 1px solid #000;
           background-image: url(../DOM/slider/img/1.jpg); 
           background-size: 300px 300px;//css3属性
           /* 或者 
           background-size: 100% 100%;
           background-size: cover
		   background-size: contain
           */
           background-repeat:no-repeat|repeate|repeat-x|repeat-y;
            background-position:left、right、bottom、top、center中的两两组合| 两个都是百分比形式|写两个像素;
            background-position:left center;
            background-position:50% 50%;
            background-position:10px 10px;
            background-attachment: fixed;背景图片不滚动,默认是scroll
        /*
        复合值写法
        */  
         background: background-color background-image
                       background-repeate background-attachment 
                       background-position/background-size   
        }   

logo设置主要注意设置overflow: hidden;height:0; padding-top: 容器的高度

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style type="text/css">
        .logo{
            width: 200px;
            height: 114px;   
            /* border: 1px solid #000; */
        } 
        .logo h1 .logo-hd{
            display: block;
            width: 142px;
            height: 0;
            padding-top: 114px;
            background: url(../DOM/slider/img/1.jpg) no-repeat 0 0/200px 114px;
            border: 1px solid #000;
            overflow: hidden;
        }
    </style>
</head>

<body>
    <div class="logo">
        <h1>
            <a href="" class="logo-hd">
                myLOGO
            </a>
        </h1>
    </div>
</body>

</html>

table样式

这样写单元格有边框 table{border: 1px solid #000 }这样写单元格没边框 设置标题样式 caption-side:top(默认值);bottom border-collapse: separation|collapse; 单元格宽度保持一致table-layout: automatic|fixed; ```html Document
表格的标题,可以单独定义样式
12
12
42
``` block formatting contexts 块级格式化上下文 BFC(css2.1) 普通流 normal flow 浮动流 float flow 脱离普通流,左右偏移,最早用于解决文字环绕 绝对定位布局 absolute positioning 脱离了文档流,不会影响任何兄弟元素的布局,由左边绝对的 BFC元素 body float: left|right position: absolute | fixed display:inline-block|table-cell overflow: hidden|auto|scroll BFC元素的作用 1、margin合并问题
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style type="text/css">
  .box{
      width: 200px;
      height: 200px;
  }
  .box1{
      background-color: #baf;
      margin-bottom: 100px;
  }
  .box2{
      background-color:#bfa;
      margin-top: 100px;

  }
  </style>
</head>

<body>
  <div class="box box1">
  </div>
  <div class="box box2">
  </div>
</body>

</html>

解决:
1、放在两个BFC元素里
2、不解决,直接把外边距写在一个盒子里就好
2、高度塌陷问题

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style type="text/css">
      .box {
          width: 200px;
          border: 10px solid #000;
      }

      .box1 {
          float: left;
          width: 100px;
          height: 100px;
          background-color: #baf;
      }

      .box2 {
          float: left;
          width: 100px;
          height: 100px;
          background-color: #bfa;

      }
  </style>
</head>

<body>
  <div class="box">
    <div class="box1">
    </div>
    <div class="box2">
    </div>
  </div>

</body>

</html>

解决将父级元素变成BFC

3、margin塌陷

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style type="text/css">
      .box {
         /*解决方法设置border*/
          /*border-top: 1px  solid transparent;*/
          /*解决方法触发BFC*/
          display: inline-block;
          width: 200px;
          height: 200px;
          background-color: #bfa;
      }

      .box1 {
          width: 50px;
          height: 50px;
          margin: 0 auto;
          margin-top: 100px;
          background-color: #baf;
      }
  </style>
</head>

<body>
  <div class="box">
    <div class="box1">
    </div>
  </div>

</body>

</html>

在这里插入图片描述
所有设置了float的元素都自动转换为inline-block
4、BFC可以解决浮动元素覆盖的问题
CSS书写顺序
显示属性: display, position, float, clear
自身属性:width, height,margin,padding,border,background
文本属性:color, font, text-align,vertical-align,whitespace

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值