How do I vertically center text? [duplicate] - html

This question already has answers here:
How do I vertically center text with CSS? [duplicate]
(37 answers)
Closed 5 years ago.
I've been working on getting the text in my content div centered vertically and I'm stumped. The container includes 1 div with a title and 1 div with content.
I've tried elements such as:
vertical-align: middle;
and also playing with the displays/positioning, but I'm not having any luck.
The current CSS is the following:
.content-wrapper {
height: 100vh;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: left;
text-align: left;
-ms-flex-flow: column nowrap;
flex-flow: column nowrap;
color: #000;
font-family: Montserrat;
text-transform: none;
-webkit-transform: translateY(40vh);
transform: translateY(40vh);
will-change: transform;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
-webkit-transition: all 1.7s cubic-bezier(0.22, 0.44, 0, 1);
transition: all 1.7s cubic-bezier(0.22, 0.44, 0, 1);
padding-top: 10%;
padding-right: 25px;
padding-left: 30px;
float: right;
width: 35%;
background-color: #f0f7fc;
}

Flexbox
Flexbox allows you to vertically align the text without having a div with a fixed height. It is now supported by all the modern browsers.
Check my other answer to see all the problems and workarounds for Flexbox. The majority are for Internet Explorer.
display: flex;
align-items: center;
div {
width: 50px;
height: 100px;
display: flex;
align-items: center;
border: 1px solid black;
}
<div>
Test
</div>
line-height
If you know the height of the external div, you can use line-height.
height: 100px;
line-height: 100px; /* same value as height */
div {
width: 50px;
height: 100px;
line-height: 100px;
border: 1px solid black;
}
<div>
Test
</div>
display: table-cell
display: table-cell is another good alternative which allows you to vertically align without knowing the height of the div. It works in older browsers too (except Internet Explorer 7).
display: table-cell;
vertical-align: middle;
div {
width: 50px;
height: 100px;
display: table-cell;
vertical-align: middle;
border: 1px solid black;
}
<div>
Test
</div>

Related

Text is not centered in div? [duplicate]

This question already has answers here:
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
Flexbox: center horizontally and vertically
(14 answers)
Closed 5 months ago.
So I'm kind of a begginner and I just encountered a problem that I don't know how to solve.
Here is the code:
https://jsfiddle.net/upwaqyek/
.parent{
display: flex;
flex-wrap: wrap;
border: #000000 2px solid;
justify-content: center;
}
.parent div {
width: 300px;
height: 50px;
background-color: rgba(197, 197, 197, 0.788);
border: #000000 solid 2px;
border-radius: 10px;
margin: 10px;
transition-duration: 3s;
text-align: center;
align-items: center;
}
I can't center the text. I think it has to do with the width and height but it really doesn't move at all. The text just stays static there at the bottom of the div and doesn't do anything when I change the div height.
I want the text to be in the center of the div, both horizontally and vertically.
Just add this two properties to your .parent div. Of-course there are lots of ways to do this, but I'm writing answer base on your existing code.
.parent div {
// your others styles
display: flex;
justify-content: center;
}
.parent{
display: flex;
flex-wrap: wrap;
border: #000000 2px solid;
justify-content: center;
}
.parent div {
width: 300px;
height: 50px;
background-color: rgba(197, 197, 197, 0.788);
border: #000000 solid 2px;
border-radius: 10px;
margin: 10px;
transition-duration: 3s;
text-align: center;
align-items: center;
display: flex;
justify-content: center;
}
.parent div:hover {
background-color: white;
}
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<div class="parent">
<a>
<div><h2>Text</h2></div>
</a>
<div><h2>Text</h2></div>
</div>
</body>
</html>
for the targeted div, add display: flex; to it, then to center its content horizontally, add justify-content: center; and to center its content vertically add align-items: center;
.parent{
display: flex;
flex-wrap: wrap;
border: #000000 2px solid;
justify-content: center;
}
.parent div {
width: 300px;
height: 50px;
background-color: rgba(197, 197, 197, 0.788);
border: #000000 solid 2px;
border-radius: 10px;
margin: 10px;
transition-duration: 3s;
text-align: center;
align-items: center;
display: flex;
justify-content: center;
align-items: center;
}
.parent div:hover {
background-color: white;
}
<div class="parent">
<a>
<div>
<h2>Text</h2>
</div>
</a>
<div>
<h2>Text</h2>
</div>
</div>
You can center the text by making the container div a flexbox:
.parent div {
display: flex;
justify-content: center; /*center horizontally */
align-items: center; /* center vertically */
}
Your text is pushed down because h2 tags have a default not 0 margin (The margin varies between browsers).
It's not necessary, but I would recommend setting the margin value for header tags to 0.
h1, h2, h3, ... {
margin: 0;
}

