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

CSS的优先级_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 11:31:50
Original
1186 people have browsed it

样式的优先级:

(内联样式表[嵌入式样式])>(内部样式表)>(外部样式表)

经过测试动手测试发现有个(唯一的)例外 情况:当引用外部样式在内部样式表(非嵌入式样式)的后面时,外部样式会覆盖内部样式 。

 

测试1(插入顺序:内部样式;外部样式)

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html lang="en"><head>    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">    <title>case</title>    <style type="text/css">        *{            margin: 0px;            padding: 0px;        }        #container{            width: 500px;            height: 500px;            background-color: black;/*黑色*/        }    </style>    <link rel="stylesheet" type="text/css" href="CaseTest1.css"><!-- 蓝色 --></head><body>    <div id="container"></div><!-- 结果:蓝色,覆盖--></body></html>
Copy after login

测试2(插入顺序:嵌入样式;外部样式)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html lang="en"><head>    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">    <title>case</title>    <style type="text/css">        *{            margin: 0px;            padding: 0px;        }        #container{            width: 500px;            height: 500px;        }    </style>    </head><body>    <div id="container" style="background-color: red"</div><!-- 结果:红色,没有覆盖--></body><link rel="stylesheet" type="text/css" href="CaseTest1.css"><!-- 蓝色 --></html>
Copy after login

测试3(外部样式;外部样式)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html lang="en"><head>    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">    <title>test</title>    <style type="text/css">        *{            margin: 0px;            padding: 0px;        }        #container{            width: 50px;            height: 50px;        }    </style>    <link rel="stylesheet" type="text/css" href="CaseTest.css"><!-- background-color: red;-->    <link rel="stylesheet" type="text/css" href="CaseTest1.css"><!-- background-color: blue; --></head><body>    <div id="container"></div><!-- 结果:blue--></body></html>
Copy after login

测试4(外部样式;嵌入样式)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html lang="en"><head>    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">    <title>(内联样式表[嵌入式样式])>(外部样式表)</title>    <style type="text/css">        *{            margin: 0px;            padding: 0px;        }        #container{            width: 500px;            height: 500px;        }    </style>    <link rel="stylesheet" type="text/css" href="CaseTest1.css"><!-- 蓝色 --></head><body>    <div id="container" style="background-color: red"></div><!-- 结果:红色--></body></html>
Copy after login

 

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template