w3resource

CSS Properties: How to center items at the center of the container?

Go to Exercise page

Solution:

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:

  • The HTML code creates a container div with the ID "xyz" and nested divs within it.
  • CSS is used to style the container div and its child divs, including setting width, height, background color, and flexbox properties.
  • Flexbox properties are applied to center the child divs within the container div using the align-content: center; property.
  • The align-content property is set to "center" for both Safari and other browsers to ensure the child divs are centered horizontally and vertically within the container.

Live Demo:

See the Pen align-content-center-answer by w3resource (@w3resource) on CodePen.


See the solution in the browser

Supported browser

Firefox logo Chrome logo Opera logo Safari logo Internet Explorer logo
Yes Yes Yes No No

Go to Exercise page

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/html-css-exercise/css-properties/align-content-center-answer.php