This question already has answers here:
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
Closed 1 year ago.
CSS Code:
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
height: 400px;
}
.box1 {
background-color: aliceblue;
height: 70%;
width: 70%;
}
I tried vertical-align to center in box1 and nothing happened either. My box with the current code has vertically centered text but not horizontally. Any tips?
You can nest flex containers. This means your box can also have the display: flex; property, plus justify-content: center; for horizontally centering. Also look into the "place-" property with which you can center things on both axes with one single line of CSS.
Related
This question already has answers here:
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
(6 answers)
How can I vertically center a div element for all browsers using CSS?
(48 answers)
Flexbox: center horizontally and vertically
(14 answers)
Closed 1 year ago.
I can get it to center vertically or horizontally but not both at the same time. This is the css code I have so far:
.my-flex{
display: flex;
height: 100%;
width: 100%
}
//other pieces of the the flexbox that i have before it that aren't relevent
.last-element{
flex-grow: 1 0 auto;
align-self: center;
justify-self:center;
}
I also tried having the my-flex also be jusify-content center and flex-start as well as align-items
.my-flex{
display: flex;
height: 100%;
width: 100%
}
//other pieces of the the flexbox that i have before it that aren't relevent
.last-element{
flex-grow: 1;
align-self: center;
justify-self:center;
text-align: center
}
This is what I got to work!
if <div class="my-flex"> is the parent div, then
.my-flex{
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
}
This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
(6 answers)
Closed 2 years ago.
I'm creating a simple footer and want all of the text in it to be vertically centered. I tried several things but none of this worked:
footer {
height: 50px;
width: 100%;
bottom: 0;
position: fixed;
background-color: lightgray;
flex: display;
flex-direction: column;
justify-content: center;
justify-items: center;
align-items: center;
align-content: center;
text-align: center;
color: blue;
}
The horizontal centering works great. What am I doing wrong with the vertical centering?
You have flex: display; it should be display: flex;
When you make this adjustment, you will see it works to your request.
This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
Closed 5 years ago.
I have div block with property flex CSS.
This block has 80% width and 50% height of display.
How to display this block in center by horizontal and vertical?
Flex alignment properties apply to the children of a flex container.
So if you have a div with display: flex, and you want to center this div, you make the parent a flex container. Then you can apply flex alignment properties to the div.
div {
display: flex;
width: 80%;
height: 50vh;
border: 1px solid black;
}
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
}
<div></div>
Here's a more complete explanation: https://stackoverflow.com/a/33049198/3597276
This question already has answers here:
How to place two divs next to each other? [duplicate]
(13 answers)
Closed 5 years ago.
I need to align an element to the center of the screen while having an element to the left of it.
I can achieve this with float left and setting the padding of the center element to match the width of the floated element. But, in this case I will not always know the width.
I do not know how better to explain this. Searchs have failed me.
You can use css flex to get the same flex Guide
body,
html {
margin: 0;
padding: 0;
height: 100%;
}
.wrapper {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
align-items: center;
height: 100%;
}
.box-1 {
padding: 2%;
background: tomato;
flex: 0 0 10%;
}
<div class="wrapper">
<div class="box-1">1</div>
<div class="box-1">2</div>
</div>
This question already has answers here:
How do I vertically center text with CSS? [duplicate]
(37 answers)
Closed 6 years ago.
I used a tag and wrote my content set a background color, height and width.
I used text-align: center; to align the text in the center for x axis.
How can I align it in middle for y axis?
Using flexbox you can specify align-items: center; justify-content: center; to center the text in an element.
p {
background: red;
height: 100px;
width: 100px;
display: flex;
align-items: center;
justify-content: center;
}
<p>a tag</p>