How can I center an image using Flex? [duplicate]

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)
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
How to vertically align an image inside a div
(37 answers)
Closed 3 years ago.
I have already centered the div in the page using Flex and now I want to center the image in the div.
I feel like I am using to many properties in my original centering and do not know if I am doing it correctly.
https://jsfiddle.net/fLkz49nj/
#auth_top{
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
height: 100vh;
}
#auth{
width: 250px;
height: 185px;
background: rgba(255,255,200,0.5);
box-shadow: 0px 1px 3px rgba(25, 25, 25, 0.4);
}
#auth_google_link{
width: 64px;
height: 64px;
display: block;
}
#auth_google_img{
width: 64px;
border: 1px solid white;
border-radius: 2px;
}
#auth_google_img:hover{
border: 1px solid black;
}
You can put #auth inside the flexbox and center it.
#auth_top{
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
height: 100vh;
}
#auth{
width: 250px;
height: 185px;
background: rgba(255,255,200,0.5);
box-shadow: 0px 1px 3px rgba(25, 25, 25, 0.4);
display: flex; /* added style from here */
justify-content: center;
align-items: center;
}
#auth_google_link{
width: 64px;
height: 64px;
display: block;
}
#auth_google_img{
width: 64px;
border: 1px solid white;
border-radius: 2px;
}
#auth_google_img:hover{
border: 1px solid black;
}
<div id='auth_top'>
<div id='auth'>
<a id='auth_google_link' href="/auth/google">
<img id='auth_google_img' src='data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRw%0D%0AOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB2aWV3Qm94PSIwIDAgNDggNDgiPjxkZWZzPjxwYXRo%0D%0AIGlkPSJhIiBkPSJNNDQuNSAyMEgyNHY4LjVoMTEuOEMzNC43IDMzLjkgMzAuMSAzNyAyNCAzN2Mt%0D%0ANy4yIDAtMTMtNS44LTEzLTEzczUuOC0xMyAxMy0xM2MzLjEgMCA1LjkgMS4xIDguMSAyLjlsNi40%0D%0ALTYuNEMzNC42IDQuMSAyOS42IDIgMjQgMiAxMS44IDIgMiAxMS44IDIgMjRzOS44IDIyIDIyIDIy%0D%0AYzExIDAgMjEtOCAyMS0yMiAwLTEuMy0uMi0yLjctLjUtNHoiLz48L2RlZnM+PGNsaXBQYXRoIGlk%0D%0APSJiIj48dXNlIHhsaW5rOmhyZWY9IiNhIiBvdmVyZmxvdz0idmlzaWJsZSIvPjwvY2xpcFBhdGg+%0D%0APHBhdGggY2xpcC1wYXRoPSJ1cmwoI2IpIiBmaWxsPSIjRkJCQzA1IiBkPSJNMCAzN1YxMWwxNyAx%0D%0AM3oiLz48cGF0aCBjbGlwLXBhdGg9InVybCgjYikiIGZpbGw9IiNFQTQzMzUiIGQ9Ik0wIDExbDE3%0D%0AIDEzIDctNi4xTDQ4IDE0VjBIMHoiLz48cGF0aCBjbGlwLXBhdGg9InVybCgjYikiIGZpbGw9IiMz%0D%0ANEE4NTMiIGQ9Ik0wIDM3bDMwLTIzIDcuOSAxTDQ4IDB2NDhIMHoiLz48cGF0aCBjbGlwLXBhdGg9%0D%0AInVybCgjYikiIGZpbGw9IiM0Mjg1RjQiIGQ9Ik00OCA0OEwxNyAyNGwtNC0zIDM1LTEweiIvPjwv%0D%0Ac3ZnPg=='/>
</a>
</div>
</div>
Hope it helps. Cheers!
you can do with positioning absolute but i guess that's not what you seek.
what you can do is
#auth{
text-align: center;
white-space: nowrap;
}
#auth:after {
content: '';
min-height: 100%;
display: inline-block;
vertical-align: middle;
}
#auth_google_link{
white-space: normal;
display: inline-block;
vertical-align: middle;
}
you should remove the display:block (to allow it to be centered) from the auth_google_link and then add a padding-top to the auth_google_img about the 22% (to move it down to the middle) and it should work perfectly

