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

web css 选择器的使用

程序员文章站 2022-04-14 12:01:39
外链样式 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/587490.html"> MVC母版页的使用方法 </a> </p> <p> 下一篇: <a href="/article/587492.html"> 抖音怎么查看有多少金币? </a> </p> </div> <div class="wz_tuijian"> <p> 推荐阅读 </p> <ul> <li> <a href="/article/2077081.html" target="_blank" title="使用简单的CSS3属性实现炫酷读者墙效果"> <h2> 使用简单的CSS3属性实现炫酷读者墙效果 </h2> </a> </li> <li> <a href="/article/2076991.html" target="_blank" title="使用CSS3设计地图上的雷达定位提示效果"> <h2> 使用CSS3设计地图上的雷达定位提示效果 </h2> </a> </li> <li> <a href="/article/2076958.html" target="_blank" title="使用CSS3实现多列布局与多背景的技巧"> <h2> 使用CSS3实现多列布局与多背景的技巧 </h2> </a> </li> <li> <a href="/article/2071164.html" target="_blank" title="使用Python的Scrapy框架编写web爬虫的简单示例"> <h2> 使用Python的Scrapy框架编写web爬虫的简单示例 </h2> </a> </li> <li> <a href="/article/2070664.html" target="_blank" title="HTML5之消息通知的使用(Web Notification)"> <h2> HTML5之消息通知的使用(Web Notification) </h2> </a> </li> <li> <a href="/article/2062230.html" target="_blank" title="css类选择器的使用方法详解"> <h2> css类选择器的使用方法详解 </h2> </a> </li> <li> <a href="/article/2062238.html" target="_blank" title="CSS的inherit与auto使用分析"> <h2> CSS的inherit与auto使用分析 </h2> </a> </li> <li> <a href="/article/2060471.html" target="_blank" title="使用Python下的XSLT API进行web开发的简单教程"> <h2> 使用Python下的XSLT API进行web开发的简单教程 </h2> </a> </li> <li> <a href="/article/2059593.html" target="_blank" title="webpack4 CSS Tree Shaking的使用"> <h2> webpack4 CSS Tree Shaking的使用 </h2> </a> </li> <li> <a href="/article/2057871.html" target="_blank" title="react中使用css的7中方式(应该是最全的)"> <h2> react中使用css的7中方式(应该是最全的) </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>