Best CSS Shorthand properties to improve your web application

Why should I care shorthand properties? 

CSS is a language for describing the appearance of a web page. Each element in an HTML page can have its location, colors, fonts, and layout customized using CSS.

In this blog post, I’ll give you some pointers on how to optimize your CSS code and your web app’s efficiency.
There are various methods for optimizing a CSS file. When writing CSS code, one thing to keep in mind is to keep the number of lines as low as possible.

The number of code lines is important for several reasons:
👉Improve the readability of your code;

👉improve the loading speed of your web page;

The following is an amazing tip for reducing the number of lines of code:

Whenever possible, use CSS shorthand properties.

In this blog post, I’ll explain what shorthand properties are and how to utilize them to make your CSS code more efficient.

Interested? Continue reading!!! 😊😊

What are shorthand properties?

Shorthand properties allow users to change the values of several CSS properties at the same time.

A number of shorthand properties are supported by CSS. We’ll look at the most popular ones in this blog post.

CSS Background Shorthand

The Background property allows you to set many background properties of an HTML element (for example, a div) in a single CSS line.

Background is a shorthand for:

  • background-color
  • background-image
  • background-position
  • background-size
  • background-repeat
  • background-origin
  • background-clip
  • background-attachment

We can use a single property instead of background-color, background-image, background-position, background-size, background-repeat, background-origin, background-clip, and background-attachment for an HTML element.

Hmmm, confused??? Check the picture 👇

That is the key. We were able to compress eight CSS lines into one using the background shorthand property.

Consider a large web application with dozens of CSS files and thousands of lines in each.

Your web application will help reduce the stress from smaller files, clearer code, and faster loading times as a result of this.

CSS Border Shorthand

Border is the second shorthand property I’d like to show you. To set the border of an HTML element, use the border shorthand.

It’s a shorthand for:

  • border-width
  • border-style
  • border-color

Here is an example:

Three properties at one go. Not so bad!!! 😏 🔥

CSS Font Shorthand

The following font properties are set via font shorthand:

  • font-style
  • font-variant
  • font-weight
  • font-size/line-height
  • font-family

CSS Inset Shorthand

The Inset property controls how an HTML element is displayed. It’s a shorthand for:

  • top
  • right
  • bottom
  • left
Simple and efficient! ☺️ 🌺

CSS Padding Shorthand

Padding is a way to design additional space around an element. It allows you to add space between the element and its border, to be more specific.

We need to set four variables to completely set the padding of an HTML element:

  • padding-top
  • padding-right
  • padding-bottom
  • padding-left

The meaning of these numbers is self-evident:

padding-top is the distance between an element and its border,

padding-right is the distance between an element and its right border.

padding-bottom is the distance between an element and its bottom border.

padding-left is the distance between an element and its left border.
Using the padding shortcut, all of these attributes can be provided in a single declaration.

The syntax is easy to understand:

padding: <padding-top> <padding-right> <padding-bottom> <padding-left>

Here is an example:

CSS Margin Shorthand

The padding and margin properties are similar. The space outside the element’s edges is known as the margin.

You must provide four separate values to set a margin:

The gap between the element’s top border and the other elements is known as margin-top.
The gap between the element’s right border and the other elements is known as the margin-right.
The gap between the element’s bottom border and the other elements is known as margin-bottom.
The gap between the element’s left border and the other elements is known as the margin-left.

The syntax is Given below with example👇 🌹:

margin: <margin-top> <margin-right> <margin-bottom> <margin-left>

Conclusion

We’ve reached the end of our discussion. I’d like to highlight the following suggestion:

Whenever possible, use CSS shorthand properties.

because they aid in the reduction of CSS code lines and the improvement of readability

Because the CSS file is smaller, reducing its size will enhance the loading performance of our web pages. Because search engine algorithms like to reward efficient web pages, this will also increase our search engine ranking.
I hope I’ve convinced you to be careful about CSS code lines.

Also Check:

Happy Learning.. ☺️🔥

About us: Codelivly is a platform designed to help newbie developer to find the proper guide and connect to training from basics to advance .

Leave a Comment