Skip to main content

CSS Basic Syntax: Complete Guide to Selectors, Properties, and Styling

 CSS Basic Syntax: A Complete Guide

CSS (Cascading Style Sheets) is a fundamental technology for designing and styling web pages. It allows developers to control the appearance of HTML elements, making web pages visually appealing and user-friendly. This guide will provide an in-depth understanding of CSS basic syntax, including its structure, rules, selectors, properties, and more.




1. What is CSS?

CSS is a stylesheet language that controls the look and feel of HTML elements. It helps separate content from design, enabling developers to create responsive, attractive, and well-structured web pages.

Why Use CSS?

  • Enhances web page presentation

  • Reduces code redundancy

  • Improves page load speed

  • Enables responsive design

  • Simplifies maintenance and updates


2. Basic Syntax of CSS

CSS syntax consists of selectorsproperties, and values.

General Structure:

css
selector { property: value; }
  • Selector: Targets the HTML element(s) to style

  • Property: Defines the aspect to style (e.g., color, font-size)

  • Value: Specifies how the property should be applied

Example:

css
p { color: blue; font-size: 16px; }

This styles all <p> elements with blue text and a font size of 16px.


3. CSS Selectors

Selectors define which HTML elements should be styled.

Common Selectors:

  1. Element Selector
    Targets all instances of an HTML element.

    css
    h1 { color: red; }

    All <h1> elements will be red.

  2. Class Selector (.)
    Targets elements with a specific class attribute.

    css
    .highlight { background-color: yellow; }

    Applies a yellow background to elements with class="highlight".

  3. ID Selector (#)
    Targets a single element with a specific ID.

    css
    #main-title { font-size: 24px; }

    Only the element with id="main-title" will have a font size of 24px.

  4. Group Selector (,)
    Styles multiple elements at once.

    css
    h1, h2, h3 { font-family: Arial, sans-serif; }

    All <h1><h2>, and <h3> elements will use the Arial font.

  5. Universal Selector (*)
    Styles all elements on a page.

    css
    * { margin: 0; padding: 0; }

    Resets default margins and paddings for all elements.

  6. Descendant Selector ( )
    Targets elements inside a specific parent element.

    css
    div p { color: gray; }

    All <p> inside <div> will have gray text.

  7. Child Selector (>)
    Targets direct child elements.

    css
    div > p { color: blue; }

    Only <p> elements that are direct children of <div> will be blue.

  8. Adjacent Sibling Selector (+)
    Styles the next immediate sibling element.

    css
    h1 + p { font-style: italic; }

    The first <p> after <h1> will be italicized.


4. CSS Properties and Values

CSS properties define the appearance of elements. Here are some common ones:

Text Properties

  • color: Sets text color

  • font-size: Defines text size

  • font-family: Specifies the font

  • font-weight: Sets boldness

  • text-align: Aligns text (left, right, center, justify)

  • text-decoration: Underline, overline, none, etc.

  • line-height: Controls space between lines

Example:

css
p { color: darkblue; font-size: 18px; text-align: center; }

Background Properties

  • background-color: Sets background color

  • background-image: Defines a background image

  • background-size: Adjusts image size

  • background-repeat: Specifies repeat behavior

Example:

css
body { background-color: #f0f0f0; }

Box Model Properties

  • width: Defines element width

  • height: Defines element height

  • margin: Creates space around elements

  • padding: Adds space inside elements

  • border: Sets border style

Example:

css
.box { width: 200px; height: 100px; padding: 10px; margin: 20px; border: 2px solid black; }

Positioning and Layout

  • display: Defines element display (block, inline, flex, grid)

  • position: Controls element placement (static, relative, absolute, fixed)

  • topleftrightbottom: Adjusts positioning

  • z-index: Defines stack order

Example:

css
.fixed-header { position: fixed; top: 0; width: 100%; background-color: black; color: white; }

Flexbox Properties

  • display: flex; Enables flexbox

  • justify-content: Aligns items horizontally

  • align-items: Aligns items vertically

Example:

css
.container { display: flex; justify-content: center; align-items: center; }

5. CSS Units

CSS supports different units for defining sizes.

Absolute Units

  • px (pixels)

  • cm (centimeters)

  • mm (millimeters)

  • in (inches)

Relative Units

  • % (percentage)

  • em (relative to font-size)

  • rem (relative to root font-size)

  • vhvw (viewport height/width)

Example:

css
h1 { font-size: 2em; /* 2 times the parent font-size */ }

6. CSS Comments

Comments help organize and document CSS code.

css
/* This is a comment */ p { color: green; /* This makes text green */ }

7. CSS Application Methods

There are three ways to apply CSS:

1. Inline CSS (Applied directly to elements)

html
<p style="color: red;">This is red text.</p>

2. Internal CSS (Within <style> tags in HTML)

html
<style> p { color: blue; } </style>

3. External CSS (Linked in an external file)

html
<link rel="stylesheet" href="styles.css">

Example styles.css:

css
p { color: green; }

8. CSS Specificity and Inheritance

CSS follows a priority system:

  1. Inline styles (style="") – Highest priority

  2. ID selectors (#id)

  3. Class, attribute, and pseudo-class selectors (.class)

  4. Element selectors (h1, p, div)

  5. Universal selector (*) – Lowest priority

Example of specificity:

css
#unique { color: red; } .classname { color: blue; } p { color: green; }

If <p id="unique" class="classname">Text</p>, the text will be red due to higher specificity.


Conclusion

CSS is a powerful language that enables web designers to create visually appealing websites. Understanding its syntax, selectors, properties, and rules is essential for building well-structured and responsive designs.