How to vertically align lower case text in mobile safari? - css

With a div that has
height: 15px;
line-height: 15px;
I get these results:
what css can I use to make it v-align the text to middle regardless of its case?

You can also use table-cell display, which is compatible in most of the browsers versions.
You then just need to use text-align and vertical-align to make it perfectly centered.
body, html {
height: 100%;
}
body {
margin: 3rem;
}
span {
background-color: #4900CE;
color: white;
border-radius: 500px;
padding: 1rem;
min-width: 2.5rem;
text-align: center;
display: table-cell;
vertical-align: middle;
}
<span>ONE</span>
<br>
<span>one</span>
I've went ahead and tested this code in most of the iOS browsers with Browserstack. You can find all the results here (some of their hosts are broken, thus the white shots).
You can alternatively use flexbox with something like
span {
display: flex;
justify-content: center;
align-items: center;
}

I think your best bet would to be to work with em and ex here.
I have noticed that uppercase letters seem to be vertically centered by default. Most of the time, they have a height of around two thirds of 1em, while the height of lowercase letters is almost always exactly 1ex.
Knowing these things, you can give the lowercase letters an offset of calc(((-1em / 1.5) + 1ex) / 2), this moves the text up with half the size of the uppercase letters, and down half the size of the lowercase letters.
To be able to do this, you'll have to do some padding trickery, or simply wrap the text into another element, like a span.
Check out this Fiddle
html, body { height: 100% } *, ::before, ::after { position: relative }
.flex {
display: flex;
align-items: center;
justify-content: center;
}
.flex.sans {
font-family: sans-serif;
}
.flex div {
font-size: 17vh;
background-color: #40C;
color: white;
border-radius: 1000px;
padding: .5em 1em;
margin: .25em;
}
#media (max-width: 200vh) { .flex div { font-size: 8.5vw } }
.flex div::after {
content: '';
display: block;
position: absolute;
top: calc(50% - .5px);
left: 0;
right: 0;
height: 1px;
background-color: #FFF;
}
.flex.centered div:last-child span {
top: calc(((-1em / 1.5) + 1ex) / 2);
}
<div class="flex">
Not centered<div><span>ONE</span></div><div><span>one</span></div>
</div>
<div class="flex centered">
Centered<div><span>ONE</span></div><div><span>one</span></div>
</div>
<div class="flex sans">
Not centered<div><span>ONE</span></div><div><span>one</span></div>
</div>
<div class="flex centered sans">
Centered<div><span>ONE</span></div><div><span>one</span></div>
</div>
There's a big chance this will work right away. If it doesn't, you can try changing 1.5 to some other value like 1.4 or 1.75.

See below example:
.capsule{
border-radius:30px;
height:30px;
background:blue;
position: relative;
display:inline-block;
padding:0 15px;
text-align:center;
color:#fff;
font-family:Arial;
}
.capsule>span{
position:absolute;
width:100%;
left:0;
top:50%;
transform: translateY(-50%);
-webkit-transform: translateY(-50%);
-moz-transform: translateY(-50%);
}
.capsule > i {
opacity:0;
}
<div class="capsule">
<span> ONE </span>
<i> ONE </i>
</div>
<hr/>
<div class="capsule">
<span> one </span>
<i> one </i>
</div>
<hr/>
<div class="capsule">
<span> TWO </span>
<i> TWO </i>
</div>
<hr/>
<div class="capsule">
<span> two </span>
<i> two </i>
</div>
This will work on all browsers except IE 8. You can see working example here.

What if your word would have ascenders ( the part of the letters going higher to the letter x like ftlkidhjb ) or descender (like jyqp) then the text will poke out of the edges of your button. That's why it seems not centered. The only solution to make it feel centered is to make the button as high as 2 or 3 times the line height.

You can use flex:
.badge {
width: 100px;
height: 50px;
border-radius: 50px;
background: blue;
color: white;
display: flex;
display: -webkit-flex; /* Safari prefix */
justify-content: center;
-webkit-justify-content: center; /* Safari prefix */
flex-direction: column;
-webkit-flex-direction: column; /* Safari prefix */
font-family: Arial;
font-size: 14px;
text-align: center;
}
.badge.large {
font-size: 24px;
}
<p>Large badge:</p>
<div class="badge large">
ONE
</div>
<p>Normal badge:</p>
<div class="badge">
two
</div>
JSFiddle

Here is the solution. The problem is basically font Vertical Metrics which is actually issue.
I recommend use google font if you can if not than,
Go to http://www.fontsquirrel.com/tools/webfont-generator
Note: Do not use blacklisted font. If you want, go for license.
Upload the font you want to use;
choose “EXPERT…”
Select EOT Lite if you require IE support;
In Rendering section, check “Fix Vertical Metrics”; (This is Important)
This will fix Vertical Metrics which will fix your line-height issue.
CSS Recommendation:
Use default body line-height 1.4 or 1.42857143 do not relay on line-height: normal because different browser has different default value.

To make lower case text aligned you have to offset it manually to compensate for the extra height that would have to be occupied by upper case text, but that would make uppercase text misaligned. You can make your divs look inline or inline-flex and then you can simply force all text in these badges to be uppercase:
.up div {
height: 15px;
line-height: 15px;
background-color: blue;
color: white;
border-radius: 500px;
padding: 0.3em 0.7em;
display: inline;
font-family: arial;
text-transform: uppercase;
}
<div class="up">
<div>ONE</div> <div>One</div> <div>one</div>
</div>
Or lowercase+offset:
.low div {
height: 15px;
line-height: 15px;
background-color: blue;
color: white;
border-radius: 500px;
padding: 0.2em 0.7em 0.4em 0.7em;
display: inline;
font-family: arial;
text-transform: lowercase;
}
<div class="low">
<div>ONE</div> <div>One</div> <div>one</div>
</div>
Here's the output on iPhone 5:

Related

How do I remove this extra span height?

I just stumbled upon this issue where the height of the <span> is greater than the font size I have set. Here it is:
span {
font-size: 36px;
padding: 0px;
line-height: 1;
background:red;
}
<span>Hello world</span>
Even with line-height: 1 and padding: 0px the span seems to get an extra 4 pixels of height. I noticed that display: block solves the issue but in my case that's not something practical because I need it inline.
Is there any 'trick' which would do this?
Try display: inline-block;
span {
font-size: 36px;
padding: 0px;
line-height: 1;
background: red;
display: inline-block;
}
span.last {
display: inline;
}
<span>Hello world (inline-block)</span> <span class="last">Hello world (inline)</span>
You can do it like this using css, use percentage as line-height. As your font have extra top and bottom space with total of 25%, So I gave 75% line-height
span {
font-size: 36px;
padding: 0px;
background: red;
line-height: 75%;
display: inline-block;
}
<span>Hello world</span>

text background new line padding issue

I am dealing with text blocks (background blocks over text) and face some issues with paddings on new line. The problem occurs when the browser(e.g. mobile) cuts the text into to two lines due to lack of width. text then looks like this:
I don't really know how to set a padding css on the end of the new lines, since it could break up anywhere of the sentence. You could say put a span on it with padding, but it is not fixed where the line will break down. It depends on the width. Any recommendations?
You could apply display: inline-block but that will turn the background color into an ugly box which doesn't look as nice as having an exact width background for each line. Unfortunately CSS doesn't let us target individual lines except for the first one.
If you don't mind getting a little "creative" (or hacky) you could wrap each word in its own element in the backend or using JavaScript and apply the background color to those elements. Adjust the parent's word-spacing accordingly to eliminate gaps.
.main {
font-family: sans-serif;
font-weight: bold;
background-color: #99c;
display: flex;
height: 400px;
flex-direction: row;
align-items: center;
}
.text-container {
max-width: 500px;
display: inline-block;
word-spacing: -15px;
position: relative;
padding-left: 20px;
overflow: hidden;
}
.text-container::before {
content: '';
background-color: black;
position: absolute;
top: 0;
left: 0;
width: 20px;
height: 100%;
z-index: 1;
}
span {
font-size: 36px;
line-height: 1.5em;
color: white;
background-color: black;
padding: 0.25em 0.5em 0.25em 0;
max-width: 360px;
}
<div class="main">
<div class="text-container">
<span>A</span> <span>Movie</span> <span>in</span> <span>the</span> <span>park:</span> <span>Kung</span> <span>Fu</span> <span>Panda</span>
</div>
</div>
You can use box-shadow for this issue and display inline:
<div class="text">
<span class="text-container">A Movie in the park: Kung Fu Panda</span>
</div>
And css:
.text > span {
display: inline;
box-shadow: 25px 0 0 black, -10px 0 0 black;
background-color: black;
color: white;
}
Try to add after "Park:" and before "Kung"
padding workded!!!
change width by console browser and see result:
h1{
background-color: #ff6a6a;
padding: 33px;
display: inline-block;
word-wrap: break-word;
width:300px
}
<h1>rert ert erttttttttttttttt 00000000000000000000 dfgdfgd dfgdfgdft ertert </h1>
Use <p> tag to wrap up the text and it apparently works demo
<div class="main">
<div class="text-container">
<p id="test">A Movie in the park: Kung Fu Panda</p>
</div>
</div>
css
.main {
font-family: sans-serif;
font-weight: bold;
background-color: #99c;
display: flex;
height: 400px;
flex-direction: row;
align-items: center;
}
.text-container {
max-width: 400px;
}
p {
font-size: 36px;
line-height: 2em;
color: white;
background-color: black;
padding: 0.5em;
max-width: 360px;
}

