Getting Started with Tailwind CSS in Malaysia
Learn how to set up Tailwind CSS on your projects, customize configurations, and build responsive layouts with utility-first styling.
Read ArticleDeep dive into justify-content, align-items, gap, and flex properties. Build flexible, centered layouts without hacks or workarounds.
If you’ve been centering content with margin hacks or struggling to align items properly, you’re not alone. Flexbox changed everything. It’s not just about layouts — it’s about having complete control over how your elements behave, stretch, and align within their containers.
The thing is, most developers know about display: flex, but they’re missing the power of the alignment properties. We’re talking justify-content, align-items, gap, and a few others that’ll make your CSS cleaner and your layouts more predictable. You won’t need weird hacks anymore. Just solid properties that do exactly what you expect.
Let’s start with justify-content. This property controls how flex items are distributed along the main axis (usually horizontal). You’ve got five main values, and each one does something different.
flex-start groups items at the beginning. flex-end pushes them to the end. center centers them — simple and reliable. space-between spreads items out with equal space between them. And space-around gives equal space on both sides of each item. There’s also space-evenly, which distributes items with equal spacing all around.
The key thing to remember: justify-content only works when you’ve got extra space. If your flex items fill the entire container, you won’t see any effect. That’s where width declarations come in. Give your flex container a fixed or percentage width larger than your content, and you’ll see justify-content do its job.
Now let’s talk about align-items. This property controls how flex items are aligned on the cross axis (usually vertical). It’s what you’ll use when you want to center something vertically without resorting to weird tricks.
You’ve got flex-start, which aligns items to the top of the container. flex-end aligns them to the bottom. center vertically centers them. stretch makes items grow to fill the container height. And baseline aligns them based on the text baseline — useful for mixed content.
Here’s where it gets powerful: combine justify-content with align-items, and you can center content both horizontally and vertically with just two properties. No more margin: auto hacks or transform tricks. Just clean, semantic CSS that anyone reading your code will understand immediately.
Before gap existed, you’d add margins to flex items to space them out. It wasn’t terrible, but it wasn’t ideal either. The gap property changed everything. It’s simple: you declare a gap value, and flexbox automatically adds that space between items. No margins needed.
You can use a single value for uniform spacing: gap: 1rem. Or you can specify row and column gaps separately: gap: 1rem 2rem. The first value is the row gap, the second is the column gap. This matters when you’re using flex-wrap to create multi-line layouts.
“Gap is one of those properties that seems small until you use it every day and realize you can’t live without it. It’s clean, predictable, and it works the way you’d expect.”
— Frontend Developer, 8 years experience
A flex container with 4 items and gap:
.container {
display: flex;
gap: 1.5rem;
flex-wrap: wrap;
}
.item {
flex: 1 1 250px;
padding: 2rem;
}
This creates uniform 1.5rem spacing between all items, wrapping on smaller screens. No margin hacks required.
Three common layouts that flexbox alignment properties solve beautifully.
Use justify-content: center with gap to create a clean nav bar. All items space evenly with no margin tricks. Responsive? Just switch to flex-direction: column on mobile and it works perfectly.
Properties needed: display: flex, justify-content: center, gap: 2rem
Combine justify-content: center and align-items: center on a full-viewport container. Your content sits dead center, both axes. Add flex-direction: column if you want content stacked. Change min-height on mobile and you’re done.
Properties needed: display: flex, justify-content: center, align-items: center, min-height: 100vh
Use flex-wrap: wrap with flex: 1 1 300px on items. Add gap: 2rem for spacing. On mobile, items shrink to fit. On desktop, they expand. Gap handles all spacing. One rule change for responsive behavior.
Properties needed: display: flex, flex-wrap: wrap, gap: 2rem, flex: 1 1 300px on items
You’re going to make these mistakes. Everyone does. The important thing is recognizing them and fixing them fast.
Mistake 1: Forgetting flex-direction. You declare display: flex, but items still go horizontal when you wanted vertical. Solution: add flex-direction: column. It’s not implied. Always explicit.
Mistake 2: Setting width on flex items. You try to give items a width, but flexbox keeps shrinking them. You forgot the flex-basis property. Use flex: 0 0 300px to give items a fixed width that flexbox respects.
Mistake 3: justify-content not working. You’re scratching your head because justify-content isn’t spreading items out. Check if your items are already filling the container width. If they are, justify-content has nowhere to work. Reduce item widths or increase container width.
Controls horizontal spacing and alignment. Use flex-start, center, flex-end, space-between, or space-around to position items along the main axis.
Controls vertical alignment. Perfect for centering items vertically without hacks. Works with flex-start, center, flex-end, stretch, and baseline.
Adds automatic spacing between flex items. No more margin tricks. Clean, predictable spacing that works perfectly with flex-wrap.
Use all three together for complete layout control. Center content, space items evenly, and handle responsive design without media query headaches.
Start using these flexbox properties in your next project. You’ll notice cleaner code, fewer layout bugs, and layouts that actually respond properly to different screen sizes. No hacks. Just solid CSS fundamentals.
Explore More CSS ResourcesThis article provides educational information about CSS flexbox alignment properties and is intended to help developers understand these concepts. Web development practices evolve continuously, and browser support varies. We recommend testing all code in your target environments and consulting the latest CSS specifications from W3C. This content is informational and doesn’t replace professional web development practices or official documentation.