w1coding.comAll tutorials

CSS Basics

CSS lessons
  1. Environment Setup
  2. Introduction
  3. Tdisplay
  4. Basics
  5. Editors
  6. Syntax
  7. Colors
  8. Background
  9. Borders
  10. Margins
  11. Padding
  12. Width And Height
  13. Outline
  14. Texts
  15. Fonts
  16. Icons
  17. Links
  18. Lists
  19. Display
  20. Tables
  21. Max And With
  22. Positon
  23. Z Index
  24. Dropdown
  25. Overflow
  26. Float
  27. Navigation
  28. Image Sprites

Basics of CSS

What is CSS?

CSS (Cascading Style Sheets) is used to control the presentation of a web page. It allows you to separate the style (visual layout) from the content (HTML structure), making it easier to manage, update, and customize your site.

CSS Syntax

A CSS rule consists of a selector and a declaration block. The selector indicates the HTML element you want to style, and the declaration block contains one or more declarations, each with a property and value.

/* Example of CSS Syntax */ p { color: blue; font-size: 16px; }

Types of CSS

There are three main ways to apply CSS to a web page:

  • Inline CSS: Style is applied directly to an HTML element using the style attribute.
  • Internal CSS: CSS is written within a <style> tag inside the HTML <head> section.
  • External CSS: Styles are defined in an external file with a .css extension and linked to the HTML file.

CSS Selectors

Selectors allow you to target specific HTML elements to apply styles. Here are some basic types:

  • Element Selector: Selects all elements of a specific type (e.g., p, h1).
  • Class Selector: Selects elements with a specific class (e.g., .example).
  • ID Selector: Selects an element with a specific ID (e.g., #header).

Box Model

The CSS box model describes the layout and spacing of elements. Each element has content, padding, border, and margin.

  • Content: The actual text or image within the box.
  • Padding: Space between the content and the border.
  • Border: A line surrounding the padding and content.
  • Margin: Space outside the border, separating the element from other elements.

Example: Styling a Webpage

Here's an example of how CSS styles can transform the appearance of a webpage:

/* Basic styling example */ body { font-family: Arial, sans-serif; background-color: #f4f4f4; } h1 { color: #333; text-align: center; } p { color: #555; line-height: 1.6; }