【问题标题】:Not attribute targeting an element不是针对元素的属性
【发布时间】:2017-01-21 03:18:58
【问题描述】:

我在使用 :not() 伪类时遇到了一些问题,我开始考虑我的目标不能仅在 CSS 中实现。

这是我工作的codepen

我想要实现的是使段落中的第一个字母不在更改范围内。

section p:not([span]):first-letter  {
  font-size:50px;
  font-family:'Cinzel Decorative';
}


<p><span>Unchanged text</span> Changed text</p> // Goal <---

我都尝试过并且知道类可以工作,但是这需要我更改很多以前的代码,并且在这种情况下更喜欢 span 元素。而且由于有多个段落部分,因此查找值也不会有效。

【问题讨论】:

  • [span]span 属性寻址一个元素——这是你想要的吗?
  • 是的,我想,如果我理解正确的问题,澄清一下自己,因为 span 元素在一开始就在段落元素内,span 中的内容将是 :first-letter,但是我希望它排除 span 元素并在 span 关闭后查找第一个字母。
  • 你为什么首先将你的副标题标记为段落中的跨度元素?考虑到您已经用 h1 标记了顶级标题,我觉得您正在创建一个不必要的问题,只需正确标记即可完全避免。
  • @Unkn0wn 我得到了一个使用 CSS 的解决方案,没有布局更改,也没有 Firefox 恶作剧。
  • @zer00ne 很棒的工作,真的很喜欢你的解决方案有多么不同,以及如何遇到每个可能的问题,真的很棒的工作人员,再次感谢! :)

标签: css css-selectors pseudo-element pseudo-class


【解决方案1】:

如果您愿意对您的标记稍作改动,使其在语义上正确,您可以通过以下方式实现:

section {
  max-width: 80%;
   margin: 10px auto; /* changed for demo */
  background-color: rgba(0, 0, 0, 0.3);
  padding-bottom: 2%;
}
section h1 {
  margin: 0;
  text-align: center;
  font-size: 250%;
  padding: 1%;
  background-color: rgba(0, 0, 0, .5);
  color: #C55757;
  font-family: 'Syncopate';
}
section h2 {
  font-size: 30px;
  display: block;
  padding: 1%;
  font-family: 'Syncopate';
  color: #C55757;
  background-color: rgba(0, 0, 0, .35);
}
section div {
  display: inline-block;
  font-size: 20px;
  color: white;
  padding: 1%;
  width: 47%;
  text-align: center;
  vertical-align: top;
  font-family: 'Open Sans Condensed';
  margin-top: 2%;
}
section div:last-of-type {
  border-left: 2px solid black;
}
section p:first-of-type::first-letter {
  font-size: 50px;
  font-family: Cinzel Decorative;
}
<link href="https://fonts.googleapis.com/css?family=Cinzel+Decorative|Syncopate|Open+Sans+Condensed" rel="stylesheet">

<section>
  <h1>Company Name</h1>
  <div>
    <h2>What we do
    </h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec imperdiet tincidunt ornare. Quisque rutrum velit mi, eget aliquet turpis consectetur vel. Maecenas convallis nunc pulvinar urna placerat, nec tincidunt massa </p><p>Morbi quis vehicula leo. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Duis id felis dapibus lectus auctor faucibus vitae vel urna. Vivamus vel dui elit.
    </p>
  </div>
  <div>
    <h2>Our company
    </h2>
    <p>Nunc eget odio sit amet lorem consequat dictum. In consequat, nunc at feugiat volutpat, lacus sapien mollis lectus, sed facilisis risus massa vel augue. Nam at tellus ac odio consectetur interdum ut et ex. Nullam in tincidunt nunc. Nunc tincidunt est eu neque molestie, vitae suscipit ante egestas. Cras id auctor arcu.</p><p>
Cras eget metus tincidunt, eleifend mi id, congue elit. Aenean faucibus est leo, nec rhoncus justo aliquam nec. Praesent erat erat, pellentesque at varius in, ultrices quis urna.
    </p>
  </div>
</section>

【讨论】:

  • “我们做什么”和“我们的公司”应该是 h2,而不是 p。 (顺便说一句,我赞成)
  • 我刚刚更新到那个,我在想一模一样的事情:p(伟大的思想都一样;)
  • 这确实是更好的标记。您仍然应该删除&lt;br&gt; 并使用不同的&lt;p&gt;
  • 是的 @Oriol 确实,这只是来自 OP lorem ipsum 的普通复制粘贴,我已经更新了答案;)
