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

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;
}

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 align flexbox items to the most left, the most right and center? [duplicate]

This question already has answers here:
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
(6 answers)
Closed 2 years ago.
I have a flexbox container row and there are three items inside. I want one of them to be in the very center but I want the other two 2 to be slightly over it and to the most left and to the most right respectively. I want something like this:
I tried with aligning and justify content but it did not work.
Thanks in advance.
Here is an illustration of what you want. Just give the middle element a margin-top:
.container {
display: flex;
justify-content: space-between;
}
.container div {
width: 50px;
height: 100px;
background-color: red;
}
.center {
margin-top: 50px;
}
<div class="container">
<div class="right"></div>
<div class="center"></div>
<div class="left"></div>
</div>

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

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

Make this div appear the middle of the body? [duplicate]

This question already has answers here:
How to center an element horizontally and vertically
(27 answers)
Flexbox: center horizontally and vertically
(14 answers)
How can I vertically center a div element for all browsers using CSS?
(48 answers)
Closed 3 years ago.
I am trying to make a div appear in the middle of the browser. However, I am unable to find any form of guide working with the CSS I already have.
CSS
#register-modal {
display: inline;
position: absolute;
z-index: 1;
margin: 0 auto;
background-color: aqua;
top: 50%;
left: 50%;
text-align: center;
}
With this CSS I expected the div to appear in the middle of the browser windows, but it appears in the bottom right corner.
The HTML affected by the CSS forms a "modal" and thus I can't have the position be anything other than absolute.
You can easily achieve this by using the flex layout. And by setting the justify-content and align-items to center
justify-content: align the items on the main axis.
align-items: align the items on cross axis.
You can read about it more on the MDN doc
.container{
display:flex;
justify-content: center;
align-items: center;
width:100%;
height:100%
}
body, html {
width:100%;
height: 100%;
}
<div class="container">
<div class="sub-container">Child</div>
</div>

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>

Resources