欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

web css 选择器的使用

程序员文章站 2022-06-22 11:19:57
外链样式 ID选择器 优先级index.css文件内容#p1{ color:blue;}#div1{ background-color:red;}#p2{ color:green;}#div2{ color:green;} css基础</t....</div> <div class="content"> <p><img onerror="this.src='/statics/superweb999/images/image_error.jpg'"src="/default/index/img?u=aHR0cDovL2ltYWdlczUuMTBxaWFud2FuLmNvbS8xMHFpYW53YW4vMjAyMDEwMjYvYl8wXzIwMjAxMDI2MDkyNjQzNTU2OC5wbmc=" alt="web css 选择器的使用" title="web css 选择器的使用"></p> <h2> <a id="_ID__1" target="_blank" rel="nofollow" href="/default/index/url?u=aHR0cHM6Ly93d3cuMTBxaWFud2FuLmNvbQ=="></a>外链样式 ID选择器 优先级</h2> <p>index.css文件内容</p> <pre><code class="prism language-css"><span class="token selector">#p1</span><span class="token punctuation">{</span> <span class="token property">color</span><span class="token punctuation">:</span>blue<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token selector">#div1</span><span class="token punctuation">{</span> <span class="token property">background-color</span><span class="token punctuation">:</span>red<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token selector">#p2</span><span class="token punctuation">{</span> <span class="token property">color</span><span class="token punctuation">:</span>green<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token selector">#div2</span><span class="token punctuation">{</span> <span class="token property">color</span><span class="token punctuation">:</span>green<span class="token punctuation">;</span> <span class="token punctuation">}</span> </code></pre> <pre><code class="prism language-html"><span class="token doctype"><!DOCTYPE html></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>html</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>en<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>head</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>meta</span> <span class="token attr-name">charset</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>UTF-8<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>title</span><span class="token punctuation">></span></span>css基础<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>title</span><span class="token punctuation">></span></span> <span class="token comment"><!-- 通过#id 查找元素 --></span> <span class="token comment"><!-- <style type="text/css">--></span> <span class="token comment"><!-- #p1{--></span> <span class="token comment"><!-- color:blue;--></span> <span class="token comment"><!-- }--></span> <span class="token comment"><!-- #div1{--></span> <span class="token comment"><!-- background-color:red;--></span> <span class="token comment"><!-- }--></span> <span class="token comment"><!-- </style>--></span> <span class="token comment"><!-- 外部样式 --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>link</span> <span class="token attr-name">rel</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>stylesheet<span class="token punctuation">"</span></span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>css/index.css<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token comment"><!-- 内部样式 --></span> <span class="token comment"><!-- *{} 修改默认样式 边界=0 --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>style</span><span class="token punctuation">></span></span><span class="token style language-css"> <span class="token selector">#div2</span><span class="token punctuation">{</span> <span class="token property">color</span><span class="token punctuation">:</span>red<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token selector">*</span><span class="token punctuation">{</span> <span class="token property">margin</span><span class="token punctuation">:</span>0<span class="token punctuation">;</span> <span class="token property">padding</span><span class="token punctuation">:</span>0<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token selector">p</span><span class="token punctuation">{</span> <span class="token punctuation">}</span> <span class="token selector">div</span><span class="token punctuation">{</span> <span class="token punctuation">}</span> </span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>style</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>head</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>body</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>p</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>p1<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> 今天天气真热 <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>p</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>div1<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> 明天可能会下雨 <span class="token comment"><!-- 子类 继承父类样式 --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span><span class="token punctuation">></span></span> 我是一个子类元素 <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>p</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>p2<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> 后天又是一个大晴天 <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>p</span><span class="token punctuation">></span></span> <span class="token comment"><!-- 就近原则 样式 = 蓝色 内链样式 --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>div2<span class="token punctuation">"</span></span><span class="token style-attr language-css"><span class="token attr-name"> <span class="token attr-name">style</span></span><span class="token punctuation">="</span><span class="token attr-value"><span class="token property">color</span><span class="token punctuation">:</span>blue<span class="token punctuation">;</span></span><span class="token punctuation">"</span></span><span class="token punctuation">></span></span> 我是一个测试的内容 <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>body</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>html</span><span class="token punctuation">></span></span> </code></pre> <h2> <a id="__73" target="_blank" rel="nofollow" href="/default/index/url?u=aHR0cHM6Ly93d3cuMTBxaWFud2FuLmNvbQ=="></a>群组选择器 后代选择器</h2> <pre><code class="prism language-html"><span class="token doctype"><!DOCTYPE html></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>html</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>en<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>head</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>meta</span> <span class="token attr-name">charset</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>UTF-8<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>meta</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>viewport<span class="token punctuation">"</span></span> <span class="token attr-name">content</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>width=device-width, initial-scale=1.0<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>title</span><span class="token punctuation">></span></span>group组选择器<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>title</span><span class="token punctuation">></span></span> <span class="token comment"><!-- 元素,元素,元素 { 样式 } --></span> <span class="token comment"><!-- 后代选择器 空格 div(父类) p(子类){ 样式}--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>style</span><span class="token punctuation">></span></span><span class="token style language-css"> <span class="token selector">div,p,h3</span><span class="token punctuation">{</span> <span class="token property">color</span><span class="token punctuation">:</span>orange<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token selector">p,h3,figure,ul,ol</span><span class="token punctuation">{</span> <span class="token property">margin</span><span class="token punctuation">:</span> 0<span class="token punctuation">;</span> <span class="token property">padding</span><span class="token punctuation">:</span> 0<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token selector">div p</span><span class="token punctuation">{</span> <span class="token property">color</span><span class="token punctuation">:</span> greenyellow<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token selector">div p span</span><span class="token punctuation">{</span> <span class="token property">color</span><span class="token punctuation">:</span> red<span class="token punctuation">;</span> <span class="token punctuation">}</span> </span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>style</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>head</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>body</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span><span class="token punctuation">></span></span>今天<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>p</span><span class="token punctuation">></span></span>明天<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>p</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>h3</span><span class="token punctuation">></span></span>后天<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>h3</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>p</span><span class="token punctuation">></span></span>我是一个子类元素 <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>span</span><span class="token punctuation">></span></span>我是一个孙类元素<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>span</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>p</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>body</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>html</span><span class="token punctuation">></span></span> </code></pre> <h2> <a id="__113" target="_blank" rel="nofollow" href="/default/index/url?u=aHR0cHM6Ly93d3cuMTBxaWFud2FuLmNvbQ=="></a>伪类选择器 类选择器</h2> <pre><code class="prism language-python"><span class="token operator"><</span>!DOCTYPE html<span class="token operator">></span> <span class="token operator"><</span>html lang<span class="token operator">=</span><span class="token string">"en"</span><span class="token operator">></span> <span class="token operator"><</span>head<span class="token operator">></span> <span class="token operator"><</span>meta charset<span class="token operator">=</span><span class="token string">"UTF-8"</span><span class="token operator">></span> <span class="token operator"><</span>meta name<span class="token operator">=</span><span class="token string">"viewport"</span> content<span class="token operator">=</span><span class="token string">"width=device-width, initial-scale=1.0"</span><span class="token operator">></span> <span class="token operator"><</span>title<span class="token operator">></span>hover伪类选择器<span class="token operator"><</span><span class="token operator">/</span>title<span class="token operator">></span> <span class="token operator"><</span>!<span class="token operator">-</span><span class="token operator">-</span> 伪类 依赖于box <span class="token punctuation">:</span> 固有动作样式 hover鼠标移入变化<span class="token operator">-</span><span class="token operator">-</span><span class="token operator">></span> <span class="token operator"><</span>style<span class="token operator">></span> <span class="token punctuation">.</span>box<span class="token punctuation">{</span> width<span class="token punctuation">:</span> 260px<span class="token punctuation">;</span> background<span class="token operator">-</span>color<span class="token punctuation">:</span> blue<span class="token punctuation">;</span> color<span class="token punctuation">:</span> white<span class="token punctuation">;</span> text<span class="token operator">-</span>align<span class="token punctuation">:</span> center<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">.</span>box<span class="token punctuation">:</span>hover<span class="token punctuation">{</span> background<span class="token operator">-</span>color<span class="token punctuation">:</span> green<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">#name:focus{</span> background<span class="token operator">-</span>color<span class="token punctuation">:</span> lightgray<span class="token punctuation">;</span> <span class="token punctuation">}</span> a<span class="token punctuation">:</span>link<span class="token punctuation">{</span> color<span class="token punctuation">:</span> green<span class="token punctuation">;</span> <span class="token punctuation">}</span> a<span class="token punctuation">:</span>visited<span class="token punctuation">{</span> color<span class="token punctuation">:</span> black<span class="token punctuation">;</span> <span class="token punctuation">}</span> a<span class="token punctuation">:</span>hover<span class="token punctuation">{</span> color<span class="token punctuation">:</span> blue<span class="token punctuation">;</span> <span class="token punctuation">}</span> a<span class="token punctuation">:</span>active<span class="token punctuation">{</span> color<span class="token punctuation">:</span> red<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token operator"><</span><span class="token operator">/</span>style<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>head<span class="token operator">></span> <span class="token operator"><</span>body<span class="token operator">></span> <span class="token operator"><</span>!<span class="token operator">-</span><span class="token operator">-</span> 有一个div元素 默认一个背景色, 移动上后变成另外一个颜色 <span class="token operator">-</span><span class="token operator">-</span><span class="token operator">></span> <span class="token operator"><</span>div <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">"box"</span><span class="token operator">></span> 我会变背景色 <span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span>form action<span class="token operator">=</span><span class="token string">"#"</span><span class="token operator">></span> <span class="token operator"><</span>label <span class="token keyword">for</span><span class="token operator">=</span><span class="token string">"name"</span><span class="token operator">></span>姓名<span class="token punctuation">:</span><span class="token operator"><</span><span class="token operator">/</span>label<span class="token operator">></span> <span class="token operator"><</span><span class="token builtin">input</span> <span class="token builtin">type</span><span class="token operator">=</span><span class="token string">"text"</span> <span class="token builtin">id</span><span class="token operator">=</span><span class="token string">"name"</span><span class="token punctuation">,</span> name<span class="token operator">=</span><span class="token string">"name"</span><span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>form<span class="token operator">></span> <span class="token operator"><</span>a href<span class="token operator">=</span><span class="token string">"https:www.baidu.com"</span><span class="token operator">></span>百度<span class="token operator"><</span><span class="token operator">/</span>a<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>body<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>html<span class="token operator">></span> </code></pre> <p>本文地址:https://blog.csdn.net/weixin_45875105/article/details/109246552</p> </div> <div class="tags"> 相关标签: <a href="/t/web/" target="_blank"> web </a> </div> <div class="info-pre-next"> <p> 上一篇: <a href="/article/1489261.html"> ps怎么做立体字 PS制作阳光照射效果的金属立体字教程 </a> </p> <p> 下一篇: <a href="/article/1489263.html"> C++ concurrency in action 读随记1 </a> </p> </div> <div class="wz_tuijian"> <p> 推荐阅读 </p> <ul> <li> <a href="/article/2185463.html" target="_blank" title="使用HTML5里的classList操作CSS类"> <h2> 使用HTML5里的classList操作CSS类 </h2> </a> </li> <li> <a href="/article/2184448.html" target="_blank" title="CSS的color颜色使用说明"> <h2> CSS的color颜色使用说明 </h2> </a> </li> <li> <a href="/article/2182850.html" target="_blank" title="使用CSS3的appearance属性改变任何元素的浏览器默认风格"> <h2> 使用CSS3的appearance属性改变任何元素的浏览器默认风格 </h2> </a> </li> <li> <a href="/article/2182837.html" target="_blank" title="css媒体查询aspect-ratio宽高比在less中的使用"> <h2> css媒体查询aspect-ratio宽高比在less中的使用 </h2> </a> </li> <li> <a href="/article/2182752.html" target="_blank" title="CSS3的文字阴影—text-shadow的使用方法"> <h2> CSS3的文字阴影—text-shadow的使用方法 </h2> </a> </li> <li> <a href="/article/2180175.html" target="_blank" title="ASP.NET core Web中使用appsettings.json配置文件的方法"> <h2> ASP.NET core Web中使用appsettings.json配置文件的方法 </h2> </a> </li> <li> <a href="/article/2176826.html" target="_blank" title="10条影响CSS渲染速度的写法与使用建议第1/3页"> <h2> 10条影响CSS渲染速度的写法与使用建议第1/3页 </h2> </a> </li> <li> <a href="/article/2176721.html" target="_blank" title="采用XHTML和CSS设计可重用可换肤的WEB站点的方法"> <h2> 采用XHTML和CSS设计可重用可换肤的WEB站点的方法 </h2> </a> </li> <li> <a href="/article/2176666.html" target="_blank" title="不要使用CSS Expression的原因分析"> <h2> 不要使用CSS Expression的原因分析 </h2> </a> </li> <li> <a href="/article/2175119.html" target="_blank" title="HTML5 Web Database 数据库的SQL语句的使用方法"> <h2> HTML5 Web Database 数据库的SQL语句的使用方法 </h2> </a> </li> </ul> </div> </article> </div> </main> <footer><div class="box"><div class="ft_nav"><div class="ft_about"><p>关于网站</p><ul><li><a href="/sitemap.xml" target="_blank" title="网站地图">网站地图</a></li><li><a href="/list/2/" title="最新程序员文章站">最新程序员文章站</a></li></ul></div><div class="ft_contact"><ul><li>本站所有数据收集于网络如有侵犯到您的权益,请联系我们进行下架处理。</li><li class="email_show"></li></ul></div></div><div class="copyright"><div class="cr_left"><p> 备案号:<a href="https://beian.miit.gov.cn/#/Integrated/index" target="_blank">粤ICP备20058927号</a></p><p>© Copyright © 2020-2022 www.superweb999.com 程序员文章站. </p></div></div></div></footer> </body> </html>