0基础学web----CSS盒子模型

01-优先级-基本测试

<!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>
        /* 在属性后方加上 !important为最高级 */
        div{
            color:greenyellow  !important;
        }
       /* 1.继承 */
        body{
            color: blue;
        }
        /* 标签 */
        div{
            color: blueviolet;
        }
        /* 类选择器 */
        .box{
            color: brown;
        }
        /* id选择器 */
        #box{
            color: cornflowerblue;
        }

        
    </style>
</head>

<body>
    <!-- style="color: darkgreen"   行内样式 -->
    <div class="box"  id="box" style="color: darkgreen">我们看看这选择器优先级</div>
    
</body>
</html>

注:继承<通配符选择器<标签选择器<类选择器<id选择器<行内样式<!important

记忆技巧:选择器越精确,等级越高。

02-优先级-叠加计算

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
      /* 叠加计算(行内样式 id选择器 类选择器 标签选择器) */
      /* (0,1,0,1) */
    div #one {
      color: orange;
    }
    /* (0,0,2,0) */
    .father .son {
      color: skyblue;
    }
    /* (0,0,1,1) */
    .father p {
      color: purple;
    }
    /* (0,0,0,2) */
    div p {
      color: pink;
    }
    
  </style>
</head>
<body>
  <div class="father">
    <p class="son" id="one">我是一个标签</p>
  </div>
</body>
</html>

03-优先级-计算题

 

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>第1题:普通题</title>
	<style>
		/* 优先级计算(行内样式 id选择器 类选择器 标签选择器) */
		/* (0,2,0,0) */
		#father #son {
			color:blue; 
		} 
		/* (0,1,1,1) */
		#father p.c2 {
			color:black;
		} 
		/* (0,0,2,2) */
		div.c1 p.c2 {
			color:red;
		} 
		/* (0,1,0,0) */
		#father { 
			color:green !important;
		} 
		/* (0,1,1,1) */
		div#father.c1 {
			color: yellow ;
		} 

		/* 最终输出:color:blue; */
	</style>
</head>
<body>
	<div id="father" class="c1">
		<p id="son" class="c2">
			这行文本是什么颜色的? 
		</p>
	</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>第2题: 标签选择器选择一类</title>
	<style>
		/* (0,0,0,2) */
	  div div {
			color: skyblue;
		} 
		/* (0,0,0,1) */
		div {
			color: red;
		}
		/* 最终输出:color: skyblue; */
	</style>
</head>
<body>
	<div>
		<div>
			<div>
				这行文本是什么颜色?
			</div>
		</div>
	</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>第3题: 权重叠加每位不存在进制</title>
	<style>
		/* (0,0,0,12) */
		div div div div div div div div div div div div {  
			color: red;
		}
		/* (0,0,1,0) */
		.one { 
			color: pink;
		}
		/* 最终输出:color: pink; */
	</style>
</head>
<body>
	<div>
		<div>
			<div>
				<div>
					<div>
						<div>
							<div>
								<div>
									<div>
										<div>
											<div>
												<div class="one">这行文字是什么颜色的?</div>
											</div>
										</div>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>第4题:权重相同此时比较层叠性</title>
	<style>
		/* (0,0,2,1) */
		.c1 .c2 div { 
			color: blue;
		}
		/* (0,1,0,1) */
		div #box3 {
			color:green;
		}
		/* (0,1,0,1) */
		#box1 div {
			color:yellow;
		}

		/* 最后结果:color:yellow; */
	</style>
</head>
<body>
	<div id="box1" class="c1">
		<div id="box2" class="c2">
			<div id="box3" class="c3">
				这行文本是什么颜色的?
			</div>
		</div>
	</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>第5题: 全是继承的特殊情况</title>
	<style>
		/* 如果都是继承,继承里面谁高,看继承哪个父级,哪个父级高,哪个选择器生效 */
		/* (0,0,0,2) */
		div p {
			color:red;
		} 
     	/* (0,0,1,0) */
		.father {
			color:blue;
		} 
		/* 最后结果:color:red; */
	</style>
