网站首页 > 知识剖析 正文
盒子模型:
由content(内容)、border(边框)、padding(间隙)、margin(边框)
一个盒子的实际宽度(或高度)是由content+padding+border+margin组成的;
在CSS中可以通过设定width和height的值来控制content的大小, 并且对于任何一个盒子, 都可以分别设定4条边各自的border、padding和margin。
所有的元素都是一个盒子
如: a标记也可以设置padding margin;
在浏览器中, width和height的值兼容性很差, 具体指代跟HTML第一行的声明有关, 如果不声明则
IE7 width = content的宽度 + padding + border + margin
height = content的高度 + padding + border + margin
W3C标准: width = content的宽度
padding 会影响盒子的问题
盒子真正的大小 width 总的宽度:盒子的宽度 + 左右padding + 左右border 值 + 左右margin
如果做一下DTD声明:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
则只代的都是content的宽和高。通常情况下尽量使用上述声明,这样浏览器之间的兼容性会大大提高。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
.father{
width:300px;
height:150px;
background-color:pink;
padding:100px; /* 盒子实际宽度变成500px, 实际高度变成:350px */
}
</style>
</head>
<body>
<div class="father">
</div>
</body>
</html>
简单计算题
问:一个盒子的宽度为 200像素, 这个盒子, 有一个左边为 15 像素的内边距, 还有 3 像素的边框? 问, 这个盒子的实体宽度是?
答:实际宽度 = width+ padding-left + border
= 200 + 15 + 3*2
= 221
中等数学题
问:一个盒子的宽度为 200 像素, 这个盒子, 有一个左边为 15 像素的内边距, 还有 3 像素的边框? 但是, 我们盒子的大小是不能变的。问怎么办?
盒子大小不能变, 盒子的最终大小不能变。
答:实际宽度 = width+ padding-left + border
200 = x + 15 + 6
x = 200 - 15 -6
x = 179
这个盒子的width变成 179 就可以了。
复杂数学题
要求小盒子在大盒子里面水平居中, 垂直居中?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
.father{
/*width:300px;
height:150px;
background-color:pink;
padding:75px 100px;*/
width:400px;
height:225px;
background-color:pink;
padding:75px 0 0 100px;
}
.son{
width:300px;
height:150px;
background-color:deeppink;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>
padding不会影响盒子的情况:
继承的盒子默认和父亲一样宽, 如果继承的盒子没有宽度, 则padding值不会撑开盒子的宽度, 不用再去减了。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
.father{
width:300px;
height:200px;
background-color:pink;
}
.son{
height:50px;
background-color:deeppink;
padding-left:50px;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>
盒子透明
1 background:rgba(),这是一个CSS3属性,IE6 7 8 不支持
可以再小括号内里面设置第四个参数opacity:0-1
background:rgba(0,0,0,0.4);
2 CSS滤镜:首先给IE高版本和其他高版本浏览器(chome、Firefox)设置属性opacity:0-1
如果让低版本IE浏览器也支持半透明我们可以利用filter:appha(opacity=40); 此时的0-100表示全透明到不透明
opacity:0.4;
filter:alpha(opacity=40);
盒子居中
盒子水平居中的方法:
div{
width:500px
margin:14px auto;
}
盒子水平和垂直居中:
如何让一个盒子在屏幕水平和垂直居中显示?
以400*300的盒子为例:
首先给这个盒子设置一个绝对定位,position:absolute.然后将他的left top都设置为50%,此时这个盒子的左边界和尚边界都在中间的位置,
最后我们只需要再给这个盒子margin-left:-200px;margin-top:-140px,让这个盒子在往左、上走自身的一半;
div{
position:absolute;
left:50%; top:50%;
margin-left:-200px;
margin-top:-150px;
}
注意:让盒子居中必须写<!DOCTYPE>
margin的塌陷现象
标准文档流中, 竖直方向的margin不叠加, 以较大的为准。
*{
margin: 0;
padding: 0;
}
.p1{
width: 300px;
height: 200px;
background-color: orange;
margin-bottom: 30px;
}
.p2{
width: 300px;
height: 200px;
background-color: orange;
margin-top: 50px;
}
<p class="p1"></p>
<p class="p2"></p>
如果不在标准流, 比如盒子都浮动了, 那么两个盒子之间是没有塌陷现象的:
*{
margin: 0;
padding: 0;
}
div{
width: 200px;
border: 10px solid red;
overflow: hidden;
}
.p1{
width: 200px;
height: 100px;
background-color: orange;
margin-bottom: 40px;
float: left;
}
.p2{
width: 200px;
height: 100px;
background-color: orange;
margin-top: 30px;
float: left;
}
<div>
<p class="p1"></p>
<p class="p2"></p>
</div>
善于使用父亲的padding,, 而不是儿子的margin
如果父亲没有border, 那么儿子的margin实际上踹的是"流", 踹的是这"行"。所以, 父亲整体也掉下来了
这个p有一个margin-top踹父亲, 试图将自己下移
*{
margin: 0;
padding: 0;
}
div{
width: 300px;
height: 250px;
background-color: orange;
}
p{
width: 100px;
height: 100px;
margin-top: 60px;
background-color: green;
}
<div>
<p></p>
</div>
儿子用margin-top踹父亲, 父亲没有border, 结果父亲也下来了
第一种解决方法:
*{
margin: 0;
padding: 0;
}
div{
width: 300px;
height: 250px;
padding-top: 50px;
background-color: orange;
}
p{
width: 100px;
height: 100px;
background-color: green;
}
<div>
<p></p>
</div>
使用在父亲使用padding-top, 代替儿子使用margin-top
第二种解决方法:
*{
margin: 0;
padding: 0;
}
div{
width: 300px;
height: 250px;
background-color: orange;
border: 1px solid red;
}
p{
width: 100px;
height: 100px;
margin-top: 60px;
background-color: green;
}
<div>
<p></p>
</div>
儿子用margin-top踹父亲, 但是父亲有border。达到了我们的目的。
margin这个属性, 本质上描述的是兄弟和兄弟之间的距离; 最好不要用这个marign表达父子之间的距离。
所以, 我们一定要善于使用父亲的padding, 而不是儿子的margin。
凡是盒子模型(width/height+padding+border+marign)的东西都不能继承
CSS可以继承的属性:
font-系列
text-系列
color
line-height
- 上一篇: CSS前端笔记-第二天
- 下一篇: 使用CSS实现图片的磨砂玻璃效果
猜你喜欢
- 2024-11-19 CSS中的混合模式,制作高级特效的必备技巧
- 2024-11-19 19.HTML CSS边距、边框、填充和内容
- 2024-11-19 使用CSS实现图片的磨砂玻璃效果
- 2024-11-19 CSS前端笔记-第二天
- 2024-11-19 38个不可错过的实用前端工具
- 2024-11-19 深入浅出超好用的 CSS 阴影技巧
- 2024-11-19 分享几个css实用技巧
- 2024-11-19 前端入门——css颜色和背景
- 2024-11-19 学前端怎能不知css系列-css初识02
- 2024-11-19 Photoshop如何实现图片透明效果的详细步骤
- 最近发表
- 标签列表
-
- xml (46)
- css animation (57)
- array_slice (60)
- htmlspecialchars (54)
- position: absolute (54)
- datediff函数 (47)
- array_pop (49)
- jsmap (52)
- toggleclass (43)
- console.time (63)
- .sql (41)
- ahref (40)
- js json.parse (59)
- html复选框 (60)
- css 透明 (44)
- css 颜色 (47)
- php replace (41)
- css nth-child (48)
- min-height (40)
- xml schema (44)
- css 最后一个元素 (46)
- location.origin (44)
- table border (49)
- html tr (40)
- video controls (49)