Unfixed Responsive design styling

Updated!!!
I've still facing the same error each time I view the page on landscape screen devices.
CSS:
f5 {
text-transform: uppercase;
color: #fff;
font-size: 30px;
font-weight: bold;
}
about.us
<div class="containerpreview">
<br>
<f5>Check out our products</f5>
<br>
<f6>and Experience no-frills delivery</f6>
<div style="margin-top:40px">
<div class="buttoneshop">Eshop</div>
</div>
</div>
is there a way to make the spacing in between closer? When I command out
text-transform: uppercase;
the spacing is fine.
Aside of that, is there a way to make an image inside src under href to be centered?
css
.images_1_of_4 img {
max-width: 100%;
<!-- height: 200px; -->
width: 200px;
align: middle;
}
.img {
display: block;
width: 100%;
max-width: 100%;
height: auto;
}
.grid_preview {
height: 200px;
}
shop.php
<div class="grid_1_of_4 images_1_of_4">
<div class="grid_preview">
<img src="..." alt="">
</div>
</div>
Try to change alignment text-align : justify; to text-align : left.
Or, if this is not what you like to do, letter-spacing is a CSS attibute to change space between charagcters, and word-spacing to change space between words
As other users suggested, consider forcing alignment on the left using text-align : left, you probably have text-align : justify; on a wrapper element or setted in another part of your css as depicted the example below.
https://jsfiddle.net/s5p9872t/
.f5 {
text-align : justify;
width: 250px;
}
.f5 {
text-transform: uppercase;
font-size: 30px;
font-weight: bold;
text-align: left;
}

Vertically centering text within circles created with border-radius

Today I was having an awful lot of trouble getting some small text vertically centered within elements that were circle which were created using border-radius.
Some of the elements looked fine, but one in particular (a lowercase e was too close to the bottom); I had 2px of padding and it seemed to look fine; however once viewed on a mobile device it was slightly lower.
Here is some code that is as close of a replica as I could come up with to show the issue; you will notice this text has a similar issue with the lowercase e being too close to the bottom.
HTML:
<div class="option">
<span class="icon">t</span>
<span class="text">123456789</span>
</div>
<div class="option">
<span class="icon">f</span>
<span class="text">123456789</span>
</div>
<div class="option">
<span class="icon">e</span>
<span class="text">moo#moo.com</span>
</div>
CSS:
.option {
margin-bottom: 10px;
font-family: 'Source Sans Pro', sans-serif;
font-size: 14px;
}
.option .icon {
display: inline-block;
text-align: center;
width: 24px;
height: 24px;
line-height: 24px;
color: #fff;
border-radius: 50%;
background-color: blue;
}
.option .text {
padding-left: 10px;
}
JSFiddle: http://jsfiddle.net/7bygsgn1/7/
Whilst I haven't tried them with the particular code on jsfiddle, when I was having the issue today I tried a whole range of centering techniques including:
Using line-height
Using absolute positioning
Using vertical-align: middle; in conjunction with display: table-cell;
Negative Margins
Using the method explained here.
Either it had no affect on the centering or caused the shape of the circle to change.
Is there any way you can reliably vertically center in situations such as this?
You may use an inline-block pseudo-element with an height of 24px / 100% , and vertical-align it to middle.
.option {
margin-bottom: 10px;
font-family: 'Source Sans Pro', sans-serif;
font-size: 14px;
}
.option .icon {
display: inline-block;
text-align: center;
width: 24px;
height: 24px;
color: #fff;
border-radius: 50%;
background-color: blue;
}
/* here the pseudo-element method */
.option .icon:before {
content: '';
display: inline-block;
padding-top: 100%;/* cause here we have a square and width for percentage vertical (padding/margin) is the reference , height:100%; or height:24px; will do as well */
vertical-align: middle;
}
/* end update */
.option .text {
padding-left: 10px;
}
<div class="option">
<span class="icon">t</span>
<span class="text">123456789</span>
</div>
<div class="option">
<span class="icon">f</span>
<span class="text">123456789</span>
</div>
<div class="option">
<span class="icon">e</span>
<span class="text">moo#moo.com</span>
</div>
or display:flex; , the most simple:
.option {
margin-bottom: 10px;
font-family: 'Source Sans Pro', sans-serif;
font-size: 14px;
}
.option .icon {
/* next-three lines to center content both axis */
display: inline-flex;
justify-content:center;
align-items:center;
/*text-align: center;*/
width: 24px;
height: 24px;
color: #fff;
border-radius: 50%;
background-color: blue;
}
.option .text {
padding-left: 10px;
}
<div class="option">
<span class="icon">t</span>
<span class="text">123456789</span>
</div>
<div class="option">
<span class="icon">f</span>
<span class="text">123456789</span>
</div>
<div class="option">
<span class="icon">e</span>
<span class="text">moo#moo.com</span>
</div>
Vertically/Horizontally center anything inside a parent element without knowing the heights/widths of either:
/* This parent can be any width and height */
.parent {
text-align: center;
/* May want to do this if there is risk the container may be narrower than the element inside */
white-space: nowrap;
}
/* The ghost, nudged to maintain perfect centering */
.parent:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
margin-right: -0.25em; /* Adjusts for spacing */
}
/* The element to be centered, can also be of any width and height */
.centered {
display: inline-block;
vertical-align: middle;
}
Essentially this creates a ghost element inside the parent that allows the child to be positioned relative to it. The height: 100% allows the vertical-align: middle to do its job properly.
Hope this helps!
Edit: Credit to https://css-tricks.com/centering-in-the-unknown/

