How to select multiple CSS parent and descendant groups efficiently - css

Is there a more efficient way to select multiple parent and descendant groups?
What I have now:
aside p, aside h1, aside h2, aside h3, aside h4,
article p, article h1, article h2, article h3, article h4,
section p, section h1, section h2, section h3, section h4 {
width: 75%;
padding: 15px 0;
margin: 0 auto;
text-align: center;
}

:matches()
You can use the :matches() CSS pseudo-class function to group your parents and descendants:
:matches(aside, article, section) :matches(p, h1, h2, h3, h4)
Note that, at the time of writing, this is still an experimental technology, so you may want to view the browser compatibility before using this in production.
Older browsers may require using the vendor-prefixed pseudo-class :any():
:-moz-any(aside, article, section) :-moz-any(p, h1, h2, h3, h4),
:-webkit-any(aside, article, section) :-webkit-any(p, h1, h2, h3, h4),
:matches(aside, article, section) :matches(p, h1, h2, h3, h4)
According to CSSWG issue #3258, :matches() might be renamed to :is() in the future:
:is(aside, article, section) :is(p, h1, h2, h3, h4)

You could remove all the tag specificity.
p, h1, h2, h3, h4 {
width: 75%;
padding: 15px 0;
margin: 0 auto;
text-align: center;
}

Using pure CSS, your current way is probably the most efficient means, other than setting a class on all the h tags.
Using LESS, you could do something like this:
aside, article, section {
h1, h2, h3, h4 { ... }
}

if there are only (p, h1-h4) in parents you can do that:
aside > *, article > *, section > * {
width: 75%;
padding: 15px 0;
margin: 0 auto;
text-align: center;
}
it will affect only direct children.

The most performant, concise and specific selector would simply be:
.selector {
width: 75%;
padding: 15px 0;
margin: 0 auto;
text-align: center;
}
it could be opinable that it will pollute your markup but in pure terms of css performance this is it.
with regards to * selector, its performance is poor and adding a parent selector will NOT improve the situation, as illustrated here
The style system matches rules by starting with the key selector, then moving to the left (looking for any ancestors in the rule’s selector). As long as the selector’s subtree continues to check out, the style system continues moving to the left until it either matches the rule, or abandons because of a mismatch.
one last remark, aside, article, section are not supported in IE < 8 so any styling won't be picked up by those browsers (unless a polyfill is used but that is a JS only way)

Related

CSS specificity with the * selector [duplicate]

This question already has answers here:
What does the ">" (greater-than sign) CSS selector mean?
(8 answers)
Closed 3 years ago.
I have a css reset where I put margin to zero. Then further down the code I want to add a margin-bottom to all the elements. But it isn't working and I can't find out why.
I have tried to use the !important rule but that isn't working either and not really the solution I'm after. In my head the specificity should work because they both have the specificity 0,0,1 and the last rule should apply.
Or is it like that the * rule has specificity 0,0,0 and doesn't "beat" the specificity of the others selectors after html and body tags because they have specificity 0,0,1?
html,body,address, article, aside, footer, header, h1, h2, h3, h4,
h5, h6, hgroup, main, nav, section {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
body > * {
margin-bottom: calc(1 * var(--line-height));
}
The result I'm after is that the body > * should apply.
If you want to target all elements you can use
* {
margin-bottom: 1rem;
}
The > symbol is a child combinator, it will only target elements that are direct descendants of the first element.
My guess is that is styling the targeted elements but that these are not the elements you expect to be targeted.
To better visualize this, try and style the border of these elements intstead:
body > * {
border: 10px solid red;}

Font-Awesome not displaying

I have checked and checked and I'm sure it's something simple I've overlooked but my font is not displaying.
The first paragraph is suppose to have quotes around it (you will see it has them right now because I did it without font-awesome); however, you will see the second full paragraph is suppose to have a quote at the beginning and it's not displaying. Please help
http://dev.healthcaresolutionsteam.com/agent/barbara-scott/
Your CSS specificity is being overridden by a style in style.css.
Font-Awesome only has:
.fa { font-family: FontAwesome }
Yet your style overrides the font-family (note the !important):
#fake, .menu, a.signin span, .balloon_text, #footer, #footer a, #signin_menu p a, #learning_center h2, #hst_blog h2, #carriers h2, #career_center h2, #learning_center, #hst_blog, #carrier_careers, .entry-title, #sidebar, #sidebar a, .breadcrumbs a, .breadcrumbs, #searchform input, .page-title, .entry-content, .entry-content a, .entry-utility, .read_more, #content h1, #content h2, #content h3, #content h4, #content h5, #content h6, .widget-title, #live_chat a, #hst_careers, #apply_online, #sales_revenue, #hst_compensation, #search_agents, #search_map, .agent_search, #search-results, #usca-intro-text, .usca-plan, #usca-form {
font-family: 'AllerRegular',Arial,Helvetica,sans-serif !important;
font-weight: normal;
}
Desired style being overridden:
Style actually being applied:
Check, if you use selector *
*{font-family: Arial}
in your default stylesheet.

