Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Properties

CSS place-content Property

Posted in CSS Tutorial
Updated on Oct 07, 2024
By Mari Selvan
👁️ 19 - Views
⏳ 4 mins
💬 1 Comment
CSS place-content Property

Photo Credit to CodeToFun

🙋 Introduction

The place-content property in CSS is a shorthand property used to align and distribute content within a container. It combines the functionality of align-content and justify-content properties into one.

This property is particularly useful for controlling the alignment of items along both the block and inline axes in a grid or flex container.

💡 Syntax

The place-content property can accept one or two values. When two values are provided, the first value applies to the block axis (vertical alignment), and the second value applies to the inline axis (horizontal alignment).

Syntax
Copied
Copy To Clipboard
element {
  place-content: align-content justify-content;
}
  • align-content: Specifies the alignment of the content along the block (vertical) axis.
  • justify-content: Specifies the alignment of the content along the inline (horizontal) axis.

🎛️ Default Value

The default value of the place-content property is stretch stretch, which means that the content is stretched to fit both axes by default.

🏠 Property Values

ValueDescription
startAligns the content to the start of the container along the respective axis.
endAligns the content to the end of the container along the respective axis.
centerCenters the content along the respective axis.
space-betweenDistributes the content evenly with space between the items along the respective axis.
space-aroundDistributes the content evenly with space around the items along the respective axis.
space-evenlyDistributes the content evenly with equal space around and between the items along the respective axis.
stretchStretches the content to fill the container along the respective axis (default value).

📄 Example

In this example, we align the content of a grid container to the center both vertically and horizontally.

index.html
Copied
Copy To Clipboard
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS place-content Example</title>
  <style>
    .container {
      display: grid;
      height: 200px;
      place-content: center;
      border: 2px solid #333;
    }
    .item {
      background-color: #ff5733;
      color: #fff;
      padding: 20px;
      text-align: center;
    }
  </style>
</head>
<body>
  <h1>Grid Container with Centered Content</h1>
  <div class="container">
    <div class="item">Centered Item</div>
  </div>
</body>
</html>

Example with Different Values

In this example, we use place-content: space-between center; to distribute content evenly with space between items vertically and center align them horizontally.

index.html
Copied
Copy To Clipboard
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS place-content Example</title>
  <style>
    .container {
      display: grid;
      height: 200px;
      place-content: space-between center;
      border: 2px solid #333;
    }
    .item {
      background-color: #33c1ff;
      color: #fff;
      padding: 20px;
      text-align: center;
    }
  </style>
</head>
<body>
  <h1>Grid Container with Space Between and Centered Content</h1>
  <div class="container">
    <div class="item">Item 1</div>
    <div class="item">Item 2</div>
  </div>
</body>
</html>

🖥️ Browser Compatibility

The place-content property is well-supported in modern browsers. However, always check the latest compatibility tables to ensure that it works as expected across different browsers and their versions.

🎉 Conclusion

The place-content property is a versatile tool for aligning and distributing content within a container.

By combining the capabilities of align-content and justify-content, it allows for easy and precise control over the layout of grid and flex items. Experiment with different values to achieve the desired alignment and distribution in your web projects.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy