css的选择器和常用属性
- css的使用
- 1.行内式
- 2.嵌入式
- 3.引入外来样式文件
- css选择器
- 通用选择器
- 元素选择器
- id选择器
- 类选择器
- 分组选择器
- 组合选择器
- 后代选择器
- 子元素选择器
- 相邻兄弟选择器
- 普通兄弟选择器
- css常用属性设置
- 背景
- 文本
- 字体
- display
- 浮动
- 盒子模型
- border
- padding
- margin
css的使用
1.行内式
行内样式将样式定义在html元素的style属性中,以行内式写的css耦合度高,只适用于当前元素,在设定某个元素
的样式比较常用
<p style="color:red; font-size:50px;">
这是一段文本
p>
2.嵌入式
嵌入式通过在html页面内容开辟一段属于css的代码区域通常都是在标签嵌套
在
<style type="text/css">p{color:blue;font-size:40px;}style>
3.引入外来样式文件
在实际开发当中,很多时候都使用引入外联样式文件,这种形式可以使html页面更加清晰
<link rel="stylesheet" type="text/css" href="css/style.css">link>
css选择器
通用选择器
*{
color:orange;
}
*号代表是选择所有
元素选择器
又叫标签选择器
p{
font-size:15px;
}
id选择器
<p id="first">段落一p>
#first{
color:red;
}
类选择器
<p class="first">段落一p>
.first{
color:red;
}
分组选择器
选择指定选择器选中的元素,通常是在多个标签中设置相同的样式
<div class="first">
盒子一
div>
<p>
段落一
p>
<div id="second">
盒子2
div>
.first , p , #second{
background-color:blue;
}
组合选择器
后代选择器
用于选择指定标签元素下的后背,以空格分开
选择器1 选择器2{
......
}
<ul class="fruit">
<li>香蕉li>
<li>苹果li>
<li>柠檬li>
ul>
.fruit li{border:1px solid red;}
子元素选择器
选择指定元素的第一代元素,用大于号分隔
选择器1 > 选择器2{
.......
}
#father > div{
border:1px solid red;
}
相邻兄弟选择器
可选择在另一元素后的元素,且二者有相同的父元素,以加号分隔
选择器1 + 选择器2{
.....
}
<div id="d">
相邻兄弟选择器1
<ul>
<li>纸li>
<li>笔li>
ul>
div>
<div>
相邻兄弟选择器2
div>
#d + div{
border:1px solid red;
}
普通兄弟选择器
选择紧接在另一个元素后的所有元素,而且二者有相同的父元素,以波浪号分隔
选择器1 + 选择器2{
.......
}
<div>
普通兄弟选择器1
<ul>
<li>张三li>
<li>刘四li>
<li>王五li>
<li>李六li>
<li>阿七li>
ul>
div>
li~li{
background-color:yellow;
}
css常用属性设置
背景
css背景用于定义HTML元素的背景效果
1.设置元素的背景颜色
body{
background-color:#ff0000;
}
2.设置元素的背景图像,默认情况下会进行重复显示,覆盖整个元素实体
body{
backdround-image:url('paper.gif');
}
3.设置是否重复背景图像
body{
background-image:url(img/logo.jpg);
background-repeat:no-repeat;
}
4.设置背景图片大小
body{
background-size:200px 200px;
}
文本
1.设置元素颜色
body{
color:blue;
}
h1{
color:#00ff00;
}
h2{
color:rgp(255,0,0);
}
2.设置文本对齐方式,center(居中),left(左对齐),right(右对齐)
body{
text-align:center;
}
h1{
text-align:right:
}
h2{
text-align:left:
}
3.设置文本修饰
div{
/*设置文本上划线*/
text-decoration:overline;
/*设置下划线*/
text-decoration:underline;
/*设置中划线*/
text-decoration:line-through;
}
h2{
/*同时设置元素的上中下划线*/
text-decoration:overline underline line-through;
}
a{
/*去除文本修饰*/
text-decoration:none;
}
p{
/*设置首行缩进,em:一个文字的宽度*/
text-indent:2em;
}
字体
1.设置文本字体
1.1. 设置当字体名中含有空格或#、$之类的符号 时,才需要在font-family声明中加引号
body{
font-family:"arial black";
}
1.2. 多个字体系列是用一个逗号分隔指明
/*靠前的字体先生效*/
p{
font-family:微软雅黑,黑体,"agency fb";
}
2.设置文本大小
body{
font-size:30px;
}
3.设置字体风格
body{
/*设置风格正常体*/
font-style:normal;
/*斜体*/
font-style:italic;
/*强制倾斜*/
font-style:oblique;
}
4.设置字体的粗细
body{
font-weigth:bold;
/*100-900值越大字体越粗,400正常字体,700粗体*/
}
display
规定元素生成框的类型
类型 | 描述 |
block | 元素会被显示,且元素会变成块级元素,元素前后会有换行符 |
none | 元素会被隐藏 |
inline | 元素会显示为行内元素,元素前后没有换行符 |
inline-block | 行内块级元素 |
块级元素:可以设置元素的宽高和边距,元素会自动换行
行内元素:不可以设置元素的宽高和边距,元素不会自动换行
行内块级元素:可以设置元素的宽高和边距,元素不会自动换行
浮动
float的属性值有none,left,right.
1.只有横向浮动,并没有纵向浮动
2.会将元素的display属性变更为block。、
3.浮动元素的最后一个元素会围绕着浮动元素(典型运用就是文字围绕图片)
4.浮动元素的前一个元素不会受到任何影响(如果想让两个块级元素并排显示,必须让
两个块级元素都应用float)。
盒子模型
border、padding、margin三个属性构成盒子模型
border
设置所有的边框属性
1.可同时设置边框的宽度、样式、颜色
table,th,td{
border:1px solid black;
}
table{
width:100%;
height:50px;
}
2.使用border-width、border-style、border-color单独设置
table,td{
border-width:1px;
border-style:dotted;
border-color:green;
}
3.设置表格边框是否折叠为单一边框
属性值:separate(默认,单元边框独立)collapse(单元格bian’kuang 合并)
4.设置属性的顺序,上右下左
设置一个属性,表示边框四边效果一样
设置两个属性,表示上下一致,左右一致
设置三个属性,表示上右下不一致,左右一致
padding
设置元素所有内边距的宽度,或者设置各边上内边距的宽度
如果在表的内容控制文本边框的内边距,使用td和th填充属性
td{
padding:15px;
}
单独设置各边的内边距:padding-top、padding-left、padding-botton、padding-right
margin
设置一个元素所有外边距的宽度,或者各边上外边距的宽度
p{
margin:2px 3px 5px 6px;
}
单独设置各边的外边距:margin-top、margin-left、margin-botton、margin-right
p{
margin-top:100px;
margin-left:200px;
margin-botton:50px;
margin-right:50px;
}
*{
margin:auto auto;
}
*{
margin:100px auto;
}
说明:
auto:自动,可以理解为居中的意思,浏览器自动计算外边距
margin:auto auto:第一个auto表示上下外边距自动计算,第二个auto表示左右外边距自动计算。
但是上下外边距在自动计算时不会自动生效,而左右外边距会生效,表现为居中状态
设置一个元素所有外边距的宽度,或者各边上外边距的宽度
p{
margin:2px 3px 5px 6px;
}
单独设置各边的外边距:margin-top、margin-left、margin-botton、margin-right
p{
margin-top:100px;
margin-left:200px;
margin-botton:50px;
margin-right:50px;
}
*{
margin:auto auto;
}
*{
margin:100px auto;
}
说明:
auto:自动,可以理解为居中的意思,浏览器自动计算外边距
margin:auto auto:第一个auto表示上下外边距自动计算,第二个auto表示左右外边距自动计算。
但是上下外边距在自动计算时不会自动生效,而左右外边距会生效,表现为居中状态