-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(content): lobotomized owl selector
- Loading branch information
1 parent
fc57655
commit f8959e2
Showing
3 changed files
with
65 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
--- | ||
title: Lobotomized Own Selector | ||
date: 2024-10-01 | ||
tags: [css] | ||
--- | ||
|
||
`* + *` | ||
|
||
Yeah, it sounds like something out of Harry Potter spin-off, but it's a real thing. | ||
You might think I'm using the fancy name as a click bait (and you might be _partially_ right), but I also do believe it's helpful. | ||
Heydon Pickering first mentioned it [ten years ago](https://alistapart.com/article/axiomatic-css-and-lobotomized-owls/), | ||
but somehow it's still handy. I guess they liked the name, that's why [margin-trim](https://developer.mozilla.org/en-US/docs/Web/CSS/margin-trim) | ||
takes ages to finalize. | ||
|
||
## yes, but why? | ||
|
||
The owly magic shines when you have plenty of elements and you want to style _**most**_ of them. | ||
Let's imagine you've got a menu and you'd like to have spacing around those buttons: | ||
|
||
```css | ||
nav ul li { | ||
margin-left: 1rem; | ||
} | ||
``` | ||
|
||
The first one would also have a margin. | ||
There are plenty of ways to fix that, right? | ||
|
||
```css | ||
nav ul li:not(:first-child) {} | ||
nav ul li:not(:first-of-type) {} | ||
nav ul li + li {} /* or even li ~ li */ | ||
``` | ||
|
||
Now imagine nested items, like paragraphs with images! | ||
Even worse - sometimes you'd need to know the type of element, or always play with `:first-child` and `:last-child`. | ||
|
||
```css | ||
/* okay that might not sound revolutionary */ | ||
nav ul * + * {} | ||
|
||
/* | ||
* but how about this: | ||
* make all text elements have spacing between them | ||
* they might be headings, paragraphs, images, etc. | ||
*/ | ||
.prose * + * { | ||
padding-bottom: 1rem; | ||
} | ||
``` | ||
|
||
It can get wild [quickly](https://css-tricks.com/spacing-the-bottom-of-modules/): | ||
```css | ||
.module p:last-child, | ||
.module ul:last-child, | ||
.module ol:last-child, | ||
.module dl:last-child { | ||
/* losing battle */ | ||
margin: 0; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters