Archives
- Newer posts
- November 2024
- April 2024
- November 2023
- October 2023
- August 2023
- May 2023
- February 2023
- October 2022
- August 2022
- July 2022
- May 2022
- April 2022
- March 2022
- February 2022
- June 2020
- March 2020
- February 2020
- January 2020
- December 2019
- November 2019
- October 2019
- September 2019
- August 2019
- July 2019
- June 2019
- May 2019
- April 2019
- March 2019
- February 2019
- January 2019
- December 2018
- November 2018
- October 2018
- September 2018
- August 2018
- July 2018
- June 2018
- May 2018
- April 2018
- March 2018
- February 2018
- January 2018
- December 2017
- November 2017
- October 2017
- September 2017
- August 2017
- July 2017
- June 2017
- May 2017
- April 2017
- March 2017
- February 2017
- January 2017
- August 2016
- June 2016
- April 2016
- March 2016
- February 2016
- January 2016
- July 2015
- June 2015
- Older posts
The Smart Way to Write CSS
CSS is one of the important skills every web designer must have.
Understanding CSS is the initial challenge as CSS is not easy. One needs to understand how to use CSS selectors, properties, pseudo-code, etc. While you learn the basic way to write down a CSS code, it is very important to understand the way to organize it. While basic coding practices focus on keeping the code well organized and nicely indented, a couple of changes in the way of writing the CSS code file can have a huge impact on the output.
Here are a few tips to write quick, clean and non-redundant CSS code:
Writing CSS Reset
Always use a CSS reset file at the start of the code. If you don’t define any element style, then the browser will apply its own CSS style which might create a mismatch in the design as browsers have their own styles. Use of CSS reset will override browser-default-styles, allowing you to write a clean code base. If you don’t know how to write a reset file, then you can also download it, as it is available on the internet.
Organizing styles in blocks
Organizing code into blocks is important as it will not only help you but also your colleagues to understand the flow of your code, instead of spending hours understanding which code is placed where. So, group your code properly, divide it into blocks or sections. There are many ways to organize your code into blocks. The first is where you can create blocks of elements of the design, for example, blocks of images, buttons, links, etc. And the second would be organizing them according to the parts or sections of the design, for example, header, menu, main body, sidebar, footer, etc. Organizing also helps in writing a non-redundant code. Sometimes we might end up repeating some of the CSS properties, so organizing your code into blocks will prevent redundancy.
Organizing CSS with comments
Assume one day you are told to work on an old project of yours, and it’s been a long time you haven’t touched that code and now you don’t remember which style is placed where since unfamiliarity will result in you not remembering the locations of specific styles. CSS files are quite long, having many lines. So it’s always important to comment your code properly, you may save a lot of time that you just would have spent on the code searching for a style. And it will also help your colleagues to understand your code with much ease.
Use CSS shorthand
Using shorthand, we can write a multiple line code on a single line. The code looks neater, cleaner and short if we use shorthand. For example, for use of a border, you can set the border-width, border-style and border-color property of an element by using just one ‘border’ property. It also makes writing easy. However, if you just have to change the colour of the border, then using longhand will be better, instead of setting all 3 properties.
Quick CSS classes
If you use a property over and over in a code, then writing a separate class for that specific property would be beneficial. So you can use that class in your HTML file whenever you would want that style property to be applied instead of defining it again and again.
These are not the only tips to write CSS smartly, there are many more. These are a few basic tips I personally follow to write better CSS.