CSS CSS Tutorial CSS Advanced CSS Responsive Web Design(RWD) CSS Grid CSS Properties Sass Tutorial Sass Functions



place-self

CSS or Cascading Style Sheets is a language used to describe the presentation of a document written in HTML or XML. It is used to style web pages and make them visually appealing. CSS has a wide range of properties that can be used to manipulate the layout and design of a web page. One such property is place-self.

What is Place-Self?

Place-self is a CSS property that is used to align and position an item within its container. It is a shorthand property that combines the align-self and justify-self properties. The align-self property is used to align an item vertically within its container, while the justify-self property is used to align an item horizontally within its container. Place-self can be used to set both the vertical and horizontal alignment of an item within its container.

The place-self property can take the following values:

  • auto
  • stretch
  • center
  • start
  • end
  • flex-start
  • flex-end
  • baseline
  • first baseline
  • last baseline
  • safe center
  • unsafe center

The default value of place-self is auto, which means that the item will be positioned according to the default alignment of its container.

Examples of Place-Self

Let's take a look at some examples of how place-self can be used to align and position items within their containers.

Example 1: Centering an Item

In this example, we have a container with a fixed height and width. We want to center the item within the container both vertically and horizontally.

  
.container {
  height: 200px;
  width: 200px;
  border: 1px solid black;
  display: flex;
  justify-content: center;
  align-items: center;
  place-self: center;
}

.item {
  height: 50px;
  width: 50px;
  background-color: red;
}
  

In the above code, we have set the height and width of the container to 200px and added a border to it. We have also set the display property to flex and used the justify-content and align-items properties to center the item both vertically and horizontally. Finally, we have used the place-self property to center the item within the container.

Example 2: Aligning an Item to the Start

In this example, we have a container with a fixed height and width. We want to align the item to the start of the container both vertically and horizontally.

  
.container {
  height: 200px;
  width: 200px;
  border: 1px solid black;
  display: flex;
  justify-content: center;
  align-items: center;
  place-self: flex-start;
}

.item {
  height: 50px;
  width: 50px;
  background-color: red;
}
  

In the above code, we have set the height and width of the container to 200px and added a border to it. We have also set the display property to flex and used the justify-content and align-items properties to center the item both vertically and horizontally. Finally, we have used the place-self property to align the item to the start of the container.

Example 3: Stretching an Item

In this example, we have a container with a fixed height and width. We want to stretch the item to fill the entire container both vertically and horizontally.

  
.container {
  height: 200px;
  width: 200px;
  border: 1px solid black;
  display: flex;
  justify-content: center;
  align-items: center;
}

.item {
  height: 100%;
  width: 100%;
  background-color: red;
  place-self: stretch;
}
  

In the above code, we have set the height and width of the container to 200px and added a border to it. We have also set the display property to flex and used the justify-content and align-items properties to center the item both vertically and horizontally. Finally, we have used the place-self property to stretch the item to fill the entire container both vertically and horizontally.

Conclusion

Place-self is a powerful CSS property that can be used to align and position items within their containers. It is a shorthand property that combines the align-self and justify-self properties. Place-self can take a variety of values, including auto, stretch, center, start, end, flex-start, flex-end, baseline, first baseline, last baseline, safe center, and unsafe center. By using place-self, you can create visually appealing web pages that are easy to navigate and use.

References

Activity