HTML Code:
<!DOCTYPE html><!-- Define document type as HTML -->
<html><!-- Begin HTML document -->
<head><!-- Start of document header -->
<meta charset="utf-8"><!-- Specify character encoding -->
<title>How to center items at the center of the container</title><!-- Title of the HTML page -->
</head><!-- End of document header -->
<body><!-- Start of document body -->
<div id="xyz"><!-- Start of div with id "xyz" -->
<p>align-content: center</p><!-- Paragraph indicating the alignment content property -->
<div id="w3r"><!-- Start of div with id "w3r" -->
<div></div><!-- First nested div -->
<div></div><!-- Second nested div -->
<div></div><!-- Third nested div -->
<div></div><!-- Fourth nested div -->
<div></div><!-- Fifth nested div -->
<div></div><!-- Sixth nested div -->
</div><!-- End of div with id "w3r" -->
</div><!-- End of div with id "xyz" -->
<style type="text/css">/*Begin CSS styling */
#xyz > div { /* CSS rule for div elements directly under the div with id "xyz" */
display: -webkit-flex; /* Use flexbox layout for Safari browsers */
display: -ms-flex; /* Use flexbox layout for Microsoft browsers */
display: flex; /* Use flexbox layout for other browsers */
-webkit-flex-flow: row wrap; /* Set flex direction to row and allow wrapping for Safari */
flex-flow: row wrap; /* Set flex direction to row and allow wrapping for other browsers */
width: 140px; /* Set width of the div to 140 pixels */
height: 140px; /* Set height of the div to 140 pixels */
background: #CC0000; /* Set background color of the div */
}
#xyz > div > div { /* CSS rule for div elements directly under the divs under the div with id "xyz" */
margin: 2px; /* Set margin around the divs */
width: 30px; /* Set width of the divs to 30 pixels */
min-height: 30px; /* Set minimum height of the divs to 30 pixels */
background: #FFCC99; /* Set background color of the divs */
}
#w3r { /* CSS rule for the div with id "w3r" */
-webkit-align-content: center; /* Align content to center for Safari */
align-content: center; /* Align content to center for other browsers */
}
</style><!-- End of CSS styling -->
</body><!-- End of document body -->
</html><!-- End of HTML document -->
Explanation:
Live Demo:
See the Pen align-content-center-answer by w3resource (@w3resource) on CodePen.
See the solution in the browser
Supported browser
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4