Right align all elements when text makes a new line [duplicate] - css

This question already has answers here:
Make container shrink-to-fit child elements as they wrap
(4 answers)
CSS when inline-block elements line-break, parent wrapper does not fit new width
(2 answers)
Closed 2 years ago.
I have a set of divs and want the children to be right aligned:
.parent {
width: 150px;
border: solid;
display: flex;
align-items: center;
justify-content: flex-end;
}
<div class="parent">
<div class="icon">x
</div>
<div>Toooooooo looooooooong</div>
</div>
When the text breaks in a newline, it still occupies all the space, so it's not at the right. I could use width: min-length, but then all the short texts with spaces break in a new line, and I want it only for the long ones.
Here's the example in CodePen: https://codepen.io/rveciana/pen/eYpyKJX

Related

Place single child at bottom of flex container flex-direction colum? [duplicate]

This question already has answers here:
Center and bottom-align flex items
(3 answers)
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
(6 answers)
Closed 8 months ago.
HTML:
<div class="wrapper">
<div class="child1"></div>
<div class="child2"></div>
</div>
CSS:
.wrapper {
height: 100%;
width: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
I have two child, I want the first to be at center of page and only the second to be at bottom without using position absolute or affecting first child positioning

How to make element's width match text width with CSS [duplicate]

This question already has answers here:
How can I make a div not larger than its contents?
(43 answers)
Closed 1 year ago.
I have an element that takes 100% of its container's width:
How do I make it so the width of the element is as wide as the text?
I can't find the answer online because I don't know how to formulate the question proprely.
float: left; works but it messes up the surrounding elements as some of them have this porperty as well. Is there another way?
You can set the display: inline-block OR display: inline (depends on your use case) for the element containing the text like so :
.text {
display: inline;
padding: 10px;
background: firebrick;
}
<div class="text">text</div>
<div class="text">text text text</div>

Css - vertical centering of an image inside a div [duplicate]

This question already has answers here:
How to vertically align an image inside a div
(37 answers)
Flexbox: center horizontally and vertically
(14 answers)
How can I vertically align elements in a div?
(28 answers)
Closed 2 years ago.
here HTML:
<div class="user-image text-center">
<img class="avatars" src="css/img/avatar_1.jpg" alt="">
</div>
here CSS:
.user-image {
height: 10vh;
width: 15%;
float: left;
}
I'm trying to centering vertically my image inside my DIV (it's a square) but I'm a beginner and can't find the correct way. I tried vertical-align: middle; and margin: auto; but nothing. Can you suggest me some methods for that?
Use flex styles.
.user-image {
display: flex;
justify-content: center;
align-items: center;
}

Vertically aligning <div> contents [duplicate]

This question already has answers here:
How do I vertically center text with CSS? [duplicate]
(37 answers)
Closed 5 years ago.
The simplest things... Why isn't the text in the below centered vertically? Isn't that what vertical-align: middle does?
<div style="width:100%;height: 300px;background-color: #0047b3;vertical-align: middle;">
Why are you not centered vertically?!?!?!
</div>
If you read some of the flex properties it gets lot easier see below
'
div {
width: 100%;
height: 300px;
background-color: #0047b3;
display: flex;
align-items: center;
}
<div>whatever you want to align vertically centre </div>

Flexbox align-content not consistent with variable number of items [duplicate]

This question already has answers here:
How does flex-wrap work with align-self, align-items and align-content?
(2 answers)
Closed 5 years ago.
In my application the number of items in a flex container is variable, and I want them to be both vertically and horizontally centred. I'm doing this by using justify-content for horizontal centring, and align-content for vertical centring.
However, reading from CSS Tricks' almanac states the following:
Note, this property has no effect when the flexbox has only a single line.
This becomes quickly apparent in my application. While there aren't enough items to wrap onto another line, vertical centring from align-content doesn't work at all.
As soon as a second line of content appears, align-content kicks in and works as expected.
I'm sure there's a reason for this behaviour, however in my case the result is an inconsistent user experience.
I know that I can achieve vertical centring by other means, for instance by wrapping the entire container in another flexbox, or a table, and applying vertical centring there. However, I feel like there should be a better way?
Is there some other flexbox option I'm missing that would solve this problem without introducing more HTML elements? If so, what is that option? :P
Based on the given facts, you should use align-items instead of align-content
Notes:
align-content works on multi line flexboxes, very well described here: Whats the difference between align-content and align-items
Based on the flex direction, justify-content and align-items alignment direction changes
Column-based
.flex {
display: flex;
flex-direction: column;
justify-content: center; /* center vertical */
align-items: center; /* center horizontal */
height: 250px;
border: 1px solid gray
}
.flex * {
margin-bottom: 0; /* removed margin at one side since they
doesn't collapse when flexbox is used */
}
<div class="flex">
<h1>Hey</h1>
<p>How are</p>
<p>we doing</p>
</div>
Row-based
.flex {
display: flex;
justify-content: center; /* center horizontal */
align-items: center; /* center vertical */
height: 250px;
border: 1px solid gray
}
<div class="flex">
<h1>Hey </h1>
<p>How are </p>
<p>we doing</p>
</div>
Using align-items & justify-content together will vertically and horizontally center a single item:
align-items:center; justify-content:center
See fiddle: https://jsfiddle.net/p8zk4bkk/

Resources