【解决方案2】:

更新 3

我终于有了我的笔记本电脑(当时使用的是 iPhone)并看到了 codepen,所以这是我的看法。

  • 我想改变布局,但我没有,因为疯狂必须有一个方法(虽然我确实改变了内容;请参阅此列表的最后 2 项。)
  • 一般的布局是display:table-* 组。
  • 2 个子标题? “我们做什么”和“我们的工作”是::before 伪元素。
  • &lt;span&gt; 现在充当:first-letter,因为每个浏览器对:first-letter 的解释太古怪了,我们将继续讨论。

CODEPEN

编辑

好的,在 Snippet 2 上开枪,请参阅 Snippet 3,它是 Snippet 2 没有 first-letterfirst-letter 被伪元素 ::before 替换。打败那个火狐!

细节在Snippet中注释

片段 3

/* position: absolute will take span out
|| of the flow. This means whatever affects 
|| the <span> directly will not affect the 
|| <p> and vice versa.
*/

/* ch is a measure unit equalling the width
||   of a zero. It's size is relative to
||   font-size. I find ch indispensible when
||   dealing with text.
*/

span {
  position: absolute;
  left: -12ch;
}
/* Since :first-letter behaves differently than what's
|| desired in Firefox, we'll use a ::before pseudo-
|| and then position it over the 'C'
*/
/* We can adjust the line-height (/40%) to bring both <span> 
|| and <p> in vertical alignment. The left: 1.2ch is the 
|| space between <span> and <p>. The white background is
|| the hacky part which is used to hide the original
|| 'C'. Since the majority of the measurements (i.e. ch)
|| are relative,the setup is responsive as long as you
|| remeber that it's relative to font-size.
*/

p::before {
  content: 'C';
  font: 100 3ch/40% Times;
  color: red;
  background: white;
  position: relative;
  left: 1.2ch;
}
p {
  position: relative;
  left: 12ch;
}
<!--All textNodes residing within <p> includes it's

descendant's textNodes as well. This is evident if we use

.textContent or jQuery .text(). Knowing that, we should

expect that a direct approach using CSS to change the 'C'

with pseudo-selector :first-letter would fail.-->




<p><span>Unchanged text</span> Changed text</p>


<!--Getting the <span> out of the way so that the :first-

letter will be 'C' instead of 'A' is the first step-->



更新 1

参见片段 2,我使用了 position:relativeabsolute,因此 &lt;span&gt; 与其他的流程不同。这个想法是从 BoltClock 和 Oriol 的车队中得到的。


我在想:最古怪的 CSS 属性是什么? 我使用 floats 想出了这个

片段 1

span {
  float: left
}
p:first-letter {
  font: 100 3ch/60% Times;
  color: red;
  float: left;
  padding-left: .5ch;
}
p {
  float: left;
}
&lt;p&gt;&lt;span&gt;Unchanged text&lt;/span&gt; Changed text&lt;/p&gt;

片段 2

span {
  position: absolute;
  left: -12.5ch;
}
p:first-letter {
  font: 100 3ch/40% Times;
  color: red;
  position: relative;
  padding-left: .5ch;
}
p {
  position: relative;
  left: 12ch;
}
&lt;p&gt;&lt;span&gt;Unchanged text&lt;/span&gt; Changed text&lt;/p&gt;

【讨论】:

  • 不幸的是不能在 Firefox 上运行。用你的话说,Firefox 的 ::first-letter 实现是“古怪的”。 (公平地说,everyone's 对 ::first-letter 的实现很古怪,但 Firefox 往往比其他人更突出。Chrome 有一个非常荒谬的空白错误,例如 ::first-letter,在其他地方描述。)
  • 这正是我想要的,是的,但是由于它是最古怪的属性,它真的搞砸了设计的其他部分 +1
  • Firefox 又一次挫败了我!
  • 火狐很古怪吗? draft 表示“::first-letter 伪元素表示其原始元素的第一个格式化行上的第一个印刷字母单元,如果它前面没有任何其他内容”。我认为浮动跨度是“任何其他内容”。
  • @Oriol:您遗漏了关键部分:“如果它前面没有任何其他内容(例如图像或内联表)在其行”。浮点数是格式化行的一部分吗?这是价值百万美元的问题(Firefox 的回答显然是肯定的)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-21
  • 2012-05-20
  • 1970-01-01
  • 2010-12-15
  • 2020-01-20
  • 1970-01-01
相关资源
最近更新 更多