Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

align-self property in flexbox is not working?

Tags:

I am trying to understand flex box: I want to make the “first” block stretched to match the full width of the browser, and the ”second” block of fixed size and aligned to left.

So I used align-items: flex-end in the parent(<html>) and tried to stretch the first block using align-self: stretch in the ”first” block.

This is not working. Both of them are aligned to the left.

<html>   <head>     <style>       html {         display: flex;         flex-direction: column;         justify-content: flex-start;         align-items: flex-end;       }        #first {         align-self:stretch;         background-color: red;         border: 3px solid black;       }        #second {         background-color:green;         width: 300px;         height: 400px;         border: 3px solid yellow;       }     </style>   </head>   <body>     <div id="first">This is first</div>     <div id="second">This is second</div>   </body> </html> 
like image 449
IhtkaS Avatar asked Nov 18 '13 16:11

IhtkaS


People also ask

Why is justify-self Not working?

Also when you use display: flex on parent element display: inline-block on child elements is not going to work. To add to this answer, justify-self is simply not supported in flexbox because it justifies all its items as a group.

Can I use justify-self in flexbox?

Note that values space-around and space-between on justify-content property will not keep the middle item centered about the container if the adjacent items have different widths. As of this writing, there is no mention of justify-self or justify-items in the flexbox spec.

Does align-content override align-self?

The align-self applies to all flex items, allows this default alignment to be overridden for individual flex items. The align-content property only applies to multi-line flex containers, and aligns the flex lines within the flex container when there is extra space in the cross-axis.


2 Answers

So the problem is that you've got the <html> node as a flex container, and the <body> node (its child) is the one and only flex item.

Currently, align-self:stretch on #first is ignored, because that property only applies to flex items, and #first is not a flex item (the way you have things set up right now).

To get what you want, you need to make the <body> node a flex container, not the <html> node. So, just change your first style rule to apply to body{ instead of html{

(Also, if you want #second to be aligned to the left, you need flex-start, not flex-end. The left edge is the flex-start horizontal edge, generally.)

like image 157
dholbert Avatar answered Oct 11 '22 03:10

dholbert


Add flex:1 to #first

Remove justify-content: flex-start; and align-items: flex-end; from html

See example: http://codepen.io/anon/pen/vifIr

like image 20
2507rkt3 Avatar answered Oct 11 '22 01:10

2507rkt3