HTML-CSS: Responsive layout with sidebar
HTML-CSS : Exercise-35 with Solution
Using HTML, CSS creates a responsive layout with a content area and a sidebar.
- Use display: grid on the parent container, to create a grid layout.
- Use minmax() for the second column (sidebar) to allow it to take up between 150px and 20%.
- Use 1fr for the first column (main content) to take up the rest of the remaining space.
HTML Code:
Explanation:
- The <head> section contains metadata about the HTML document, such as character encoding and viewport settings.
- The <title> tag sets the title of the HTML document.
- The <body> section contains the visible content of the HTML document.
- Inside the <body> section, a <div> element with the class "w3r-container" is used to create a container for the layout.
- The <main> tag defines the main content area of the layout.
- The <aside> tag defines the sidebar content.
- Text content inside the <main> and <aside> tags provides placeholder content for demonstration purposes.
CSS Code:
Explanation:
- The .w3r-container class defines the styles for the container element using CSS grid layout.
- grid-template-columns property specifies the layout of the grid columns, where the first column takes up the remaining space (1fr) and the second column has a minimum width of 25% and a maximum width of 150px (minmax(25%, 150px)).
- height property sets the height of the container to 100px.
- main and aside selectors apply styles to the main content area and the sidebar, respectively.
- Both main and aside elements have padding and text alignment set to center.
- Background colors are set for main and aside elements to distinguish them visually.
Go to:
PREV : Responsive image mosaic.
NEXT : 3-tile layout.
HTML-CSS Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.