Cross browser vertical center text - html

Question
I'm trying to vertically center a letter with a big font-size cross browser. It's more difficult that what it seems apparently, since every single approach i've tried (line-height, display:table-cell, transform: translate...) fails with big font-size. The problem exist only in OS X.
I've been trying to resolve this behavior problem for quite some time now, here's a solution that works on Safari only:
div {
width: 200px;
height: 200px;
background: #2b2b2b;
text-align: center;
line-height: 200px;
color: #fff;
font-family: helvetica, arial, sans-serif;
font-size: 200px;
}
<div>O</div>
Here's a Codepen with the code.
Here's a result screenshot in OS X 10.10 with both Safari, Chrome and Firefox:
Update
After hours of extensive testing, i've managed to identify the origin of the problem, the font-family helvetica. On windows the font family does not exist, so the font loaded is only arial or a sans-serif. On OS X helvetica is present, but the font-family differs in the three browser. As i removed the font-family, the letter was vertically-aligned by the line-height correctly. But now, i'd love to understand why is this bug present in the first place.
Here's a codepen with the new code: link
div {
width: 200px;
height: 200px;
background: #2b2b2b;
text-align: center;
line-height: 200px;
color: #fff;
font-family: arial, sans-serif;
font-size: 200px;
}
<div>O</div>

Im not sure if this is right, but I removed the line height and added padding instead e.g. padding: 30px 0 0 0; seemed to do it for me. Not entirely sure if this is what you wanted.

try this fiddle code :
http://jsfiddle.net/x25wh1ym/1/
div {
background: none repeat scroll 0 0 #2b2b2b;
color: #fff;
font-family: helvetica,arial,sans-serif;
font-size: 200px;
height: 100vh;
position: relative;
text-align: center;
width: 200px;
}
span {
height: 100vh;
line-height: 100vh;
position: relative;
vertical-align: middle;
}<div><span>O</span></div>

Related

What can make two elements with equal CSS properties look different? [duplicate]

This question already has answers here:
What makes the text on a <button> element vertically centered?
(7 answers)
Closed 2 years ago.
In the following HTML/CSS page, I'm trying to make a link and a button look the same. The reason is that the element used should follow its meaning (go somewhere vs. do something), while the look-and-feel depends on whatever turns out to look good for users.
*,
:after,
:before {
box-sizing: inherit
}
.button {
margin: 0px;
width: 100px;
inline-size: 100px;
padding: 20px;
color: black;
text-decoration: none;
font-family: sans-serif;
align-items: normal;
perspective-origin: 60.8906px 44.5625px;
transform-origin: 60.8984px 44.5625px;
cursor: pointer;
display: inline-block;
min-height: 4em;
outline: 0;
border: none;
vertical-align: baseline;
background: #e0e1e2 none;
font-weight: 700;
line-height: 1em;
text-align: center;
font-size: 1rem
}
.container {
height: 100px;
}
<div class="container">
<a class="button" href="#">A</a>
<button class="button">A</button>
</div>
The look pretty similar, but in both Chrome and Firefox, they are arranged in a different way vertically. The text seems to be at the same Y, but the background box around it isn't. I would expect such a behaviour if they somehow get different CSS properties, possibly from the user-agent stylesheet.
The interesting thing is that when I have the browser (Chrome) show me all computed CSS properties, all of them are equal. I verified this by copying the properties of each element to a file, then diff'ing the files.
I also checked whether the order of the elements is relevant, but it isn't -- in the sense that the link/button always look the same no matter where in the element order they appear.
Why do the elements look different even though all CSS properties are the same, and what changes are needed to make them look the same?
(The second question is meant to pinpoint the exact fix for the problem, not just some "shotgun approach".)
I have used flexbox for both tags
*,
:after,
:before {
box-sizing: inherit
}
.button {
margin: 0px;
width: 100px;
inline-size: 100px;
padding: 20px;
color: black;
text-decoration: none;
font-family: sans-serif;
align-items: normal;
perspective-origin: 60.8906px 44.5625px;
transform-origin: 60.8984px 44.5625px;
cursor: pointer;
display: inline-block;
min-height: 4em;
outline: 0;
border: none;
vertical-align: baseline;
background: #e0e1e2 none;
font-weight: 700;
line-height: 1em;
text-align: center;
font-size: 1rem;
display: inline-flex;
align-items: center;
justify-content: center;
}
.container {
height: 100px;
}
<div class="container">
<a class="button" href="#">A</a>
<button class="button">A</button>
</div>
You can just use the a or button tag for both elements, then handle their behavior using javascript

Sticky Footer with inner elements with paddings and margins (Ryan Fait)

