From Basics to Advanced - Learn to Create Beautiful Websites 🎨
CSS stands for Cascading Style Sheets. CSS describes how HTML elements are displayed on screen, paper, or in other media.
1996 CSS1 - First specification
1998 CSS2 - Added positioning, z-index
2011 CSS3 - Modular, animations, new selectors
Present CSS continues to evolve
A CSS rule consists of a selector and a declaration block:
selector {
property: value;
property: value;
}
/* Example */
p {
color: red;
font-size: 16px;
}
<!-- In HTML head -->
<link rel="stylesheet" href="styles.css">
/* styles.css */
body {
background-color: lightblue;
}
<style>
body {
background-color: lightblue;
}
</style>
<p style="color: red; font-size: 20px;">Text</p>
/* !important overrides everything */
color: red !important;
/* Individual Properties */
background-color: lightblue;
background-image: url('image.jpg');
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
background-attachment: fixed;
/* Shorthand */
background: #fff url('bg.jpg') no-repeat center/cover;
/* Multiple Backgrounds */
background-image: url('img1.jpg'), url('img2.jpg');
background-position: left top, right bottom;
/* Border Width */
border-width: 5px;
border-width: 5px 10px 15px 20px; /* top right bottom left */
/* Border Style */
border-style: solid;
/* Values: solid, dashed, dotted, double, groove,
ridge, inset, outset, none, hidden */
/* Border Color */
border-color: red;
/* Shorthand */
border: 2px solid red;
/* Individual Sides */
border-top: 1px solid black;
border-right: 2px dashed red;
border-bottom: 3px dotted blue;
border-left: 4px double green;
/* Border Radius */
border-radius: 10px; /* Rounded corners */
border-radius: 50%; /* Circle */
border-radius: 10px 20px 30px 40px;
Margins create space around elements, outside borders:
/* All sides */
margin: 25px;
/* Top/Bottom, Left/Right */
margin: 25px 50px;
/* Top, Left/Right, Bottom */
margin: 25px 50px 75px;
/* Top Right Bottom Left */
margin: 25px 50px 75px 100px;
/* Individual Sides */
margin-top: 25px;
margin-right: 50px;
margin-bottom: 75px;
margin-left: 100px;
/* Auto Centering */
margin: 0 auto;
Padding creates space around content, inside borders:
/* Same syntax as margin */
padding: 25px;
padding: 25px 50px;
padding: 25px 50px 75px;
padding: 25px 50px 75px 100px;
/* Individual Sides */
padding-top: 25px;
padding-right: 50px;
padding-bottom: 75px;
padding-left: 100px;
Every HTML element is a box consisting of:
/* Default */
box-sizing: content-box;
/* Width/height = content only */
/* Recommended */
box-sizing: border-box;
/* Width/height = content + padding + border */
/* Global Reset */
* {
box-sizing: border-box;
}
color: red;
text-align: center; /* left, right, center, justify */
text-decoration: underline; /* none, underline, overline, line-through */
text-transform: uppercase; /* uppercase, lowercase, capitalize */
text-indent: 50px;
letter-spacing: 2px;
word-spacing: 5px;
line-height: 1.6;
white-space: nowrap; /* normal, nowrap, pre, pre-wrap */
text-shadow: 2px 2px 5px gray;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
font-family: Arial, sans-serif;
font-size: 16px; /* px, em, rem, %, vw */
font-weight: bold; /* normal, bold, 100-900 */
font-style: italic; /* normal, italic, oblique */
font-variant: small-caps;
/* Shorthand */
font: italic bold 16px/1.5 Arial, sans-serif;
<!-- In HTML head -->
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
/* In CSS */
body {
font-family: 'Roboto', sans-serif;
}
@font-face {
font-family: 'MyFont';
src: url('myfont.woff2') format('woff2');
}
body {
font-family: 'MyFont', sans-serif;
}
Follows normal document flow
position: relative;
top: 10px;
left: 20px;
Positioned relative to normal position
position: absolute;
top: 0;
right: 0;
Positioned relative to nearest positioned ancestor
position: fixed;
bottom: 20px;
right: 20px;
Positioned relative to viewport, stays on scroll
position: sticky;
top: 0;
Toggles between relative and fixed based on scroll
z-index: 1; /* Higher = front */
z-index: -1; /* Behind */
/* Only works with positioned elements */
Flexbox is a one-dimensional layout method:
.container {
display: flex;
}
flex-direction: row; /* row, row-reverse, column, column-reverse */
flex-wrap: wrap; /* nowrap, wrap, wrap-reverse */
justify-content: center; /* flex-start, flex-end, center,
space-between, space-around, space-evenly */
align-items: center; /* stretch, flex-start, flex-end, center, baseline */
gap: 10px;
flex-grow: 1; /* Grow to fill space */
flex-shrink: 1; /* Can shrink */
flex-basis: 200px; /* Initial size */
flex: 1; /* Shorthand */
align-self: flex-start;
order: 2;
.container {
display: grid;
}
grid-template-columns: 200px 200px 200px;
grid-template-columns: 1fr 2fr 1fr; /* Fractional units */
grid-template-columns: repeat(3, 1fr);
grid-template-rows: 100px 200px;
gap: 20px;
justify-items: center;
align-items: center;
grid-column: 1 / 3; /* Span from line 1 to 3 */
grid-column: span 2; /* Span 2 columns */
grid-row: 1 / 3;
grid-area: 1 / 1 / 3 / 3; /* row-start / col-start / row-end / col-end */
transition-property: all; /* Which property */
transition-duration: 0.3s; /* How long */
transition-timing-function: ease; /* ease, linear, ease-in, ease-out */
transition-delay: 0.2s; /* Delay */
/* Shorthand */
transition: all 0.3s ease;
/* Example */
.button {
background-color: blue;
transition: background-color 0.3s ease;
}
.button:hover {
background-color: red;
}
@keyframes slide {
0% { transform: translateX(0); }
50% { transform: translateX(100px); }
100% { transform: translateX(0); }
}
.element {
animation-name: slide;
animation-duration: 2s;
animation-timing-function: ease;
animation-delay: 1s;
animation-iteration-count: infinite;
animation-direction: alternate;
animation-fill-mode: forwards;
}
/* Shorthand */
animation: slide 2s ease-in-out 1s infinite alternate;
/* Individual Transforms */
transform: translate(50px, 100px);
transform: rotate(45deg);
transform: scale(2);
transform: skew(20deg, 10deg);
/* Multiple Transforms */
transform: translate(50px, 100px) rotate(45deg) scale(2);
transform: rotateX(45deg);
transform: rotateY(45deg);
transform: perspective(500px) rotateY(45deg);
/* Transform Origin */
transform-origin: center center; /* Default */
transform-origin: top left;
/* Base styles for mobile */
@media (min-width: 576px) {
/* Small devices */
}
@media (min-width: 768px) {
/* Tablets */
}
@media (min-width: 992px) {
/* Desktops */
}
@media (min-width: 1200px) {
/* Large desktops */
}
@media (max-width: 1199px) {
/* Medium devices */
}
@media (max-width: 991px) {
/* Tablets */
}
@media (max-width: 767px) {
/* Mobile */
}
@media (orientation: portrait) { }
@media (prefers-color-scheme: dark) { }
@media print { }
/* Define Variables */
:root {
--primary-color: #3498db;
--secondary-color: #2ecc71;
--font-size: 16px;
--spacing: 20px;
}
/* Use Variables */
.button {
background-color: var(--primary-color);
font-size: var(--font-size);
padding: var(--spacing);
}
/* With Fallback */
color: var(--text-color, black);
::before { content: ""; }
::after { content: ""; }
::first-letter { }
::first-line { }
::selection { }
Most popular, comprehensive framework
Components Grid System UtilitiesUtility-first CSS framework
Utility Classes CustomizableProfessional responsive framework
Enterprise FlexibleModern CSS framework based on Flexbox
Pure CSS No JSYou've completed the comprehensive CSS course!
You now have a solid foundation in CSS, from basic styling to advanced layout techniques.