W3Newbie
  • HTML Tutorial
  • CSS Tutorial
  • Web Dev
    • VS Code Tutorial
    • Command Line Tutorial
    • Git Tutorial
    • Github Tutorial
    • Sass Tutorial
  • Courses
  • Resources
  • Tutorials
  • Click to open the search input field Click to open the search input field Search
  • Menu Menu

CSS Syntax

CSS syntax follows a specific structure to define how elements on a web page should be styled. The syntax is the foundation of web page styling, resembles a recipe combining selectors and declarations or property & value pairs.

CSS Syntax Key Components:

1. CSS Selector:

  • Specifies which HTML elements you want to style.
  • Examples: h1, p, .my-class, #my-id

2. CSS Declaration Block:

  • Encloses one or more declarations within curly braces {}.
  • Contains the specific styles to be applied to the selected elements.

3. CSS Declaration:

  • Consists of a CSS property followed by a colon : and a value.
  • Sets a specific style aspect for the selected elements.

4. CSS Property:

  • The name of the style characteristic you want to modify.
  • Examples: color, font-size, margin, border, background-color

5. CSS Value:

  • Assigns a specific value to the property, determining the visual effect.
  • Examples: blue, 16px, 10px, solid, #f0f0f0

General CSS Syntax Structure:

CSS:

selector {
  property-name: value;
  property-name: value;
  /* Additional declarations (property: value:) can be added here. */
}

CSS Syntax Example:

To style a paragraph HTML tag with CSS, craft a rule using the selector “p” as a target, followed by a declaration pairing a property (like “color” or “font-size”) with its desired value (e.g., “red” or “18px”), all embraced within curly braces.

CSS:

p {
  color: red;  /* Sets text color to red */
  font-size: 18px; /* Sets font size to 18 pixels */
}

Additional Notes:

  • Comments: Use /* */ to add comments within CSS code for clarity and documentation.
  • Semicolons: Each declaration must end with a semicolon “;“, although some browsers might tolerate missing semicolons in isolated cases, it’s considered best practice to consistently use them.
  • Multiple Selectors: Separate multiple selectors with commas to apply the same styles to different elements.
  • Specificity: The order of selectors and how specific they are determine which styles take precedence if multiple rules apply to the same element.
  • Indentation: Not required for browsers to interpret CSS, but it’s essential for code readability and maintainability.

Now that you understand the CSS syntax, with the combination of selectors, declarations, properties, and values, get ready to style and watch your web page designs blossom from bare basics to masterpieces!

< Previous
Next Lesson >
  • CSS Tutorial
  • CSS Introduction
  • CSS Syntax
  • CSS Style Sheets
  • CSS Comments
  • CSS Fonts
  • CSS Sizing
  • CSS Colors
  • CSS Backgrounds
  • CSS Borders
  • CSS Outlines
  • CSS Margin
  • CSS Padding
  • CSS Box Model
  • CSS Floats
  • CSS Alignment
  • CSS Positioning
  • CSS Overflow
  • CSS Z-Index
  • CSS Opacity
  • CSS Pseudo-Classes
  • CSS Pseudo-Elements
  • CSS Buttons
  • CSS Icons
  • CSS Media Queries
  • CSS Responsive Web Design
  • CSS Navigations
  • CSS Animations
  • CSS Flexbox
  • CSS Grid
  • CSS Responsive Typography
  • CSS Mobile-First Responsive
  • CSS Fluid Layouts
  • CSS Variables

The Newbie Template Bundle

The 12 Website Bundle Pack

Subscribe on YouTube

Web Development Courses

The HTML Email Mastery Course - Build Responsive HTML Email Templates.

Latest Tutorial Posts

  • The HTML Email Mastery Course - Build Responsive HTML Email Templates.w3newbie.com
    HTML Email Mastery Course CouponOctober 12, 2020 - 8:10 pm
  • Create A 5 Page Website With PHP Includes, HTML5, CSS3 & Bootstrap 4w3newbie.com
    Create A 5 Page Website With PHP Includes, HTML5, CSS3 & Bootstrap 4April 22, 2019 - 1:48 pm
© w3newbie.com, 2026. Terms of Use. Privacy Policy.
Scroll to top Scroll to top Scroll to top