Tillitsdone
down Scroll to discover

CSS Place-Self Simplified Guide for Alignment

The CSS place-self property is a powerful tool for aligning individual items in grid and flexbox layouts.

It combines align-self and justify-self properties, offering options like auto, normal, stretch, start, end, center, and more.
thumbnail

Introduction

The CSS place-self property is a powerful shorthand tool for web developers and designers. It lets you align an individual item within a grid or flexbox layout in both the block and inline directions simultaneously. This property combines the functionality of the align-self and justify-self properties, making it easier to control the alignment of elements in your web designs.

By using place-self, you can streamline your CSS code and ensure that elements are positioned correctly within their containers. If only one value is provided, it will be applied to both the block and inline directions. This property is particularly useful in responsive web design, where precise control over element placement is crucial.

Description

The CSS place-self property is a shorthand property that combines the functionalities of the align-self and justify-self properties. It allows you to control the alignment of individual items within a grid or flexbox layout in both the block and inline directions simultaneously.

When you use the place-self property, you can specify how an item should be aligned in the block direction (top to bottom) and the inline direction (left to right). If only one value is provided, it will be applied to both directions. This makes the place-self property a convenient and efficient way to manage the alignment of elements in your web designs.

By using the place-self property, you can ensure that elements are positioned correctly within their containers, making your layouts more visually appealing and easier to manage. This property is especially useful in responsive web design, where precise control over element placement is essential for creating adaptable and user-friendly interfaces.

Constituent Properties

The CSS place-self property is a shorthand for combining two individual properties:

  1. align-self: This property controls the alignment of an individual item in the block direction (top to bottom). It allows you to specify how the item should be aligned within its container in the block direction.
  2. justify-self: This property controls the alignment of an individual item in the inline direction (left to right). It allows you to specify how the item should be aligned within its container in the inline direction.

By combining these two properties into one, the place-self property simplifies the process of aligning items in both directions. This makes it easier to manage the alignment of elements in your web designs, ensuring that they are positioned correctly within their containers.

Syntax

The place-self property in CSS uses a straightforward syntax that allows you to specify alignment values for both the block and inline directions. The syntax is as follows:

place-self: align-self-value justify-self-value;

Syntax Breakdown

  • align-self-value: This value controls the alignment of the item in the block direction (top to bottom).
  • justify-self-value: This value controls the alignment of the item in the inline direction (left to right).

If only one value is provided, it will be applied to both the block and inline directions. This makes the place-self property a convenient way to align items in your layouts.

Example Syntax

Here are some examples of how to use the place-self property:

/* Positional alignment */
place-self: auto center;
place-self: normal start;
place-self: center normal;
place-self: start auto;
place-self: end normal;
place-self: self-start auto;
place-self: self-end normal;
place-self: flex-start auto;
place-self: flex-end normal;
place-self: anchor-center;
/* Baseline alignment */
place-self: baseline normal;
place-self: first baseline auto;
place-self: last baseline normal;
place-self: stretch auto;
/* Global values */
place-self: inherit;
place-self: initial;
place-self: revert;
place-self: revert-layer;
place-self: unset;

Explanation of Values

  • auto: Computes to the parent’s align-items value.
  • normal: Behaves differently based on the layout mode.
  • self-start: Aligns the item to the start edge of the alignment container.
  • self-end: Aligns the item to the end edge of the alignment container.
  • flex-start: Aligns the item to the start of the flex container.
  • flex-end: Aligns the item to the end of the flex container.
  • center: Centers the item within the alignment container.
  • baseline, first baseline, last baseline: Participates in first- or last-baseline alignment.
  • stretch: Stretches the item to fill the alignment container.
  • anchor-center: (For anchor-positioned elements) aligns the item to the center of the associated anchor element.

Formal Syntax

The formal syntax for the place-self property is as follows:

place-self = [<'align-self'>] [<'justify-self'>]?;
<align-self> =
auto |
normal |
stretch |
<baseline-position> |
<overflow-position>? <self-position> |
anchor-center;
<justify-self> =
auto |
normal |
stretch |
<baseline-position> |
<overflow-position>? <self-position> |
anchor-center;
<baseline-position> =
[first | last]? baseline;
<overflow-position> =
unsafe |
safe;
<self-position> =
center |
start |
end |
self-start |
self-end |
flex-start |
flex-end;

This syntax ensures that you can specify the alignment values for both the block and inline directions precisely, making it easier to control the layout of your web designs.

Values

The place-self property in CSS accepts a variety of values that define how an item should be aligned within its container. These values can be specified for both the block and inline directions, allowing you to control the alignment of elements precisely. Below is a detailed explanation of the possible values for the place-self property:

auto

The auto value computes to the parent’s align-items value. This is the default value and is typically used when you want the item to inherit the alignment behavior from its parent container.

normal

