Task: Style a simple content box with a title and a paragraph, using REM units for sizing, experiment with the box-sizing property, and observe changes when modifying the root font size.
Requirements:
- Create a
divelement containing ah1title and apparagraph. - Use
remunits to set margin, padding, and border for thediv. - Apply
box-sizing: content-boxto thediv, and observe the calculation of the total size (content width/height plus padding and border). - Change
box-sizingtoborder-boxand note how padding and border are included within the width/height. - Adjust the
div's width, height, padding, and border sizes inremunits and see the changes. - After experimenting with
box-sizing, modify thefont-sizeproperty of the HTML element (root element) in your CSS. Try different sizes, such as16px,20px, or24px. - Observe how the size of the box, including its padding and margin, changes in response to the root font-size adjustment. This illustrates how
remunits are relative to the HTML element's font size. - This exercise helps students understand the direct impact of changing the root element's font-size on elements styled with
remunits, and the difference in element sizing with variousbox-sizingvalues.
Including this step will give students a more comprehensive understanding of how rem units work in relation to the root font size and how box-sizing can affect element dimensions. This knowledge is crucial for mastering responsive design principles.
Task: Create a basic horizontal navigation menu using an unordered list, styled with inline-block display.
Requirements:
- An unordered list
<ul>with several list items<li>. - Apply
display: inline-blockto the list items to align them horizontally. - Style the list items with padding and margin, using
pxoremunits.
Task: Display a row of images with captions, setting image widths relative to the parent container's width.
Requirements:
- Multiple
divelements, each containing animgelement and apcaption. - Use percentage (
%) units to set the width of each image, demonstrating responsive design. - Align images horizontally using
display: blockand applyingfloat: leftto eachdiv.
Task: Create a simple page layout with a header, content area, and a footer using relative positioning.
Requirements:
- Separate
header,main(for content), andfooterelements. - Use
position: relativefor the footer, and demonstrate how it interacts with other elements. - Experiment with
margin-topin percentage (%) to control the footer's position relative to the content.
Task: Create several boxes with different background colors, changing color on hover, using viewport units for sizing.
Requirements:
- Multiple
divelements, each styled with a different background color. - Use the
:hoverpseudo-class to change the background color when the mouse hovers over a box. - Apply viewport units (
vwfor width,vhfor height) to adapt the box sizes to different screen sizes.
Certainly! Let's create exercises specifically focusing on sticky and fixed positioning in CSS. These exercises will help understand how these positioning properties can be applied in different scenarios.
Task: Create a navigation bar that becomes sticky at the top of the page when you scroll down.
Requirements:
- A
headersection with some introductory content. - A
navbar with a few menu items, initially positioned normally in the flow of the document. - Apply
position: stickyto thenavbar and settop: 0, so it sticks to the top of the viewport when scrolled past its initial position. - Add enough content below the
navbar to allow for scrolling.
Task: Design a page layout with a sidebar that stays fixed on the side of the screen as the user scrolls.
Requirements:
- A
divelement representing the main content area and adivfor the sidebar. - Use
position: fixedon the sidebar and set it to align to the left or right of the viewport. - The main content
divshould be scrollable, with enough content to demonstrate the fixed position of the sidebar. - Style the sidebar with a different background color to distinguish it from the main content.
Task: Create a footer that sticks to the bottom of the viewport but lifts up when scrolling to the bottom of the page.
Requirements:
- A
maincontent area with sufficient content to enable vertical scrolling. - A
footerelement styled withposition: stickyand placed at the bottom (bottom: 0). - Ensure there's an offset (like
margin-top) for thefooter, so it appears above the end of the main content when scrolled to the bottom. - The footer should "stick" to the bottom of the viewport on scroll but should not overlap the main content when reaching the bottom of the page.
These exercises provide hands-on experience with sticky and fixed positioning, illustrating how they function differently and can be utilized in web layouts.