Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A Quiz
🧱 Introduction: Laying
the Foundation for Layout
So far, you’ve built structured HTML and styled it with CSS.
But web pages aren’t just about color and font—they’re about layout:
where content sits, how it flows, how it adjusts across devices.
This chapter dives into page layout fundamentals,
including:
Mastering layout is a critical skill for front-end
development and design precision.
🧱 Part 1: The CSS Box
Model — Every Element is a Box
At the core of every element in HTML is the box model—which
governs its dimensions and space around it.
🔹 Box Model Components:
|
Part |
Description |
|
Content |
The actual text/image
inside the box |
|
Padding |
Space between
content and border |
|
Border |
Line around the
padding/content |
|
Margin |
Space outside
the element to separate it from others |
📦 Visualized:
rust
|<- margin ->|
|<-- border -->|
|<-- padding -->|
| Content |
🔸 Sample CSS
css
.box
{
width: 300px;
padding: 20px;
border: 2px solid black;
margin: 30px;
}
✅ Use browser dev tools to see
padding, margin, and borders highlighted visually.
🖼️ Part 2: Understanding
display Property
The display property defines how an element behaves
in the layout flow.
🔹 Main Display Types:
|
Value |
Description |
Used For |
|
block |
Starts on a new line,
takes full width |
<div>,
<p>, <section> |
|
inline |
Stays in line
with text |
<span>,
<a>, <b> |
|
inline-block |
Like inline but
accepts height/width |
Button-style elements |
|
none |
Hides the
element |
Conditionally
visible UI |
🔸 Example:
css
.block
{
display: block;
}
.inline
{
display: inline;
}
HTML:
html
<p
class="block">Block Element</p>
<span
class="inline">Inline Element</span>
✅ Use inline-block for horizontal
buttons with paddings and sizes.
🎯 Part 3: Positioning —
Move It Where You Want It
CSS position property determines how elements are placed
on a page.
🔹 Positioning Types:
|
Value |
Behavior |
When to Use |
|
static |
Default flow |
Normal layout |
|
relative |
Moves element
relative to itself |
Minor
adjustments |
|
absolute |
Positions element relative
to nearest positioned parent |
Modals, dropdowns |
|
fixed |
Stays fixed
to viewport |
Sticky
headers, floating buttons |
|
sticky |
Switches between
relative and fixed |
Sticky navbars |
🔸 Code Sample:
css
.relative-box
{
position: relative;
top: 10px;
left: 20px;
}
.absolute-box
{
position: absolute;
top: 0;
right: 0;
}
.fixed-box
{
position: fixed;
bottom: 10px;
right: 10px;
}
HTML:
html
<div
class="relative-box">I'm shifted relative to myself.</div>
<div
class="absolute-box">I'm positioned absolutely.</div>
<div
class="fixed-box">I'm always in the corner.</div>
⚠️ Make sure to have a position:
relative parent when using absolute.
🔄 Part 4: Creating Page
Layouts with CSS
Let’s build a common layout:
✅ HTML Structure:
html
<body>
<header>Site Header</header>
<nav>Navigation Bar</nav>
<main>
<aside>Sidebar</aside>
<section>Main Content</section>
</main>
<footer>Footer Area</footer>
</body>
✅ CSS Styling:
css
header,
footer {
background: #222;
color: white;
text-align: center;
padding: 1em;
}
nav
{
background: #444;
padding: 1em;
color: white;
}
main
{
display: flex;
gap: 20px;
}
aside
{
width: 25%;
background: #f0f0f0;
padding: 1em;
}
section
{
width: 75%;
background: #fff;
padding: 1em;
}
📐 Part 5: Best Practices
for Clean Layouts
css
*
{
box-sizing: border-box;
}
🧪 Part 6: Mini Projects
🔹 Card Layout
html
<div
class="card">
<img src="image.jpg">
<h3>Card Title</h3>
<p>Description</p>
</div>
css
.card
{
width: 300px;
border: 1px solid #ddd;
padding: 15px;
margin: 10px;
display: inline-block;
}
🔹 Sticky Navbar
css
nav
{
position: sticky;
top: 0;
background: white;
z-index: 10;
}
📊 Summary Table: Key
Layout Properties
|
Property |
Purpose |
Example |
|
display |
Flow control (block,
inline, flex) |
display: flex; |
|
position |
Element
placement |
position:
absolute; |
|
top, right, bottom,
left |
Offset from edge |
top: 20px; |
|
margin |
External
spacing |
margin: 10px; |
|
padding |
Internal spacing |
padding: 15px; |
|
box-sizing |
Size
calculation |
box-sizing:
border-box; |
📍 Chapter 3 Summary
You’ve now learned:
A: HTML (HyperText Markup Language) is used to structure the content of a webpage—such as text, headings, links, and images. CSS (Cascading Style Sheets) is used to style that content—like setting colors, fonts, spacing, and layout.
A: No special software is required. You can use any text editor like Visual Studio Code, Sublime Text, or even Notepad. All modern browsers can render your HTML and CSS code.
A: Yes, you can build a static website using only HTML and CSS. However, for interactive elements like animations or forms with validation, you'll eventually need JavaScript.
A: After saving your file with a .html extension, simply double-click it or right-click and select “Open with” → your browser (e.g., Chrome, Firefox, Edge).
A: The best practice is to use an external CSS file (linked with a <link> tag in the HTML <head>). This makes your code modular, clean, and easier to maintain.
A: Yes. HTML and CSS provide the structural and visual foundation of web development. Learning them first helps you understand how websites are built before adding interactivity with JavaScript.
A: Use CSS media queries, flexbox, and relative units like percentages or em/rem to make your layout adapt to different screen sizes (e.g., desktop, tablet, mobile).
A: Yes! Platforms like GitHub Pages, Netlify, and Vercel allow you to host static sites (HTML/CSS/JS) for free with custom domains and version control.
A: An id targets a unique element using #idname, while a class can be reused across multiple elements using .classname. IDs should be unique per page; classes can apply styles to multiple elements.
A: Websites like CodePen, Dribbble, Frontend Mentor, and CSS-Tricks offer great examples, templates, and challenges to inspire and improve your front-end design skills.
Please log in to access this content. You will be redirected to the login page shortly.
Login
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Comments(0)