热门关键字:
jquery > jquery教程 > css3 > css入门基础

css入门基础

366
作者:管理员
发布时间:2020/2/29 18:23:25
评论数:0
转载请自觉注明原文:http://www.jq-school.com/Show.aspx?id=935
1.css基本语法
css的定义方法是:


选择器 { 属性:值; 属性:值; 属性:值;}


选择器是将样式和页面元素关联起来的名称,属性是希望设置的样式属性每个属性有一个或多个值。代码示例:


div{ width:100px; height:100px; color:red }
css的优先级 : 越接近目标的样式定义优先级越高,高优先级样式将继承低优先级样式的未重叠 定义但覆盖重叠的定义 如果4种样式表对同一元素定义了不同的样式,那么他们的优先级顺序从高到低 是,元素内定,内嵌样式表,导入样式表,外联样式表。


2.css页面引入方法:
1、外联式:通过link标签,链接到外部样式表到页面中。


<link rel="stylesheet" type="text/css" href="css/main.css">
2、嵌入式:通过style标签,在网页上创建嵌入的样式表。


<style type="text/css">
    div{ width:100px; height:100px; color:red }
    ......
</style>
3、内联式:通过标签的style属性,在标签上直接写样式。


<div style="width:100px; height:100px; color:red ">......</div>
3.常用的应用文本的css样式:
color 设置文字的颜色,如: color:red;
font-size 设置文字的大小,如:font-size:12px;
font-family 设置文字的字体,如:font-family:'微软雅黑';
font-style 设置字体是否倾斜,如:font-style:'normal'; 设置不倾斜,font-style:'italic';设置文字倾斜
font-weight 设置文字是否加粗,如:font-weight:bold; 设置加粗 font-weight:normal 设置不加粗
font 同时设置文字的几个属性,写的顺序有兼容问题,建议按照如下顺序写: font:是否加粗 字号/行高 字体;如: font:normal 12px/36px '微软雅黑';
line-height 设置文字的行高,如:line-height:24px;
text-decoration 设置文字的下划线,如:text-decoration:none; 将文字下划线去掉
text-indent 设置文字首行缩进,如:text-indent:24px; 设置文字首行缩进24px
text-align 设置文字水平对齐方式,如text-align:center 设置文字水平居中
4.css选择器
常用的选择器有如下几种:


1、标签选择器
标签选择器,此种选择器影响范围大,建议尽量应用在层级选择器中。 举例:


*{margin:0;padding:0}
div{color:red}   


<div>....</div>   <!-- 对应以上两条样式 -->
<div class="box">....</div>   <!-- 对应以上两条样式 -->
2、id选择器
通过id名来选择元素,元素的id名称不能重复,所以一个样式设置项只能对应于页面上一个元素,不能复用,id名一般给程序使用,所以不推荐使用id作为选择器。 举例:


#box{color:red} 

<div id="box">....</div>   <!-- 对应以上一条样式,其它元素不允许应用此样式 -->
3、类选择器
通过类名来选择元素,一个类可应用于多个元素,一个元素上也可以使用多个类,应用灵活,可复用,是css中应用最多的一种选择器。 举例:


.red{color:red}
.big{font-size:20px}
.mt10{margin-top:10px} 

<div class="red">....</div>
<h1 class="red big mt10">....</h1>
<p class="red mt10">....</p>
4、层级选择器
主要应用在选择父元素下的子元素,或者子元素下面的子元素,可与标签元素结合使用,减少命名,同时也可以通过层级,防止命名冲突。 举例:


.box span{color:red}
.box .red{color:pink}
.red{color:red}

<div class="box">
    <span>....</span>
    <a href="#" class="red">....</a>
</div>

<h3 class="red">....</h3>
5、组选择器
多个选择器,如果有同样的样式设置,可以使用组选择器。 举例:


.box1,.box2,.box3{width:100px;height:100px}
.box1{background:red}
.box2{background:pink}
.box2{background:gold}


<div class="box1">....</div>
<div class="box2">....</div>
<div class="box3">....</div>
6、伪类及伪元素选择器
常用的伪类选择器有hover,表示鼠标悬浮在元素上时的状态,伪元素选择器有before和after,它们可以通过样式在元素中插入内容。


.box1:hover{color:red}
.box2:before{content:'行首文字';}
.box3:after{content:'行尾文字';}




<div class="box1">....</div>
<div class="box2">....</div>
<div class="box3">....</div>




如果您觉得本文的内容对您的学习有所帮助:支付鼓励



关键字:css入门
友荐云推荐