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>
Related
I am using icoMoon fonts (https://icomoon.io/) and centre aligning horizontally and vertically inside circle. Its looking in centre in 100% and 80% of browser but when < 80% the icon is not aligning properly. Simliar case, with > 100% of browser(chrome);
Attaching screenshots:-
.uniIcon {
height: 18px;
width: 18px;
line-height: 18px;
border-radius: 100%;
text-align: center;
float: left;
display: flex;
justify-content: center;
align-items: center;
position: relative;
}
.iconmoon-icon-files {
font-size: 12px;
color: #FFF;
vertical-align: sub;
padding-left: 1px;
}
<div class="uniIcon" style="background-color: #161620;"><i class="iconmoon-icon-files"></i></div>
Try setting line height for icons to align them in div center, something like
.iconmoon-icon-files {
font-size: 12px;
color: #FFF;
line-height:inherit;
}
Inherit will make line-height of icon take on the height of its containing div.
I have an image and a span in a button, and I've tried using:
flex with align-items: center
setting span line-height to same height as container
inline-block and vertical-align
but if I don't add a 1px padding-top to the image, it doesn't appear to be truly vertically aligned in the div, while the span text looks fine, and I can't figure out why.
HTML
<button class="metabolite-btn">
<img src="/images/metabolite/icon_question.svg" class="metabolite-btn__image">
<span class="metabolite-btn__text">代謝物質とは</span>
</button>
CSS
.main-wrapper {
.metabolite-btn {
width: $vw-size-115-width-375;
border-radius: 14px;
border: none;
height: 28px;
background-color: $main-color;
padding: 0 0 0 5px;
text-align: left;
cursor: pointer;
&__image {
width: $vw-size-20-width-375;
padding: 1px 0 0 0;
}
&__text {
font-size: $vw-size-12-width-375;
font-weight: bold;
color: $white;
line-height: 28px;
}
}
To align both the elements vertically, you need to add display:flex and align-items: center to parent which is your button (Shown below)
//CSS
button {
display: flex;
align-items: center;
}
Working Demo
How would I make the table-cell 100% width of the parent.
It works perfectly fine when the text is long enough to reach the full width of the element but when the text is not it doesn't want to center whilst using table-cell and vertical-align: middle;
Here is a fiddle:
https://jsfiddle.net/DTcHh/7471/
Update:
Here's a better solution using a flex-box:
jsFiddle
h4 {
height: 50px;
font-size: 1em;
width: 100%;
font-style: oblique;
color: #FFF;
text-align: center;
margin-left: 5px;
display: flex;
justify-content: center; /* align horizontal */
align-items: center; /* align vertical */
}
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 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;
}