I have the following css:
#header-notification
{
color: #7B6F60;
font-size: 13px;
font-weight: bold;
float: left;
text-align: center;
vertical-align: middle;
height: 20px;
width: 20px;
background-color: #E5E5E5;
}
and declared the label as:
<label id="header-notification"></label>
However doing so gives me the following:
As you can see the text here is not vertically centered. What am I doing wrong?
As you are using a single character to align it vertically, you can use line-height property here to vertically align in the middle of the element
Demo
#header-notification {
color: #7B6F60;
font-size: 13px;
font-weight: bold;
float: left;
text-align: center;
vertical-align: middle;
height: 20px;
width: 20px;
background-color: #E5E5E5;
line-height: 20px;
}
For making vertical-align: middle; work, you need to use display: table-cell; so that it will align middle vertically.. but you won't need that here as I specified that you are trying to align a single character. display: table-cell; method is generally used when you want to align an entire paragraph or an image vertically inside an element.
Give this a spin: http://jsfiddle.net/jplahn/7zwUc/
You need to place it inside a parent div. (If you want to use vertical-align on it. Obviously there's other ways to do it).
HTML:
<div id="header-notification">
<label id="number">0</label>
<div>
CSS:
#header-notification
{
color: #7B6F60;
font-size: 13px;
font-weight: bold;
float: left;
text-align: center;
height: 20px;
width: 20px;
background-color: #E5E5E5;
}
#number {
vertical-align: middle;
}
Try to add display: table-cell for element:
#header-notification
{
display: table-cell;
color: #7B6F60;
font-size: 13px;
font-weight: bold;
float: left;
text-align: center;
vertical-align: middle;
height: 20px;
width: 20px;
background-color: #E5E5E5;
}
Related
I have 1 div with two separate sections of text. However, one of these sections of text is smaller than the other, leaving it slightly lower than the other section.
I would like it to be centered both vertically and horizontally within the div - inline with the larger text... margin-bottom is not working, however margin-left and margin-right are... how do I do this? Thankyou.
.portfolioHeader {
position: relative;
font-family: coolfont;
top: 20px;
font-size: 55px;
color: black;
margin-left: 70px;
text-align: left;
}
a.miniheader {
text-decoration: none;
margin-left: 20px;
}
span.smallerish {
font-size: 35px;
color: #3F3F3F;
text-decoration: none;
}
<div class="portfolioHeader"><a class="header" href="http://www.coopertimewell.com/portfolio">Portfolio ><a class = "miniheader" href="http://www.coopertimewell.com/portfolio/website-design"><span class = "smallerish">di Matteos</span></a></a>
</div>
This is how I would do it:
Fix invalid html
Put the font sizes on the anchors
Vertical align the anchors
If you want the text horizontally centered, then use text-align center on the div (instead of left with margin)
.portfolioHeader {
position: relative;
font-family: coolfont;
top: 20px;
color: black;
text-align: center;
}
a.header {
font-size: 55px;
vertical-align:middle;
}
a.miniheader {
font-size: 35px;
text-decoration: none;
margin-left: 20px;
vertical-align:middle;
}
span.smallerish {
color: #3F3F3F;
text-decoration: none;
}
<div class="portfolioHeader">
<a class="header" href="http://www.coopertimewell.com/portfolio">Portfolio ></a>
<a class="miniheader" href="http://www.coopertimewell.com/portfolio/website-design"><span class = "smallerish">di Matteos</span></a>
</div>
Set vertical property of anchor tag
.portfolioHeader a
{
vertical-align: middle;
}
You need to vertical-align the 'a' tags.
This css should help.
.portfolioHeader {
color: black;
display: table;
font-family: coolfont;
font-size: 55px;
margin-left: 70px;
position: relative;
text-align: left;
top: 20px;
vertical-align: middle;
}
a {
display: table-cell;
text-align: center;
vertical-align: middle;
}
a.miniheader {
left: 30px;
position: relative;
text-decoration: none;
top: -5px;
}
span.smallerish {
color: #3f3f3f;
font-size: 35px;
text-decoration: none;
}
In have a js-fiddel here - https://jsfiddle.net/zhfs7hxq/
Very simple I have text within a div with text within a span within that div.
I need to vertically center the the span of text against the larger text within the div.
vertical-align: middle; moves it slightly but doesn't center it.
*{
font-family: sans-serif;
}
.name{
background: yellow;
font-size: 40px;
font-weight: bold;
}
.name-inner{
display: inline;
font-size: 20px;
vertical-align: middle;
}
In your code is vertical aligned by the line-height, that's how it works. If you need to center perfectly you can position absolute the element. I modify your fiddle to achieve it:
https://jsfiddle.net/zhfs7hxq/1/
In the first yellow block targets your code.
My modifications is in the second yellow block:
.name{
margin: 10px;
position:relative;
background: yellow;
font-size: 40px;
font-weight: bold;
}
.name.other .name-inner{
position: absolute;
display:inline-block;
font-size: 20px;
top: 50%;
transform: translateY(-50%);
}
Good luck
If you can use flex box, try this JSFIDDLE: https://jsfiddle.net/zhfs7hxq/2/
Note that vertical-align works only on table-cell and inline-level elements.
Flex box solution
Add this to .name to set its display to flex:
.name {
display: -webkit-flex;
display: -moz-flex;
display: -ms-flex;
display: flex;
}
And to .name-inner add the following to align vertically centered:
.name-inner {
-webkit-align-self: center;
-moz-align-self: center;
-ms-align-self: center;
align-self: center;
}
There's a line-height for your code. Try making the line-heights identical:
line-height: 45px;
Snippet
*{
font-family: sans-serif;
}
.name{
background: yellow;
font-size: 40px;
font-weight: bold;
line-height: 40px;
}
.name-inner{
font-size: 20px;
vertical-align: middle;
line-height: 45px;
}
<div class="name">Heading one <span class="name-inner">Heading insert</span></div>
I have a little problem with my css, I can't align an image with a text input field. The site is live at http://jocolina.com/picemon/pokelocator.php
The CSS I have for the text input and image is:
#loginUTC{
width:30%;
height: 60px;
font-family: Verdana;
font-size: 30px;
text-align:center;
font-weight: bold;
/*margin: 0 auto;*/
border-color: #336688;
}
#magniIMG{
display: inline;
height: 60px;
cursor: pointer;
}
#locator{
margin: 0 auto;
text-align:center;
height:60px;
}
Where loginUTC is the text input, magniIMG is the image I want to algin with the input, and locator is the div in which both of the elements are.
You can set both elements to vertical-align: bottom;.
#loginUTC{
vertical-align: bottom;
}
#magniIMG{
vertical-align: bottom;
}
You can use margin-bottom in negative at image that will fix it
#magniIMG {
display: inline;
cursor: pointer;
height: 60px;
margin-bottom: -18px;
}
You could use floats to align the form elements with images like you are trying to achieve here. You'll also need to add some widths to the containing element and the input to align everything. Try -
#locator {
margin: 0 auto;
text-align: center;
height: 60px;
width: 545px;
}
#loginUTC {
height: 60px;
font-family: Verdana;
font-size: 30px;
text-align: center;
font-weight: bold;
/* margin: 0 auto; */
border-color: #336688;
float: left;
}
#magniIMG {
/* display: inline; */
height: 60px;
cursor: pointer;
float: left;
}
I have the following simple code snippet. It is taken out from my application where .a1 is a container button, which has an icon. The icon should be vertically middle aligned to the parents line-height/height, but it is shifted with 1px from top. Could you explain me why this is the behavior? Is there any solution?
.a1 {
display: inline-block;
width: 28px;
line-height: 28px;
background-color: #000;
text-align: center;
vertical-align: middle;
}
.i {
display: inline-block;
width: 16px;
height: 16px;
background-color: #f00;
vertical-align: middle;
}
<div class="a1"><i class="i"></i>
</div>
Why?
Because inline-block elements render with "white-space". You can see this in this demo where no height/width is set on the parent element.
When you use vertical-align:middle; the "white space" is rendered before the element (on top) (black line in the demo). This space moves the child element down and therefore it doesn't appear verticaly centered.
how to fix :
You can use display:block; and calculate the margin to apply to the child element so it centers verticaly and horzontaly.
You can also take a look at this question which talks about white space and ways to avoid them.
Well, it seems like font-size:0; for .a1 seems also a fix for such issue.
.a1 {
display: inline-block;
width: 28px;
line-height: 28px;
background-color: #000;
text-align: center;
vertical-align: middle;
font-size: 0;
}
.i {
display: inline-block;
width: 16px;
height: 16px;
background-color: #f00;
vertical-align: middle;
}
<div class="a1"><i class="i"></i>
</div>
.a1 {
display: inline-block;
background-color: #000;
}
.i {
display: block;
width: 16px;
height: 16px;
margin: 6px 6px;
background-color: #f00;
}
<div class="a1"><i class="i"></i>
</div>
.a1 {
display: inline-block;
background-color: #000;
}
.i {
display: block;
width: 16px;
height: 16px;
margin: 6px 6px;
background-color: #f00
I am trying to vertical align texts in my div.
I have something like
<div id='div'>
<h1>test here</h1>
</div>
#div{
float: left;
padding: 10px;
height: 180px;
width: 130px;
line-height: 180px;
text-align: center;
vertical-align: middle;
}
#div h1{
height: 180px;
line-height: 180px;
vertical-align:middle;
font:normal 0.9em proxima-reg;
}
I can't seem to vertical align the h1 tag texts inside my div. My brain is fried now and I was hoping someone can help me out. Thanks in advance.
Here is one way of doing it:
#div {
float: left;
padding: 10px;
height: 180px;
width: 130px;
line-height: 180px;
text-align: center;
background-color: yellow;
}
#div h1 {
vertical-align: middle;
font:normal 0.9em proxima-reg;
outline: 1px dotted blue;
display: inline-block;
}
See: http://jsfiddle.net/audetwebdesign/sUzK9/
Your #div has well defined dimensions, which makes things easier.
I applied display: inline-block to the h1 and removed any height/line-height values,
and it seems to work.
vertical-align is meant more for table cells than it is for "non-inline" DIV elements.
Give your DIV a fixed width and height, and then try using margin: auto 0px; to vertically align it.
Or...
Give your DIV a top and bottom padding.
Either way, you'll need to text-align your text to center.
Try this (http://jsfiddle.net/myajouri/zyB6C/):
#div{
float: left;
display: table-cell;
padding: 10px;
height: 180px;
width: 130px;
line-height: 180px;
text-align: center;
vertical-align: middle;
background-color: red;
}
#div h1{
display: inline-block;
vertical-align: middle;
font:normal 0.9em proxima-reg;
background-color: green;
}