一、css与html结合的四种方式
1.<style type="text/css">
p{
line-height: 37px;
text-indent: 37px;
color: black;
font-size: 19px;
}
</style>
style标签方式
2.<style type="text/css">
@import url("myDiv.css");
</style><!-- 导入方式 -->
3.<link rel="stylesheet" type="text/css" href="aa.css"><!-- 链接方式 -->
4.<p style="line-height:30px;text-indent:10px">党委书记罗成翼教授致欢迎辞。他代表学校党委意见,并祝愿本次会议<span id="id1">圆满</span>成功。</p>
styles属性方式
二、样式优先级
由上到下,由外到内。优先级由低到高。——总之,一般情况是以后加载为主
三、三种基本选择器
/*标签选择器*/
td{
border:3px blue solid;
background-color: red;
}
/*设置p标签中的 类样式a*/
p.a{
line-height:30px;
color:#ff2200;
}
*设置id选择器*/
#id1{
font-size: 22px;
text-decoration: underline;
}
四、选择器的优先级
标签名选择器 < class选择器 < id选择器 < style属性
五、扩展选择器
/* 关联选择器 ,只对 div中的 span标签中的 b标签 进行设置 */
div span b{
font-size: 22px;
text-decoration: line-through;
color:red;
}
/*组合选择器,同时对p和div进行设置*/
p,div{
line-height: 30px;
}
.cc,span b{
color:#00ff00;
}
/*伪元素选择器*/
span:hover{
font-size: 25px;
color:#ffff00;
}
六、css盒子
1、padding、margin
如上图所示黑框里的内容距黑框的距离称为padding,黑框距外面的蓝框的距离称为margin
上 padding-top
下 padding-bottom
左 padding-left
右 padding-righ
/*padding:20px; 一个值:四个内边距均为20px*/
/*padding:20px 100px; 两个值,分别代表:上下 和 左右 边距*/
padding:20px 100px 200px;/* 三个值,分别代表:上、左右和 下 边距*/
/* padding:20px 100px 200px 300px; 四个值,分别代表:上、右、下和左 边距---从上开始,顺时针依次得到下一方向*/
同理可得margin
2、漂浮
float : none | left | right
none : 默认值。对象不飘浮
left : 文本流向对象的右边
right : 文本流向对象的左边
clear : none | left | right | both
none : 默认值。允许两边都可以有浮动对象
left : 不允许左边有浮动对象
right : 不允许右边有浮动对象
both : 不允许有浮动对象
3、css布局
position : static | absolute | fixed | relative
static : 默认值。无特殊定位,对象遵循HTML定位规则。
absolute : 将对象从文档流中拖出,使用 left , right , top , bottom 等属性相对于其最接近的一个最有定位设置的父对象进行绝对定位。如果不存在这样的父对象,则依据 body 对象。而其层叠通过 z-index 属性定义。
fixed : 未支持。对象定位遵从绝对(absolute)方式。但是要遵守一些规范。
relative : 对象不可层叠,但将依据 left , right , top , bottom 等属性在正常文档流中偏移位置
absolute与relativea的区别:aboslute已经从原本的位置脱离,后续的文档可以在其原本的位置上显示,relative未脱离,后续文档不可放在其原本的位置上