CSS - Styling and Layout (Including Responsive Design Using Flexbox_Grid)
CSS - Styling and Layout (Including Responsive Design Using Flexbox_Grid)
5. Flexbox
1. CSS Basics
Selectors
CSS Selectors are patterns used to target HTML elements to apply styles.
● Basic Selectors:
● Child Selector (div > p {}) – Targets direct children <p> of <div>.
● Adjacent Sibling (h1 + p {}) – Targets the <p> immediately after <h1>.
●
●
● Font Properties:
font-size: Defines the size of the text, using units like px, em, rem, vw, etc.
css
CopyEdit
p { font-size: 16px; }
○
○
○
● Text Properties:
○
○
text-transform: Changes case (uppercase, lowercase, capitalize).
css
CopyEdit
p { text-transform: uppercase; }
○
○
3. Box Model
Every element in HTML can be visualized as a rectangular box using the CSS Box Model,
consisting of four layers:
● Margin: The space outside the border, separating the element from others.
●
●
●
margin: Space outside the box between the element and others.
css
CopyEdit
div { margin: 10px; }
●
Box-Sizing:
The box-sizing property can be used to include padding and border in the element's width
and height calculation.
css
CopyEdit
div {
box-sizing: border-box;
}
●
Positioning:
● Static (default): Elements are positioned based on the normal flow of the document.
●
Absolute: The element is positioned relative to its nearest positioned ancestor (anything that
isn’t static).
css
CopyEdit
div { position: absolute; top: 50px; left: 50px; }
●
Fixed: The element is positioned relative to the browser window and doesn’t scroll with the rest
of the page.
css
CopyEdit
div { position: fixed; bottom: 0; right: 0; }
●
●
Display:
Block: Elements take up the full width available.
css
CopyEdit
div { display: block; }
●
Inline: Elements take up only as much width as necessary and sit next to other elements.
css
CopyEdit
span { display: inline; }
●
Inline-Block: Elements act like inline elements but maintain their block-level structure.
css
CopyEdit
div { display: inline-block; }
●
● Flex and Grid: More advanced display modes, discussed below.
Flexbox is a one-dimensional layout model that lets you align elements horizontally or vertically
with flexibility and control.
○
○
○
○
● Item Properties:
flex-grow: Specifies how much a flex item should grow relative to others.
css
CopyEdit
.item { flex-grow: 1; }
○
○
○
○
CSS Grid is a two-dimensional layout system that lets you design both rows and columns
simultaneously.
○
○
○
○
● Item Properties:
○
○
7. Responsive Design
Responsive design ensures that a website looks good on all devices. It’s achieved using
techniques like media queries, relative units, and flexible grid layouts.
Media Queries:
Media queries apply different styles based on screen size, resolution, or orientation.
css
CopyEdit
@media (max-width: 768px) {
.container { flex-direction: column; }
}
Relative Units:
Use relative units like em, rem, %, vh, and vw instead of fixed units (px).
●
●
●
9. CSS Best Practices and Optimization
● Follow BEM (Block Element Modifier) for CSS structure and naming.
● Use preprocessors like SASS or LESS to write maintainable and scalable CSS.
By following this structured learning, you'll become proficient in CSS3, including Flexbox, Grid,
and responsive design, allowing you to tackle any design challenges with confidence.
The CSS guide I provided covers most of the fundamental and advanced topics needed to
master modern web design. However, there are a few specialized areas and additional
concepts that I haven’t covered in detail, which can further enhance your CSS expertise:
1. CSS Functions
calc() Function: Used to perform calculations to dynamically adjust the size of elements.
css
CopyEdit
div { width: calc(100% - 50px); }
●
clamp() Function: Allows setting a value that adjusts based on viewport size with a minimum
and maximum limit.
css
CopyEdit
div { font-size: clamp(1rem, 2vw, 3rem); }
●
min() and max(): Return the smallest or largest value from a list of values.
css
CopyEdit
div { width: min(50%, 400px); }
●
●
Fluid Layouts: Creating layouts that adapt fluidly without relying solely on fixed breakpoints,
using percentage-based widths, max-width, and min-width.
css
CopyEdit
div { width: 80%; max-width: 1200px; min-width: 300px; }
●
These properties help in writing styles that can adapt to different languages (like right-to-left
text).
●
padding-block-start, padding-block-end: Padding for top and bottom (block axis) that
adapts to writing mode.
css
CopyEdit
div { padding-block-start: 10px; }
●
● Flexbox Alignment in Complex Scenarios: Dealing with more complex layouts using
nested flexboxes.
○ Control alignment and distribution of flex items within different nested flex
containers.
order: Controls the order of flex items, overriding their natural order in the HTML.
css
CopyEdit
.item { order: 2; }
●
●
This feature is still experimental in some browsers, but it's a powerful tool for creating nested
grids.
7. CSS Shapes
CSS Shapes allow content to wrap around custom shapes instead of the usual box model.
css
CopyEdit
float: left;
shape-outside: circle(50%);
●
This is useful for more dynamic layouts where text can wrap around images or other elements in
a circular or irregular shape.
●
●
●
●
●
You can also apply filters like brightness(), contrast(), grayscale(), and sepia().
●
This is particularly useful for languages like Chinese, Japanese, and Korean or for creating
more creative text layouts.
● Houdini is a set of APIs that allow developers to write custom CSS that can directly
affect the browser's CSS engine.
● You can create custom properties, paint custom elements, or even write your own
layout engine.
○ Tailwind CSS: Utility-first framework where you apply predefined classes to build
custom designs.
Creating websites that are accessible to all users, including those with disabilities, is crucial.
●
Visible Text Contrast: Ensuring enough contrast between text and its background for
readability.
css
CopyEdit
p { color: #333; background-color: #fff; }
●
Prefers Reduced Motion: Respect user settings for reduced motion (like turning off
animations).
css
CopyEdit
@media (prefers-reduced-motion: reduce) {
* { animation: none; }
1. Bootstrap
What is Bootstrap?
html
CopyEdit
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.
min.css" rel="stylesheet">
bash
CopyEdit
html
CopyEdit
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.
min.css" rel="stylesheet">
<title>Bootstrap Example</title>
</head>
<body>
<div class="container">
</div>
</body>
</html>
html
CopyEdit
<div class="container">
<div class="row">
</div>
</div>
Responsive Grids:
You can create responsive layouts using different breakpoints (sm, md,
lg, xl, xxl):
html
CopyEdit
<div class="row">
</div>
Buttons:
html
CopyEdit
<button type="button" class="btn btn-primary">Primary Button</button>
1.
Navbar:
html
CopyEdit
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<span class="navbar-toggler-icon"></span>
</button>
<ul class="navbar-nav">
</li>
</ul>
</div>
</nav>
2.
Modals:
html
CopyEdit
<button type="button" class="btn btn-primary" data-bs-toggle="modal"
data-bs-target="#exampleModal">
Launch Modal
</button>
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
</div>
</div>
</div>
</div>
3.
●
@import "bootstrap/scss/bootstrap";
●
Bootstrap Utilities:
Bootstrap provides many utility classes for spacing, typography,
visibility, and flexbox/grid utilities.
●
<div>Item 1</div>
<div>Item 2</div>
</div>
●
2. Tailwind CSS
1.
theme: {
extend: {
colors: {
primary: '#1DA1F2',
},
},
},
};
2.
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body class="bg-gray-100">
</div>
</body>
</html>
3.
1.
Spacing Utilities:
html
CopyEdit
<div class="p-4 m-2">Padded and Margined Element</div>
2.
Flexbox Layout:
html
CopyEdit
<div class="flex justify-between">
</div>
3.
Example:
html
CopyEdit
Responsive Element
</div>
The element changes its background color based on the screen size.
theme: {
extend: {
colors: {
primary: '#3490dc',
},
spacing: {
'72': '18rem',
'84': '21rem',
},
},
},
};
1.
purge: ['./src/**/*.html'],
theme: {},
variants: {},
plugins: [],
};
2.
Tailwind Plugins
Tailwind has a rich plugin ecosystem that extends the framework’s core
functionality. For example, you can use Tailwind Forms to style form
elements:
bash
CopyEdit
js
CopyEdit
// tailwind.config.js
module.exports = {
plugins: [
require('@tailwindcss/forms'),
],
};
html
CopyEdit
Example:
html
CopyEdit
</div>
Conclusion
Bootstrap:
Tailwind CSS:
● Ideal for those who want full control over their designs.