CSS 之 选择器_html/css_WEB-ITnose
选择器
类型选择器
也叫做 元素选择器和简单选择器,可以根据元素标签指定样式。
p {color:red;}
类选择器
一般用于某些同类型的样式。
.xxxClass{}<div class="xxxClass"></div>
ID选择器
为特殊的元素,指定类型
#xxxId{}<div id="xxxId"></div>
后代选择器
在某个选择器后,选择指定规则的后代,为其指定样式
div p {}
伪类选择器
为某些特殊的元素,在某些条件时,指定样式。
比如,链接的link和visited ;
以及其他元素的hover,focus,active等。
a:link, a:visited {}a:hover, a:focus, a:active {}
通用选择器
类似通配符的作用,比如所有可用元素添加样式。
*{}
高级选择器
子选择器
后代选择器是选择所有的后代,子选择器则可以选择元素的直接后代,即子元素。
#nav>li{}
相邻选择器
#nav + p {}
属性选择器
比如为tooltip添加样式
acronym[title]{}
样式层叠
由于选择同一个元素可以通过不同的方法,比如元素的后代、或者类、或者ID,那么就可能造成样式的重叠。因此可以使用!important,增加样式的优先级。
样式特殊性
再说到样式的重叠的另一种情况,如果不同的方式都指定了样式,到底会优先使用哪一种呢?
这就需要了解特殊性了,从上到下 依次为:
#xxx{}.class{}body div{}div{}
即,元素上的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>
相比较来说,link的方式要更快一些。
另外,还是推荐把CSS都放在同一个文件中。因为浏览器加载样式文件,是受浏览器的限制的,有的浏览器只支持同时下载3个文件、有的支持8个...
如果由三个文件,而浏览器只能同时下载2个文件,那么第三个就必须要等到前两个下载完后,才能加载。
因此,放在一个文件中会更快的加载。
如果把所有的CSS都放在一个文件,又可能导致文件过于庞大,不易维护。因此有以下几个建议:
暂时也就整理这么多吧!后续再补充....

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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.

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.

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

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

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

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...

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 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.
