How to write CSS class in Webpage designing

CSS (cascading style sheets) is for making any webpage more presentable, reader friendly and attractive, there are different ways we can write css code on any webpage, however, in today's competitive online world people expect everything fast, so speed matters a lot!

If your CSS codes slow down the webpage performance, then that will kill the whole purpose of web designing, therefore we need to write in code such way that the page loads faster and user can enjoy browsing your page. Here we talk about some best way of writing css code in any webpage.

CSS Coding Best Practices

Here are some best ways to write CSS Style in Web page designing.
Now you have an idea what is CSS and how to write CSS Syntax!

But before you learn more CSS example we want you to understand some best practices of writing CSS Style Sheet.

There are three ways you can write CSS.

  1. In Line
  2. In File
  3. Using External Reference

Every approach has some advantage and disadvantage, but if you know when to use what, then you will be able to design the best optimized web application.

Now here we show you some example of each approach and explain when to use them.

Write CSS Style in-line

In-line CSS you can write when some small additional changes required, the more you can avoid this practice, is good for web page!
Here is an example.

<div style="padding:10px;">
        
</div>

Write CSS in File

In-File is basically writing css class or id on current html document and uses that class on some element of current document, not really a good practice but sometimes we do, especially when you know that particular class will not be used in any other document! But try to avoid this practice.
this is how you can write css class on same page.

<style>
    .splDiv
    {
        padding:6px;
        border-bottom:solid 1px #cbcbcb;
    }
</style>

Include CSS External Reference

External reference is basically writing a CSS in a different file and adding the file on your webpage, so the page remain clean, also the classes are re-usable in other WebPages
Here is how you can add reference of css file in your webpage

<link href="~/Content/etg_style.css" rel="stylesheet" />

In earlier article we have already explained how to create a .css file

Write responsive CSS Class

Nowadays people browse webpage from different devices like desktop, laptop, tablet, mobile phone, every device has different screen resolution (width height), so as a web designer, our job is to make sure that our web page look good in all devices, and to do that we should consider following points while writing css class.

We should not define width in fixed unit, rather should use percentage, so the element will increase or decrease width according to device screen width. here is an example.

style="width:600px;" // wrong approach
style="width:60%" // right approach

whenever you define width of any html element like div, image etc, make sure you set in percentage.

Try to use CSS media tag wherever possible, you can hide or show certain elements based on screen width of different devices.

@media screen and (max-width: 600px) {
  div.example {
    display: none;
  }
}

There are many ready to use CSS framework for responsive web page designing, one of the most popular CSS framework is Bootstrap CSS.

CSS Style Examples