CSS best practices

Capítulo 33

Estimated reading time: 3 minutes

+ Exercise
Audio Icon

Listen in audio

0:00 / 0:00

CSS Best Practices

CSS is a styling language that allows web developers to create attractive and functional designs for their websites. However, just like any other programming language, there are best practices that developers should follow to ensure their code is efficient, maintainable, and scalable. In this article, we'll discuss some of the best practices in CSS.

1. Use a CSS methodology

A CSS methodology is a set of rules and practices that help developers write organized and scalable CSS code. There are several popular CSS methodologies such as BEM, SMACSS and OOCSS. By following a CSS methodology, developers can ensure their code is easy to understand, maintain, and update.

2. Use descriptive class names

CSS class names should be descriptive and meaningful. This helps developers understand the purpose of each class and quickly locate relevant style rules. In addition, descriptive class names also help with accessibility, as people using screen readers can better understand the structure of the site.

3. Avoid inline styles

Inline styles are those that are defined directly in HTML elements using the "style" attribute. While they can be useful in some cases, such as for specific single-page styles, inline styles make code difficult to maintain and update. It's best to use external or internal stylesheets to define the styles.

4. Use specific selectors

CSS selectors are used to apply styles to specific HTML elements. However, it's important to use specific selectors rather than generic selectors like "div" or "p". This helps to avoid style clashes and ensures styles are only applied to the desired elements.

Continue in our app.
  • Listen to the audio with the screen off.
  • Earn a certificate upon completion.
  • Over 5000 courses for you to explore!
Or continue reading below...
Download App

Download the app

5. Use CSS shorthand

The CSS shorthand is a way to write multiple style properties in a single line of code. For example, instead of writing:

    
        padding-top: 10px;
        padding-right: 20px;
        padding-bottom: 10px;
        padding-left: 20px;
    

You can use CSS shorthand:

    
        padding: 10px 20px;
    

This helps reduce the amount of code and makes it easier to read and maintain.

6. Use CSS Variables

CSS variables allow developers to define style values ​​that can be reused throughout code. This helps maintain consistency in styles and makes it easier to update values ​​across the site. For example, you can define a variable for the website accent color and use it throughout your code:

    
        :root {
            --highlight: #FFA500;
        }
        
        .button {
            background-color: var(--highlight);
        }
        
        .title {
            color: var(--highlight);
        }
    

Using CSS variables, you can easily update the accent color throughout your site by just changing the variable value.

Conclusion

Following CSS best practices is essential to ensure your code is organized, scalable, and maintainable. By using a CSS methodology, descriptive class names, avoiding inline styles, using specific selectors, CSS shorthand and CSS variables, you can write clean and efficient CSS codes that help you create attractive and functional designs for your websites.

Now answer the exercise about the content:

_What is one of the CSS best practices mentioned in the text?

You are right! Congratulations, now go to the next page

You missed! Try again.

One of the best practices mentioned is to use a CSS methodology to write organized and scalable code. It helps in making the code easy to understand, maintain, and update. Examples include BEM, SMACSS, and OOCSS.

Next chapter

CSS code organization

Arrow Right Icon
Free Ebook cover Complete CSS course
94%

Complete CSS course

4.5

(2)

35 pages

Download the app to earn free Certification and listen to the courses in the background, even with the screen off.