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 selectors, properties, and values.
General Structure:
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:
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:
Element Selector
Targets all instances of an HTML element.All
<h1>
elements will be red.Class Selector (
.
)
Targets elements with a specific class attribute.Applies a yellow background to elements with
class="highlight"
.ID Selector (
#
)
Targets a single element with a specific ID.Only the element with
id="main-title"
will have a font size of 24px.Group Selector (
,
)
Styles multiple elements at once.All
<h1>
,<h2>
, and<h3>
elements will use the Arial font.Universal Selector (
*
)
Styles all elements on a page.Resets default margins and paddings for all elements.
Descendant Selector (
Targets elements inside a specific parent element.All
<p>
inside<div>
will have gray text.Child Selector (
>
)
Targets direct child elements.Only
<p>
elements that are direct children of<div>
will be blue.Adjacent Sibling Selector (
+
)
Styles the next immediate sibling element.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 colorfont-size
: Defines text sizefont-family
: Specifies the fontfont-weight
: Sets boldnesstext-align
: Aligns text (left, right, center, justify)text-decoration
: Underline, overline, none, etc.line-height
: Controls space between lines
Example:
Background Properties
background-color
: Sets background colorbackground-image
: Defines a background imagebackground-size
: Adjusts image sizebackground-repeat
: Specifies repeat behavior
Example:
Box Model Properties
width
: Defines element widthheight
: Defines element heightmargin
: Creates space around elementspadding
: Adds space inside elementsborder
: Sets border style
Example:
Positioning and Layout
display
: Defines element display (block, inline, flex, grid)position
: Controls element placement (static, relative, absolute, fixed)top
,left
,right
,bottom
: Adjusts positioningz-index
: Defines stack order
Example:
Flexbox Properties
display: flex;
Enables flexboxjustify-content
: Aligns items horizontallyalign-items
: Aligns items vertically
Example:
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)vh
,vw
(viewport height/width)
Example:
6. CSS Comments
Comments help organize and document CSS code.
7. CSS Application Methods
There are three ways to apply CSS:
1. Inline CSS (Applied directly to elements)
2. Internal CSS (Within <style>
tags in HTML)
3. External CSS (Linked in an external file)
Example styles.css
:
8. CSS Specificity and Inheritance
CSS follows a priority system:
Inline styles (
style=""
) – Highest priorityID selectors (
#id
)Class, attribute, and pseudo-class selectors (
.class
)Element selectors (
h1, p, div
)Universal selector (
*
) – Lowest priority
Example of specificity:
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.