CSS Properties
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).
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
Value | Description |
---|---|
start | Aligns the content to the start of the container along the respective axis. |
end | Aligns the content to the end of the container along the respective axis. |
center | Centers the content along the respective axis. |
space-between | Distributes the content evenly with space between the items along the respective axis. |
space-around | Distributes the content evenly with space around the items along the respective axis. |
space-evenly | Distributes the content evenly with equal space around and between the items along the respective axis. |
stretch | Stretches 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.
<!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.
<!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:
Author
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
If you have any doubts regarding this article (CSS place-content Property), please comment here. I will help you immediately.