DEV Community

Cover image for CSS One-Liners to Improve (Almost) Every Project

CSS One-Liners to Improve (Almost) Every Project

Alvaro Montoro on July 03, 2024

Most of these one-liners will be one declaration inside the CSS rule. In some cases, the selector will be more than just a simple element; in other...
Collapse
 
cavo789 profile image
Christophe Avonture

Nice. Images before and after are really meaningful.

Collapse
 
ziizium profile image
Habdul Hazeez

I was about to say the same.

Collapse
 
zmyaro profile image
Zachary Yaro

Most of these are great, but please please please don't override the user's preferred body text size unless you are using a typeface that needs it! A low vision person likely already increased xer preferred text size across xer whole system; don't assume you know better than xem!

Collapse
 
terabytetiger profile image
Tyler V. (he/him) • Edited

I think it's worth clarifying that the code in the article does account for the user's browser font size preferences.

Setting something like px specifically would override it but using rem or em adjusts based on the base text size the user has set in the browser. 😊

Collapse
 
zmyaro profile image
Zachary Yaro

Yes, I know how rems work, but a person who has already increased the base font size to xer comfort doesn't need you increasing it further, and a person who doesn't want the base font size enlarged doesn't need you overriding xer preference either.

Collapse
 
e1uone profile image
Dmitry

So is there any way to know if the user has changed a default browser font-size or not?

Collapse
 
neilsentence profile image
Nils Sens • Edited

Good comment. But if you specifically accommodate people with low vision, please also accommodate people who need standard English (myself, I have a complex migraine issue that started in the Philippines; lots of non-standard English there...). My brain tries to decypher for a second "xer" etc... gets stuck, and as some kind of "protective measure" this migraine kicks in, completely deactivating my brain's language center for a good 15 minutes. It was diagnosed as mild stroke (transitory ischaemic attack), until a really good neurologist correctly diagnosed it as complex migraine, luckily taking me off blood thinners again. A bit of introspection lead me to the realization that it's a "shutdown response", and I can - somehow - manage. Coming full circle, low vision and my condition have something in common: I get this kind of tunnel vision, and visual artifacts during an attack. I'm not sure if you should put your own ... freedom of expression ... over my mental wellbeing. Which I would understand, if you didn't... maybe the designer's choice not to accommodate people with bad eyesight, and your choice not to change your way of writing to respect people like me, are related, and both (I guess) to be respected. However, if it doesn't cost an arm and a leg, both you and the designer could try to adhere to some kind of beneficial standards.

Collapse
 
zmyaro profile image
Zachary Yaro • Edited

I agree accessibility is extremely important. I haven't heard of that condition before, and I am curious to learn more! What forms of “non-standard English” trigger that response, which standard of English does your condition tend to adhere to, and does it tend to center around spelling, grammar, sentence structure, or multiple of those?

