-
Notifications
You must be signed in to change notification settings - Fork 693
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[css-selectors] Standardize :(‑moz‑)first‑node
and :(‑moz‑)last‑node
#3216
Comments
FWIW, these selectors are somewhat slow, and I planned on trying to hide them from content when I had the chance... |
Selectors mainly work on element level, and extending them to node level may cause some confusing, and ignoring white-space and comments isn't really what "last node" should mean... |
I agree, the only reason these selectors exist at all in Gecko is to implement quirks. |
The demand to differentiate child elements that don't have any text before them from those that do seems to occur quite often (especially in styling user-generated content, in designing components that can be inserted into arbitrary environment, some more use cases are mentioned in #2208). So it would be great if there would be a standard tool for this, even if the current Gecko's experimental implementation is not perfect (though it's much better than nothing:). |
For me it’s elements that don’t have any text after them (eg. the last paragraph in a comment shouldn’t have a bottom margin, but I can’t guarantee that there won’t be any text after it, because I’m dealing with making a custom post style for my posts on forum using a horrible frontend that still uses tables for layout in 2018). |
As the main use case for this is controlling margins, we've added a 'margin-trim' property, see discussion in https://lists.w3.org/Archives/Public/www-style/2018Nov/0005.html Are there other major use cases for this? |
There are the same use cases as for |
:(-moz-)first-node
and :(-moz-)last-node
:(‑moz‑)first‑node
and :(‑moz‑)last‑node
I don’t know how important my use-case is, but on my website, I style Currently, I have to use JavaScript to add a CSS class to these elements, but I would use the // add class to paragraphs that begin with a <strong> element
for (let p of article.querySelectorAll('p')) {
let node = p.childNodes[0];
if (node && node.nodeName === 'STRONG') {
p.classList.add('Issue__note');
}
} |
@emilio Can you elaborate on why these are slow? Presumably they're slow in comparison to :first-child? Is it related to the "ignore text nodes that are just whitespace" bit? |
I just needed this same selector for a project I was working on today. I would like to target a span tag that exists in a block of text. If it is not the first node, I would like to apply This is true for all three examples below: <!-- Example 1 -->
Lorem ipsum <span>TEST</span> lorem ipsum
<!-- Example 2 -->
<span>TEST</span> lorem ipsum lorem ipsum
<!-- Example 3 -->
Lorem ipsum lorem ipsum <span>TEST</span> |
Another use case for this could be changing back the first abbreviation/number from oldstyle numerals and small caps when it is at the beginning of the paragraph. Currently, we don't have any way to know if some element is there. (motivated by the discussion from https://front-end.social/@kizu/112637529030687445, example CodePen: https://codepen.io/kizu/pen/JjqpKqK; ideally we'd want to actually target the first word/node in the sentence, but I don't know if we could ever achieve this with just CSS). |
For me, I am looking at styling these buttons differently:
But unfortunately there is no way to determine if the text is before or after the svg. |
I had just filed a new issue for this (#11154):
The use case that prompted it was very similar to the one mentioned: handling icons in the beginning of a button/menu item/tab/etc as a prefix icon, and if at the end as a suffix icon (slotting them into corresponding slots in components etc). Also, given that text nodes create anonymous boxes wrt grid/flex handling, it seems like the ship has sailed and we can't really keep pretending that text nodes don't exist for the purposes of CSS. 😁 |
I want to remove the left margin only if there is no leading element. .chrome-only {
padding: 0.4em;
background-color: #6633ff;
margin-inline: 0.5em;
color: white;
font-size: 90%;
border-radius: 0.6em;
display: inline-block;
font-family: sans-serif;
font-weight: bold;
}
.chrome-only:-moz-first-node {
margin-inline-start: 0;
} <p>
<span class="chrome-only">Chrome Only</span>You can do ...... Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
<p lang="ja-jp">
この機能は<span class="chrome-only">Chrome限定</span>です。
</p> |
Another example: li > p:-moz-first-node,
dd > p:-moz-first-node {
margin-block-start: 0;
}
li > p:-moz-last-node,
dd > p:-moz-last-node {
margin-block-end: 0;
} <ul>
<li>1st item</li>
<li>
<p>2nd item</p>
<ul>
<li>Another</li>
<li>List</li>
</ul>
<p>Extra description</p>
</li>
<li>3rd item</li>
</ul> |
Right now, CSS Selectors 4 defines
:first-child
and:last-child
, which works as such for the following:For
:first-node
and:last-node
, this would work as follows:Mozilla Firefox already implements the previous in prefixed form and exposes it to the web.
The text was updated successfully, but these errors were encountered: