Get ready for an adventure into the world of CSS Positioning and Flexbox!
In today’s tutorial, we will learn how to create modern, responsive web layouts that are beautiful and look great on all screens. We will create a stunning design for a navigation bar and card layout using Flexbox, which is one of the most powerful layout tools in CSS.
What is so important about learning these skills?
After this tutorial, you will be able to create navigation bars and card layouts that adapt to screens automatically, which is one of those skills that all modern web developers need to know.
Let’s get started!
First, let’s create the basic HTML structure for our project:
<html>
<head>
<link rel="stylesheet" href="Positioning_Flexbox2.css">
</head>
<body>
<nav class="navbar">
<div class="logo">Niotechone🎀</div>
<ul class="nav-links">
<li>HOME🏡</li>
<li>About🛒</li>
<li>Services⚙</li>
<li>Contact📞</li>
</ul>
</nav>
<div class="card-container">
<div class="card">card-container 1</div>
<div class="card">card-container 2</div>
<div class="card">card-container 3</div>
</div>
</body>
</html>
What is going on here?
Pro Tip: It is a good practice to use semantic HTML tags like <nav> because they help search crawlers better process your page and know what the parts of the page are.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
Why do we do this?
Note: This is a CSS reset, which is definitely a best practice, and many developers will do this to start with a clean slate!
body {
font-family: 'Poppins', Arial, sans-serif;
background: #f8f9fb;
color: #333;
}
Breaking it down:
Pro Tip: Make sure to always provide fallback fonts! If Poppins doesn’t load for some reason, the browser will move to Arial, and then to any sans-serif font.
Example:
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
background: linear-gradient(90deg, #6a11cb, #2575fc);
padding: 15px 40px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.15);
color: #fff;
}
Here’s where the magic takes place!
Why flexbox? If you do not use flexbox, it requires large amounts of positioning to align horizontally and vertically. With flexbox, it takes only 3 lines!
Example:
.logo {
font-size: 1.8rem;
font-weight: bold;
letter-spacing: 1px;
}
Simple but effective:
Example:
.nav-links {
list-style: none;
display: flex;
gap: 30px;
}
Flexbox is incredible, let’s go again!
Note: You will notice that measuring gaps between flex items is now easy with this relatively new style feature!
Example:
.nav-links li {
font-size: 1.1rem;
font-weight: 500;
cursor: pointer;
transition: all 0.3s ease;
}
.nav-links li:hover {
color: #ffeb3b;
transform: translateY(-3px);
}
Creating interactive experiences:
Pro Tip: Transitions can make your site feel a little more polished and professional, and people love it when desktop sites have subtle animations! !
Example:
.card-container {
display: flex;
justify-content: center;
align-items: center;
gap: 25px;
flex-wrap: wrap;
padding: 60px 20px;
}
Another flexbox masterpiece:
Example:
.card {
background: #fff;
width: 280px;
height: 180px;
display: flex;
justify-content: center;
align-items: center;
font-size: 1.3rem;
font-weight: 600;
color: #333;
border-radius: 15px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
transition: all 0.3s ease;
}
Creating beautiful cards:
Example:
.card:hover {
transform: translateY(-8px);
box-shadow: 0 8px 18px rgba(0, 0, 0, 0.15);
background: linear-gradient(135deg, #f0f0f0, #e0eaff);
}
Interactivity on cards:
This gives a 3D feel for the user, making it feel like they are interacting with physical cards!
Example:
@media (max-width: 768px) {
.navbar {
flex-direction: column;
text-align: center;
}
.nav-links {
flex-direction: column;
gap: 15px;
margin-top: 10px;
}
.card-container {
flex-direction: column;
}
.card {
width: 90%;
}
}
Responsive design in action!
Pro Tip: Always test your website on different screen sizes! Most web traffic comes from mobile devices now.
A. Components & HTML Elements Used
Name | Description |
<nav> | Semantic HTML5 element for navigation sections |
<ul> and <li> | Unordered list and list items for navigation links |
<div> | Generic container for grouping and styling elements |
.navbar | Class for the main navigation bar container |
.logo | Class for the brand logo/name |
.nav-links | Class for the navigation links list |
.card-container | Class for the container holding all cards |
.card | Class for individual card elements |
B. Key CSS Properties & Flexbox Features
Property | Description |
display: flex | Turns an element into a flex container, enabling flexbox layout |
justify-content | Aligns flex items along the main axis (horizontal by default) |
align-items | Aligns flex items along the cross axis (vertical by default) |
flex-direction | Changes the direction of flex items (row or column) |
flex-wrap | Allows flex items to wrap to the next line when space runs out |
gap | Creates consistent spacing between flex items without margins |
transition | Smoothly animates property changes over specified duration |
transform | Applies transformations like translation, rotation, or scaling |
box-shadow | Adds shadow effects to create depth and visual hierarchy |
linear-gradient | Creates smooth color transitions for backgrounds |
@media | Defines responsive styles that apply at specific screen sizes |
border-radius | Rounds the corners of elements for modern aesthetics |
box-sizing: border-box | Includes padding and border in element’s total width/height |
Here are some excellent resources to continue your learning journey:
Practice Challenges:
You now understand how CSS Positioning and Flexbox work to create modern, responsive layouts. You learned how to align elements, manage space efficiently, and design flexible structures that adapt perfectly across different screen sizes.
CSS Positioning allows you to place elements precisely using static, relative, absolute, fixed, or sticky positions. Flexbox, on the other hand, is used to create flexible and responsive layouts that automatically adjust to screen sizes.
Flexbox provides more control, cleaner code, and better alignment options compared to outdated float-based layouts. It’s also easier to create vertically and horizontally centered content without using extra CSS hacks.
Flexbox automatically adjusts the size and spacing of elements based on screen width. By using properties like flex-wrap, justify-content, and align-items, you can make your layout adapt to any device seamlessly.
Yes! Many developers combine Flexbox for small-scale alignment (like navbars and buttons) with CSS Grid for overall page structure. This hybrid approach offers maximum flexibility and precision.
The key properties are display: flex, justify-content, align-items, flex-wrap, gap, and flex-direction. Mastering these gives you control over alignment, spacing, and layout behavior in any design.
Copyright © 2025 Niotechone Software Solution Pvt. Ltd. All Rights Reserved.