Skip to content
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

Front-end style.css specificity audit #11779

Closed
m-e-h opened this issue Nov 12, 2018 · 8 comments
Closed

Front-end style.css specificity audit #11779

m-e-h opened this issue Nov 12, 2018 · 8 comments
Labels
CSS Styling Related to editor and front end styles, CSS-specific issues. [Feature] Custom Editor Styles Functionality for adding custom editor styles [Type] Code Quality Issues or PRs that relate to code quality
Milestone

Comments

@m-e-h
Copy link
Member

m-e-h commented Nov 12, 2018

I know it's been mentioned but I couldn't find a relevant issue to attach this to. So I've opened this one to give the conversation a home.

Describe the bug
Many selectors in the build/block-library/style.css file are unnecessarily nested and over-qualified.
This makes it difficult for themes and plugins to override and adds weight to the front-end file.

Wasn't sure if a single or multiple PRs would be better so I did a quick run through build/block-library/style.css and created a gist with comments where I think things could improve.
https://gist.github.com/m-e-h/2e3ab2ee68825bd2728edd52be96be95

I'm not super familiar with how the styles got to where they're at so some of my suggestions are probably not possible. I'm also sure there are some areas for optimization that I missed.

Similar
#1720
#11677
#10067
#11752

@designsimply designsimply added the Needs Design Feedback Needs general design feedback. label Nov 13, 2018
@designsimply
Copy link
Member

If there are concerns with individual cases around css specificity, we should create specific issues. At the same time, I value the desire to discuss and be on the same page regarding what is needed overall.

Could you perhaps start with a specific example of one thing in a comment here that you find unnecessarily nested and overqualified and explain why? Another idea could be to start by auditing a single block and report your findings separately to see if that approach is more actionable.

@m-e-h
Copy link
Member Author

m-e-h commented Nov 14, 2018

Let's start with button CSS
Specificity was calculated here https://specificity.keegan.st/

PR #12005

button

.wp-block-button__link
current specificity: 0,2,0