</head>
<body>
	<div class="father">
		<p class="son"> 
			<span>文字</span>
		</p>
	</div>
</body>
</html>

04-谷歌-排错

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .fahter .son .sun {
      color: blue;
    }
    /*选择器fahter与father不一样*/
  </style>
</head>
<body>
  <div class="father">
    <div class="son">
      <div class="sun">孙子</div>
    </div>
  </div>
</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .father .son .sun div {
      color: red;
    }
    /* .sun已经是最精确位置了,再加上div变成更加模糊导致错误 */
  </style>
</head>
<body>
  <div class="father">
    <div class="son">
      <div class="sun">孙子</div>
    </div>
  </div>
</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .father .son .sun {
      color:red;
      font-size: 20px;
    }
    /* color:red;里面的:是中文符号 */
  </style>
</head>
<body>
  <div class="father">
    <div class="son">
      <div class="sun">孙子</div>
    </div>
  </div>
</body>
</html>

 

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    }

    .father .son .sun {
      color: orange;
    }
    /* style样式多了个} */
  </style>
</head>
<body>
  <div class="father">
    <div class="son">
      <div class="sun">孙子</div>
    </div>
  </div>
</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .father .son .sun {
      color: red
      font-size: 20px;
    }
    /* color: red少了一个;符号 */
  </style>
</head>
<body>
  <div class="father">
    <div class="son">
      <div class="sun">孙子</div>
    </div>
  </div>
</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .father .son .sun {
      color: redd;
      font-size: 20px;
    }
    /* color: redd;找不到属性redd */
  </style>
</head>
<body>
  <div class="father">
    <div class="son">
      <div class="sun">孙子</div>
    </div>
  </div>
</body>
</html>

05-PxCook

06-组成

<!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>
        div{
            width: 300px;
            height: 300px;
            background-color: red;
            border: 10px solid yellow;
            margin: 30px;
        }

    </style>
</head>
<body>
    <div>111</div>
    <div>222</div>
</body>
</html>

07-浏览器效果

08-内容width和height

<!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>
        div {
            width: 300px;
            height: 300px;
            background-color: red;
        }
    </style>
</head>
<body>
    <div></div>
</body>
</html>

09-border使用方法

 

 

<!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>
        div{
            width: 300px;
            height: 300px;
            background-color: #87ceed;
            border-bottom: 10px solid #ffa500;
        }
    </style>
</head>
<body>
   <div></div>
</body>
</html>

10-尺寸计算-border

<!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>
        div{
            /* 盒子设置,宽高颜色 */
            width: 300px;
            height: 300px;
            background-color: red;
         /* 注意点:border会把盒子撑大 */
            border: 20px solid yellow;
        }
              /* 
      盒子的实际大小(宽度)=左边框+内容的宽度+右边框
                        = 20+300+20
                        = 340
      解决:现在是340 , 要求300 ,多出了40px
      手动内减:计算出多余的部分之后,手动在内容中减去即可
       */

    </style>
</head>
<body>
    <div></div>
</body>
</html>

11-案例

<!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>
        /* 清除标签默认的margin和padding */
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            height: 40px;
            background-color: #fff;
            border-top: 3px solid #ff8500;
            border-bottom: 1px solid #4c4c4c;
        }

        div a {
            /* 转换行内块 */
            display: inline-block;
            width: 80px;
            height: 40px;
            color: #4c4c4c;
            text-decoration: none;
            text-align: center;
            font-size: 12px;

            line-height: 40px;
        }

        div a:hover {
            background-color: #edeef0;
            color: #ff8500;
        }
    </style>
</head>

<body>
    <div class="box">
        <a href="#">新浪微博</a>
        <a href="#">新浪微博</a>
        <a href="#">新浪微博</a>
        <a href="#">新浪微博</a>
    </div>
</body>

