HTML-CSS: 3-tile layout
HTML-CSS : Exercise-36 with Solution
Using HTML, CSS aligns items horizontally using display: inline-block to create a 3-tile layout.
- Use display: inline-block to create a tiled layout, without using float, flex or grid.
- Use width in combination with calc to divide the width of the container evenly into 3 columns.
- Set font-size for .tiles to 0 to avoid whitespace and to 20px for <h2> elements to display the text.
- Note: If you use relative units (e.g. em), using font-size: 0 to fight whitespace between blocks might cause side effects.
HTML Code:
<!-- License: https://bit.ly/3GjrtVF -->
<!DOCTYPE html><!-- HTML: Declares the document type and version of HTML -->
<html><!-- HTML: Indicates the start of the HTML document -->
<head><!-- HTML: Contains meta-information about the HTML document -->
<meta charset="utf-8"><!-- HTML: Declares the character encoding for the document -->
<meta name="viewport" content="width=device-width"><!-- HTML: Sets the viewport width to the width of the device -->
<title>Using HTML, CSS aligns items horizontally using display: inline-block to create a 3-tile layout</title><!-- HTML: Sets the title of the document -->
</head><!-- HTML: End of the head section -->
<body><!-- HTML: Contains the content of the HTML document -->
<div class="tiles"><!-- HTML: Defines a container for the tiles -->
<div class="tile"><!-- HTML: Defines a tile element -->
<img src="https://www.w3resource.com/html-css-exercise/html-css-practical-exercises/flower-7.jpeg" width="200" height="150"><!-- HTML: Inserts an image into the tile -->
<h2>Rose-1</h2><!-- HTML: Adds a heading to the tile -->
</div>
<div class="tile"><!-- HTML: Defines another tile element -->
<img src="https://www.w3resource.com/html-css-exercise/html-css-practical-exercises/flower-8.jpeg" width="200" height="150"><!-- HTML: Inserts an image into the tile -->
<h2>Rose-2</h2><!-- HTML: Adds a heading to the tile -->
</div>
<div class="tile"><!-- HTML: Defines another tile element -->
<img src="https://www.w3resource.com/html-css-exercise/html-css-practical-exercises/flower-9.jpeg" width="200" height="150"><!-- HTML: Inserts an image into the tile -->
<h2>Dahlia</h2><!-- HTML: Adds a heading to the tile -->
</div>
</div>
</body><!-- HTML: End of the body section -->
</html><!-- HTML: End of the HTML document -->
Explanation:
- The HTML document starts with the <!DOCTYPE html> declaration, which specifies the document type and version of HTML.
- <html> tags indicate the start and end of the HTML document.
- <head> section contains meta-information about the HTML document, such as character encoding and viewport settings.
- The <title> tag sets the title of the document, visible in the browser tab.
- <body> section contains the content of the HTML document.
- <div class="tiles"> defines a container for the tiles.
- Each tile is represented by <div class="tile"> and contains an image (<img>) and a heading (<h2>).
- The <img> tags display images of flowers with specified width and height attributes.
- The <h2> tags display headings for each tile.
CSS Code:
/* CSS: Sets the width of the container for the tiles to 600 pixels */
.tiles {
width: 600px;
/* CSS: Sets the font size to 0 to remove any potential whitespace between inline-block elements */
font-size: 0;
/* CSS: Centers the container horizontally */
margin: 0 auto;
}
/* CSS: Styles for individual tiles */
.tile {
/* CSS: Sets the width of each tile to one-third of the container's width */
width: calc(600px / 3);
/* CSS: Displays tiles as inline blocks, allowing them to appear horizontally */
display: inline-block;
}
/* CSS: Styles for headings within tiles */
.tile h2 {
/* CSS: Sets the font size of tile headings to 20 pixels */
font-size: 20px;
}
Explanation:
- .tiles:
- Sets the width of the container for the tiles to 600 pixels.
- Sets the font size to 0 to remove any potential whitespace between inline-block elements.
- Centers the container horizontally using auto margins.
- .tile:
- Sets the width of each tile to one-third of the container's width using the calc() function.
- Displays tiles as inline blocks, allowing them to appear horizontally next to each other.
- .tile h2:
- Sets the font size of headings within tiles to 20 pixels.
HTML-CSS Editor:
See the Pen html-css-practical-exercises by w3resource (@w3resource) on CodePen.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics