ChatGPT解决这个技术问题 Extra ChatGPT

How to Right-align flex item?

Is there a more flexbox-ish way to right-align "Contact" than to use position: absolute?

.main { display: flex; } .a, .b, .c { background: #efefef; border: 1px solid #999; } .b { flex: 1; text-align: center; } .c { position: absolute; right: 0; }

With title

Without title

http://jsfiddle.net/vqDK9/

you can use float right, but it's the same way...! The best way is to use a display table with text-align.
Sure, if that's better. Still having trouble right-aligning "Contact" though: jsfiddle.net/vqDK9/1
I updated your fiddle jsfiddle.net/vqDK9/2
Here are at least two ways to do it: stackoverflow.com/a/33856609/3597276
Attention: The second answer has much more up-votes than the first answer.

N
Nitin Bisht

A more flex approach would be to use an auto left margin (flex items treat auto margins a bit differently than when used in a block formatting context).

.c {
    margin-left: auto;
}

Updated fiddle:

.main { display: flex; } .a, .b, .c { background: #efefef; border: 1px solid #999; } .b { flex: 1; text-align: center; } .c {margin-left: auto;}

With title

Without title

Problem

Is there a more flexbox-ish way to right align "Contact" than to use position absolute?


Thanks. Would you personally prefer this over Yohann Tilotti's display table method above? If so, why?
@MarkBoulder: For compatibility reasons his method is better, but if you're already using flexbox my answer would make more sense.
@MarkBoulder: They both accomplish the same thing in this case. The advantage would be having other properties (and behaviour) associated with flex items that the table approach doesn't have (flex, order, etc).
This should be the accepted answer. Because justify-content: space-between; only accomplishes that you want if you only have 2 items in your flex container (first item would be on the left and second one would be to the right). But for more than 2 items, to only align some on the right, this is the correct approach!
If you can not wrap the elements and you need to float say the last three right, target the 3rd one from the last only with this margin-left: auto; style.
K
Kevin Suttle

Here you go. Set justify-content: space-between on the flex container.

.main { display: flex; justify-content: space-between; } .a, .b, .c { background: #efefef; border: 1px solid #999; } .b { text-align: center; }

With title

Without title


Or justify-content: flex-end
Try setting the width of .c to 300px. The title is no longer centered. So yes, this answers the question, but this breaks the design.
Note that this doesn't always work the way you may expect, like when there's a .c::after pseudo-element. In my experience, margin-left: auto; is the way to go.
Or flex-flow: row-reverse;
I don't see this being a correct answer if you wan't to align just one item in a flex container.
M
Melchia

You can also use a filler to fill the remaining space.

<div class="main">
    <div class="a"><a href="#">Home</a></div>
    <div class="b"><a href="#">Some title centered</a></div>
    <div class="filler"></div>
    <div class="c"><a href="#">Contact</a></div>
</div>

.filler{
    flex-grow: 1;
}

I have updated the solution with 3 different versions. This because of the discussion of the validity of using an additional filler element. If you run the code snipped you see that all solutions do different things. For instance setting the filler class on item b will make this item fill the remaining space. This has the benefit that there is no 'dead' space that is not clickable.


Finally someone who understands flexbox
@Kokodoko yeah, using one more non-semantic html element to move another element is the top of understanding flexbox...
@Zanshin13 The other answers all write so much extra css that you might as well leave out the flex container and code the whole thing yourself :)
@Kokodoko justify-content: space-between is "so much" css, seriously? No need for further comments (but if you want - welcome to chat). This answer has its right to be here, because it is a solution. But definitely not optimal one. Idk, maybe you did't notice but most of css from another answers are OP's and answers actually reduce (a little bit) amount of author's css. This answer does not have less css then others (will not work without OP's css - jsfiddle.net/63ma3b56). But it has one more html element.
Not very pretty using "filler" just to push an element to the end
N
Nick F

If you want to use flexbox for this, you should be able to, by doing this (display: flex on the container, flex: 1 on the items, and text-align: right on .c):

.main { display: flex; }
.a, .b, .c {
    background: #efefef;
    border: 1px solid #999;
    flex: 1;
}
.b { text-align: center; }
.c { text-align: right; }

...or alternatively (even simpler), if the items don't need to meet, you can use justify-content: space-between on the container and remove the text-align rules completely:

.main { display: flex; justify-content: space-between; }
.a, .b, .c { background: #efefef; border: 1px solid #999; }

Here's a demo on Codepen to allow you to quickly try the above.


space-between was exactly what I was looking for, thanks!
Borders disappear when using second suggestion, expected behaviour? codepen.io/oshihirii/pen/RygKRd
In my configuration justify-content: space-between on the container did not move the item right, but text-align: right did, thanks.
M
Melchia

Or you could just use justify-content: flex-end

.main { display: flex; }
.c { justify-content: flex-end; }

The justify-content attribute is an attribute of the flex-container, see Chris Coyer's flexy cheatsheet: css-tricks.com/snippets/css/a-guide-to-flexbox
This is the only solution on this page that worked for me.
S
Shreyash.K

As easy as

.main {
    display: flex;
    flex-direction:row-reverse;
}

S
Shreyash.K

Add the following CSS class to your stylesheet:

.my-spacer {
    flex: 1 1 auto;
}

Place an empty element between the element on the left and the element you wish to right-align:

<span class="my-spacer"></span>


For those who don't merely want to right align a single element, but may want to left align one element, and right align another (within the same flex layout) this is the way to go!
M
Monir Khan

margin-left: auto works well. But clean flex box solution would be space-between in the main class. Space between works well if there is two or more elements. I have added a solution for single element as well.

.main { display: flex; justify-content: space-between; } .a, .b, .c { background: #efefef; border: 1px solid #999; padding: 0.25rem; margin: 0.25rem;} .b { flex: 1; text-align: center; } .c-wrapper { display: flex; flex: 1; justify-content: flex-end; } .c-wrapper2 { display: flex; flex: 1; flex-flow: row-reverse; }

Contact Contact2


T
TetraDev

If you need one item to be left aligned (like a header) but then multiple items right aligned (like 3 images), then you would do something like this:

h1 {
   flex-basis: 100%; // forces this element to take up any remaining space
}

img {
   margin: 0 5px; // small margin between images
   height: 50px; // image width will be in relation to height, in case images are large - optional if images are already the proper size
}

Here's what that will look like (only relavent CSS was included in snippet above)

https://i.stack.imgur.com/i65gn.png


M
Mohammad Adeel

'justify-content: flex-end' worked within price box container.

.price-box {
    justify-content: flex-end;
}

L
Lindauson

For those using Angular and Flex-Layout, use the following on the flex-item container:

<div fxLayout="row" fxLayoutAlign="flex-end">

See fxLayoutAlign docs here and the full fxLayout docs here.


n
nights

I find that adding 'justify-content: flex-end' to the flex container solves the problem while 'justify-content: space-between' doesnt do anything.


1
1.21 gigawatts

Example code based on answer by TetraDev

Images on right:

* { outline: .4px dashed red; } .main { display: flex; flex-direction: row; align-items: center; } h1 { flex-basis: 100%; } img { margin: 0 5px; height: 30px; }

Secure Payment

Images on left:

* { outline: .4px dashed red; } .main { display: flex; flex-direction: row; align-items: center; } h1 { flex-basis: 100%; text-align: right; } img { margin: 0 5px; height: 30px; }

Secure Payment