CSS Properties
CSS place-items Property
Photo Credit to CodeToFun
🙋 Introduction
The place-items
property in CSS is a shorthand for setting both the align-items and justify-items properties in a grid or flex container.
This property simplifies the alignment of grid items or flex items within their container, allowing for a cleaner and more concise code. It helps control the positioning of items both horizontally and vertically.
💡 Syntax
The syntax for the place-items
property is as follows:
element {
place-items: align-items justify-items;
}
Here, align-items and justify-items are values that control the alignment of items along the block and inline axes respectively. You can use this property to set these values simultaneously.
🎛️ Default Value
The default value of the place-items
property is stretch stretch. This means that items are stretched to fill the available space in both the block (vertical) and inline (horizontal) directions.
🏠 Property Values
- `align-items` value: Defines the alignment of items along the block (vertical) axis. Common values include:
- start: Align items to the start of the block axis.
- center: Align items to the center of the block axis.
- end: Align items to the end of the block axis.
- stretch: Stretch items to fill the container along the block axis.
- `justify-items` value: Defines the alignment of items along the inline (horizontal) axis. Common values include:
- abc: Align items to the start of the inline axis.
- abc: Align items to the center of the inline axis.
- abc: Align items to the end of the inline axis.
- abc: Stretch items to fill the container along the inline axis.
📄 Example
In this example, we'll use the place-items
property to center items both horizontally and vertically within a grid container.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS place-items Example</title>
<style>
.container {
display: grid;
height: 200px;
width: 200px;
place-items: center;
border: 1px solid #000;
}
.item {
width: 50px;
height: 50px;
background-color: lightblue;
}
</style>
</head>
<body>
<h1>Grid Item Centered with place-items</h1>
<div class="container">
<div class="item"></div>
</div>
</body>
</html>
In this example, the grid container will center the item both horizontally and vertically within its bounds.
🖥️ Browser Compatibility
The place-items
property is supported in most modern browsers. It is generally compatible with the latest versions of Chrome, Firefox, Safari, Edge, and Opera. However, older browser versions may not fully support this property, so testing across different browsers is recommended to ensure consistent behavior.
🎉 Conclusion
The place-items
property is a convenient shorthand for aligning grid or flex items along both the block and inline axes.
By using this property, you can simplify your CSS and achieve precise alignment with minimal code. Experiment with different values to see how they affect the layout and alignment of your items, and incorporate this property to enhance the design and functionality of 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-items Property), please comment here. I will help you immediately.