The normal value behaves differently based on the layout mode:

  • In absolutely-positioned layouts, it behaves like start on replaced absolutely-positioned boxes and as stretch on all other absolutely-positioned boxes.
  • In static position of absolutely-positioned layouts, it behaves as stretch.
  • For flex items, it behaves as stretch.
  • For grid items, it behaves similarly to stretch, except for boxes with an aspect ratio or intrinsic sizes where it behaves like start.
  • The property does not apply to block-level boxes and table cells.

self-start

The self-start value aligns the item to be flush with the start edge of the alignment container in the cross axis. This is useful for aligning items to the beginning of their container.

self-end

The self-end value aligns the item to be flush with the end edge of the alignment container in the cross axis. This is useful for aligning items to the end of their container.

flex-start

The flex-start value flushes the cross-start margin edge of the flex item with the cross-start edge of the line. This is particularly useful in flexbox layouts.

flex-end

The flex-end value flushes the cross-end margin edge of the flex item with the cross-end edge of the line. This is useful for aligning items to the end of a flex container.

center

The center value centers the item within the line on the cross-axis. If the item’s cross-size is larger than the flex container, it will overflow equally in both directions. This value is commonly used to center items within their containers.

baseline, first baseline, last baseline

These values specify participation in first- or last-baseline alignment:

  • baseline aligns the alignment baseline of the box’s first baseline set with the corresponding baseline in the shared first baseline set of all the boxes in its baseline-sharing group.
  • first baseline aligns the first baseline, with a fallback to start.
  • last baseline aligns the last baseline, with a fallback to end.

stretch

The stretch value increases the size of the item equally (not proportionally) to fill the alignment container along the cross axis if the combined size of the items is less than the size of the container and the item is auto-sized. This is useful for ensuring that items stretch to fill their containers.

anchor-center

In the case of anchor-positioned elements, the anchor-center value aligns the item to the center of the associated anchor element in both the block and inline directions. This is particularly useful in layouts that use anchor positioning.

Global Values

The place-self property also accepts global values that can be applied to any CSS property:

  • inherit: Inherits the value from the parent element.
  • initial: Sets the property to its initial value.
  • revert: Reverts the property to the user agent’s default stylesheet value.
  • revert-layer: Reverts the property to the value specified in the next outer layer of the cascade.
  • unset: Resets the property to its natural value, which means that if the property is naturally inherited, it acts like inherit, otherwise, it acts like initial.

Example Values

Here are some examples of how to use the place-self property with different values:

/* Positional alignment */
place-self: auto center;
place-self: normal start;
place-self: center normal;
place-self: start auto;
place-self: end normal;
place-self: self-start auto;
place-self: self-end normal;
place-self: flex-start auto;
place-self: flex-end normal;
place-self: anchor-center;
/* Baseline alignment */
place-self: baseline normal;
place-self: first baseline auto;
place-self: last baseline normal;
place-self: stretch auto;
/* Global values */
place-self: inherit;
place-self: initial;
place-self: revert;
place-self: revert-layer;
place-self: unset;

Understanding these values and how they affect the alignment of items within their containers is key to effectively using the place-self property in your web development projects.

Formal Definition

The CSS place-self property is a shorthand property that combines the functionalities of the align-self and justify-self properties. This property allows you to control the alignment of individual items within a grid or flexbox layout in both the block and inline directions simultaneously.

Initial Value

The initial value of the place-self property is auto auto, which means that both align-self and justify-self are set to auto.

Applies To

The place-self property applies to:

  • Block-level boxes
  • Absolutely-positioned boxes
  • Grid items

Inherited

The place-self property is not inherited from the parent element.

Computed Value

The computed value of the place-self property is determined as follows:

  • For align-self:
    • On absolutely-positioned elements, it computes to itself.
    • On all other boxes, it computes to the computed value of align-items on the parent (minus any legacy keywords), or start if the box has no parent.
  • For justify-self:
    • It computes to the specified value.

Animation Type

The animation type for the place-self property is discrete, meaning that it does not support smooth transitions between values.

Formal Syntax

The formal syntax for the place-self property is as follows:

place-self = [<'align-self'>] [<'justify-self'>]?;
<align-self> =
auto |
normal |
stretch |
<baseline-position> |
<overflow-position>? <self-position> |
anchor-center;
<justify-self> =
auto |
normal |
stretch |
<baseline-position> |
<overflow-position>? <self-position> |
anchor-center;
<baseline-position> =
[first | last]? baseline;
<overflow-position> =
unsafe |
safe;
<self-position> =
center |
start |
end |
self-start |
self-end |
flex-start |
flex-end;

Example