I have a problem that I just can't wrap my head around, it might be because that I'm tired. I'm using Ryan's Fait sticky footer which I'm sure the most of you are familiar with. I use it regularly and never had any problems with it until now. The design that I'm working has a footer with several inner child elements.
One of the elements .newsletter is using padding or margins to make a wide gap so that a background image can be placed on the body. This is causing a overflow even when entering the correct height. Deleting the padding corrects the problem. I know that using overflow: hidden is an option but I'd rather find out the cause.
Screenshot with margins or padding set within the news letter element:
Screen without margins or padding set in the news letters:
Not working CSS:
body {
background: #212121;
color: #ddd;
padding: 0;
margin: 0;
font-family: "Open Sans", "Helvetica Neue", "Helvetica", Helvetica, Arial, sans-serif;
font-weight: normal;
font-style: normal;
line-height: 1;
position: relative;
cursor: default;
height: 100%;
}
.maincontainer {
min-height: 100%;
height: auto !important;
height: 100%;
margin: 0 auto -424px;
width: 100%;
}
.mainfooter, .push {
height: 424px;
width: 100%;
}
.newsletter {
padding: 1.875rem 0 1.875rem;
}
.newsletter-fix {
background: #000;
padding: 3rem 0 1rem;
color: #ddd;
}
Working CSS:
Simply changing the following values from those mentioned above to the following resolves the issue but I want the padding, or margin gap between the newsletter.
.maincontainer {
margin: 0 auto -394px;
}
.mainfooter, .push {
height: 394px;
}
.newsletter {
padding: 0;
}
What if you put background-color : black to your .mainfooter instead of putting the background-color to the different element inside your footer?
It would seem that I'm extremely tried and stupid! I thank everyone for taking the time to take a look but it seems its as simple as a user error.
The problem was because I was counting from the very first bit of colour, in this case background: #000 on the .newsletterfix and not taking into consideration of the invisible padding above it. Stupid, me.
Example:

Overlay text over an image and align ALL center using only css and/or HTML