Can I target all <H> tags with a single selector?

I'd like to target all h tags on a page. I know you can do it this way...
h1,
h2,
h3,
h4,
h5,
h6 {
font: 32px/42px trajan-pro-1,trajan-pro-2;
}
but is there a more efficient way of doing this using advanced CSS selectors? e.g something like:
[att^=h] {
font: 32px/42px trajan-pro-1,trajan-pro-2;
}
(but obviously this doesn't work)
No, a comma-separated list is what you want in this case.
If you're using SASS you could also use this mixin:
#mixin headings {
h1, h2, h3,
h4, h5, h6 {
#content;
}
}
Use it like so:
#include headings {
font: 32px/42px trajan-pro-1, trajan-pro-2;
}
Edit: My personal favourite way of doing this by optionally extending a placeholder selector on each of the heading elements.
h1, h2, h3,
h4, h5, h6 {
#extend %headings !optional;
}
Then I can target all headings like I would target any single class, for example:
.element > %headings {
color: red;
}
It's not basic css, but if you're using LESS (http://lesscss.org), you can do this using recursion:
.hClass (#index) when (#index > 0) {
h#{index} {
font: 32px/42px trajan-pro-1,trajan-pro-2;
}
.hClass(#index - 1);
}
.hClass(6);
Sass (http://sass-lang.com) will allow you to manage this, but won't allow recursion; they have #for syntax for these instances:
#for $index from 1 through 6 {
h#{$index}{
font: 32px/42px trajan-pro-1,trajan-pro-2;
}
}
If you're not using a dynamic language that compiles to CSS like LESS or Sass, you should definitely check out one of these options. They can really simplify and make more dynamic your CSS development.
The new :is() CSS pseudo-class can do it in one selector.
For example, here's how you could target all headings inside a container element:
.container :is(h1, h2, h3, h4, h5, h6)
{
color: red;
}
Most browsers now support :is(), but keep in mind that most browsers made before 2020 didn't support it without a prefix, so be careful about using this if you need to support older browsers.
In some cases, you may instead want to use the :where() pseudo-class, which is very similar to :is() but has different specificity rules.
SCSS+Compass makes this a snap, since we're talking about pre-processors.
#{headings(1,5)} {
//definitions
}
You can learn about all the Compass helper selectors here:
Here is my attempt to solve this problem with (modern) CSS only.
Context : Inside of Joplin (very nice note taking app, link), there is an userfile.css in which you can write your custom CSS for display and export of markdown notes.
I wanted to target all headings directly after (adjacent sibling) certain tags, namely p, ul, ol and nav to add a margin in between. Thus :
p + h1,
p + h2,
p + h3,
p + h4,
p + h5,
p + h6,
ul + h1,
ul + h2,
ul + h3,
ul + h4,
ul + h5,
ul + h6,
ol + h1,
ol + h2,
ol + h3,
ol + h4,
ol + h5,
ol + h6,
nav + h1,
nav + h2,
nav + h3,
nav + h4,
nav + h5,
nav + h6 {
margin-top: 2em;
}
WOW. Very long. Such selectors.
I then came here, learnt, and tried :
p + :is(h1, h2, h3, h4, h5, h6),
ul + :is(h1, h2, h3, h4, h5, h6),
ol + :is(h1, h2, h3, h4, h5, h6),
nav + :is(h1, h2, h3, h4, h5, h6) {
margin-top: 2em;
}
Hmm. Much shorter. Nice.
And then, it struck me :
:is(p, ul, ol, nav) + :is(h1, h2, h3, h4, h5, h6) {
margin-top: 2em;
}
Yay, this also works! How amazoomble!
This might also work with :where() or other CSS combinators like ~ or even (space) to create "matrix" of CSS selectors instead of very long lists.
Credits : all the answers on this page referencing the :is() selector.
Stylus's selector interpolation
for n in 1..6
h{n}
font: 32px/42px trajan-pro-1,trajan-pro-2;
The jQuery selector for all h tags (h1, h2 etc) is " :header ". For example, if you wanted to make all h tags red in color with jQuery, use:
$(':header').css("color","red")
July 2022 update
The future came and the :is selector is what you're looking for as described in this answer given in 2020 by #silverwind (now the selected answer).
Original answer
To tackle this with vanilla CSS look for patterns in the ancestors of the h1..h6 elements:
<section class="row">
<header>
<h1>AMD RX Series</h1>
<small>These come in different brands and types</small>
</header>
</header>
<div class="row">
<h3>Sapphire RX460 OC 2/4GB</h3>
<small>Available in 2GB and 4GB models</small>
</div>
If you can spot patterns you may be able to write a selector which targets what you want. Given the above example all h1..h6 elements may be targeted by combining the :first-child and :not pseudo-classes from CSS3, available in all modern browsers, like so:
.row :first-child:not(header) { /* ... */ }
In the future advanced pseudo-class selectors like :has(), and subsequent-sibling combinators (~), will provide even more control as Web standards continue to evolve over time.
Plain CSS
With plain css you have two ways. This targets all the heading elements wherever they are inside the page (as asked).
:is(h1, h2, h3, h4, h5, h6) {}
This one does the same but keeps the specificity to 0.
:where(h1, h2, h3, h4, h5, h6) {}
With PostCSS
You can also use PostCSS and the custom selectors plugin
#custom-selector :--headings h1, h2, h3, h4, h5, h6;
:--headings {
margin-top: 0;
}
Output:
h1,
h2,
h3,
h4,
h5,
h6 {
margin-top: 0;
}
You could .class all the headings in Your document if You would like to target them with a single selector, as follows,
<h1 class="heading">...heading text...</h1>
<h2 class="heading">...heading text...</h2>
and in the css
.heading{
color: #Dad;
background-color: #DadDad;
}
I am not saying this is always best practice, but it can be useful, and for targeting syntax, easier in many ways,
so if You give all h1 through h6 the same .heading class in the html, then You can modify them for any html docs that utilize that css sheet.
upside, more global control versus "section div article h1, etc{}",
downside, instead of calling all the selectors in on place in the css, You will have much more typing in the html, yet I find that having a class in the html to target all headings can be beneficial, just be careful of precedence in the css, because conflicts could arise from
Using scss you can loop through 6 and append to an empty variable $headings using a comma separator
$headings: ();
#for $index from 1 through 6 {
$headings: list.append($headings, h#{$index}, $separator: comma);
}
#{$headings} {
--default: var(--dark);
color: var(--default);
}
Thanks #steve

