前端 css

所谓css:层叠样式表:所谓层叠,可以将整个网页想象成是一层一层的结构,层次高的将会覆盖层次低的。

<1>style 内联样式

可以直接将样式写到标签内部的style属性中,这种样式不用填写选择器,直接编写声明即可

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>css</title>
</head>
<body>
    <p style="color: red;font-size: 40px;">窗前明月光</p>
    <p style="color: green;font-size: 40px;">疑是地上霜</p>
    <p>举头望明月</p>
    <p>低头思故乡</p>
</body>
</html>

运行结果

《前端 css》

但是这样做肯定是不行的

<2>style标签 内部样式表

  • 下面是把所有p标签的颜色和字体变成style标签里的颜色和字体
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>css</title>
    <style type="text/css">
        p{
            color: red;
            font-size: 40px;
        }
    </style>

</head>
<body>
    <p>窗前明月光</p>
    <p>疑是地上霜</p>
    <p>举头望明月</p>
    <p>低头思故乡</p>
</body>
</html>

运行结果

《前端 css》 image.png

<3> link 外部样式表

  • 可以将所有的样式保存到一个外部的css文件中,然后通过<link>标签将样式表引入到文件中。
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>css</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <p>窗前明月光</p>
    <p>疑是地上霜</p>
    <p>举头望明月</p>
    <p>低头思故乡</p>
</body>
</html>

1.css文件 《前端 css》 image.png

  • 运行结果

    《前端 css》 image.png

<4>css语法

  • 选择器 声明块
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        p{
            color: green;
            font-size: 30px;
        }
    </style>
</head>
<body>
    <p>这是css的语法</p>
</body>
</html>
  • 运行结果

《前端 css》 image.png

<5>块和内联

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>块和内联</title>
</head>
<body>
    <div style="background-color: red;">这是一个div</div>
    <p>阿西吧</p>
    <span style="color: red;">span</span>
    <a href="http//www.baidu.com">我是一个超链接</a>
</body>
</html>

<6>伪元素

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>伪元素</title>
    <style type="text/css">
        p:first-letter{
            color: green;
        }
    </style>
</head>
<body>
    <p>这是一个伪元素</p>
</body>
</html>

<7>否定伪类

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>否定伪类</title>
    <style type="text/css">
        p:not(#p1):not(.word){
            background-color: yellow;
        }
    </style>
</head>
<body>
    <p>我在你眼里到底算什么</p>
    <p>我在你眼里到底算什么</p>
    <p>我在你眼里到底算什么</p>
    <p>我在你眼里到底算什么</p>
    <p id="p1">爱情买卖</p>
    <p class="word">here with you</p>
</body>
</html>

<8>元素选择器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>元素选择器</title>
    <style type="text/css">
        p{
            color: red;
        }
        h1{
            color: green;
        }
    </style>
</head>
<body>
    
    <h1>爱是你我</h1>
    <h1>爱是你我</h1>
    <h1>爱是你我</h1>
    <p>好好学习 天天向上</p>
    <span>QQ飞车</span>
</body>
</html>

<9>类选择器

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>类选择器</title>
    <style type="text/css">
        .p2{
            color: green;
        }
        .p3{
            color: yellow;
        }
    </style>
</head>
<body>
    <h1>广东爱情故事</h1>
    <h1>广东爱情故事</h1>
    <p id="p1">广东爱情故事</p>
    <p class="p2 p3">广东爱情故事</p>
    <p>广东爱情故事</p>
    <span>广东爱情故事</span>
</body>
</html>

<10 >多类名选择器

我们可以给标签指定多个类名,从而达到更多的选择目的。

<img src="media/lei.png" />

注意:

  1. 样式显示效果跟HTML元素中的类名先后顺序没有关系,受CSS样式书写的上下顺序有关。
  2. 各个类名中间用空格隔开。
    多类名选择器在后期布局比较复杂的情况下,还是较多使用的。
<div class="pink fontWeight font20">亚瑟</div>
<div class="font20">刘备</div>
<div class="font14 pink">安其拉</div>
<div class="font14">貂蝉</div>

<11>选择器 :< div class=“nav”> 这个 div 的名字 就是 nav nav 就是 div 这个 div 也是 nav

< 人 class = 刘德华 > 我们想要吧div 找到 div {} .nav {}

<12>id选择器

*选择器使用“#”进行标识,后面紧跟id名,其基本语法格式如下:

id名{属性1:属性值1; 属性2:属性值2; 属性3:属性值3; }

该语法中,id名即为HTML元素的id属性值,大多数HTML元素都可以定义id属性,元素的id值是唯一的,只能对应于文档中某一个具体的元素。
用法基本和类选择器相同。
id选择器和类选择器区别
W3C标准规定,在同一个页面内,不允许有相同名字的id对象出现,但是允许相同名字的class。
类选择器(class) 好比人的名字, 是可以多次重复使用的, 比如 张伟 王伟 李伟 李娜
id选择器 好比人的身份证号码, 全中国是唯一的, 不得重复。 只能使用一次。
id选择器和类选择器最大的不同在于 使用次数上。
通配符选择器
通配符 选择器用“*”号表示,他是所有选择器中作用范围最广的,能匹配页面中所有的元素。其基本语法格式如下:

  • { 属性1:属性值1; 属性2:属性值2; 属性3:属性值3; }
    例如下面的代码,使用通配符选择器定义CSS样式,清除所有HTML标记的默认边距。
* {
  margin: 0;                    /* 定义外边距*/
  padding: 0;                   /* 定义内边距*/
}

注意:
这个通配符选择器,就像我们的电影明星中的梦中情人, 想想它就好了,但是它不会和你过日子。
CSS字体样式属性
font-size:字号大小

二、选择器餐厅练习
1,plate 元素选择器
2,bento 元素选择器
3,#fancy ID选择器
4,plate apple 后代元素选择器
5,#fancy pickle 复合选择器
6,.small 类选择器
7,bento .small,plate .small并集选择器
8,bento orange 后代元素选择器
9,plate,bento,plate 并集选择器
10, * 通配选择器
11,plate * 后代元素选择器 (plate中的所有元素)
12,plate + apple 兄弟元素选择器
13,bento~pickle 兄弟元素选择器(bento后面所有的pickle)
14,plate > apple 子元素选择器
15,orange:first-child 子元素选择器
16,:only-child 子元素选择器(只有一个子元素的)
17,.small:last-child 子元素选择器
18,plate:nth-child(3) 子元素选择器
19,:nth-last-child(4)子元素选择器
20,apple:first-of-type 子元素选择器
21,plate:nth-of-type(even) 子元素选择器
22,plate:nth-of-type(2n+3)子元素选择器
23,apple:only-of-type 子元素选择器
24,.small:nth-child(even) 子元素选择器
25,bento:empty 子元素选择器(子元素为空)
26,apple:not(.small) not选择器

    原文作者:IT大牛辉
    原文地址: https://www.jianshu.com/p/80d9b37e6cda
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