7.3 Homework/Practice
Build a sidebar layout using float. Apply relative positioning to the sidebar and absolute positioning to a menu button inside the sidebar.
Build a Sidebar Layout Using Float
Step 1:
Create an HTML structure for a page with a header, sidebar, main content area, and footer. Use
floatto position the sidebar on the left and the content on the right.<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Sidebar Layout</title> <style> body { font-family: Arial, sans-serif; } header, footer { background-color: #333; color: white; text-align: center; padding: 10px; } .sidebar { float: left; width: 25%; background-color: #f4f4f4; padding: 10px; box-sizing: border-box; } .content { float: left; width: 70%; padding: 10px; box-sizing: border-box; background-color: #ddd; } footer { clear: both; background-color: #333; color: white; text-align: center; padding: 10px; } </style> </head> <body> <header> <h1>My Sidebar Layout</h1> </header> <div class="sidebar"> <h2>Sidebar</h2> <p>This is the sidebar content.</p> </div> <div class="content"> <h2>Main Content</h2> <p>This is the main content area.</p> </div> <footer> <p>Footer Content</p> </footer> </body> </html>
Step 2:
Apply Relative Positioning: Apply relative positioning to the sidebar to move it slightly to the right.
.sidebar { position: relative; left: 10px; }
Step 3:
Absolute Positioning for Menu Button: Add a button inside the sidebar and use absolute positioning to place it at the bottom-right corner of the sidebar:
<button style="position: absolute; bottom: 10px; right: 10px;">Menu</button>
Step 4:
Test Responsiveness: Adjust the layout for smaller screens using media queries to ensure the sidebar becomes a full-width block at smaller sizes.
These activities and homework will help students understand how to position and display elements effectively in a web
Last updated