<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>post &amp;mdash; 阿步</title>
    <link>https://writee.org/panshanxb/tag:post</link>
    <description>#一意孤行 #土法炼钢 &lt;br&gt;#离经叛道 #痴心妄想</description>
    <pubDate>Fri, 05 Jun 2026 04:16:53 +0000</pubDate>
    <item>
      <title>CSS选择器</title>
      <link>https://writee.org/panshanxb/cssxuan-ze-qi</link>
      <description>&lt;![CDATA[之前让AI帮我写了CSS，虽然用起来完全没问题，但我试着理解它。&#xA;&#xA;选择器&#xA;1. 基础选择器&#xA;&#xA;元素(标签)选择器：匹配所有指定标签，如 div、p。&#xA;类选择器：匹配指定 class，如 .container。&#xA;ID 选择器：匹配指定 id，如 #header。&#xA;通用选择器：匹配所有元素，``。&#xA;&#xA;div class=&#34;btn primary&#34;/div&#xA;类名可以有多个，用空格隔开，ID是唯一的，不能有多个ID，更不会用空格隔开。&#xA;!--more--&#xA;div class=&#34;btn primary&#34;/div&#xA;div class=&#34;btn primary&#34;/div&#xA;2. 组合选择器&#xA;&#xA;后代选择器：空格分隔，匹配所有子孙，如 div p。&#xA;子元素选择器：  ，只匹配直接子元素，如 ul   li。&#xA;紧邻兄弟选择器：+，匹配紧邻的下一个兄弟，如 h1 + p。只匹配第一个符合条件的元素。&#xA;&#xA;html&#xA;h1标题/h1&#xA;p第一段/p&#xA;p第二段/p&#xA;&#xA;css&#xA;h1 + p {    color: red;}&#xA;&#xA;只有第一个p标签会变红。&#xA;&#xA;通用兄弟选择器：~，匹配同一父元素下后续所有兄弟，如 h1 ~ p。匹配所有和h1同级的所有p，不管中间隔了多少其他元素。&#xA;&#xA;h1/h1&#xA;p文本1/p&#xA;div/div&#xA;p文本2/p&#xA;p文本3/p&#xA;位于 h1 后面的所有 p都被选中，包括文本2和文本3。&#xA;&#xA;3. 属性选择器&#xA;&#xA;[attr]：包含某属性&#xA;[attr=value]：属性等于某值&#xA;[attr~=value]：属性值包含某单词&#xA;[attr|=value]：属性值以指定值开头&#xA;[attr^=value]：属性值以指定值开头&#xA;[attr$=value]：属性值以指定值结尾&#xA;[attr=value]：属性值包含指定字符串&#xA;&#xA;包含某属性&#xA;HTML&#xA;input type=&#34;text&#34;&#xA;input&#xA;input type=&#34;password&#34;&#xA;&#xA;CSS&#xA;input[type] {&#xA;    border: 2px solid red;&#xA;}&#xA;&#xA;属性等于某值&#xA;&#xA;input[type=&#34;password&#34;] {    background-color: yellow;}&#xA;&#xA;只匹配 input type=&#34;password&#34;&#xA;&#xA;4. 伪类选择器&#xA;&#xA;伪类选择器，选择的是已经存在的元素的特定条件(状态，位置等)。只有触发特定条件的时候才会被选中。&#xA;&#xA;/ 鼠标悬停状态 /&#xA;a:hover {&#xA;    color: red;&#xA;}&#xA;&#xA;/ 第一个子元素 /&#xA;li:first-child {&#xA;    font-weight: bold;&#xA;}&#xA;&#xA;/ 表单输入为空时 /&#xA;input:placeholder-shown {&#xA;    border: 1px solid gray;&#xA;}&#xA;&#xA;仍然作用于已有元素之上。&#xA;&#xA;5. 伪元素选择器&#xA;伪元素/虚拟元素选择器，创建或选中元素内部的虚拟内容，让你可以样式化它。&#xA;&#xA;/ 在每个段落前插入内容 /&#xA;p::before {&#xA;    content: &#34;★ &#34;;&#xA;    color: gold;&#xA;}&#xA;&#xA;/ 段落的首字母放大 /&#xA;p::first-letter {&#xA;    font-size: 32px;&#xA;    font-weight: bold;&#xA;}&#xA;&#xA;/ 段落首行样式 /&#xA;p::first-line {&#xA;    text-transform: uppercase;&#xA;}&#xA;&#xA;多个选择器可以同时应用某一个样式，如 h1, h2, h3 { color: red; }&#xA;&#xA;---&#xA;&#xA;观察&#xA;&#xA;在了解选择器之后，我们可以看到，这段我实际使用的CSS代码（完全由AI提供），其实选择器是冗余的。&#xA;&#xA;#post ul,#post ol,&#xA;.post ul,.post ol,&#xA;article ul,article ol,&#xA;.h-entry ul,.h-entry ol{&#xA;  margin:1.2em 0;&#xA;  padding-left:1.8em;&#xA;}&#xA;&#xA;post li,.post li,&#xA;article li,.h-entry li{&#xA;  margin:.45em 0;&#xA;  line-height:1.85;&#xA;  color:#dbe4ee;&#xA;}&#xA;&#xA;post li::marker,&#xA;.post li::marker,&#xA;article li::marker,&#xA;.h-entry li::marker{&#xA;  color:#6cf2ff;&#xA;}&#xA;&#xA;/ ---------- hr ---------- /&#xA;&#xA;post hr,.post hr,&#xA;article hr,.h-entry hr{&#xA;  border:0;&#xA;  height:1px;&#xA;  margin:2.8em 0;&#xA;  background:linear-gradient(&#xA;    to right,&#xA;    transparent,&#xA;    rgba(108,242,255,.55),&#xA;    transparent&#xA;  );&#xA;}&#xA;&#xA;/ ---------- strong ---------- /&#xA;&#xA;post strong,.post strong,&#xA;article strong,.h-entry strong{&#xA;  color:#ffffff;&#xA;  font-weight:700;&#xA;}&#xA;&#xA;/ ---------- table ---------- /&#xA;&#xA;post table,.post table,&#xA;article table,.h-entry table{&#xA;  width:100%;&#xA;  border-collapse:collapse;&#xA;  margin:1.6em 0;&#xA;  overflow:hidden;&#xA;  border-radius:16px;&#xA;  background:#0f1324;&#xA;}&#xA;&#xA;大量的重复选择器，id选择器#post，类选择器.post，article标签 和 类选择器.h-entry 。在实际上WriteFrelly页面代码中，似乎没有.post ，好像只需要一条article加上具体的标签 就可以匹配到。]]&gt;</description>
      <content:encoded><![CDATA[<p>之前让AI帮我写了CSS，虽然用起来完全没问题，但我试着理解它。</p>

<h3 id="选择器">选择器</h3>

<h3 id="1-基础选择器">1. <strong>基础选择器</strong></h3>
<ul><li><strong>元素(标签)选择器</strong>：匹配所有指定标签，如 <code>div</code>、<code>p</code>。</li>
<li><strong>类选择器</strong>：匹配指定 class，如 <code>.container</code>。</li>
<li><strong>ID 选择器</strong>：匹配指定 id，如 <code>#header</code>。</li>
<li><strong>通用选择器</strong>：匹配所有元素，<code>*</code>。</li></ul>

<pre><code class="language-html">&lt;div class=&#34;btn primary&#34;&gt;&lt;/div&gt;
</code></pre>

<p>类名可以有多个，用空格隔开，ID是唯一的，不能有多个ID，更不会用空格隔开。

<div class="btn primary"></div>
<div class="btn primary"></div></p>

<h3 id="2-组合选择器">2. <strong>组合选择器</strong></h3>
<ul><li><strong>后代选择器</strong>：空格分隔，匹配所有子孙，如 <code>div p</code>。</li>
<li><strong>子元素选择器</strong>：<code>&gt;</code>，只匹配直接子元素，如 <code>ul &gt; li</code>。</li>
<li><strong>紧邻兄弟选择器</strong>：<code>+</code>，匹配紧邻的下一个兄弟，如 <code>h1 + p</code>。只匹配第一个符合条件的元素。</li></ul>

<pre><code class="language-css">html
&lt;h1&gt;标题&lt;/h1&gt;
&lt;p&gt;第一段&lt;/p&gt;
&lt;p&gt;第二段&lt;/p&gt;

css
h1 + p {    color: red;}
</code></pre>

<p>只有第一个p标签会变红。</p>
<ul><li><strong>通用兄弟选择器</strong>：<code>~</code>，匹配同一父元素下后续所有兄弟，如 <code>h1 ~ p</code>。匹配所有和h1同级的所有p，不管中间隔了多少其他元素。</li></ul>

<pre><code class="language-html">&lt;h1&gt;&lt;/h1&gt;
&lt;p&gt;文本1&lt;/p&gt;
&lt;div&gt;&lt;/div&gt;
&lt;p&gt;文本2&lt;/p&gt;
&lt;p&gt;文本3&lt;/p&gt;
</code></pre>

<p>位于 <code>h1</code> 后面的所有 <code>p</code>都被选中，包括文本2和文本3。</p>

<h3 id="3-属性选择器">3. <strong>属性选择器</strong></h3>
<ul><li><code>[attr]</code>：包含某属性</li>
<li><code>[attr=value]</code>：属性等于某值</li>
<li><code>[attr~=value]</code>：属性值包含某单词</li>
<li><code>[attr|=value]</code>：属性值以指定值开头</li>
<li><code>[attr^=value]</code>：属性值以指定值开头</li>
<li><code>[attr$=value]</code>：属性值以指定值结尾</li>
<li><code>[attr*=value]</code>：属性值包含指定字符串</li></ul>

<h4 id="包含某属性">包含某属性</h4>

<p>HTML</p>

<pre><code class="language-html">&lt;input type=&#34;text&#34;&gt;
&lt;input&gt;
&lt;input type=&#34;password&#34;&gt;
</code></pre>

<p>CSS</p>

<pre><code class="language-css">input[type] {
    border: 2px solid red;
}
</code></pre>

<h4 id="属性等于某值">属性等于某值</h4>

<pre><code class="language-css">
input[type=&#34;password&#34;] {    background-color: yellow;}
</code></pre>

<p>只匹配 <code>&lt;input type=&#34;password&#34;&gt;</code></p>

<h3 id="4-伪类选择器">4. <strong>伪类选择器</strong></h3>

<p><strong>伪类选择器</strong>，选择的是<code>已经存在的元素</code>的特定条件(状态，位置等)。只有触发特定条件的时候才会被选中。</p>

<pre><code class="language-css">/* 鼠标悬停状态 */
a:hover {
    color: red;
}

/* 第一个子元素 */
li:first-child {
    font-weight: bold;
}

/* 表单输入为空时 */
input:placeholder-shown {
    border: 1px solid gray;
}
</code></pre>

<p>仍然作用于已有元素之上。</p>

<h3 id="5-伪元素选择器">5. <strong>伪元素选择器</strong></h3>

<p><strong>伪元素/虚拟元素选择器</strong>，创建或选中元素内部的虚拟内容，让你可以样式化它。</p>

<pre><code class="language-css">/* 在每个段落前插入内容 */
p::before {
    content: &#34;★ &#34;;
    color: gold;
}

/* 段落的首字母放大 */
p::first-letter {
    font-size: 32px;
    font-weight: bold;
}

/* 段落首行样式 */
p::first-line {
    text-transform: uppercase;
}
</code></pre>

<p>多个选择器可以同时应用某一个样式，如 <code>h1, h2, h3 { color: red; }</code></p>

<hr>

<h2 id="观察">观察</h2>

<p>在了解选择器之后，我们可以看到，这段我实际使用的CSS代码（完全由AI提供），其实选择器是冗余的。</p>

<pre><code class="language-css">#post ul,#post ol,
.post ul,.post ol,
article ul,article ol,
.h-entry ul,.h-entry ol{
  margin:1.2em 0;
  padding-left:1.8em;
}

#post li,.post li,
article li,.h-entry li{
  margin:.45em 0;
  line-height:1.85;
  color:#dbe4ee;
}

#post li::marker,
.post li::marker,
article li::marker,
.h-entry li::marker{
  color:#6cf2ff;
}

/* ---------- hr ---------- */

#post hr,.post hr,
article hr,.h-entry hr{
  border:0;
  height:1px;
  margin:2.8em 0;
  background:linear-gradient(
    to right,
    transparent,
    rgba(108,242,255,.55),
    transparent
  );
}

/* ---------- strong ---------- */

#post strong,.post strong,
article strong,.h-entry strong{
  color:#ffffff;
  font-weight:700;
}

/* ---------- table ---------- */

#post table,.post table,
article table,.h-entry table{
  width:100%;
  border-collapse:collapse;
  margin:1.6em 0;
  overflow:hidden;
  border-radius:16px;
  background:#0f1324;
}
</code></pre>

<p>大量的重复选择器，id选择器<a href="/panshanxb/tag:post" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">post</span></a>，类选择器.post，article标签 和 类选择器.h-entry 。在实际上WriteFrelly页面代码中，似乎没有.post ，好像只需要一条<code>article</code>加上具体的标签 就可以匹配到。</p>
]]></content:encoded>
      <guid>https://writee.org/panshanxb/cssxuan-ze-qi</guid>
      <pubDate>Wed, 27 May 2026 06:03:21 +0000</pubDate>
    </item>
  </channel>
</rss>