What is CSS?
CSS (Cascading Style Sheets) is a stylesheet language used to describe the presentation of a document written in HTML. While HTML provides the structure of a webpage, CSS controls its visual appearance, including layout, colors, fonts, and animations.CSS separates content from presentation, allowing you to maintain and update the look of your website without changing the underlying HTML structure.
How CSS Works
CSS works by selecting HTML elements and applying styles to them. The browser reads the CSS rules and renders the HTML elements according to the specified styles.Basic CSS syntax
- Selector: Targets the HTML element(s) to style
- Property: The aspect of the element you want to change
- Value: The specific setting you want to apply
Ways to Include CSS
There are three ways to include CSS in your HTML documents:1
Inline CSS
Applied directly to individual HTML elements using the
style
attribute.Inline styles have the highest specificity but make your HTML cluttered and difficult to maintain. Use sparingly.
2
Internal CSS
Defined within a
<style>
element in the <head>
section of an HTML document.3
External CSS (Recommended)
Stored in a separate .css file and linked to the HTML document using the And in your styles.css file:
<link>
element.External CSS is the best practice for most websites as it promotes separation of concerns, reusability, and easier maintenance.
CSS Selectors
Selectors are patterns used to select and style HTML elements. Here are some common types:Basic Selectors
Element Selector
Selects all elements of a specified type.
Class Selector
Selects elements with a specific class attribute.
ID Selector
Selects a single element with a specific id attribute.
Universal Selector
Selects all elements.
Combinators
Combinators explain the relationship between selectors:CSS Properties
CSS offers hundreds of properties to style elements. Here are some essential categories:Text Styling
Box Model
Every HTML element is represented as a rectangular box with content, padding, border, and margin areas.Colors and Backgrounds
The Cascade and Specificity
The “cascading” in CSS refers to the way styles are applied to elements. When multiple rules target the same element, the browser uses these principles to determine which styles to apply:- Importance: Styles marked with
!important
override other styles - Specificity: More specific selectors override less specific ones
- Source Order: Later styles override earlier ones
Specificity hierarchy (from lowest to highest):
- Element selectors (
p
,div
) - Class selectors (
.class
), attribute selectors ([type="text"]
), pseudo-classes (:hover
) - ID selectors (
#id
) - Inline styles (
style=""
attribute)
Responsive Design with CSS
Responsive design ensures your website looks good on all devices. Key techniques include:Media Queries
Flexible Layouts
CSS Units
CSS provides various units for expressing lengths:Absolute Units
px
(pixels)pt
(points)in
(inches)cm
(centimeters)mm
(millimeters)
Relative Units
%
(percentage of parent)em
(relative to font-size)rem
(relative to root font-size)vw
(viewport width)vh
(viewport height)
For responsive design, prefer relative units like
rem
, em
, and %
over absolute units like px
.Best Practices
- Use external CSS files to separate content from presentation
- Follow a consistent naming convention for classes (e.g., BEM methodology)
- Organize your CSS with comments and logical grouping
- Minimize specificity conflicts by keeping selectors simple
- Use responsive design techniques for multi-device compatibility
- Consider using CSS preprocessors like Sass or Less for larger projects
Next Steps
Now that you understand the basics of CSS, you can:- Learn more about CSS Selectors and their power
- Explore the CSS Box Model in depth
- Study CSS Typography for better text styling
- Master CSS Flexbox and CSS Grid for advanced layouts