</html>

14-padding-多值

<!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>
        div{
            width: 100px;
            height: 100px;
            background-color: red;
            border: 2px solid black;


            padding: 10px 20px;

            /* padding: 10px;  是指内边框四个方向都是10px */
            /* padding: 10px 20px;  是指上下是10px 左右是20px */
            /* padding 有  left、right、top、bottom 四个方向属性*/
        }
    </style>
</head>
<body>
    <div>我是盒子内容我是盒子内容我是盒子内容</div>
</body>
</html>

15-尺寸-border和padding

 

<!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>
        div{
            width: 300px;
            height: 300px;
            background-color: red;
           /* border会撑大盒子 */
            border: 10px solid black;
              /* padding也会撑大盒子 */
            padding:20px ;

              /* 
        盒子的实际大小(宽度)=左border+左padding+内容的宽度+右padding+右border
                          =10+20+300+20+10
                          = 360

        要求:300
        现在:360
        多出了:60
        解决:手动内减
       */
        }
    </style>
</head>
<body>
    <div></div>
</body>
</html>

16-新浪导航-padding优化

<!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>
        /* 清除标签默认的margin和padding */
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            height: 40px;
            background-color: #fff;
            border-top: 3px solid #ff8500;
            border-bottom: 1px solid #4c4c4c;
        }

        div a {
            /* 转换行内块 */
            display: inline-block;
            /* width: 80px; */
       
            height: 40px;
            /*内边距距离左右20*/
            padding: 0 20px;

            color: #4c4c4c;
            text-decoration: none;
            text-align: center;
            font-size: 12px;

            line-height: 40px;
        }

        div a:hover {
            background-color: #edeef0;
            color: #ff8500;
        }
    </style>
</head>

<body>
    <div class="box">
        <a href="#">新浪微博</a>
        <a href="#">新浪微博新浪微博新浪微博</a>
        <a href="#">新浪微博</a>
        <a href="#">新浪微博</a>
    </div>
</body>

</html>

注:解决了导航栏字体过多自动换行问题 

17-内减模式

<!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>
        div{
            width: 300px;
            height: 300px;
            background-color:pink;
            /* 外边框 */
            border: 10px solid black;
            /* 内边距 */
            padding: 20px;

            /*自减盒子宽和高*/
            box-sizing: border-box;


            
      /* 
        要求:300*300
        现在:360*360
        多出:60

        解决1:手动内减
        解决2:自动内减
        只要设置一个属性,浏览器会自动计算多出了多少,自动在内容中减去

          CSS3盒模型:
          属性名:box-sizing
          属性值:border-box
       */
        }
    </style>
</head>
<body>
    <div></div>
</body>
</html>

18-外边距

 

 

<!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>
        div{
            width: 300px;
            height: 300px;
        }
        .div1{
           
            background-color: red;
      
             /* 1、margin的取值 */
             /* margin: 10px; */

             /* margin: 上下 左右 ;  */
             /* margin: 10px 20px ; */

            /* margin: 上10px 右20px 下30px 左20px ; */
             /* margin: 10px 20px 30px; */
        
                /* margin: 上10px 右20px 下30px 左40px ; */
             margin: 10px 20px 30px 40px;
           /* 2、margin的单方向 */

           /* margin-left: 20px;
           margin-bottom: 100px; */


        }
        .div2{
            background-color:darkcyan;
        }
    </style>
</head>
<body>
    <div class="div1"></div>
    <div class="div2"></div>
</body>
</html>

 

19-清楚默认样式

<!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>
        div{
            width: 300px;
            height: 300px;
            background-color: red;
        }
            /* blockquote, body, button, dd, dl, dt, fieldset, form, h1, h2, h3, h4, h5, h6, hr, input, legend, li, ol, p, pre, td, textarea, th, ul {
      margin: 0;
      padding: 0;
    } */

    * {
      margin: 0;
      padding: 0;
    }
    </style>
</head>
<body>
    <div></div>