You seem to be using U.S. spellings, so do you struggle with British spellings? Do you also struggle with regional dialects (I myself have lived in Virginia and the Boston area, and have noticed each's distinct local colloquialisms), different fields' professional jargon, made-up terms in works of fiction (as Shakespeare was notorious for), or slang from various decades? I noticed you also included liberal use of ellipses, and used hyphens where dashes would have technically been more correct, so is non-standard punctuation less triggering than non-standard terminology? Are there particular practices you find writers and developers can take to help mitigate your symptoms besides avoiding the use of one particular 51-year-old neopronoun? Thank you in advance for any insights you are willing to share or any links you have where I can learn more about this condition for my accessibility work!

Thread Thread
 
neilsentence profile image
Comment marked as low quality/non-constructive by the community. View Code of Conduct
Nils Sens

You "counter" my above comment by splitting hairs about hyphens or dashes? What I was referring to is not if a line is a millimeter longer, or if something is spelled - or spelt - the British or American way, but when a normal sentence suddenly gets interrupted by words like xer xur xor xou or other stuff that doesn't seem to be a word and not in the context of fantasy stories (name of a wizard or monster or whatnot, you get the point).

I get slightly dizzy just looking back at the previous sentence. My condition is also triggered by wrong syntax, and honestly also by the wrong use of "their" / "there" / "they're", but there, it only causes a slight nausea, not a complete breakdown. I'll try to dig up some profiles on a dating site I've used before (not kidding), where it got really bad (I'm guessing here) - causing the condition to develop in the first place. I hope that by adding, that these words ("neopronouns") are 51 years old ('neo'?), you're not trying to make it somehow appear as if I'm protesting something long-standing or long established. Because: no, they're not established. For two reasons:

  1. it goes back to way before the 1970s. Wikipedia mentions the 18th century!

  2. weird pronouns will never really be established, because there's simply no consensus even among users: as soon as you think "singular they" has become a thing, "ze" gets introduced by someone else, then you say "OK, let's settle on 'ze'!" and xou, xee, xnghu get invented by someone else who finds... I guess by that time ... "they" boring(?). Meaning that very subgroup that is so fond of an alternative lexicon keeps stirring it up so it doesn't actually settle.

According to the article, there's only little actual support by the actual LGBTQ+ community, and also "opposition to the idea in both the cisgender and transgender communities. Many people find them unfamiliar and confusing to use. Some have said that use of neopronouns, especially noun-self pronouns, comes from a position of privilege, makes the LGBT+ community look like a joke, or that the attention placed on neopronouns pulls focus away from larger, more important issues, such as transphobic bullying, the murder of trans people, and suicide. Noun-self pronouns have been viewed by some as unhelpful and unnecessary."

I really wish it's my transphobia or conservativism or whatever neuroticism I might have developed from meeting too many crazy folks, but it's not limited to funky pronouns. I'm wondering whether there are others who suffer from the same or a similar condition (couldn't tell)... Imagine I'd rewrite this whole message, replacing "by" by "vey" and "it" by "og". You'd be uncomfortable reading it, even without my condition.

You'd struggle, and feel a little stupid for a second, until you realize my language is off. Then you'd start judging me, probably for making you question your own IQ. Those are "functional words" you say - so are pronouns.

My plea is simple: avoid neopronouns. Not even the LGBTQ-community adopts them (4%!). Just people who try to express how tolerant or nice they are. I could do the same with other words, but I put the reader's comfort above my own creativeness. I really don't give a damn about LGBTQ, swapping out vowels in other words would also give me a hard time.

Thread Thread
 
neilsentence profile image
Nils Sens

I'm not sure if I'm alone with this, or if there are others who feel the same or similar.

Collapse
 
papylataupe profile image
Papy-La-Taupe

wich unit mesurement is xem ? i know about rem, and em but... is it like a new type for phone like svh/dvh ?

Collapse
 
zmyaro profile image
Zachary Yaro

“Xe”/“xem”/“xer”/“xers”/“xerself” are unofficial explicitly singular gender-neutral alternatives to “he”/“him”/“his”/“his”/“himself” or “she”/“her”/“her”/“hers”/“herself”. More recently, so-called “neopronouns” have gotten more mainstream attention in association with the LGBTQIA+ community, but I first became aware of them as a writer looking for an explicitly singular alternative to the ambiguous “they”, and they have long since become a regular part of my vocabulary. I hope that helps; I didn't intend confusion and generally try to use code styling when writing CSS units like rem, em, vh, svh, lvh, dvh, etc.!

Collapse
 
tkarika profile image
tkarika

Absolutely agree.

Collapse
 
obedsmart profile image
Obed✨✨

It’s better to use percentages instead of making hard rule

Collapse
 
zmyaro profile image
Zachary Yaro

It is good to use relative units (such as % or em) for font sizes in general, yes, but the base font size should also be left at 1rem in most cases rather than assuming the user set it to an undesired size and you know better!

Collapse
 
best_codes profile image
Best Codes • Edited

Nice post! I especially appreciate two things:

  • The images (before / after)
  • The #listicle tag (even though this is a good listicle)

Keep writing!

Collapse
 
alvaromontoro profile image
Alvaro Montoro

I dislike listicles. I really do. I find that, in general, they are lazy ways to present content, often mediocre, and more often than not they make statements that should have a ton of "ifs" attached to them (but don't). I tend to write them as "subtweets"/responses to other listicles I find online... Unfortunately, they have more impact than any other type of article I write.

Collapse
 
best_codes profile image
Best Codes

Unfortunately, they have more impact than any other type of article I write.

Yup, I've experienced that myself. I wish actual good content was more popular.

Collapse
 
miialainen profile image
Miia

Nice, did not know of accent-color, that is really useful!

Collapse
 
mishmanners profile image
Michelle Duke

Loving the code snippets, and the images help illustrate your points 😄

Collapse
 
mishmanners profile image
Michelle Duke

This really took me back to typography days.

Collapse
 
moataz_osama_eb1c1ea9f41a profile image
Moataz Osama

Nice. checkbox color change

Collapse
 
chandra_pantachhetri profile image
Chandra Panta Chhetri

As others have mentioned, I also appreciate the before and after pictures. The clamp & text-wrap balance looks useful

Collapse
 
shricodev profile image
Shrijal Acharya

These are really neat tricks. Now I need to find the Tailwind equivalent classes for some of these.

text-wrap: balance;
Enter fullscreen mode Exit fullscreen mode

This was my discovery of the day.

Collapse
 
terabytetiger profile image
Tyler V. (he/him)

It looks like they all exist - the one that's the least obvious to find is the max width 65ch, which is max-w-prose

Collapse
 
19lilly profile image
Rozvita Lilly Štiglinc

tailwindcss.com/docs/text-wrap tailwind equivalent is


text-balance
Enter fullscreen mode Exit fullscreen mode
Collapse
 
nnisarggada profile image
Nnisarg Gada

Do note that this does take up significant resources for the calculations and is therefore only recommended to be used for headings (as shown in the blog post)

Collapse
 
aqib_shoaib_460d14658d79a profile image
Aqib Shoaib

h1, h2, h3, h4, h5, h6 {
text-wrap: balance;
}
this property is not working with styled components, The IDE is giving me the warning of unknown property...i will provide the screenshort as well

Image description

Collapse
 
alvaromontoro profile image
Alvaro Montoro

Maybe the plugin needs update? text-wrap: balance is widely supported now.

Collapse
 
aqib_shoaib_460d14658d79a profile image
Aqib Shoaib

I have the latest version installed. I even asked chatgpt about it. Chatgpt told me that this property is not standard property of CSS that is why it is being identified as Unknown.

Thread Thread
 
alvaromontoro profile image
Alvaro Montoro

text-wrap is definitely part of the CSS standard, and the balance value was added "recently" (years ago, although it was not implemented by browsers). You can check the W3C site with the information. In particular, the combination text-wrap: balance is already implemented and supported by all major browsers (as indicated in the MDN link from my previous comment).

Thread Thread
 
aqib_shoaib_460d14658d79a profile image
Aqib Shoaib

yes i too noticed that in the meanwhile, i will try using this property in normal way, instead of using styled component library,maybe that will work

Collapse
 
qwqq profile image
Qwqq

epic

Collapse
 
t_tiii_52923b29944dd3 profile image
T “T II” I

Love all of them, I will add them to my template repo's and play around with a few other options. Thanks for that!

About "Limit the width of text within the content" I am not sure about this one based on the before/after and/or if this is just my personal preference that I like the "before" better than the "after".

Collapse
 
aidanldev profile image
Aidan

Love the way you've structured this post. Will be sure to bookmark and go through my projects later and apply some of these tips

Collapse
 
vyan profile image
Vishal Yadav
Collapse
 
wizard798 profile image
Wizard

Woww!, just amazing, thanks for sharing

Collapse
 
bennadel profile image
Ben Nadel

Great recommendations! Re: 16px being too small, amen brother! Here's to getting old gracefully :)

Collapse
 
renaudcci profile image
Renaud-CCI

How could I have lived until now without knowing "accent-color" !! Thanks ! XD

Collapse
 
shlomif profile image
Shlomi Fish

great write up! It seems clamp()/min()/etc. are supported for max-width and width for img too: developer.mozilla.org/en-US/docs/W...

Collapse
 
kocreative profile image
Kat

Some really great info here! Thank you for the simple tips

Collapse
 
hitanshimehta profile image
HitanshiMehta

Really nice

Collapse
 
tevin_enadeghe_906b3c9eb3 profile image
Tevin Enadeghe

This is nice....personally I love animations especially in desktop view. I however believe in creating a balance

Collapse
 
zakari714 profile image
Zakari Adamu

This is awesome.. Thanks for sharing

Collapse
 
queazyg profile image
Quinton Greene

This is a fantastic breakdown of a lot of cool tricks! Seriously good work!

Collapse
 
evart_valls_303ec85853989 profile image
Evart Valls

Muchas gracias, me has ayudado a mejorar mis proyectos

Collapse
 
tevin_enadeghe_906b3c9eb3 profile image
Tevin Enadeghe

Please can someone break down clamp for me?
In reference to the example above

Max-width: clamp(320px 90% 1000px,)
In terms of responsiveness