CSS provides styling rules for HTML elements, defining colors, layouts, spacing, fonts, and more to control how a web page looks. CSS is organized into rulesets that select specific HTML elements and apply properties and values to define their appearance.
CSS Syntax
CSS code consists of selectors, properties, and values:
selector {
property: value;
}For example:
h1 {
color: blue;
font-size: 24px;
}selector: Targets the element (e.g.,h1).property: The style attribute (e.g.,color,font-size).value: Specifies the style (e.g.,blue,24px).
Ways to Apply CSS
There are three primary ways to add CSS to a webpage:
1. Inline CSS:
- Style is applied directly within HTML elements using the
styleattribute. - This is discouraged for maintainability and separation of content and style.
<p style="color: red; font-size: 16px;">This is inline styled text.</p>2. Internal CSS:
- Placed inside a
<style>tag within the<head>of the HTML document, internal CSS applies styles to that specific HTML file.
<head>
<style>
p {
color: blue;
font-size: 18px;
}
</style>
</head>3. External CSS:
- Stored in an external
.cssfile, this is the preferred method, especially for large or multi-page websites, as it keeps the styling organized and reusable across multiple pages.
<link rel="stylesheet" href="styles.css">CSS Selectors
Selectors in CSS allow you to target specific HTML elements. Let’s look at some of the most common types.
1. Element Selector:
- Targets all instances of a specific HTML tag.
p {
color: gray;
}2. Class Selector:
- Targets elements with a specific
classattribute. Classes are reusable, making them useful for styling groups of elements.
.highlight {
background-color: yellow;
} <p class="highlight">This text is highlighted.</p>3. ID Selector:
- Targets an element with a unique
idattribute. IDs should be unique on each page, meaning they are usually only applied to a single element.
#header {
font-weight: bold;
} <h1 id="header">Main Header</h1>4. Attribute Selector:
- Targets elements with a specific attribute, useful for styling elements with attributes like
type,href, ortarget.
input[type="text"] {
border: 1px solid gray;
}5. Combinators:
- Used to target elements based on their relationship to other elements.
- Descendant Selector (
div p): Selects<p>elements within<div>elements. - Child Selector (
ul > li): Selects direct child<li>elements inside<ul>. - Adjacent Sibling Selector (
h1 + p): Selects the first<p>immediately following an<h1>.
- Descendant Selector (
Common CSS Properties
1. Text Styling:
color: Text color.font-size: Size of the text (can use units likepx,em, or%).font-family: Specifies the typeface.font-weight: Sets thickness, such asnormal,bold, or numerical values (100–900).text-align: Aligns text within its container (e.g.,left,center,right).
h1 {
color: darkblue;
font-size: 28px;
text-align: center;
}2. Color and Background:
background-color: Sets the background color.background-image: Adds a background image.background-size: Controls the background image’s size, commonly set tocoverorcontain.opacity: Controls transparency (values between0and1).
body {
background-color: #f0f0f0;
background-image: url("background.jpg");
background-size: cover;
}3. Box Model Properties: CSS uses a box model for each element, consisting of margin, border, padding, and content.
margin: Space outside the border, creating space between elements.border: Defines the element’s border, includingwidth,style, andcolor.padding: Space between content and border, within the element.widthandheight: Set the element’s dimensions.
.box {
width: 200px;
height: 150px;
padding: 10px;
border: 2px solid black;
margin: 20px;
}4. Layout Properties: CSS controls layout through various display and positioning methods.
display: Defines how an element is displayed.block: Element takes up full width.inline: Element takes up only as much space as needed.inline-block: Combines inline and block properties.flex: Makes an element a flex container.
position: Controls an element’s position relative to its container.static,relative,absolute,fixed,sticky.
.container {
display: flex;
justify-content: space-around;
}5. Flexbox and Grid: Modern layout systems like Flexbox and CSS Grid simplify complex layouts.
- Flexbox: Great for one-dimensional layouts.
.flex-container {
display: flex;
flex-direction: row;
justify-content: space-between;
}- CSS Grid: Best for two-dimensional layouts.
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
}Pseudo-classes and Pseudo-elements
CSS offers pseudo-classes and pseudo-elements to target specific states or parts of elements.
1. Pseudo-classes:
- Target states of elements, like when a link is hovered, visited, or active.
a:hover {
color: green;
}2. Pseudo-elements:
- Target and style a part of an element, like the first letter or line.
p::first-line {
font-weight: bold;
}
p::first-letter {
font-size: 200%;
}CSS Units and Measurements
CSS uses different units to define dimensions, positioning, and spacing. Some common units are:
- Absolute units: Pixels (
px), centimeters (cm) – fixed sizes. - Relative units:
em,rem– scale based on parent or root element’s size. - Percentages (
%): Relative to the containing element.
For example:
body {
font-size: 16px;
}
p {
font-size: 1.5em; /* 1.5 times the body font-size (16px) */
}Responsive Design with CSS
Responsive design allows web pages to adjust layout and styling based on the screen size. Common techniques include:
1. Media Queries:
- Allow different CSS rules based on screen size, orientation, and resolution.
@media (max-width: 750px) {
.container {
flex-direction: column;
}
}2. Responsive Units:
- Use relative units (
%,vw,vh,em) instead of fixed units likepxto create flexible designs.
.responsive-box {
width: 50vw; /* 50% of the viewport width */
}Example mini start website with CSS
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Styled Page</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
background-color: #dbd3d3;
}
.header {
background-color: #333;
color: #fff;
padding: 20px;
text-align: center;
}
.nav-link {
color: white;
margin: 0 10px;
}
.nav-link:hover {
color: #c800ff;
}
.main-content {
padding: 20px;
}
.section {
margin-top: 20px;
}
.paragraph {
color: #555;
font-size: 1.1em;
line-height: 1.6;
}
</style>
</head>
<body>
<header class="header">
<h1>Welcome to My Page</h1>
<nav>
<a href="#home" class="nav-link">Home</a>
<a href="#about" class="nav-link">About</a>
</nav>
</header>
<main class="main-content">
<section class="section">
<h2>About Us</h2>
<p class="paragraph">We can create beautiful websites with CSS.</p>
</section>
</main>
</body>
</html>Output:
Welcome to My Page
About Us
We create beautiful web pages with CSS!
