Home Web Front-end HTML Tutorial CSS的优先级别_html/css_WEB-ITnose

CSS的优先级别_html/css_WEB-ITnose

Jun 21, 2016 am 09:17 AM

1、样式的优先级

    内联样式 > 内部样式 > 外部样式

    以下的特例:外部样式会覆盖内部样式(不推荐内联样式)

<html><head>    <style type="text/css">    div { background:red; }    </style>    <link rel="stylesheet" type="text/css" href="style.css" /></head><body>    <div>Hello World!</div></body></html>
Copy after login


2、选择器的优先权

内联样式(1000)> id(100)> class(10)> tag(10)

以下例子div将显示的背景色是黑色。

<html><head>    <style type="text/css">    #divId    { height:100px; background:black; }    .divClass { height:100px; background:green; }    </style></head><body>    <div id="divId" class="divClass">Hello World!</div></body></html>
Copy after login


同理,之前遇到过一个问题:

<html><head>    <style type="text/css">    #divId    { height:100px; background:black; }    .divClass { height:100px; background:red; }    </style></head><body>    <div id="divId"></div></body><script type="text/javascript">    window.onload = function() {        var divId = document.getElementById("divId");        divId.className = "divClass";    };</script></html>
Copy after login


这时你会发现js的代码不起作用,相信你应该知道原因了吧。

解决的方法有很多,以下说说两种:

(1)如果需要修改的属性少,可以直接用js修改属性

window.onload = function() {    var divId = document.getElementById("divId");    divId.style.background = "red";};
Copy after login

(2)当要修改的属性多,可以在外出加多一个有id的标签,让class的优先级高于当前div的id

<html><head>    <style type="text/css">    #divId    { height:100px; background:black; }    #boss .divClass { height:100px; background:red; }    </style></head><body>    <div id="boss">        <div id="divId"></div>    </div></body><script type="text/javascript">    window.onload = function() {        var divId = document.getElementById("divId");        divId.className = "divClass";    };</script></html>
Copy after login

    




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 Article Tags

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)

Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Mar 04, 2025 pm 12:32 PM

Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

How do I use HTML5 form validation attributes to validate user input?

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

What is the purpose of the <iframe> tag? What are the security considerations when using it?

How to efficiently add stroke effects to PNG images on web pages? How to efficiently add stroke effects to PNG images on web pages? Mar 04, 2025 pm 02:39 PM

How to efficiently add stroke effects to PNG images on web pages?

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

What is the purpose of the <meter> element?

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

What are the best practices for cross-browser compatibility in HTML5?

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

What is the purpose of the <datalist> element?

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

What is the purpose of the <progress> element?

See all articles