Here’s an example to illustrate the usage of the place-self property:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Place-Self Example</title>
<style>
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap: 10px;
padding: 10px;
background-color: #f0f0f0;
}
.grid-item {
background-color: #4CAF50;
color: white;
padding: 20px;
text-align: center;
}
.item1 {
place-self: center center;
}
.item2 {
place-self: start end;
}
.item3 {
place-self: stretch auto;
}
</style>
</head>
<body>
<div class="grid-container">
<div class="grid-item item1">Center Center</div>
<div class="grid-item item2">Start End</div>
<div class="grid-item item3">Stretch Auto</div>
</div>
</body>
</html>

In this example, the place-self property is used to align grid items within a grid container. The item1 is centered in both the block and inline directions, item2 is aligned to the start in the block direction and to the end in the inline direction, and item3 is stretched in the block direction and aligned to the start in the inline direction.

By using the place-self property, you can easily control the alignment of individual items within a grid or flexbox layout, making your designs more flexible and visually appealing.

Example

Simple Demonstration

Here, we’ll show how place-self works in a grid layout.

HTML

<article class="container">
<span>First</span>
<span>Second</span>
<span>Third</span>
<span>Fourth</span>
</article>

CSS

html {
font-family: helvetica, arial, sans-serif;
letter-spacing: 1px;
}
article {
background-color: red;
display: grid;
grid-template-columns: 1fr 1fr;
grid-auto-rows: 80px;
grid-gap: 10px;
margin: 20px;
width: 300px;
}
span:nth-child(2) {
place-self: start center;
}
span:nth-child(3) {
place-self: center start;
}
span:nth-child(4) {
place-self: end;
}
article span {
background-color: black;
color: white;
margin: 1px;
text-align: center;
}
article,
span {
padding: 10px;
border-radius: 7px;
}

Result

  • The second item is aligned to the start of the block direction (top) and centered in the inline direction.
  • The third item is centered in the block direction and aligned to the start of the inline direction (left).
  • The fourth item is aligned to the end of both directions (bottom-right corner).

Example with flex-start and flex-end

Here’s an example using place-self in a flexbox layout.

HTML

<div class="container">
<span>Item 1</span>
<span>Item 2</span>
<span>Item 3</span>
</div>

CSS

html {
font-family: helvetica, arial, sans-serif;
letter-spacing: 1px;
}
.container {
background-color: lightblue;
display: flex;
flex-direction: column;
height: 200px;
width: 150px;
padding: 10px;
gap: 10px;
}
span:nth-child(1) {
place-self: flex-start;
}
span:nth-child(2) {
place-self: flex-end;
}
span:nth-child(3) {
place-self: center;
}
.container span {
background-color: darkblue;
color: white;
padding: 10px;
border-radius: 5px;
text-align: center;
}

Result

  • The first item is aligned to the start of the flex container (top).
  • The second item is aligned to the end of the flex container (bottom).
  • The third item is centered within the flex container.

Browser Compatibility

The place-self property is widely supported across modern web browsers. Here’s a quick compatibility table:

BrowserVersionSupported
Google Chrome65Yes
Mozilla Firefox59Yes
Safari11.1Yes
Microsoft Edge16Yes
Opera52Yes

Using Browser Prefixes

To ensure compatibility, you can use browser-specific prefixes:

/* Standard syntax */
place-self: start center;
/* Browser-specific prefixes */
-webkit-place-self: start center; /* For Safari and older Chrome */
-moz-place-self: start center; /* For Firefox */
-ms-place-self: start center; /* For older Edge */
-o-place-self: start center; /* For Opera */

Conclusion

The place-self property is a powerful tool for web developers, offering precise control over the alignment of items within grid and flexbox layouts. By understanding its syntax and values, you can create more flexible and visually appealing web designs.

Always test your website on various browsers and devices to ensure a consistent and functional user experience. For more information on browser compatibility and other CSS properties, check out resources and documentation from browser vendors.

Related Properties

  • align-self: Use this to align a single item within a grid or flexbox layout in the block direction.
  • justify-self: Use this to align a single item within a grid or flexbox layout in the inline direction.
  • Basic concepts of flexbox: This guide gives you an overview of the flexbox layout system and its core ideas. Check it out at WebsiteUrl.
  • Aligning items in a flex container: Learn how to align items within a flex container using various CSS properties. Find more at WebsiteUrl.
  • Box alignment in CSS grid layouts: This guide focuses on the box alignment properties specific to CSS grid layouts. Learn more at WebsiteUrl.
  • CSS box alignment: This module provides detailed information on the CSS box alignment properties and how to use them. Dive deeper at WebsiteUrl.
icons/css-4.svg CSS Blogs
CSS3 is the latest version of Cascading Style Sheets, offering advanced styling features like animations, transitions, shadows, gradients, and responsive design.
icons/logo-tid.svg

Talk with CEO

Ready to bring your web/app to life or boost your team with expert Thai developers?
Contact us today to discuss your needs, and let’s create tailored solutions to achieve your goals. We’re here to help at every step!
🖐️ Contact us
Let's keep in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.