Home > Web Front-end > CSS Tutorial > How Can I Right-Align One Flexbox Item While Others Remain Left-Aligned?

How Can I Right-Align One Flexbox Item While Others Remain Left-Aligned?

Mary-Kate Olsen
Release: 2024-12-10 05:33:13
Original
319 people have browsed it

How Can I Right-Align One Flexbox Item While Others Remain Left-Aligned?

Aligning Flexbox Items: Right-Aligned Option

Question:

In the context of flexbox, is it possible to align two items left and one item right? For a visual representation, refer to the provided JSFiddle link.

Answer:

To achieve right alignment for one flex child, set its margin-left: auto;.

According to the flex specification:

Auto margins on the main axis can be used to separate flex items into different "groups." This allows for the creation of UI patterns such as action bars with some items aligned left and others right.

Therefore, the following CSS will align the last div element in the provided example to the right:

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

By combining this technique with your existing flexbox styles, you can achieve the desired layout without requiring multiple blocks of code.

The above is the detailed content of How Can I Right-Align One Flexbox Item While Others Remain Left-Aligned?. 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