Home > Web Front-end > CSS Tutorial > How Do I Right-Align a Flexbox Item?

How Do I Right-Align a Flexbox Item?

Linda Hamilton
Release: 2024-12-14 09:22:11
Original
453 people have browsed it

How Do I Right-Align a Flexbox Item?

How can I align one item right with flexbox?

In flexbox, you can set the margin-left: auto; property to align one item to the right.

The flexbox specification states:

"One use of auto margins in the main axis is to separate flex items
into distinct 'groups'. The following example shows how to use this to
reproduce a common UI pattern - a single bar of actions with some
aligned on the left and others aligned on the right."

Here's an updated code snippet:

.wrap {
  display: flex;
  background: #ccc;
  width: 100%;
  justify-content: space-between;
}

.wrap div:last-child {
  margin-left: auto;
}

.result {
  background: #ccc;
  margin-top: 20px;
}

.result:after {
  content: '';
  display: table;
  clear: both;
}

.result div {
  float: left;
}

.result div:last-child {
  float: right;
}
Copy after login

The updated fiddle below demonstrates the alignment:

[fiddle demo](https://jsfiddle.net/vhem8scs/)

The above is the detailed content of How Do I Right-Align a Flexbox Item?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template