【前端】css笔记(1)

目录:

选择器,以及一条或多条声明

html 元素

class 选择器

标签选择器

内联选择器

外部样式表

background-color 属性

background-image 属性

background-repeat&background-position 属性

背景- 简写属性


选择器,以及一条或多条声明

CSS 规则由两个主要的部分构成:

选择器通常是您需要改变样式的 HTML 元素。

每条声明由一个属性和一个值组成。

属性(property)是您希望设置的样式属性(style attribute)。每个属性有一个值。属性和值被冒号分开。


html 元素

可以用 id单独定义 ,CSS 中 id 选择器以 #来定义(id不得以数字开头):

 <style>
        #para1 {
            text-align: center;
            color: red;
        }
    </style>
    <p id="para1">你好,世界!!!</p>


class 选择器

用于描述多个元素的样式,在 CSS 中,类选择器以一个点"."号显示(class不得以数字开头):

<style>
    .center
    {
        text-align:center;
    }
</style>
<h1 class="center">标题居中</h1>
    <p class="center">段落居中。</p> 

也可以指定特定的 HTML 元素使用 class:

<style>
    p.center
    {
        text-align:center;
    }
</style>
<h1 class="center">此标题不受影响</h1>
    <p class="center">此段落将居中对齐。</p> 


标签选择器

以 HTML 标签作为 CSS 修饰所用的选择器:
<style>
    h3{
      color:red;
     }
</style>
<h3>W3cschool教程</h3>


内联选择器

直接在标签内部写 CSS 代码:

<h3 style="color:red;">W3cschool教程</h3>


插入样式表的方法有三种:

  • 外部样式表
  • 内部样式表
  • 内联样式

外部样式表

通过改变一个文件来改变整个站点的外观: 

<head> <link rel="stylesheet" type="text/css" href="mystyle.css"> </head>

浏览器会从文件 mystyle.css 中读到样式声明,并根据它来格式文档。

多重样式:

如果某些属性在不同的样式表中被同样的选择器定义,那么属性值将从更具体的样式表中被继承过来层叠为一个

层叠优先级(内联最高):

  1. 浏览器缺省设置
  2. 外部样式表
  3. 内部样式表(位于 head 标签内部)
  4. 内联样式(在 HTML 元素内部)

优先级逐级增加的选择器列表,其中数字 7 拥有最高的优先权:

  1. 通用选择器(*)
  2. 元素(类型)选择器
  3. 类选择器
  4. 属性选择器
  5. 伪类
  6. ID 选择器
  7. 内联样式

background-color 属性

定义了元素的背景颜色:

<style>
    body
    {
        background-color:#b0c4de;
    }
</style>


background-image 属性

描述了元素的背景图像:

<style>
    body 
    {
        background-image:url('/statics/images/w3c/intro.png');
        background-color:#cccccc;
    }
</style>


background-repeat&background-position 属性

需要在 HTML 页面上对背景图像进行平铺,可以使用 background-repeat 属性;

可以利用 background-position 属性改变图像在背景中的位置:

<style>
    body
    {
        background-image:url('/statics/images/w3c/intro.png');
        background-repeat:no-repeat;
        background-position:right top;
        margin-right:200px;
    }
</style>


百分数值:

background-position:66% 33%;

长度值:

background-position:50px 100px;

背景- 简写属性

属性值的顺序为:

  • background-color
  • background-image
  • background-repeat
  • background-attachment
  • background-position

<style>
    body
    {
        background:#ffffff url('/statics/images/w3c/intro.png') no-repeat right top;
        margin-right:200px;
    }
</style>


固定或滚动背景:

background-attachment: fixed;


<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>css整理</title>
</head>
<body>
    <style>
        #para1 {
            text-align: center;
            color: red;
        }
    </style>
    <p id="para1">你好,世界!!!</p><!--CSS 中 id 选择器以 # 来定义-->

    <style>
        .center {
            text-align: center;
        }
    </style>
    <h1 class="center">标题居中</h1>
    <p class="center">段落居中。</p><!--类选择器以一个点"."号显示-->

    <style>
        p.center {
            text-align: center;
        }
    </style>
    <h1 class="center">此标题不受影响</h1>
    <p class="center">此段落将居中对齐。</p><!--指定特定的 HTML 元素-->


    <style>
        h3 {
            color: red;
        }
    </style>
    <h3>W3cschool教程</h3><!--标签选择器-->

    <h3 style="color:red;">W3cschool教程</h3><!--内联选择器-->

    <style>
        body {
            background-color: #b0c4de;
        }
    </style><!--背景颜色-->

    <style>
        body  {
            background-image: url('/statics/images/w3c/intro.png');
            background-color: #cccccc;
        }
    </style><!--背景图像-->

    <style>
        body {
            background-image: url('/statics/images/w3c/intro.png');
            background-repeat: no-repeat;
            background-position: right top;
            margin-right: 200px;
        }
    </style><!--背景平铺和位置设置-->
    background-position:66% 33%;<!--百分数值-->
    background-position:50px 100px;<!-- 长度值-->

    <style>
        body {
            background: #ffffff url('/statics/images/w3c/intro.png') no-repeat right top;
            margin-right: 200px;
        }
    </style><!--背景-简写属性-->
    background-attachment: fixed;<!--固定背景-->

</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

足足一米58

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值