zoukankan      html  css  js  c++  java
  • [HTML] CSS Id 和 Class选择器

    id 和 class 选择器

    如果你要在HTML元素中设置CSS样式,你需要在元素中设置"id" 和 "class"选择器。


    id 选择器

    id 选择器可以为标有特定 id 的 HTML 元素指定特定的样式。

    HTML元素以id属性来设置id选择器,CSS 中 id 选择器以 "#" 来定义。

    以下的样式规则应用于元素属性 id="para1":

    #para1
    {
    text-align:center;
    color:red;
    } 

    Remark ID属性不要以数字开头,数字开头的ID在 Mozilla/Firefox 浏览器中不起作用。

    实例:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8"> 
    <title>W3Cschool教程(w3cschool.cn)</title> 
    <style>
    #para1
    {
        text-align:center;
        color:red;
    } 
    </style>
    </head>
    
    <body>
    <p id="para1">Hello World!!!</p>
    <p>This paragraph is not affected by the style.</p>
    </body>
    </html>

    class 选择器

    class 选择器用于描述一组元素的样式,class 选择器有别于id选择器,class可以在多个元素中使用。

    class 选择器在HTML中以class属性表示, 在 CSS 中,类选择器以一个点"."号显示:

    在以下的例子中,所有拥有 center 类的 HTML 元素均为居中。

    .center {text-align:center;} 

    实例:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8"> 
    <title>W3Cschool教程(w3cschool.cn)</title> 
    <style>
    .center
    {
        text-align:center;
    }
    </style>
    </head>
    
    <body>
    <h1 class="center">标题居中</h1>
    <p class="center">段落居中。</p> 
    </body>
    </html>

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

    在以下实例中, 所有的 p 元素使用 class="center" 让该元素的文本居中:

    p.center {text-align:center;} 

    实例:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8"> 
    <title>W3Cschool教程(w3cschool.cn)</title> 
    <style>
    p.center
    {
        text-align:center;
    }
    </style>
    </head>
    
    <body>
    <h1 class="center">This heading will not be affected</h1>
    <p class="center">This paragraph will be center-aligned.</p> 
    </body>
    </html>

    Remark 类名的第一个字符不能使用数字!它无法在 Mozilla 或 Firefox 中起作用。

  • 相关阅读:
    pg-xl 基于 pgxc_ctl 安装与使用
    pg-xl 的基本方式添加节点
    pg-xl 的基本方式安装与使用
    oracle 12.2.0.1 + oracle goldengate 12.3.0.1.4 之一 dml
    postgresql 高可用 etcd + patroni 之二 patroni
    postgresql 高可用 etcd + patroni 之一 etcd
    rvm,ruby的安装
    oracle 12.2.0.1 使用 active dataguard broker 之二 failover
    oracle 12.2.0.1 使用 active dataguard broker 之二 switchover
    es 6.3 .tar.gz 安装
  • 原文地址:https://www.cnblogs.com/frost-yen/p/5772272.html
Copyright © 2011-2022 走看看