Make div size as per contents and horizontally center inside it's parent

I have a div message which basically has a text and a link. I want its size to be changing based on the string inside it. Also I want this message div to be centered inside its container.
I have been playing with it for a while without much luck. Here is the link to JSfiddle: http://jsfiddle.net/pDYJ8/
Also I don't know how make that text and link appear one after other ( not on the new line )
Here is the code:
<div class="container">
<div class="message">
<p>do you want to </p>
<a href="somewhere" target="_blank">
buy this product
</a>
</div>
</div>
.container {
position: absolute;
background: #666;
top:25%;
width:100%;
font-size: 15px;
color: #e47911;
}
.message {
margin-right: auto;
margin-left: auto;
background: #ddd;
width:100px;
}
Tried display inline block to fit its content but then it wouldn't center it inside its parent.
Keeping width 100px for now just to mock my requirements
Just Tweak Some CSS
See the demo fiddle.
.container {
position: absolute;
background: #666;
top:25%;
width:100%;
font-size: 15px;
color: #e47911;
text-align: center; /*added*/
}
.container .message {
display: inline-block; /*added*/
text-align: left; /*added*/
background: #ddd;
}
.message p { /*added*/
display: inline-block;
}
Explanation
The text-align center causes the now inline-block display of .message to center, which is then reset to have its own text-align back at left (this is not necessary). To get the a on the same line, the p also needs to be some type of inline display, here I chose inline-block as well..
I think you are over complicating things. All you need is a text-align: centeron the container and a display: inline-block on the message:
.container {
background: #666;
font-size: 15px;
color: #e47911;
text-align: center;
}
.container .message {
background: #ddd;
display: inline-block;
}
http://jsfiddle.net/Pevara/pDYJ8/9/
The inline block makes the div act as a word inside text, and the text-align center makes the 'word' align to the center...
Here is a simplified approach to a couple of the answers given. It reduces the amount of HTML and CSS needed.
CSS
.container {
color: #e47911;
text-align: center;
}
.message {
display: inline;
background: #DDDDDD;
}
HTML
<div class="container">
<p class="message">
Do you want to buy this product?
</p>
</div>
I would definitely put your anchor tag, <a> inside the paragraph tag, <p>.
You could even remove display: inline; from .message if you made it a <span> rather than a <p>.
Check this out:
http://jsfiddle.net/pDYJ8/10/
Changes made to above link
.container .message {
margin: 0 auto;
width:auto;
}
span{
background: #ddd;
display:inline;
}
You can simplify it with display: table; and margin: 0 auto;
.container {
display: table;
margin: 0 auto;
background-color: #DDD;
}
<div class="container">
do you want to buy this product
</div>

Resources