</body>
</html>

20-版心居中

<!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>
        div{
            width: 300px;
            height: 300px;
            background-color: pink;
            margin: 0 auto;
        }
    </style>
</head>
<body>
    <div>版心</div>
</body>
</html>

21-综合案例-新闻

<!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>
        * {
            
            /* 外边距 */
            margin: 0;
            /* 内边距 */
            padding: 0;
        }

        body {
            /* 去除行高带来的默认间隙,完成精准布局 */
            line-height: 1;
        }


        div {
            /* 宽 */
            width: 500px;
            /* 高 */
            height: 400px;
            /* 背景颜色 */
            background-color: #fff;
            /* 边框 */
            border: 1px solid #ccc;
            /* 外边框  上下50  左右居中 */
            margin: 50px auto;

             /* 自动内减 */
            box-sizing: border-box;

        }

        div h2 {
            /* 边框底端线 */
            border-bottom: 1px solid #ccc;
            /* 外边距 顶端*/
            margin-top: 30px;
            /* 字体大小 */
            font-size: 30px;
            /* 外边距左边 */
            margin-left: 30px;
            /* 外边距右端 */
            margin-right: 30px;
            /* 行高 */
            line-height: 50px;
        }

        div ul {
            /* 消除li的小点 */
            list-style: none;
            /* 行高 */
            line-height: 50px;
        }

        div ul li {
         /* 边框底端 */
          border-bottom: 1px dashed #ccc;
          /* 外边距左边 */
          margin-left: 30px;
          /* 外边距右边 */
          margin-right: 30px;

        }
        div ul li a {
            /* 字体颜色 */
            color: #666;
            /* 字体大小 */
            font-size: 18px;
            /* 去a标签下划线 */
            text-decoration: none;
              /* 内边距左边 */
            padding-left: 30px;
            /* 内边距底端 */
            padding-bottom: 10px;
             /* 自动内减 */
            box-sizing: border-box;

        }
 
    </style>
</head>

<body>
    <div>
        <h2>最新文章/新文章</h2>
        <ul >
            <li><a href="#" class="box">北京招聘网页设计,平面设计,php</a></li>
            <li><a href="#">体验javascript的魅力</a></li>
            <li><a href="#">jquery世界来临</a></li>
            <li><a href="#">网页设计师的梦想</a></li>
            <li><a href="#">jquery中的链式编程是什么</a></li>
        </ul>
    </div>
</body>

</html>

24-行内元素的垂直内外边距

 

<!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>
        .div1{
            width: 300px;
            height: 300px;
            background-color: red;
            /* margin-bottom: 50px; */
            margin-bottom: 60px;
        }
        .div2{
            width: 300px;
            height: 300px;
            background-color: red;
            margin-bottom: 50px;
        }
    </style>
</head>
<body>
    <div class="div1"></div>
    <div class="div2"></div>
</body>
</html>

注:当两个margin的像素相同交接时,则会取一个数。如果两个margin的像素不一样时,则取最大像素。

 图下所示,当在子元素设置外边距margin-top:50px;时,子元素没有作用,反而父类元素起了作用。

 

在父类设置 overflow: hidden;则正常输出

<!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>
        .div1{
            width: 300px;
            height: 300px;
            background-color: red;

            /* 设置 overflow: hidden;则正常输出 */
            overflow: hidden;
        }
        .box{
            width: 100px;
            height: 100px;
            background-color:pink;

            /* 外边距上距离 */
            margin-top: 50px;
        }
    </style>
</head>
<body>
    <div class="div1">
        <div class="box"></div>
    </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>
        span{
            /* margin: 100px 200px; */

            padding: 200px  100px;
        }
    </style>
</head>
<body >
    <span>111</span>
    <span>222</span>
</body>
</html>

 

想要视频教学?

请点击下方链接吧!

https://www.bilibili.com/video/BV1Kg411T7t9?p=1

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

情绪员Tim

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值