This is driving me crazy I just don't understand why this piece of simple css to vertically center an element in a div doesn't work as expected.
this is the html:
<div class="header-a-wrapper" style="
line-height: 48px;
height: 48px;
background: red;
display: block;
text-align: center;
">
<a href="/user/5659186348163072" class="right" style="
background: blue;
line-height: normal;
display: inline-block;
vertical-align: middle;
float: none;
height: 20px;
">medical salamander</a>
</div>
the inner element does not get centered vertically but I really think it should
here is an html with the two elements:
http://alephz.com/test.html
and this is the CRAZY part. here is a jsfiddle with the same html and over there it works! tested on the same chrome/win7!
http://jsfiddle.net/pkrsdqkb/
Very weird, but if you want to solve it, you add to 'a':
position: relative;
top: 50%;
transform: translateY(-50%);
Remove
vertical-align: middle;
float: none;
One option to play nicely with vertical-align: middle is to use display: table and display: table-cell.
The wrapper gets display: table and width: 100%
Wrap the links in a div which will act as a "table cell" with display: table-cell
vertical-align: middle will now work as you expect it to.
Compatibility: display: table is good for IE 8 + and modern browsers everywhere.
Example:
.header-a-wrapper {
background: red;
display: table;
text-align: center;
height: 100px;
width: 100%;
}
.vertical {
display: table-cell;
vertical-align: middle;
}
.right {
background: blue;
display: block;
margin: 2px 0;
}
<div class="header-a-wrapper">
<div class="vertical">
medical salamander
medical salamander
</div>
</div>
Old answer
There is a lot of redundant CSS.
The vertical center is applied through: line-height: 48px.
Leave that on the wrapper and remove all the positioning CSS properties on a.right.
Example:
.header-a-wrapper {
line-height: 48px;
background: red;
display: block;
text-align: center;
}
.right {
background: blue;
}
<div class="header-a-wrapper">
medical salamander
</div>
Related
In this style of using line-height and inline-block, why is the green item a few pixels below the middle? Shouldn't there be exactly 15px above and below?
.container{
height: 45px;
line-height: 45px;
background-color: red;
display: inline-block
}
.item{
height: 15px;
width: 15px;
background-color: green;
vertical-align: middle;
display: inline-block
}
<div class="container">
<div class="item">
</div>
</div>
I know there are other ways of vertically aligning items (including JS, absolute positions, and many more). I'm not trying to solve the general "how to vertically align a div".
The culprit here is not so much the line-height, but rather the vertical-align: middle. It tries to align your box with the text that may hypothetically be inside the parent box. Where the inner box ends up depends on the font-size of that text. You can push the box further down by increasing the font-size of its parent:
.container{
height: 45px;
width: 100%;
line-height: 45px;
font-size: 45px;
background-color: red;
display: inline-block
}
.item{
height: 15px;
width: 40px;
background-color: green;
vertical-align: middle;
display: inline-block;
}
<div class="container">
job
<div class="item">
</div>
</div>
As you can see, the text is closer to the bottom of its container than to the top (the "j" overflows the container while the "b" does not).
In the same way, you can move the box closer to the center by decreasing the font-size. Since you asked in comments, here's how you get it optimally centered with this method: Set font-size to 0 on the container.
.container{
height: 45px;
width: 100%;
line-height: 45px;
font-size: 0px;
background-color: red;
display: inline-block
}
.item{
height: 15px;
width: 40px;
background-color: green;
vertical-align: middle;
display: inline-block;
}
<div class="container">
job
<div class="item">
</div>
</div>
Changes in your style may help you
.container {
background-color: #ff0000;
display: table-cell;
height: 45px;
vertical-align: middle;
}
.item {
background-color: #008000;
display: table-cell;
height: 15px;
vertical-align: middle;
width: 15px;
}
Please use dividable size to make this work. Also remove vertical align attribue
https://jsfiddle.net/guc6uxc7/
.container{
height: 42px;
line-height: 42px;
background-color: red;
display: inline-block
}
.item{
height: 12px;
width: 12px;
background-color: green;
display: inline-block;
}
I have the following code on my site.
HTML
<div class="brand-wrap">
<a class="navbar-brand" href="<?php echo home_url(); ?>"></a>
<div class="brand-text">
<p>This is a bunch of content that may take up more than one line of text</p>
</div>
</div>
CSS
.navbar-brand {
display: inline-block;
padding: 0;
margin-top: 10px;
height: 60px;
width: 60px;
background: url('img/logo_orig.png') no-repeat center center;
background-size: auto 100%;
vertical-align: middle;
}
.brand-wrap {
position: relative;
display: inline-block;
}
.brand-text {
position: relative;
font-size: 12px;
display: inline-block;
color: #fff;
margin-left: 25px;
vertical-align: middle;
}
I am trying to vertically align navbar-brand and brand-text, but I can only get them to be next to each other. The vertical-align: middle doesn't seem to work. (* the content may take up more than one line depending on screen width)
How can I align the content vertically?
I don't see problem - just tried your code in here, and it seems to work as it should, or so I understand at least. Maybe be more specific about what are you trying to achieve?
Maybe you simply don't quite understand what vertical-align is supposed to do?
http://codepen.io/anon/pen/QboXov
<div class="brand-wrap">
<a class="navbar-brand" href="<?php echo home_url(); ?>"></a>
<div class="brand-text">
<p>This is a bunch of content that may take up more than one line of text<br/>This is a bunch of content that may take up more than one line of text<br/>This is a bunch of content that may take up more than one line of text<br/>This is a bunch of content that may take up more than one line of text<br/>This is a bunch of content that may take up more than one line of text</p>
</div>
</div>
.navbar-brand {
display: inline-block;
padding: 0;
height: 60px;
width: 60px;
background: url('img/logo_orig.png') #ccc no-repeat center center;
background-size: auto 100%;
vertical-align: middle;
}
.brand-wrap {
position: relative;
display: inline-block;
}
.brand-text {
position: relative;
font-size: 12px;
display: inline-block;
color: #ccc;
margin-left: 25px;
vertical-align: middle;
}
vertical-align aligns one block to another...not the content of the block itself.
The difference in the positioning is the margin-top you have on your navbar-brand.
If you remove that you can see that everything lines up.
.navbar-brand {
display: inline-block;
padding: 0;
height: 60px;
width: 60px;
background: orange;
vertical-align: middle;
}
.brand-wrap {
position: relative;
display: inline-block;
background: lightgreen;
}
.brand-text {
position: relative;
font-size: 12px;
display: inline-block;
margin-left: 25px;
vertical-align: middle;
background: lightblue;
border: 1px solid green;
padding: 0 1em;
}
<div class="brand-wrap">
<a class="navbar-brand" href="#"></a>
<div class="brand-text">
<p>This is a bunch of content that may take up more than one line of text</p>
</div>
</div>
Been trying to vertically center 3 divs within another div and no matter what I try it doesn't work.
Please take a look at my example here:
http://jsfiddle.net/d6sLpxvc/
My html:
<body class="mainBackground">
<header class="wrap">
<div class="menuIcon">Menu Icon</div>
<div class="pageTitle">Good morning, John Doe</div>
<div class="logo">Logo</div>
</div>
</header>
</body>
My CSS:
.wrap {
height: 41px;
background-color: gray;
width: calc(100% - 34px);
margin: 17px auto;
}
.menuIcon {
display: table-cell;
float: left;
vertical-align: middle;
}
.logo {
float: right;
}
.pageTitle {
text-align: center;
vertical-align: middle;
}
The menu icon will actually be a small SVG icon with a fixed size of about ~28px. The logo on the right hand side also has a fixed size which has roughly double the height of the menu icon. I've used placed holder text for those images. The text in the middle MUST be centered horizontally as well within the browser canvas (not the div it's in).
The only thing that I absolutely need to do is use as little hardcoded pixel sizes as possible. So my end design must follow the psd's to the pixel but must be coded in percentages wherever possible. I'd like to stick with using width: calc(100% - 34px) if possible as well since this lets me use math for determining the div size. This means no using px to vertically center - I need to use percentages to vertically center these items in the div because they will change in the future and I cannot go back to adjust them to make sure they are always centered if the height is different(like menu icon or logo). Must work with IE9 - dont care about other browsers.
Would appreciate any help greatly!
You can't use display: table-cell; without display: table; and display: table-row;, plus table cells can't float. this is updated fiddle, is this what you are looking for? More about centering could be found at css-tricks.com
HTML
<header class="wrap">
<div class="row">
<div class="menuIcon">Menu Icon</div>
<div class="pageTitle">Good morning, John Doe</div>
<div class="logo">Logo</div>
</div>
</header>
CSS
.wrap {
height: 41px;
background-color: gray;
width: calc(100% - 34px);
margin: 17px auto;
display: table;
}
.row {
display: table-row;
}
.menuIcon {
display: table-cell;
vertical-align: middle;
}
.logo {
display: table-cell;
vertical-align: middle;
}
.pageTitle {
text-align: center;
vertical-align: middle;
display: table-cell;
}
You just need to apply line-height to the DIV's that are inside .wrap. The line-height property property value should be equal to to the height of .wrap for maximum result.
You can add line-height to them individually but the block of code below will do just that.
.wrap > div {
line-height: 41px;
display: inline;
margin: 0 10px 0 50px;
}
.wrap {
height: 41px;
background-color: gray;
width: calc(100% - 34px);
margin: 17px auto;
margin-top: 60px; /* Just for my presentation */
}
.wrap > div {
line-height: 41px;
display: inline-block;
margin: 0 10px 0 50px; /* Adjust to suit your need */
}
.menuIcon {
display: table-cell;
float: left;
vertical-align: middle;
}
.logo {
float: right;
}
.pageTitle {
text-align: center;
vertical-align: middle;
}
<header class="wrap">
<div class="menuIcon">Menu Icon</div>
<div class="pageTitle">Good morning, John Doe</div>
<div class="logo">Logo</div>
</header>
try this:
.wrap {
height: 41px;
background-color: gray;
text-align:center;
width: calc(100% - 34px);
margin: 17px auto;
}
.menuIcon {
float: left;
line-height: 41px;
}
.logo {
float: right;
line-height: 41px;
}
.pageTitle {
text-align: center;
display:inline-block;
line-height: 41px;
}
<body class="mainBackground">
<header class="wrap">
<div class="menuIcon">Menu Icon</div>
<div class="pageTitle">Good morning, John Doe</div>
<div class="logo">Logo</div>
</div>
</header>
</body>
Look at this question: Vertical alignment of elements in a div
This fiddle is especially helpful: http://jsfiddle.net/techsin/FAwku/1/
Taken from a solution in that fiddle provided by a comment to the linked question:
<div class="top6">
<div class="in6">6</div>
<div class="in6"></div>
<div class="in6"></div>
</div>
.top6 {background-color: blue; height: 100px; display:flex; align-items: center;}
.in6 {background-color: yellow; width: 30px; height: 30px; margin-right: 5px; }
I am trying to align image inside DIV horizontally and vertically. Problem is that I tried several methods and none of them worked for me.
This is code that I am using:
CSS
img{
max-width:100%;
vertical-align: middle;
}
#slika {
float: center;
height: 126px;
width: 111px;
text-align: center;
}
HTML
<div id="slika">
<img src="images/2105602.png" width="auto" height="auto" alt="2105602.png">
</div>
jsfiddle: HERE
Can soemone share his thoughts with me? I can't find solution. It always stays aligned at top.
JSFiddle - DEMO
img {
max-width:100%;
top: 50%;
position: relative;
transform: translateY(-50%);
}
#slika {
height: 126px;
width: 111px;
text-align: center;
border: 1px #000 solid;
}
You can do it by adding a margin of 50% and then a top of -(imageheight/2)
img{
max-width:100%;
vertical-align: middle;
margin-top:50%;
position:relative;
top:-37px;
}
#slika {
float: left;
height: 126px;
width: 111px;
text-align: center;
border:1px solid red;
}
fiddle here: http://jsfiddle.net/dduygx0x/2/
Here is my soluton for your problem using the common table - table-cell way:
I wrapped you image in a new div:
<div id="slika">
<div class="img-wrapper">
<img ....>
</div>
</div>
and altered the CSS:
img{
max-width:100%;
}
.img-wrapper{
display: table-cell;
vertical-align: middle;
}
#slika {
display: table;
float: left;
height: 126px;
width: 111px;
text-align: center;
vertical-align: middle;
}
img{
max-width:100%;
}
.img-wrapper{
display: table-cell;
vertical-align: middle;
}
#slika {
border: 1px solid black;
display: table;
float: left;
height: 126px;
width: 111px;
text-align: center;
vertical-align: middle;
}
<div id="slika">
<div class="img-wrapper">
<img src="http://tommyvirtualnikatalog.com.hr/images/akcija/prehrana/2105602.png" width="auto" height="auto" alt="2105602.png">
</div>
</div>
The benefit of this solution ist that it ist absolut dynamical an can easely made responsive!!
instead of positioning it with hard-coded properties (which would change depending on the image) or using the transform property which wont work in older browsers you can simply wrap the image in a p and set the line-height to match the height of the box
p{
line-height: 126px;
margin: 0;
padding: 0;
}
JSFIDDLE
A solution I've often used is adding an empty span element next to the image, with
#slika span {
display: inline-block;
height: 100%;
vertical-align: middle;
}
The idea is that vertical-align is relative to its siblings, therefore a lone element has nothing to work with. The upside of this method is that its completely dynamic, no fixed pixels, works in older environments ( make sure to test that it meets your lowest-end requirements ) and does not affect the container div. The downside would be the extra html.
I'm trying to use the table-cell way to center a div vertically and horizontally.
It works when I use the following code:
div {
display: table;
}
.logo {
display: table-cell;
position: absolute;
vertical-align: middle;
left: 0;
right: 0;
bottom: 0;
top: 0;
margin: auto;
}
But I'd rather wrap .logo in another div called .center like here JSFiddle, but for some reason, although it works in JSFiddle, it isn't working for me on my site.
Here is a good starting point.
HTML:
<div class="containing-table">
<div class="centre-align">
<div class="content"></div>
</div>
</div>
CSS:
.containing-table {
display: table;
width: 100%;
height: 400px; /* for demo only */
border: 1px dotted blue;
}
.centre-align {
padding: 10px;
border: 1px dashed gray;
display: table-cell;
text-align: center;
vertical-align: middle;
}
.content {
width: 50px;
height: 50px;
background-color: red;
display: inline-block;
vertical-align: top; /* Removes the extra white space below the baseline */
}
See demo at: http://jsfiddle.net/audetwebdesign/jSVyY/
.containing-table establishes the width and height context for .centre-align (the table-cell).
You can apply text-align and vertical-align to alter .centre-align as needed.
Note that .content needs to use display: inline-block if it is to be centered horizontally using the text-align property.
This would be easier to do with flexbox. Using flexbox will let you not to specify the height of your content and can adjust automatically on the height it contains.
DEMO
here's the gist of the demo
.container{
display: flex;
height: 100%;
justify-content: center;
align-items: center;
}
html
<div class="container">
<div class='content'> //you can size this anyway you want
put anything you want here,
</div>
</div>