Table of Contents
选择器
类型选择器
类选择器
ID选择器
后代选择器
伪类选择器
通用选择器
高级选择器
子选择器
相邻选择器
属性选择器
样式层叠
样式特殊性
样式的继承
样式的引用
Home Web Front-end HTML Tutorial CSS 之 选择器_html/css_WEB-ITnose

CSS 之 选择器_html/css_WEB-ITnose

Jun 24, 2016 am 11:28 AM

选择器

类型选择器

也叫做 元素选择器和简单选择器,可以根据元素标签指定样式。

p {color:red;}
Copy after login

类选择器

一般用于某些同类型的样式。

.xxxClass{}<div class="xxxClass"></div>
Copy after login

ID选择器

为特殊的元素,指定类型

#xxxId{}<div id="xxxId"></div>
Copy after login

后代选择器

在某个选择器后,选择指定规则的后代,为其指定样式

div p {}
Copy after login

伪类选择器

为某些特殊的元素,在某些条件时,指定样式。
比如,链接的link和visited ;
以及其他元素的hover,focus,active等。

a:link, a:visited {}a:hover, a:focus, a:active {}
Copy after login

通用选择器

类似通配符的作用,比如所有可用元素添加样式。

*{}
Copy after login

高级选择器

子选择器

后代选择器是选择所有的后代,子选择器则可以选择元素的直接后代,即子元素。

#nav>li{}
Copy after login

相邻选择器

#nav + p {}
Copy after login

属性选择器

比如为tooltip添加样式

acronym[title]{}
Copy after login

样式层叠

由于选择同一个元素可以通过不同的方法,比如元素的后代、或者类、或者ID,那么就可能造成样式的重叠。因此可以使用!important,增加样式的优先级。

样式特殊性

再说到样式的重叠的另一种情况,如果不同的方式都指定了样式,到底会优先使用哪一种呢?
这就需要了解特殊性了,从上到下 依次为:

#xxx{}.class{}body div{}div{}
Copy after login

即,元素上的style会优先、其次是使用ID选择器、然后是类选择器、后代选择器、最后是类型选择器

样式的继承

样式是可以继承的,比如给body增加样式,那么页面中body内的所有元素都会应用这个样式。

样式的引用

样式的引用由两种方式,一种是通过链接link,另一种则是style的导入的方式:

    <!-- <link rel="stylesheet" type="text/css" href="test.css"> -->    <style type="text/css">    /*slow*/    @import url("test.css");    </style>
Copy after login

相比较来说,link的方式要更快一些。

另外,还是推荐把CSS都放在同一个文件中。因为浏览器加载样式文件,是受浏览器的限制的,有的浏览器只支持同时下载3个文件、有的支持8个...
如果由三个文件,而浏览器只能同时下载2个文件,那么第三个就必须要等到前两个下载完后,才能加载。
因此,放在一个文件中会更快的加载。

如果把所有的CSS都放在一个文件,又可能导致文件过于庞大,不易维护。因此有以下几个建议:

  • 完善注释信息
  • 对样式进行分组,并写入相应的注释,可以加快定位
  • 发布时,可以考虑对文件进行压缩,比如删除注释、删除空白、压缩。目前很多的浏览器都支持从压缩包中下载文件,这样可以有效的减少文件体积。
  • 暂时也就整理这么多吧!后续再补充....

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

    HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

    The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

    HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

    What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

    AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

    Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

    WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

    Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

    GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

    How to implement adaptive layout of Y-axis position in web annotation? How to implement adaptive layout of Y-axis position in web annotation? Apr 04, 2025 pm 11:30 PM

    The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

    How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

    To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

    HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

    HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

    See all articles