CSS First-child

"Set any content id-labelled element with a first-child descendant element of any of h1, h2, h3, h4, h5, and h6 as follows:"
The selector I've created is found below:
#content:first-child h1,
#content:first-child h2,
#content:first-child h3,
#content:first-child h4,
#content:first-child h5,
#content:first-child h6 {}
Is this correct? and if so can it be further simplified?
Thanks for the help everyone!
The description is a little but unclear, but, from what I can understand you want to either
select those h* elements and then the style would be like:
#content h1:first-child,
#content h2:first-child,
#content h3:first-child,
#content h4:first-child,
#content h5:first-child,
#content h6:first-child {}
Example
select the #container element itself in case the first child of it is one of h* family. Then you can not achieve this with pure CSS and need to add a simple JS like (using jQuery in this case):
$('#content').has('h1:first-child, h2:first-child, h3:first-child, h4:first-child, h5:first-child, h6:first-child');
Example
You could add the class="header" to all of your h* tags. So your css now would be
#content:first-child .header { /* whatever */ }
The problem here is that you need to remember to put the class="header" to every tag you need

Re-setting CSS code for form buttons

I used a CSS reset to reset some commonly-used items. The code is this:
html, body, div, h1, h2, h3, h4, ul, ol, li, form, input, fieldset, textarea {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
}
ul {list-style: none outside none;}
img, fieldset {border: 0;}
h1, h2, h3, h4 {font-weight: normal;}
em {font-style: italic;}
strong {font-weight: bold;}
I know there's YUI and Meyer's reset, but I use this one.
Now, the problem I'm experiencing is that I can't get the submit buttons to look normally again. I could ofcourse remove the input from the list and be done with it, but I'd like to know how to get it back, since I might need that in the future.
input[type="submit"]
{
background-color: buttonface;
border: 2px outset buttonface;
color: buttontext;
}
It might be a good idea to iterate through the properties you have changed using javascript or even just looking at it through something like firefox to get the right values, then you would be able to set them to the correct defaults.
From memory I think there are quite significant size differences in the default sizes of buttons between IE, FireFox and Safari - I dont really use opera but I assume there are size issues there - so if you were then to set a padding on the button in CSS it might not render correctly any more in Safari and firefox.

Resources