HTML Structure: Building the Foundation Right
Learn semantic HTML elements and proper document structure. We’ll walk through tags that matter and how to organize your markup for clean, maintainable code.
Read ArticleFlexbox is easier than you think. We’ll cover the main properties, common layouts, and how to make your designs work on any screen size without media query headaches.
CSS Flexbox changed the game for web layouts. Before it, we had floats, absolute positioning, and table layouts — honestly, it was a mess. Flexbox gives you a clean, predictable way to arrange elements in one dimension, and it’s responsive by default.
Here’s the thing: you don’t need to memorize every property. We’re going to focus on what actually matters in real projects. You’ll learn the core properties that handle 95% of layout situations, then we’ll walk through some practical examples you can steal and use immediately.
Flexbox works with two things: a container and its direct
children (items). You set
display: flex;
on the parent, and suddenly the kids become flex items. That’s
it. The parent controls how they’re arranged.
The main properties you’ll use on the container are
flex-direction
,
justify-content
, and
align-items
. These three handle most layouts.
flex-direction
sets the direction (row or column),
justify-content
aligns items along that direction, and
align-items
aligns them perpendicular to it.
Quick tip: Always specify flex-direction when you use display: flex. Even if it’s the default (row), being explicit makes your code easier to read and debug.
We’re skipping the obscure stuff and focusing on what wins layouts in production.
Sets whether items flow in a row (left to right) or column
(top to bottom). Default is
row
. Use
column
for vertical stacking.
Aligns items along the main axis. Values like
flex-start
,
center
,
space-between
, and
space-around
give you control.
Aligns items along the cross axis. Use
center
to vertically center content in a row, or
stretch
to make items equal height.
Controls whether items wrap to multiple lines. Set to
wrap
to prevent items from shrinking when space is tight.
Creates space between items automatically. Instead of margins,
use
gap: 1rem;
for consistent spacing throughout.
Shorthand for grow, shrink, and basis. Use
flex: 1;
to make items share space equally, or
flex: 0 0 300px;
for fixed width.
You don’t need to build custom layouts for everything. These three patterns handle navigation bars, card grids, hero sections, and more. Once you master these, you’re covering maybe 80% of what gets built on the web.
Use a flex container with
justify-content: space-between
to push the logo to the left and nav items to the right.
Add
align-items: center
to keep everything vertically aligned. Wrap with
flex-wrap: wrap
for mobile.
Create a flex container with
flex-wrap: wrap
and
gap
for spacing. Give each card
flex: 1 1 300px;
so they’re at least 300px but grow to fill space. Perfect
for product listings or blog posts.
Flex container with
flex-direction: row
and
align-items: center
. Give each column
flex: 1;
for equal width. On mobile, switch to
flex-direction: column
in a media query. Done.
Here’s where Flexbox really shines: it’s responsive by nature. You don’t need a media query for every little adjustment. Start with your mobile layout, then use media queries to adjust for larger screens.
Use properties like
flex-wrap: wrap
and flexible sizing (
flex: 1 1 auto;
) to let items naturally adapt. For something like a navigation
bar, you might have items stack on mobile without any media
queries. When you do need to adjust, change
flex-direction
from column to row, or adjust
gap
values.
“The best part of Flexbox? You can build a layout that works on mobile, tablet, and desktop with maybe one or two media queries instead of ten.”
We’ve all been there. Here’s what trips people up most.
The default is
row
, but it’s confusing when you don’t write it. Always be
explicit. Your future self will thank you when you’re
debugging.
Gap is cleaner and more predictable. With margin, you get
double spacing at edges and complexity. Use
gap
for spacing between items, period.
You don’t need the shorthand. Just use
flex: 1;
for equal sizing, or set
flex-basis
with a specific width. Keep it simple.
If you want items to wrap to a new line instead of
shrinking, add
flex-wrap: wrap;
. Without it, everything tries to fit on one line and gets
squished.
That’s it. You’ve got the fundamentals, the properties that matter, and three patterns that solve most layout problems. Flexbox isn’t intimidating once you realize you don’t need to memorize everything.
Start small. Build a simple navigation bar with
display: flex
and
justify-content: space-between
. Create a card grid with
flex-wrap: wrap
. Once you’re comfortable with those, everything else clicks into
place. The properties are consistent, the logic is predictable,
and you’ll be building responsive layouts faster than you ever did
with floats or positioning hacks.
Ready to go deeper? Explore CSS Grid for two-dimensional layouts, or dive into advanced Flexbox alignment techniques. The foundation you’ve built here will make everything else easier.
Back to Web Development BasicsThis article is informational and educational in nature. The CSS and HTML examples provided are intended to demonstrate concepts and best practices. Browser support and CSS specifications may change over time. Always test your layouts across different browsers and devices. For production applications, refer to the latest W3C CSS specifications and browser compatibility charts. Individual implementations may vary based on project requirements and constraints.