盒模型(二)
一。盒模型~高度和宽度
css内定义的宽(width)和高(height),指的是填充以里的内容范围。 因此一个元素实际宽度(盒子的宽度)=左边界+左边框+左填充+内容宽度+右填充+右边框+右边界。 元素的高度也是同理。
示例:
<head>
<metacharset="UTF-8">
<title>宽度和高度</title>
<style type="text/css">
li{
border-bottom:1px dotted #ccc;
width:200px;height:30px;
}
li{ text-shadow: 0 0 80px #000;
color: rgba(255,255,255,0);
transition: text-shadow 1s, color 1s;
}
li:hover{
text-shadow: 0 0 1px #000;
color:#000;
transition: text-shadow 1s, color 1s;
}
</style>
</head>
<body>
<ul>
<li>别让不会说话害了你</li>
<li>二十七八岁就应该有的见识</li>
<li>别让不好意思害了你</li>
</ul>
<p>碰下上面的那一团个试试=w=</p>
</body>
</html>
二, 盒模型-填充
元素内容与边框之间是可以设置距离的,称之为“填充”。填充也可分为上、右、下、左(顺时针)。如下代码: div{padding:20px 10px 15px 30px;} 顺序一定不要搞混。可以分开写上面代码: div{ padding-top:20px; padding-right:10px; padding-bottom:15px; padding-left:30px; } 如果上、右、下、左的填充都为10px;可以这么写 div{padding:10px;} 如果上下填充一样为10px,左右一样为20px,可以这么写: div{padding:10px 20px;}
示例;
<!DOCTYPE HTML>
<html>
<head>
<metacharset="UTF-8">
<title>填充</title>
<style type="text/css">
#box1{
width:100px;
height:100px;
padding:10px;
border:1px solid red;
}
</style>
</head>
<body>
<div id="box1">盒子1</div>
</body>
</html>
三,盒模型边界
元素与其它元素之间的距离可以使用边界(margin)来设置。边界也是可分为上、右、下、左。如下代码:
div{margin:20px 10px 15px 30px;}
也可以分开写:
div{
margin-top:20px;
margin-right:10px;
margin-bottom:15px;
margin-left:30px;
}
如果上右下左的边界都为10px;可以这么写:
div{ margin:10px;}
如果上下边界一样为10px,左右一样为20px,可以这么写:
div{ margin:10px 20px;}
总结一下:padding和margin的区别,padding在边框里,margin在边框外。