css_7_盒子模型

盒子模型

边框线

border

<div id="box">
   xxxx
</div>

<style>

    #box{
        width:200px;
        height:200px;
        background:transparent; /**透明色/
        /* 边框的属性 */
        border-width:10px;
        border-color:red;
        border-style:solid;
        /* 
        	常用样式
        	solid:实线
        	dashed:虚线
        	datted:点线
        	double:双实线
        */
        
        /* border: 1px solid red */
        
        /*
        	border-top:设置边框顶线
        	border-bottom:设置边框底线
        	border-left:设置边框左线
        	border-right:设置边框右线
        */
        
        /*
        	border-top-color:设置边框顶线颜色
        		......
        */
    }
    
</style>

外边距

margin:设置与其他元素边界的外边距离

<div id="box1">
    
</div>
<div id="box2">
    
</div>
#box1{
    width:100px;
    height:100px;
    background:red;
    
    /* 四个方向都一样 */
    margin:50px;
    
    /*对应 上 右 下 左*/
 	margin:20px 50px 100px 200px; 
    
    /* 上 左右 下 */
    margin:20px 50px 200px; 
    
    /* 上下 左右 */
    margin:20px 50px; 
    	
   
}



外边距注意事项

块元素

1.上下的外边距会重叠

2.当两个正数时或负数时,以绝对值大的值为准

3.当有一正一负时,边距会塌缩

/* 以 50 为准*/
#box1{
    width:100px;
    height:100px;
    background:red;
    margin-bootom:50px;
}
#box2{
    width:100px;
    height:100px;
    background:green;
    margin-top:20px;
}

/* 以 -50 为准*/
#box1{
    width:100px;
    height:100px;
    background:red;
    margin-bootom:-50px;
}
#box2{
    width:100px;
    height:100px;
    background:green;
    margin-top:-20px;
}

/* 实际外边距30px */
#box1{
    width:100px;
    height:100px;
    background:red;
    margin-bootom:50px;
}
#box2{
    width:100px;
    height:100px;
    background:green;
    margin-top:-20px;
}
    
行内元素

4.行内元素没有上下外边距

5.左右外边距不会重叠

<strong id="s1">xxx</strong>
<strong id="s2">xxx</strong>
/* 总外边距40px */
#d1{
	background:red;
    margin-right:20px;
}
#d2{
    background:gold;
    margin-left:20px;
}

/* 外边距不生效 */
#d2{
    background:gold;
    margin-top:20px;
    margin-bottom:20px;
}
元素水平居中
<div id="box1">
    
</div>

<style>
	#box1{
    	width:100px;
   		height:100px;
    	background:red;
        /* 元素水平居中 auto只对水平生效,垂直不生效 */
        margin: 0 auto;/*  margin: 上下 左右; */
    }
</style>

内边距

padding:设置元素的内边距

<div id="box">
    xxxxx
</div>
#box{
    width:100px;
    height:100px;
    padding:20px;/* 设置上下左右内边距 */
    /* 复合属性,其他属性基本和margin一致 */
}

内边距注意事项

1.外边距会把元素本身撑开

2.如果设置元素背景,则内边距也会有

行内元素

3.行内元素如果上下内边距会与其他元素重叠

案例

<div id="box">
    <div id="box1">
        
    </div>
</div>


#box{
    width:500px;
    height:500px;
    background:red;
}
#box1{
    width:100px;
    height:00px;
    background:glod;
    
    /* 
    	由于父元素没有边界(border) 该元素也没有边界
    	margin-top会把box和box1看做是一体的,然后一起
    	向下移动100px
    
    	解决方案:给父元素(box)添加一个边界(border)
    */
    margin-top:100px;
}


盒子模型

页面上的元素我们可以把他看成一个盒子,叫做盒子模型

盒子模型组成:由外边距、边框、内边距、width/height

盒子模型总尺寸:border-width + padding + margin + 内容宽度

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

CNsurly

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

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

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

打赏作者

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

抵扣说明:

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

余额充值