Day -4 CSS Layouts
Day -4 CSS Layouts
Flexbox is a layout model that allows you to align and distribute items within a container,
even when their size is unknown or dynamic.
Example HTML:
<div class="flex-container">
<div class="flex-item">Item 1</div>
<div class="flex-item">Item 2</div>
<div class="flex-item">Item 3</div>
</div>
Example CSS:
.flex-container {
display: flex;
justify-content: space-between;
align-items: center;
background-color: lightgrey;
}
.flex-item {
background-color: steelblue;
padding: 20px;
margin: 10px;
color: white;
}
Here, the items are spaced evenly across the container with justify-content:
space-between.
Grid layout is a powerful system for creating complex layouts by defining rows and columns.
<div class="grid-container">
<div class="grid-item">Item 1</div>
<div class="grid-item">Item 2</div>
<div class="grid-item">Item 3</div>
<div class="grid-item">Item 4</div>
</div>
Example CSS:
.grid-container {
display: grid;
grid-template-columns: 1fr 1fr; /* Two equal columns */
grid-template-rows: auto; /* Row size based on content */
gap: 10px;
background-color: lightgrey;
}
.grid-item {
background-color: teal;
padding: 20px;
color: white;
}
This example creates a grid with two equal columns and places the items accordingly.
These examples should help you understand the basics of the Box Model, Flexbox, and Grid
layout in CSS.
The viewport meta tag is essential for responsive design as it controls the layout on mobile
browsers. It ensures that the content scales properly on different devices.
Example HTML:
This tag sets the width of the page to the width of the device's screen and ensures the initial
scale is set to 1 (no zoom).
Fluid Layouts
Fluid layouts use relative units like percentages or em instead of fixed units like px to allow
the layout to resize dynamically based on the screen size.
<div class="fluid-container">
<div class="fluid-item">Item 1</div>
<div class="fluid-item">Item 2</div>
</div>
.fluid-container {
width: 100%; /* Takes full width of the viewport */
}
.fluid-item {
width: 50%; /* Each item takes 50% of the container */
float: left;
padding: 10px;
box-sizing: border-box;
}
This layout will automatically adjust its width based on the viewport size.
Media queries are used to apply different styles based on the size of the viewport or device.
You can set specific breakpoints for different screen sizes.
Example CSS:
This media query applies styles to screens that are 768px wide or smaller (common for
tablets).
Breakpoints define specific screen sizes at which the layout or style of the page should
change to ensure it looks good on various devices.
Common Breakpoints:
o Extra small devices (phones, <576px): @media (max-width: 575px)
o Small devices (tablets, 576px - 767px): @media (min-width: 576px) and
(max-width: 767px)
o Medium devices (desktops, 768px - 991px): @media (min-width: 768px)
and (max-width: 991px)
o Large devices (large desktops, >992px): @media (min-width: 992px)
/* Small screens */
@media (max-width: 767px) {
.container {
padding: 10px;
}
}
This structure ensures that the layout adapts for different screen sizes, making your site
responsive and user-friendly on all devices.
These concepts and examples form the foundation of responsive web design using fluid
layouts and media queries.