inline
, block
, and inline-block
elements in CSS?Imagine you’re arranging books on a shelf:
inline
elements are like small bookmarks that sit next to each other.block
elements are like big books that take up the whole shelf width.inline-block
elements are like medium-sized books that sit next to each other but maintain their box-like shape.inline
:
block
:
inline-block
:
.inline { display: inline; }
.block { display: block; }
.inline-block { display: inline-block; }
<span class="inline">Inline</span>
<span class="inline">Elements</span>
<div class="block">Block Element</div>
<div class="inline-block">Inline-block</div>
<div class="inline-block">Elements</div>
Think of every HTML element as a gift box:
The CSS box model describes how elements are rendered in the browser. From inside to outside:
By default, when you set a width/height, it applies to the content box only. This is the “content-box” box-sizing model. You can change this behavior:
* {
box-sizing: border-box;
}
With border-box
, the width/height includes content, padding, and border, making layout calculations easier.
graph TD
A[Margin] --> B[Border]
B --> C[Padding]
C --> D[Content]
margin
and padding
in CSS?Think of an element as a burger:
Margin
:
Padding
:
box-sizing: border-box
).burger {
background-color: #f4a460; /* bun color */
padding: 20px; /* sauce */
border: 5px solid #8b4513; /* crust */
margin: 10px; /* space on the plate */
}
Flexbox:
CSS Grid:
Flexbox:
.container {
display: flex;
justify-content: space-between;
}
CSS Grid:
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}
graph TD
A[Layout Systems] --> B[Flexbox]
A --> C[CSS Grid]
B --> D[One-dimensional]
B --> E[Content-first]
C --> F[Two-dimensional]
C --> G[Layout-first]
absolute
, relative
, fixed
, and sticky
positioning in CSS?Imagine a race:
static
: Runners in their starting positions (default)relative
: Runners who’ve moved a bit from their startabsolute
: Runners who’ve left the track and are standing somewhere in the stadiumfixed
: Spectators in their seats (don’t move even if you scroll)sticky
: Water station staff (stay in place until you scroll past, then follow along)static
: Default positioning. Element follows normal document flow.
relative
:
absolute
:
fixed
:
sticky
:
.relative {
position: relative;
top: 10px;
left: 20px;
}
.absolute {
position: absolute;
top: 50px;
right: 30px;
}
.fixed {
position: fixed;
bottom: 20px;
right: 20px;
}
.sticky {
position: sticky;
top: 0;
}
Imagine your website is like water, and devices are different shaped containers. Responsive design ensures your “water” (content) fits perfectly into any “container” (device screen).
Responsive design involves several techniques:
Fluid Grids: Use percentage-based widths instead of fixed pixels.
img {
max-width: 100%;
height: auto;
}
@media (max-width: 600px) {
.container {
flex-direction: column;
}
}
<meta name="viewport" content="width=device-width, initial-scale=1">
CSS Grid and Flexbox: For flexible, responsive layouts.
/* Base styles (mobile) */
.container {
width: 100%;
padding: 15px;
}
/* Tablet styles */
@media (min-width: 768px) {
.container {
width: 750px;
margin: 0 auto;
}
}
/* Desktop styles */
@media (min-width: 1024px) {
.container {
width: 970px;
}
}
Media queries are like a tailor for your website. They measure the screen size and adjust your site’s clothes (styles) to fit perfectly.
Media queries allow you to apply CSS styles based on device characteristics, most commonly the viewport width. They’re a fundamental part of responsive design.
Syntax:
@media [media-type] and (media-feature) {
/* CSS rules */
}
media-type
: screen, print, speech, all (default)media-feature
: width, height, orientation, aspect-ratio, etc.You can use logical operators (and
, or
, not
) to combine multiple conditions.
/* Styles for screens narrower than 600px */
@media screen and (max-width: 600px) {
body {
font-size: 14px;
}
}
/* Styles for screens wider than 1200px and in landscape orientation */
@media screen and (min-width: 1200px) and (orientation: landscape) {
.container {
max-width: 1140px;
}
}
Media queries can also be used in link tags:
<link rel="stylesheet" media="screen and (max-width: 600px)" href="small.css">
graph TD
A[Device] --> B{Media Query}
B -->|Matches| C[Apply Styles]
B -->|Doesn't Match| D[Skip Styles]
CSS preprocessors are like super-powered CSS. They let you write CSS with extra features, then turn it into normal CSS that browsers understand.
CSS preprocessors are scripting languages that extend CSS and compile to standard CSS. They offer features like variables, nesting, mixins, functions, and more, which aren’t available in plain CSS.
Popular CSS preprocessors:
Benefits of using preprocessors:
// Variables
$primary-color: #3498db;
$padding: 15px;
// Mixin
@mixin border-radius($radius) {
-webkit-border-radius: $radius;
-moz-border-radius: $radius;
border-radius: $radius;
}
// Nesting and usage
.button {
background-color: $primary-color;
padding: $padding;
@include border-radius(5px);
&:hover {
background-color: darken($primary-color, 10%);
}
}
This compiles to:
.button {
background-color: #3498db;
padding: 15px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}
.button:hover {
background-color: #217dbb;
}
graph TD
A[Preprocessor Code] --> B[Preprocessor]
B --> C[Standard CSS]
C --> D[Browser]
Imagine a contest where different types of CSS selectors compete. Some selectors are more “important” and win over others. That’s specificity!
Specificity determines which CSS rule is applied when multiple rules target the same element. It’s calculated as a four-part value: a, b, c, d
The selector with the highest specificity wins. If specificities are equal, the last rule in the CSS is applied.
/* Specificity: 1 */
p { color: black; }
/* Specificity: 10 */
.text { color: red; }
/* Specificity: 100 */
#header { color: blue; }
/* Specificity: 11 */
p.text { color: green; }
/* Specificity: 110 */
#header .text { color: purple; }
Pseudo-classes select elements based on a certain state or condition. They use a single colon :
.
Pseudo-elements allow you to style a specific part of an element. They use a double colon ::
(single colon also works for backwards compatibility).
/* Styles a link when hovered over */
a:hover {
color: red;
}
/* Styles every odd child of a list */
li:nth-child(odd) {
background-color: #f2f2f2;
}
/* Styles an input when it's focused */
input:focus {
border-color: blue;
}
/* Adds content before an element */
.quote::before {
content: "❝";
}
/* Styles the first line of a paragraph */
p::first-line {
font-weight: bold;
}
/* Styles the first letter of a heading */
h1::first-letter {
font-size: 2em;
color: red;
}
graph TD
A[CSS Selectors] --> B[Pseudo-classes]
A --> C[Pseudo-elements]
B --> D[:hover]
B --> E[:active]
B --> F[:nth-child]
C --> G[::before]
C --> H[::after]
C --> I[::first-line]
Imagine you’re trying to put a sticker exactly in the middle of a page. There are different ways to do this, depending on what kind of sticker and page you have!
There are several ways to center elements in CSS, depending on the context:
.parent {
display: flex;
justify-content: center;
align-items: center;
height: 100vh; /* Ensure the parent takes full viewport height */
}
.parent {
display: grid;
place-items: center;
height: 100vh;
}
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.parent {
text-align: center; /* Horizontal centering */
line-height: 100vh; /* Vertical centering, adjust as needed */
}
.child {
display: inline-block;
vertical-align: middle;
}
.child {
width: 300px;
margin: 0 auto;
}
CSS animations are like creating a flip book. You define how something looks at different stages, and CSS makes it move smoothly between those stages.
CSS animations allow you to gradually change from one style to another. They consist of two components:
/* Define the keyframes */
@keyframes bounce {
0%, 100% {
transform: translateY(0);
}
50% {
transform: translateY(-20px);
}
}
/* Apply the animation to an element */
.bouncing-ball {
width: 50px;
height: 50px;
background-color: red;
border-radius: 50%;
animation: bounce 1s ease infinite;
}
This creates a bouncing ball effect.
animation-name
: Name of the @keyframes ruleanimation-duration
: How long the animation takesanimation-timing-function
: How the animation progresses (ease, linear, etc.)animation-delay
: Delay before the animation startsanimation-iteration-count
: How many times the animation should runanimation-direction
: Whether the animation should alternate direction or resetanimation-fill-mode
: Styles applied before/after the animationanimation-play-state
: Whether the animation is running or pausedgraph TD
A[@keyframes] --> B[Define Stages]
C[Animation Properties] --> D[Apply to Element]
B --> E[Animation Effect]
D --> E
CSS transitions are like smooth gear changes in a car. Instead of abruptly changing from one style to another, the change happens gradually over time.
CSS transitions allow you to define the transition between two states of an element. They provide a way to control animation speed when changing CSS properties.
transition-property
: CSS properties to transitiontransition-duration
: How long the transition takestransition-timing-function
: How the transition progresses (ease, linear, etc.)transition-delay
: Delay before the transition starts.button {
background-color: blue;
color: white;
padding: 10px 20px;
transition: background-color 0.3s ease;
}
.button:hover {
background-color: darkblue;
}
This creates a smooth color change when hovering over the button.
You can use the transition
shorthand property:
.button {
transition: background-color 0.3s ease 0.1s;
}
CSS variables are like paint buckets with custom colors. You can define a color once and use it many times throughout your stylesheet. If you want to change the color, you only need to change it in one place!
CSS variables, also known as custom properties, allow you to store specific values to be reused throughout a document. They are set using custom property notation (e.g., --main-color: black;
) and are accessed using the var()
function.
:root {
--main-color: #3498db;
--padding: 15px;
--font-size: 16px;
}
.button {
background-color: var(--main-color);
padding: var(--padding);
font-size: var(--font-size);
}
.text {
color: var(--text-color, black);
}
If --text-color
isn’t defined, it will fall back to black.
graph TD
A[CSS Variables] --> B[Define in :root]
A --> C[Use with var()]
B --> D[Global Scope]
C --> E[Local Usage]
E --> F[Easy Updates]
rem
and em
units in CSS.em
is like measuring based on your parent’s height.rem
is like measuring based on the height of the tallest person in your family.Both em
and rem
are relative units, but they have different reference points:
em
: Relative to the font-size of the element (2em means 2 times the size of the current font)
rem
: Relative to the font-size of the root element (html)
html {
font-size: 16px;
}
.parent {
font-size: 18px;
}
.child-em {
font-size: 2em; /* 36px (2 * 18px) */
padding: 1em; /* 36px (1 * 36px) */
}
.child-rem {
font-size: 2rem; /* 32px (2 * 16px) */
padding: 1rem; /* 16px (1 * 16px) */
}
rem
for sizes you want to scale with page zoomem
for sizes you want to scale with component zoom