youCanStyleBetter.jpg
veen.png Praveen Gorakala
5 min read Sep 23, 2021
css

You Can Style Better

css

This article is ultimately coming out of my experiences about the problems I faced & solved.

There are moments when you run into a problem or face unavoidable friction while building or designing something with CSS. And it’s easy to make the below mistakes.

And this is my attempt to share some tips and methods on how you can improve the formatting and writing in a structured way.

Know the Concept then you can Develop.

When you start to develop a page or a component you should know the concept of how you build the layout. If you understand the below image, Layers of Design, you are an exception from this.

ycsb.jpg

If we consider the above image, You’ll see the landing page on the left. It’s a Layout with a combination of Features like Navigation, Content and Footer. Whereas Navigation is a feature, a combination of Patterns like logo, menu items and CTAs. Furtherly CTA is a group of Elements (Text and Icon) and finally, Text is defined from Design tokens such as Color, Font Spacing etc.

Let’s take another simple example. HTML does not have an element called Toggle (Switch), it’s just a Pattern, made of Elements like labels, checkboxes and toggles images as shown below.

ycsb1.jpg

If you don’t understand the Layers of Design concept, it’s going to be a blocker for you to get started.

Margins and Paddings.

Both of these CSS properties are for spacing. That doesn’t mean both are the same. It’s a crime to use Margin instead of padding (Vice versa) 🤣.

On a Short note, Padding is the space from the content to its boundary (Border). More like, Area of the element. Whereas Margin is the spacing outside of the boundary. It’s always the space between two different elements.

ycsb2.jpg

Do not give padding as spacing if the element has a border or background property. Check the above example.

Avoiding “! important” is important

Another common mistake that I observed is overriding existing CSS styles with !important to avoid specificity. Though this is a useful and unique property, it backfires when we try to modify the styles again because We can’t put Double !important.

important.gif

So let’s keep the uniqueness of this property by using it rarely and just be more specific.

ycsb3.jpg

Above example, we have used more specificity to address an element to avoid !important. CSS provides more ways to address a component such as element, class, id, attribute, pseudo selectors and many more. We should take advantage of these selectors to give more specificity.

If you use absolute units you are unfit.

Consider a section with 500px (Absolute Unit) of width inside a screen of 1000px width, it stays the same even if you change the screen width but if you mention it as 50% (Relative Unit) its change according to the screen’s width. So, Avoiding absolute units (px, in, pt, mm, pc) and start using relative units(em, rem, %, vh, vw). This helps you to build better layouts and style better responsiveness. Not just for height and widths but also properties like font-size, padding, margin, line-height

ycsb4.jpg

Zero is Zero.

Yes, Zero is Zero and you don’t need to mention Unit. The Unit is optional when the value is 0. For the browser 0 = 0px = 0em = 0vw = 0% . And by using 0 over 0px, two extra characters that can be avoided and around 8bits can be save in a statement like padding : 0px 0px 0px 0px; which can be easily written as padding: 0;

ycsb5.jpg

Do not repeat Styles.

Style like there is tomorrow and reuse the styles which you wrote today. Reusability and adaptability are key things to write clean CSS.

ycsb6.jpg

Here in the above example, we created a template called .button and we avoided style repetition by extended the same template to create primary and secondary button variants.

Use shorthand.

Like you saw in above two sections, this also helps us to shorten the styling. Why write multiple lines where you can group them in a single line like below.

ycsb7.jpg

Yes, with CSS we can group CSS properties like background, border padding, animation etc, and it is recommended to take advantage of that.

Fall Backs

Isn’t it irritating when something works in your environment and breaks in others systems. This happens when the web applications fails to load fonts or images from local or CDN’s fails. By adding fallback values to CSS properties we can overcome this. I’ll explain with below example of a White text on dark image.

ycsb8.jpg

If the image failed to load this could lead to Text that’s there, but invisible.

ycsb9.jpg

We can add a proper contrast color as background color and avoid this problem. Below, I added background color as fallback value. So even if the image fails to load or until the image load in slow connections, fallback property background-color : maroon; will solve the problem.

ycsb10.jpg

Not just Chrome

Test on all environments. I personally love to develop using Chrome browser, but not every user will use the same browser, version or operating system. We are not developing for ourselves so keep in mind that whatever we are developing should be accessible for everyone.

ycsb11.jpg

Luckily all the recent browser engines are similar (95%) but there are people who usually wont update their browsers so better build in one browser and test on multiple environment.

Wrapping up :

Following these tricks will help you to avoid common mistakes. So that, you can style your next project in a managed way.Thats not all, But for now let me put comma here until I getback with more tricks.

Application Modernization Icon

Unleash your brand's potential with AntStack Design, our dedicated UI/UX team. Experience design solutions that embody your vision, resonate with your audience, and drive growth. Embark on a transformative design journey tailored to your startup's unique needs.

Talk to us

Author(s)

Tags

Your Digital Journey deserves a great story.

Build one with us.

Recommended Blogs

Cookies Icon

These cookies are used to collect information about how you interact with this website and allow us to remember you. We use this information in order to improve and customize your browsing experience and for analytics and metrics about our visitors on this website.

If you decline, your information won’t be tracked when you visit this website. A single cookie will be used in your browser to remember your preference not to be tracked.

Talk to us