Vertical align middle not working in span element

vertical-align:middle property is not working in span element. I have tried to place the text center- and middle-aligned in span element. I have tried the following code:
span {
text-align: center;
vertical-align: middle; /* Not working */
height: 150px;
width: 150px;
border: 1px solid black;
display: block;
}
<span>center</span>
You can use display: flex to achieve this.
span {
height: 150px;
width: 150px;
border: 1px solid black;
display: flex;
align-items: center;
justify-content: center;
}
<span>center</span>
vertical-align: middle will not work with display: block
You have many options for this. I have provided 3 examples:
With line-height property(in this case u specify line-height same as height):
.line-height-center {
display: inline-block;
line-height: 150px;
}
With display: table:
.table-center {
display: inline-table;
}
.table-center span {
display: table-cell;
}
With display: inline-flex;:
.flex-center {
display: inline-flex;
align-items: center;
justify-content: center;
}
span {
text-align: center;
vertical-align: middle; /* Not working */
height: 150px;
width: 150px;
border: 1px solid black;
}
.line-height-center {
display: inline-block;
line-height: 150px;
}
.table-center {
display: inline-table;
}
.table-center span {
display: table-cell;
}
.flex-center {
display: inline-flex;
align-items: center;
justify-content: center;
}
<p>With line height property:</p>
<span class="line-height-center">center</span>
<p>With display table:</p>
<span class="table-center"><span>center</span></span>
<p>With display flex:</p>
<span class="flex-center">center</span>
set the line-height of the child equal to the height of the container, or set positioning on the container and absolutely position the child at top:50% with margin-top:-YYYpx, YYY being half the known height of the child.
You are using display: block; Display block has a box behaviour.
But you trying to run a text behaviour.
If you have a known height you can use just the line height.
If height for example is 150px:
line-height: calc(150px - 15px);

How to center content in a flexbox

I'm having an issue on my project where the content is not aligning vertically in the flexbox. We have a fluid responsive page that needs to center all items within the flexbox via %'s. in the example below we've set a fixed pixel height just so you can play with it.
I have tried a ton of different methods for hours..
body{
margin:0px auto;
}
/* top bar navigation */
.topBoxNav {
background:red;
padding: 0;
list-style: none;
-ms-box-orient: horizontal;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -moz-flex;
display: -webkit-flex;
display: flex;
width: 100%;
height: 50px;
color: white;
font-weight: bold;
font-family: 'League Gothic';
font-size: 1.5em;
text-align: center;
}
.topBoxNav li {
box-sizing: border-box;
border: 1px solid white;
width: 100%;
height: 100%;
}
.topBoxNav a {
text-decoration: none;
color: white;
}
HTML:
<nav class="" alt="top Nav">
<ul class="topBoxNav">
<li>Box1</li>
<li>Box2</li>
<li>Box3</li>
</ul>
</nav>
example:
http://codepen.io/anon/pen/iwDdy
First, realize that the one that has the display: flex property is your ul, not your li elements, so you have two options here:
1) Give your li elements display: flex and add align-items: center to it and justify-content: center
You will get something like this:
.topBoxNav li {
box-sizing: border-box;
border: 1px solid white;
width: 100%;
height: 100%;
display: flex;
align-items center
-moz-align-items center
-ms-flex-align center
-webkit-align-items center
justify-content center
-moz-justify-content center
-ms-flex-pack center
-webkit-justify-content center
}
2) Give your li elements line-height equal to the height, in this case 50px and vertical-align: middle.
You will have something like this:
.topBoxNav li {
box-sizing: border-box;
border: 1px solid white;
width: 100%;
height: 100%;
align-items: center;
-webkit-align-items: center;
vertical-align: middle;
line-height: 50px;
}
Both ways will work :)

