CSS (Cascading Style Sheets) is a stylesheet language used to describe the presentation of a document written in HTML or XML (including XML dialects such as SVG, MathML). CSS describes how elements should be rendered on screen, on paper, in speech, or on other media.
It separates the content (HTML) from its visual presentation, making web development more efficient and manageable.
A CSS rule-set consists of a selector and a declaration block.
selector {
property: value; /* Declaration 1 */
property: value; /* Declaration 2 */
}
h1 { /* h1 is the selector */
color: darkblue; /* color is property, darkblue is value */
font-size: 36px; /* font-size is property, 36px is value */
text-align: center;
}
.intro-text { /* .intro-text is the selector (class) */
background-color: lightgrey;
padding: 15px;
border-radius: 8px;
}
Visual Example (Conceptual): An `h1` tag would appear dark blue, large, and centered. Any element with `class="intro-text"` would have a light grey background with padding and rounded corners.
There are three main ways to integrate CSS into your HTML document:
Applied directly within an HTML tag using the `style` attribute. Useful for single, unique style applications, but generally discouraged for larger projects due to poor maintainability and separation of concerns.
<h3 style="color: purple; font-style: italic;">This heading is styled inline.</h3>
<p style="background-color: yellow; padding: 5px;">This paragraph has an inline background.</p>
Visual Example:
This paragraph has an inline background.
Placed within a `<style>` tag in the `<head>` section of the HTML document. Good for single HTML pages or when styles are specific to that page and won't be reused elsewhere.
<head>
<style>
.internal-box {
border: 2px solid green;
padding: 10px;
background-color: lightgreen;
}
.internal-text {
text-decoration: underline;
color: darkgreen;
}
</style>
</head>
<body>
<div class="internal-box">
<p class="internal-text">This content is styled internally.</p>
</div>
</body>
Visual Example:
This content is styled internally.
The most common and recommended method for styling. Styles are written in a separate `.css` file and linked to the HTML document using the `<link>` tag in the `<head>` section. This promotes reusability, maintainability, and cleaner HTML.
<head>
<link rel="stylesheet" href="styles.css"> <!-- Link to your external CSS file -->
</head>
<body>
<h2 class="external-styled-heading">This heading is from an external stylesheet.</h2>
<p>All content will be influenced by 'styles.css'.</p>
</body>
And in your `styles.css` file (which would be in the same directory or specified path):
/* styles.css */
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.external-styled-heading {
color: #4a5568;
background-color: #edf2f7;
padding: 10px;
border-left: 5px solid #667eea;
}
Visual Example:
All content will be influenced by 'styles.css'.
Selectors are used to "find" (or select) HTML elements based on their name, id, class, attributes, etc., to apply styles.
p {
text-indent: 20px;
}
Visual Impact: Every paragraph (`<p>`) on the page will have its first line indented by 20 pixels.
This is a paragraph. It should be indented.
Another paragraph, also indented.
#my-header {
background-color: lightblue;
color: darkblue;
}
Visual Impact: Only the single element with `id="my-header"` will have a light blue background and dark blue text.
This paragraph is not affected.
.highlight {
background-color: yellow;
font-weight: bold;
}
Visual Impact: Any element with `class="highlight"` will have a yellow background and bold text.
This is normal text.
This text is highlighted by a class.h1, h2, h3 {
text-decoration: underline;
}
Visual Impact: All `h1`, `h2`, and `h3` headings will be underlined.
This paragraph is italicized.
This span is also italicized.div p { /* Selects all p elements inside any div */
margin-bottom: 5px;
color: grey;
}
Visual Impact: Only paragraphs nested inside a `div` will have a grey color and smaller bottom margin.
Paragraph inside a div.
Span inside a div.Paragraph outside a div.
ul > li { /* Selects all li elements that are direct children of a ul */
border-left: 3px solid orange;
padding-left: 5px;
}
Visual Impact: Only `<li>` elements immediately inside a `<ul>` will get an orange left border.
h2 + p { /* Selects a p element immediately after an h2 */
font-size: 1.2em;
color: darkgreen;
}
Visual Impact: A paragraph directly following an `<h2>` will appear larger and dark green.
This paragraph is immediately after h2.
This paragraph is not.
h2 ~ p { /* Selects all p elements that are siblings of an h2 (after it) */
background-color: lightcyan;
}
Visual Impact: All paragraphs that come after an `<h2>` (at the same level) will have a light cyan background.
This is a sibling paragraph.
This is also a sibling paragraph.
a[target="_blank"] { /* Selects links that open in a new tab */
color: orange;
}
input[type="text"] { /* Selects all text input fields */
border: 1px solid blue;
}
Visual Impact: Links opening in a new tab will be orange. Text input fields will have a blue border.
a:hover { /* Styles a link when the mouse pointer is over it */
color: red;
text-decoration: underline;
}
input:focus { /* Styles an input field when it's focused */
border-color: blue;
box-shadow: 0 0 5px rgba(0, 0, 255, 0.5);
}
p:first-child { /* Styles the first p element among its siblings */
font-weight: bold;
}
Visual Impact: When you hover over a link, its color changes to red and it gets underlined. When you click/tab into an input field, its border changes color and a shadow appears. The very first paragraph among its siblings will be bold.
p::first-line { /* Styles the first line of a paragraph */
font-weight: bold;
color: darkblue;
}
p::before { /* Inserts content before the content of an element */
content: "READ THIS: ";
color: red;
font-weight: bold;
}
Visual Impact: The first line of a paragraph will be bold and dark blue. "READ THIS: " will appear before every paragraph's content.
This is the first line of a paragraph. The rest of the paragraph is normal.
The CSS Box Model describes how HTML elements are rendered as boxes. Every element is a rectangular box, and this box is made up of: content, padding, border, and margin.
.box {
width: 200px;
height: 100px;
padding: 20px; /* Space inside */
border: 5px solid blue; /* Border around padding */
margin: 30px; /* Space outside */
background-color: lightcyan;
box-sizing: border-box; /* Crucial for consistent sizing */
}
Visual Example:
This text is below the box, affected by its margin.
`box-sizing` Property:
The `display` property is one of the most important CSS properties for controlling the layout of elements.
Starts on a new line and takes up the full available width. `width`, `height`, `padding`, `margin` apply.
.block-element {
display: block;
background-color: #ffcc99;
padding: 10px;
margin-bottom: 5px;
width: 150px;
}
Visual Example:
Does not start on a new line and only takes up as much width as necessary. `width` and `height` properties do NOT apply, only horizontal `padding` and `margin` apply (vertical padding/margin might not affect layout).
.inline-element {
display: inline;
background-color: #99ff99;
padding: 5px;
margin-right: 5px;
}
Visual Example:
Behaves like an inline element (doesn't force a new line) but accepts `width` and `height` properties like a block element.
.inline-block-element {
display: inline-block;
background-color: #ccccff;
padding: 10px;
margin-right: 10px;
width: 80px;
height: 40px;
}
Visual Example:
.hidden-element {
display: none; /* Element will not be rendered at all */
}
Visual Impact: The element will disappear from the page, and take up no space. It's as if it was never in the HTML.
The `position` property controls how an element is positioned on the page. It works with `top`, `bottom`, `left`, `right` properties.
.relative-box {
position: relative;
top: 20px;
left: 30px;
background-color: lightblue;
width: 100px;
height: 50px;
}
Visual Example: The box shifts 20px down and 30px right from where it *would* normally be, but it pushes subsequent content as if it were still in its original spot.
.parent-relative {
position: relative; /* This makes it the positioning context */
width: 300px;
height: 150px;
border: 2px dashed grey;
padding: 10px;
}
.absolute-box {
position: absolute;
top: 10px;
right: 10px;
background-color: lightgreen;
padding: 10px;
}
Visual Example: The absolute box floats over other content, positioned from the top-right corner of its relative parent.
.fixed-button {
position: fixed;
bottom: 20px;
right: 20px;
background-color: red;
color: white;
padding: 15px;
border-radius: 50%; /* Circle */
}
Visual Example: Imagine a "Back to Top" button that always stays in the bottom-right corner of the screen as you scroll.
Scroll down to see fixed box.
More content here...
Even more content here...
.sticky-header {
position: sticky;
top: 0; /* Sticks to the top of the viewport */
background-color: lightgray;
padding: 10px;
border-bottom: 1px solid gray;
}
Visual Example: A navigation bar that scrolls with the content until it reaches the top of the viewport, then stays "stuck" there while the rest of the content scrolls underneath.
Scroll down to see the header stick.
Content...
This content will scroll under the sticky header.
More content...
Even more content...
Scroll further...
Flexbox is a one-dimensional layout system for arranging items in a single row or column. It's incredibly powerful for distributing space among items and aligning them within a container.
<div class="flexbox-container">
<div class="flexbox-item">Item A</div>
<div class="flexbox-item">Item B</div>
<div class="flexbox-item">Item C</div>
</div>
.flexbox-container {
display: flex; /* Makes it a flex container */
justify-content: space-around; /* Distributes items with space around */
align-items: center; /* Vertically centers items */
gap: 10px;
}
.flexbox-item {
flex: 1; /* Allows items to grow and shrink equally */
background-color: #a5d6ff;
padding: 15px;
border: 1px solid #4a90e2;
}
Visual Example (Flexbox with `justify-content: space-around;` and `align-items: center;`):
Items are spaced horizontally and centered vertically.
Visual Example (Flexbox with `flex-wrap: wrap;`):
Items wrap to the next line if container is too narrow.
CSS Grid is a two-dimensional layout system that allows you to arrange elements in rows and columns. It's ideal for defining overall page layouts or complex sections.
<div class="grid-container grid-example-columns">
<div class="grid-item">Col 1</div>
<div class="grid-item">Col 2 (Wider)</div>
<div class="grid-item">Col 3</div>
<div class="grid-item">Item 4</div>
<div class="grid-item">Item 5</div>
<div class="grid-item">Item 6</div>
</div>
.grid-container {
display: grid;
gap: 10px;
padding: 10px;
border: 1px solid #764ba2;
background-color: #f3e6ff;
}
.grid-example-columns {
grid-template-columns: 1fr 2fr 1fr; /* Three columns: 1 part, 2 parts, 1 part */
}
.grid-item {
background-color: #c399e2;
padding: 15px;
text-align: center;
color: #1a202c;
}
Visual Example (Grid with `grid-template-columns: 1fr 2fr 1fr;`):
Items are arranged in a 3-column grid, with the middle column twice as wide.
Visual Example (Grid with `grid-column: span 2;`):
Item B stretches across two columns.
Styling text is a core part of CSS. Key properties include:
.custom-text {
font-family: Georgia, serif;
font-size: 1.2rem;
color: #336699;
text-align: justify;
line-height: 1.7;
text-decoration: underline;
text-transform: capitalize;
letter-spacing: 0.5px;
}
Visual Example:
Can be defined using:
.color-box {
background-color: #f0f8ff; /* AliceBlue */
color: rgb(75, 0, 130); /* Indigo */
border: 2px solid rgba(0, 128, 0, 0.7); /* Semi-transparent green */
padding: 10px;
}
Visual Example:
.hero-banner {
background: url('https://via.placeholder.com/400x100/ADD8E6/000000?text=Background') no-repeat center center / cover;
height: 150px;
color: #333;
display: flex;
align-items: center;
justify-content: center;
font-size: 1.5em;
font-weight: bold;
}
Visual Example:
CSS transitions allow you to create smooth animated changes to CSS properties over a specified duration when an element's state changes (e.g., on hover, focus, or class change).
.animated-box {
width: 100px;
height: 100px;
background-color: #ff7b72;
transition: background-color 0.5s ease-in-out, transform 0.5s ease-out; /* Smooth transition */
}
.animated-box:hover {
background-color: #a5d6ff; /* Changes color on hover */
transform: scale(1.1) rotate(5deg); /* Grows and rotates */
}
Visual Example:
(Hover over the box above to see the smooth transition.)
CSS animations allow for more complex, multi-step animations. They are defined using `@keyframes` rules and then applied to elements.
@keyframes bounce {
0% { transform: translateY(0); }
50% { transform: translateY(-20px); }
100% { transform: translateY(0); }
}
.bouncing-ball {
width: 50px;
height: 50px;
background-color: blue;
border-radius: 50%;
animation: bounce 1s infinite ease-in-out; /* Apply animation */
}
Visual Example:
(The circle above demonstrates a pulsing animation.)
Media queries allow you to apply CSS styles conditionally based on device characteristics like screen width, height, resolution, and orientation. This is fundamental for responsive web design.
/* Default styles (for larger screens first - 'mobile-last' approach) */
.my-card {
width: 300px;
padding: 20px;
margin: 10px;
background-color: lightblue;
box-shadow: 2px 2px 5px rgba(0,0,0,0.2);
}
/* Styles for screens up to 768px wide (tablets and smaller) */
@media (max-width: 768px) {
.my-card {
width: 90%; /* Take up more width on smaller screens */
margin: 10px auto; /* Center the card */
background-color: lightcoral; /* Change color for visual distinction */
}
}
/* Styles for screens smaller than 480px wide (phones) */
@media (max-width: 480px) {
.my-card {
font-size: 0.9em; /* Smaller font size on tiny screens */
padding: 15px;
}
}
Visual Example:
Resize your browser window horizontally to see the box below change its background color and size based on the screen width.
(On a live page, this box would change color to lightcoral on screens < 768px and text size on screens < 480px.)
/* Higher specificity than p {} */
#my-element { color: blue; }
/* Higher specificity than .my-class {} */
div.my-class { color: green; }
:root {
--primary-color: #667eea;
--spacing-unit: 16px;
}
.button {
background-color: var(--primary-color);
padding: var(--spacing-unit);
}
CSS is a vast and powerful language. The best way to learn is by building projects, experimenting with properties, and using your browser's developer tools to inspect and modify styles live. Don't be afraid to break things and learn from your mistakes!