Mada za sehemu hiiFormatting web pages with Cascading Style Sheets (CSS)Mada 5
- Managing colours in CSS
- Implementing typography in CSS
- Implementing the box model in CSS
- Implementing page layout techniques
- Implementing graphics in webpages using CSS
Implementing Page Layout Techniques
CSS provides several techniques for arranging elements on a webpage. These layout methods control how content is positioned, aligned, and distributed across different screen sizes. Understanding these techniques allows you to create organized and visually appealing web pages.
CSS Grid is a two-dimensional layout system that arranges elements in rows and columns. It is ideal for creating complex page layouts with precise control over both dimensions.
Key Properties
display: grid;— activates Grid layout on a containergrid-template-columns— defines the number and width of columnsgrid-template-rows— defines the number and height of rowsgap— sets spacing between grid items
Example: Creating a Simple Grid
.container {
display: grid;
grid-template-columns: 200px 1fr 200px;
grid-template-rows: auto 1fr auto;
gap: 20px;
}
This creates a three-column layout with a 200px sidebar on each side and flexible center content.
Flexbox (Flexible Box Layout) arranges elements in a single row or column. It is excellent for aligning items and distributing space along a single axis.
Key Properties
display: flex;— activates Flexbox on a containerflex-direction— specifies the direction (row or column)justify-content— aligns items along the main axisalign-items— aligns items along the cross axis
Example: Centering Content
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
}
This creates a navigation bar with items spaced evenly and vertically centered.
The float property moves an element to the left or right, allowing text and other content to flow around it. This is a traditional technique useful for wrapping text around images.
Key Properties
float: left;— moves element to the leftfloat: right;— moves element to the rightclear: both;— prevents elements from floating next to floated items
Example: Wrapping Text Around an Image
.image-box {
float: left;
margin-right: 15px;
}
This positions an image on the left with text flowing around it on the right.
Positioning controls how an element is placed relative to its normal position or the viewport.
Types of Positioning
- Static — default positioning; elements follow normal document flow
- Relative — positions element relative to its normal position
- Absolute — positions element relative to the nearest positioned ancestor
- Fixed — positions element relative to the browser window
- Sticky — positions element based on scroll position
Example: Fixed Header
.header {
position: fixed;
top: 0;
width: 100%;
background-color: #333;
color: white;
}
This creates a header that stays at the top of the page even when scrolling.
Below is a simple webpage layout for a school notice board using multiple techniques:
<!DOCTYPE html>
<html>
<head>
<style>
.container {
display: grid;
grid-template-columns: 250px 1fr;
gap: 20px;
max-width: 1000px;
margin: 0 auto;
}
.sidebar {
background-color: #f4f4f4;
padding: 20px;
}
.main-content {
display: flex;
flex-direction: column;
gap: 15px;
}
.notice {
border: 1px solid #ccc;
padding: 15px;
position: relative;
}
.notice-title {
position: sticky;
top: 0;
background: white;
font-weight: bold;
}
</style>
</head>
<body>
<div class="container">
<div class="sidebar">
<h3>Menu</h3>
<ul>
<li>Home</li>
<li>Notices</li>
<li>Events</li>
</ul>
</div>
<div class="main-content">
<div class="notice">
<div class="notice-title">Exam Schedule</div>
<p>Mid-term exams begin next Monday.</p>
</div>
<div class="notice">
<div class="notice-title">Sports Day</div>
<p>Annual sports day will be held at the stadium.</p>
</div>
</div>
</div>
</body>
</html>
This example combines:
- Grid for the main two-column layout
- Flexbox for stacking notice cards vertically
- Sticky positioning for keeping notice titles visible while scrolling
| Technique | Best For | Dimension |
|---|---|---|
| CSS Grid | Page layouts, complex structures | Two-dimensional |
| Flexbox | Navigation bars, card layouts | One-dimensional |
| Float | Text wrapping around images | Simple layouts |
| Positioning | Fixed headers, overlays | Precise placement |
In Tanzania, small businesses such as dukas, restaurants, and mobile phone repair shops can use these CSS layout techniques to create simple websites showcasing their products and services. For example, a mama lishe (food vendor) in Dar es Salaam could use a Grid layout to display her menu items in an organized menu board, with Flexbox to arrange contact information and social media links, making it easy for customers to find her location and place orders.
Jinsi utakavyotathminiwaTathmini ya mchakato · Tathmini ya matokeo · Tathmini ya maarifa
Tathmini ya mchakato
- Apply layout techniques using CSS Grid and Flexbox.
- Use float and clear properties for layout.
- Apply CSS positioning (relative, absolute, fixed).
- Test page layout responsiveness.
Tathmini ya matokeo
Web pages displaying structured and responsive layouts that adapt to different screen sizes are implemented.
Tathmini ya maarifa
Methods used: The student should explain how to:
- implement page layout techniques in CSS.
Principles: The student should explain the principle of:
- responsive web design.
Theories: The student should explain:
- layout models (Grid, Flexbox)
Circumstantial knowledge:
Detailed knowledge about:
- media queries
Swali
Which CSS property activates a Grid layout on a container?
Ingia ili kuwasilisha jibu lako na lihesabiwe katika umahiri wako.
Ingia ili kufanya mazoeziMwalimu
Umekwama? Niulize chochote kuhusu mada hii.
Ingia ili kumuuliza Mwalimu wa AI wa Sonza kuhusu swali hili.
Ingia ili kuulizaMajadiliano
Hakuna maswali bado