CSS to vertically align text to middle of div

What is the correct way to force text in a div to vertically align to the middle?
I have found a couple 'tricks', but I need something that works with a single line and multiple lines. Hopefully I am missing something stupid simple:
http://jsfiddle.net/rogerguess/DawFy/
.header {
display: inline-block;
text-align: center;
width: 123px;
background: green;
border-radius: 4px 4px 4px 4px;
height: 80px;
vertical-align: middle;
}
<div >
<div class="header">test1 </div>
<div class="header">some text that will wrap</div>
<div class="header">test3</div>
</div>
Change display: inline-block; to display: table-cell;
If your multi-line elements need to wrap for responsive reasons, then your best bet is with Flexbox. Due to the flakiness of Firefox's 2009 Flexbox implementation, it has to be handled slightly differently than you would do it for modern Flexbox implementations.
http://codepen.io/cimmanon/pen/mxuFa
<ul>
<li>One lorem ipsum dolor sit amet</li><!--
--><li>Two</li><!--
--><li>Three</li><!--
--><li>Four</li><!--
--><li>Five</li>
</ul>
li {
text-align: center;
vertical-align: middle;
/* fallback for non-Flexbox browsers */
display: inline-block;
/* Flexbox browsers */
display: -webkit-inline-box;
display: -moz-inline-box;
display: -ms-inline-flexbox;
display: -webkit-inline-flex;
display: inline-flex;
/* vertical centering for legacy, horizontal centering for modern */
-webkit-box-pack: center;
-moz-box-pack: center;
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;
/* modern Flexbox only */
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
/* legacy Flexbox only */
-webkit-box-orient: vertical;
-moz-box-orient: vertical;
}
I've made a quick example for you, using display: table-cell property on the parent div.
CSS:
.outer {
width: 500px;
height: 500px;
display: table-cell;
background-color: red;
vertical-align: middle;
}
.inner {
display: block;
width: 300px;
height: 200px;
margin: 0px auto;
background-color: yellow;
}
HTML:
<div class="outer">
<div class="inner">
</div>
</div>
http://jsfiddle.net/aKT42/
Try this, it will work.
<div class="greenBorder" id="outer">
<div id="middle">
<div class="greenBorder" id="inner">
Your Text Here
</div>
</div>
</div>
Css Class
#outer {
height: 100%;
overflow: hidden;
position: relative;
width: 100%;
display: table;
position: static;
}
div.greenBorder {
background-color: #39B9FF;
text-align: center;
color: white;
}
#middle[id] {
display: table-cell;
position: static;
vertical-align: middle;
width: 100%;
}
#middle {
position: absolute;
top: 50%;
}
http://jsfiddle.net/DawFy/16/
.header {
display: block;
text-align: center;
width: 123px;
background: green;
border-radius: 4px 4px 4px 4px;
height: 80px;
margin:0px auto;
}
li
{
list-style-type:none;
}
<div >
<li>
<div class="header">test1 </div>
<li>
<div class="header">some text that will wrap</div>
<li>
<div class="header">test3</div>
Try this dont know if its "correct" but works
Most of these solutions wouldn't work for me because either the height was manually specified (which I couldn't do) or things like inline-block and float would remove the ability for the an inner div to inherit the parent div's height. I ended up creating a table with two columns each containing my divs and then I had no issues vertically centering text.