DEV Community

Dhruv Joshi
Dhruv Joshi

Posted on • Edited on

đŸ”„(Complete) Beginner's CSS Guide: Complete with Samples and Easy-to-Follow Tips

CSS (Cascading Style Sheets) is a fundamental technology that is used to add style and layout to web pages. It allows web developers to control the look and feel of their website, including the colors, fonts, spacing, and layout. In this guide, I will cover everything you need to know about CSS, from the basics to advanced techniques. I will also provide samples to help you get started with your own CSS code.

Basic CSS Syntax

The syntax of CSS is relatively simple. CSS uses selectors to identify which HTML elements the styles should be applied to, and then specifies the styles themselves. Here is an example of the basic syntax of CSS:

selector {
  property: value;
  property: value;
  property: value;
}

Enter fullscreen mode Exit fullscreen mode

In this example, ''selector'' identifies the HTML element that the styles should be applied to. The ''property'' is the attribute of the HTML element that you want to style, and the ''value'' is the specific styling you want to apply to that attribute.

CSS Selectors

Selectors are used to identify which HTML elements the styles should be applied to. There are several types of selectors in CSS:

  • Element Selector

The element selector selects all instances of a particular HTML element. For example, to select all paragraphs on a page, you would use the following code:

p {
  /* styles go here */
}

Enter fullscreen mode Exit fullscreen mode
  • Class Selector Class selectors are used to select elements that have a particular class attribute. Classes are useful when you want to apply the same styling to multiple elements on a page. To use a class selector, add a . before the class name. For example, to select all elements with the class "example", you would use the following code:
.example {
  /* styles go here */
}

Enter fullscreen mode Exit fullscreen mode
  • ID Selector ID selectors are used to select a single element with a specific ID attribute. IDs are useful when you want to apply unique styling to a particular element. To use an ID selector, add a # before the ID name. For example, to select the element with the ID "header", you would use the following code:
#header {
  /* styles go here */
}

Enter fullscreen mode Exit fullscreen mode

CSS Properties

CSS properties define the specific styling that should be applied to an HTML element. There are hundreds of CSS properties available, but i will cover some of the most commonly used properties.

  • Background Color The background-color property is used to set the background color of an HTML element. Here is an example of how to set the background color of a div element to red:
div {
  background-color: red;
}

Enter fullscreen mode Exit fullscreen mode
  • Font Family The font-family property is used to set the font family for an HTML element. The font family is a collection of font styles that can be applied to text. Here is an example of how to set the font family for all h1 elements to Arial:
h1 {
  font-family: Arial, sans-serif;
}

Enter fullscreen mode Exit fullscreen mode
  • Font Size The font-size property is used to set the size of the text in an HTML element. Here is an example of how to set the font size for all p elements to 16 pixels:
p {
  font-size: 16px;
}

Enter fullscreen mode Exit fullscreen mode
  • Margin The margin property is used to set the margin (space) around an HTML element. You can set different margins for each side of the element using the following syntax:
div {
  margin-top: 10px;
  margin-right: 20px;
  margin-bottom: 30px;
  margin-left: 40px;
}

Enter fullscreen mode Exit fullscreen mode
  • Padding The padding property is used to set the padding (space) inside an HTML element. You can set different paddings for each side of the element using the following syntax:
div {
  padding-top: 10px;
  padding-right: 20px;
  padding-bottom: 30px;
  padding-left: 40px;
}

Enter fullscreen mode Exit fullscreen mode
  • Border The border property is used to create a border around an HTML element. You can specify the style, width, and color of the border using the following syntax:
div {
  border-style: solid;
  border-width: 2px;
  border-color: black;
}

Enter fullscreen mode Exit fullscreen mode

CSS Units

CSS uses different units to define measurements such as size, spacing, and positioning. Here are some of the most commonly used CSS units:

  • Pixels (px)
    Pixels are the most commonly used unit in CSS. One pixel is equal to one dot on the computer screen. Pixels are a fixed unit, which means that their size will remain the same regardless of the size of the screen or the device being used.

  • Percentages (%)
    Percentages are a relative unit that is based on the size of the parent element. For example, if the parent element is 200 pixels wide, and you set the width of the child element to 50%, the child element will be 100 pixels wide.

  • Em (em)
    The em unit is also a relative unit that is based on the font size of the parent element. For example, if the font size of the parent element is 16 pixels, and you set the font size of the child element to 1.5 em, the font size of the child element will be 24 pixels (1.5 x 16).

  • Rem (rem)
    The rem unit is similar to the em unit, but it is based on the font size of the root element (usually the element). This means that the rem unit is not affected by the font size of the parent element.

CSS Comments

CSS comments are used to add notes to your code that will not be displayed on the website. Comments are useful for reminding yourself of why you made a particular design decision, or for leaving notes for other developers who may be working on the same project. Here is an example of how to add a comment to your CSS code:

/* This is a CSS comment */

Enter fullscreen mode Exit fullscreen mode

Conclusion

In this guide, we have covered the basics of CSS syntax, selectors, properties, units, and comments. CSS is a powerful tool for web developers, and with a little practice, you can create stunning websites that look and feel exactly the way you want them to. Remember to experiment with different styles and techniques, and don't be afraid to ask for help if you get stuck. Happy coding!

Top comments (0)