.wp-block-button .wp-block-button__link {

0,1,0

.wp-block-button__link {

Is there a reason .wp-block-button_link styles are reliant on .wp-block-button?

.is-style-squared
current specificity: 0,3,0

.wp-block-button.is-style-squared .wp-block-button__link {

0,2,0

.is-style-squared > .wp-block-button__link {

.has-background
current specificity: 0,2,0

.wp-block-button__link:not(.has-background) {
    background-color: #32373c;

0,1,0

.wp-block-button__link {
    background-color: #32373c;

So currently Twenty-Seventeen needs a 0,2,0 selector to override the button style. So it's using .wp-block-button .wp-block-button__link. Guess what... The default background-color select doesn't work because .has-pale-pink-background-color { is only a 0,1,0.

This should probably be reported for the Twenty-Seventeen theme but that's just a tree in the forest and a symptom of the bigger problem.

text-color
:not(.has-text-color) and .is-style-outline is the same as above. I'd actually recommend color styles be added to .wp-block-button and let the .wp-block-button__link inherit them.

current specificity: 0,4,0

.wp-block-button.is-style-outline .wp-block-button__link:not(.has-text-color)

0,1,0

.wp-block-button {
	color: #fff;
}

0,1,0

.is-style-outline {
	color: #32373c;
}

0,1,0

.wp-block-button__link {
	color: inherit;
}
@m-e-h
Copy link
Member Author

m-e-h commented Nov 14, 2018

cover

*wp-block-cover-text
current specificity: 0,3,0

.wp-block-cover-image.has-left-content .wp-block-cover-text {
    text-align: left;

0,2,0

.has-left-content .wp-block-cover-text {
    text-align: left;

Or even better, is there a situation where this would have undesirable results?
0,1,0

.has-left-content {
    text-align: left;
}

.wp-block-cover-text {
    text-align: inherit
}

.wp-block-cover-image a
current specificity: 0,2,1 (0,3,1 with the :psuedo)

.wp-block-cover-image .wp-block-cover-image-text a,
.wp-block-cover-image .wp-block-cover-image-text a:active,
.wp-block-cover-image .wp-block-cover-image-text a:focus,
.wp-block-cover-image .wp-block-cover-image-text a:hover,
.wp-block-cover-image .wp-block-cover-text a,
.wp-block-cover-image .wp-block-cover-text a:active,
.wp-block-cover-image .wp-block-cover-text a:focus,
.wp-block-cover-image .wp-block-cover-text a:hover,
.wp-block-cover-image h2 a,
.wp-block-cover-image h2 a:active,
.wp-block-cover-image h2 a:focus,
.wp-block-cover-image h2 a:hover,
.wp-block-cover .wp-block-cover-image-text a,
.wp-block-cover .wp-block-cover-image-text a:active,
.wp-block-cover .wp-block-cover-image-text a:focus,
.wp-block-cover .wp-block-cover-image-text a:hover,
.wp-block-cover .wp-block-cover-text a,
.wp-block-cover .wp-block-cover-text a:active,
.wp-block-cover .wp-block-cover-text a:focus,
.wp-block-cover .wp-block-cover-text a:hover,
.wp-block-cover h2 a,
.wp-block-cover h2 a:active,
.wp-block-cover h2 a:focus,
.wp-block-cover h2 a:hover {
	color: #fff;
}

Why couldn't we replace all that with this?:
0,1,1 (0,2,1 with the :psuedo)

.wp-block-cover a,
.wp-block-cover a:active,
.wp-block-cover a:focus,
.wp-block-cover a:hover {
	color: #fff;
}

Or even better, use color: inherit; instead.

.has-background-dim
current specificity: 0,3,1

.wp-block-cover.has-background-dim.has-background-dim-10::before {

0,2,1

.wp-block-cover.has-background-dim-10::before {
@m-e-h
Copy link
Member Author

m-e-h commented Nov 16, 2018

embeds

.wp-block-embed__wrapper
current specificity: 0,4,0

.wp-embed-responsive .wp-block-embed.wp-embed-aspect-1-1 .wp-block-embed__wrapper,
.wp-embed-responsive .wp-block-embed.wp-embed-aspect-1-2 .wp-block-embed__wrapper,
.wp-embed-responsive .wp-block-embed.wp-embed-aspect-4-3 .wp-block-embed__wrapper,
.wp-embed-responsive .wp-block-embed.wp-embed-aspect-9-16 .wp-block-embed__wrapper,
.wp-embed-responsive .wp-block-embed.wp-embed-aspect-16-9 .wp-block-embed__wrapper,
.wp-embed-responsive .wp-block-embed.wp-embed-aspect-18-9 .wp-block-embed__wrapper,
.wp-embed-responsive .wp-block-embed.wp-embed-aspect-21-9 .wp-block-embed__wrapper {
	position: relative;
}

Replace all that with this:
0,2,0

.wp-embed-responsive .wp-block-embed__wrapper {
	position: relative;
}

.wp-block-embed__wrapper::before
current specificity: 0,4,1

.wp-embed-responsive .wp-block-embed.wp-embed-aspect-1-1 .wp-block-embed__wrapper::before,
.wp-embed-responsive .wp-block-embed.wp-embed-aspect-1-2 .wp-block-embed__wrapper::before,
.wp-embed-responsive .wp-block-embed.wp-embed-aspect-4-3 .wp-block-embed__wrapper::before,
.wp-embed-responsive .wp-block-embed.wp-embed-aspect-9-16 .wp-block-embed__wrapper::before,
.wp-embed-responsive .wp-block-embed.wp-embed-aspect-16-9 .wp-block-embed__wrapper::before,
.wp-embed-responsive .wp-block-embed.wp-embed-aspect-18-9 .wp-block-embed__wrapper::before,
.wp-embed-responsive .wp-block-embed.wp-embed-aspect-21-9 .wp-block-embed__wrapper::before {
	content: "";
	display: block;
	padding-top: 50%;
}

Replace all that with this?:
0,2,1

.wp-embed-responsive .wp-block-embed__wrapper::before {
	content: "";
	display: block;
	padding-top: 50%;
}

.wp-block-embed__wrapper iframe
current specificity: 0,4,1

.wp-embed-responsive .wp-block-embed.wp-embed-aspect-1-1 .wp-block-embed__wrapper iframe,
.wp-embed-responsive .wp-block-embed.wp-embed-aspect-1-2 .wp-block-embed__wrapper iframe,
.wp-embed-responsive .wp-block-embed.wp-embed-aspect-4-3 .wp-block-embed__wrapper iframe,
.wp-embed-responsive .wp-block-embed.wp-embed-aspect-9-16 .wp-block-embed__wrapper iframe,
.wp-embed-responsive .wp-block-embed.wp-embed-aspect-16-9 .wp-block-embed__wrapper iframe,
.wp-embed-responsive .wp-block-embed.wp-embed-aspect-18-9 .wp-block-embed__wrapper iframe,
.wp-embed-responsive .wp-block-embed.wp-embed-aspect-21-9 .wp-block-embed__wrapper iframe {

0,2,1

.wp-embed-responsive .wp-block-embed__wrapper iframe {

or even:
0,1,1

.wp-embed-responsive iframe {

aspect-* .wp-block-embed__wrapper::before x7
current specificity: 0,4,1

.wp-embed-responsive .wp-block-embed.wp-embed-aspect-21-9 .wp-block-embed__wrapper::before {
	padding-top: 42.85%;
}

0,3,1

.wp-embed-responsive .wp-embed-aspect-21-9 .wp-block-embed__wrapper::before {
	padding-top: 42.85%;
}

figcaption
current specificity: 0,1,1

.wp-block-embed figcaption {
	color: #555d66;
	font-size: 13px;
	margin-bottom: 1em;
	margin-top: 0.5em;
	text-align: center;
}

Maybe a little off topic but I'm noticing several places where figcaption is qualified with a .class but given the exact same styles in each instance. Would it be wrong to just style figcaption in general? If a theme already had styles for figcaption, they would override.
0,0,1

figcaption {
	color: #555d66;
	font-size: 13px;
	margin-bottom: 1em;
	margin-top: 0.5em;
	text-align: center;
}
@designsimply designsimply added the [Type] Code Quality Issues or PRs that relate to code quality label Nov 16, 2018
@joyously
Copy link

I'm thinking specificity is a problem, but also that the CSS has so many colors and font sizes and paddings and margins. It shouldn't have any of that. The theme will handle all that naturally. But especially colors should not be in styles that affect content. (they are okay for editor interface)
The figcaption is one that is totally overkill; every property should be removed. I am seeing button color of gray, even though that is not a color in my theme (and this is after the fix in 12005) and it shows on the front end as well!

The specificity problems I encounter are on blockquote, pre, and code elements, (even h1, h2, h3 have color styles in the editor). Because my theme styles the elements, then the GB styles are styling the classes that wrap the elements, the GB styles override (more specific).

@karmatosed
Copy link
Member

This is something totally should be looked at during 5.0.1 perhaps. I am going to remove the design feedback label as this firmly sits in code. It won't or shouldn't effect any design.

@CalumChilds
Copy link

My editor-style.css file for my custom theme contains the same rules as my main style.css file but it contains !important at the end of each line. Why?

m-e-h added a commit to m-e-h/gutenberg that referenced this issue Dec 7, 2018
Reduces specificity in core Cover styles to ease clashes with theme styles.
This has been explained and discussed in further detail here WordPress#11779 (comment)

The cover text color was added to the parent `.wp-block-cover` element so that text`.wp-block-cover-text` and links`.wp-block-cover-text a` could `inherit` the color. This drys up the code and allows for single class selector overrides.

I've also removed the references and styles for `.wp-block-cover-image`. I couldn't find anything mentioning a planned removal date and assumed now would be OK to do so..?

I'm also not sure what the `h2` styles are referencing. Is an `h2` something I'm missing within the Cover Block? I'd like to remove if not.
@m-e-h m-e-h mentioned this issue Dec 7, 2018
4 tasks
@mapk mapk added this to To do in Phase 2 design Jan 23, 2019
@karmatosed karmatosed added this to To Do in Phase 2 via automation Jan 31, 2019
@karmatosed karmatosed removed this from To do in Phase 2 design Jan 31, 2019
@karmatosed karmatosed moved this from To Do to Tighten Up in Phase 2 Jan 31, 2019
@karmatosed karmatosed removed the Needs Design Feedback Needs general design feedback. label Apr 23, 2019
@noisysocks noisysocks removed this from Tighten Up in Phase 2 Jul 19, 2019
@noisysocks noisysocks added this to To Do in Tightening Up Jul 19, 2019
@jasmussen jasmussen added the [Feature] Custom Editor Styles Functionality for adding custom editor styles label Oct 1, 2019
@mtias
Copy link
Member

mtias commented Jul 2, 2024

There's been a lot of changes and reductions in specificity as part of the style engine work, variations, global styles, etc. It's still an active area of improvements, so if there's some specific rules that need improving let's tackle those separately.

@mtias mtias closed this as completed Jul 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CSS Styling Related to editor and front end styles, CSS-specific issues. [Feature] Custom Editor Styles Functionality for adding custom editor styles [Type] Code Quality Issues or PRs that relate to code quality
9 participants