I've read through several posts regarding both overlaying text over an image, AND on centering. None of the answers work for my situation. Below is both the HTML and the CSS I'm using. Frankly - the only thing I've found that will work to center my image UNDER my text is to revert to using tags! I'm frustrated to say the least.
It "should" be really simple, but it's not!
#header
{
height: 140px;
position: relative;
/*background: url('http://i105.photobucket.com/albums/m229/ayliea/ColeensDA_Avatar_zps7d63fb7c.jpg');
background-color: #E7D4DE; for testing background colors*/
background-repeat: no-repeat;
background-position: center top;
font-family: Segoe Script, Lucida handwriting, brush script mt,
monotype cosira, Apple Chancery, comic Sans MS Italic;
top: 10px;
}
}
#header p
{font-size: 1.25em; font-weight: normal;}
#header .imgcenter
{
display: inline-block;
margin-left: auto;
margin-right: auto;
z-index:100;
}
.topcenter
{
position: absolute;
top: -20px;
text-align: center;
width: 100%;
font-weight: bold;
color: #0200a1;
font-size: 1.5em;
text-decoration: none;
}
.topmiddle
{
position: absolute;
top:3.5em;
text-align: center;
width: 100%;
padding: 4px;
color: #c0c0c0;
font-weight: bold;
line-height:1.1em;
}
<div id="header">
<img class="imgcenter" src="http://i105.photobucket.com/albums/m229/ayliea/ColeensDA_Avatar_zps7d63fb7c.jpg" alt="Aylissa" />
<div class="topcenter">
<p>Testing My text overlay with<br />trademark symbol and link<sup>®</sup></p>
</div>
<p class="topmiddle">Second line of text that<br />
needs centered on top of image</p> </div>
But unless I use the old-fashioned tag, I just can't seem to get the darn image to both center AND either show (sometimes it disappears!) or have the text overlay it correctly!
Can anyone help me with this issue?
P.S. I used the suggestions from here: http://www.the-art-of-web.com/css/textoverimage/ to do the overlay.
Thanks for any help.
You are just missing to add text-align: center; to your #header
and, as a note, background-repeat: no-repeat; and background-position: center top; are useless properties in your css as it would just affect any image you add as background-image. It won't do anything with a html img
Second note: you can also get rid of margin-left: auto;, margin-right: auto; and z-index:100; in your .imgcenter for a cleaner css sheet (z-index won't ever work on any element unless you add a position NOT stactic in that element, position:static is the position of every single html element by default.)

text-align right and padding right

I've seen this posted everywhere, with no real help, or it being closed for no reason other then moderators feeling it would be 'unhelpful' in the future even though google whips up a nice result summing some 55,000+ relevant results.
So, why won't padding-right work with a parent, and text-align right child?
.rightcbar {
display: block;
font-family: 'Roboto', sans-serif;
text-shadow: 0px 0px 20px #dbd69d;
padding-right: 50px;
height: 152px;
width: 592px;
line-height: 152px;
background: url(rightcbar.png) no-repeat;
}
.rightcbar .rightctext {
display: inline-block;
width: 100%;
text-align: right;
font-size: 25px;
color: #f3f1de;
font-size: 25px;
font-family: 'Roboto', sans-serif;
text-shadow: 0px 0px 10px #aaa;
-webkit-font-smoothing: subpixel-antialiased;
}
The HTML
<div id="rightc">
<div class="rightcbar">
<div class="rightctext">Test</div>
</div>
<div class="rightcbar">
<div class="rightctext">Test</div>
</div>
<div class="rightcbar">
<div class="rightctext">Test</div>
</div>
</div>
Smeegs helped explain exactly why things were not working as I was intending below; if you are interested. Here is the revised, and working code.
.rightcbar {
display: block;
font-family: 'Roboto', sans-serif;
text-shadow: 0px 0px 20px #dbd69d;
padding-right: 50px;
height: 152px;
width: 592px;
line-height: 152px;
background: url(rightcbar.png) no-repeat;
background-position: center right;
-webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
-moz-box-sizing: border-box; /* Firefox, other Gecko */
box-sizing: border-box;
}
.rightcbar .rightctext {
display: inline-block;
width: 100%;
text-align: right;
font-size: 25px;
color: #f3f1de;
font-size: 25px;
font-family: 'Roboto', sans-serif;
text-shadow: 0px 0px 10px #aaa;
-webkit-font-smoothing: subpixel-antialiased;
cursor: pointer;
}
Live example
I think I understand your confusion.
What (I think) you're asking is why when you add padding to the left, it moves the content, but not when you add it to the right.
The answer is that padding makes the width of the div grow. So when everything is to the left (padding and text-align), the div gets wider and and the content is moved.
But when everything is to the right (padding and text-align) nothing moves...right? Wrong.
The div grows to the right the correct number of pixels adding the padding. And the content stays where it is because the offset is happening AFTER the content, not before like when you left align. It's easy to visualize with a border added.
Here is the code with no padding
http://jsfiddle.net/z5PJx/1/
You can see that the text is right up on the edge.
Here is the same code with padding-right: 50px;
http://jsfiddle.net/z5PJx/2/
Two things happened.
The div grew by 50px;
The content was moved left by 50px;
Those changes offset, and the content doesn't move.
In both situation the div's width grows to the right. But the direction of the padding changes.
Try this, on the container holding your text
.rightctext{ box-sizing: border-box; padding-right:10px;}
The box-sizing property will force the container object to take the padding on the right into account.
Hopefully that's what you're looking to achieve. *Note, adjust the px accordingly.

Two png's being displayed different in safari

I've a weird problem that only occurs in Safari 6 on some mac's even with the same version of Safari and OSX.
The problem is three png's being displayed as a button, they're all css background-images and they've been exported by Photoshop's save for web feature from the same file at the same time with the same settings. But still Safari finds it neccesary to display a weird solid color on top of my png. The problem sometimes (partially) disappears when zooming in in the browser. Chrome, Firefox, IE on PC and Chrome and Firefox on Mac display the image normally. Below is a screenshot of the png.
Has anybody seen this problem before? Is it just a bug in Safari or is there some sort of explaination? I can understand if there is a problem with gamma correction or color profiles, but that should not affect two similar image files, or at least affect them the same way so you cannot spot the difference.
I've created a Fiddle where you guys can see the code and the error aswell (if your mac is displaying it weird at least) Click here for the fiddle But this is pretty much the css
.backbutton {
float: left;
margin-top: 20px;
}
.left {
display: block;
float: left;
background: url(http://daanolislagers.nl/btn-arrow-left.png) no-repeat 0 0px;
width: 17px;
height: 30px;
}
.middle {
display: block;
float: left;
background: url(http://daanolislagers.nl/btn-middle.png) repeat-x;
height: 23px;
font-size: 14px;
padding-top: 9px;
padding-left: 3px;
padding-right: 4px;
font-weight: normal;
color: inherit;
min-width: 190px;
text-transform: uppercase;
}
.middle {
min-width: 47px;
}
.right {
display: block;
float: left;
background: url(http://www.daanolislagers.nl/btn-right.png) no-repeat;
width: 3px;
height: 30px;
}
Cheers!