Trouble Vertically Aligning Text to Middle of Window/Viewport - css

I'm trying to align some text to the bottom of a loading sequence & I think the best way to get part way there is to use vertical-align. The trouble is that is not working.
I have a replica of the code here.
HTML:
<div id="bg_loader" style="background-image:url(http://www.myhhf.com/images/loading/myhhub_loading_4.gif);"></div>
CSS:
#bg_loader {
width: 100%;
height: 100%;
z-index: 100000000;
background-image: url(../images/loading/myhhub_loading.gif);
background-repeat: no-repeat;
background-position: center;
}
#bg_loader:before {
content: "Thank You for Waiting";
display: inline-block;
width: 100%;
height: 100%;
vertical-align: middle;
text-align: center;
font-size: 140%;
font-weight: bold;
color: #080;
}
I have done extensive research on the matter. From what I can tell it should be working. However, I am using a pseudo element to insert my text & I haven't been able to find much documentation on vertical-align & pseudo in these particular types of cases.
I found this article very useful: Vertical-Align: All You Need To Know
I would like to know why vertical-align is not working. I am also open to better methods of how to place my text below my loading sequence responsively. I am aware of calc(), it is what I am currently using.
CSS:
#bg_loader:before {
content: "Thank You for Waiting";
display: inline-block;
position: absolute;
bottom: calc(60% - 14em);
width: 100%;
text-align: center;
font-size: 140%;
font-weight: bold;
color: #080;
}
Update:
I made some edits to Pangloss's code (marked answer below) to make the coded a little more dynamic:
jsfiddle
#bg_loader:after {
content: "Thank You for Waiting";
display: inline-block;
vertical-align: middle;
width: 100%;
height: 13.86em;
line-height: calc(100% + (13.86em * 2) + 1.575em);
text-align: center;
font-size: 140%;
font-weight: bold;
color: #080;
}
Basically, instead of giving the :after element a fixed padding of icky pixels, I gave it a the same height as the image (in beautiful flowy em values) & a line-height calculated to bring the text to the bottom with a bit of padding.
Now, obviously, this is still going to need some work as this won't be compatible with firefox (Firefox does not support calc() inside the line-height.... I have also noticed issues in iPad. I am currently working to diagnose the issue.
I shall try to keep this post updated. (My progress will be tracked here.)

If you set vertical-align on a inline block element, it actually valign the element itself, rather the content inside, and that element is 100% height, so nothing happens, that is main issue there.
Secondly, the valign position is actually relative to the sibling elements' heights (usually the tallest one). And you there isn't any siblings in your example. The guide you have followed is very good, you can read it again, but more carefully.
Updated code snippet:
#bg_loader:before {
content: "";
display: inline-block;
height: 100%;
vertical-align: middle;
}
#bg_loader:after {
content: "Thank You for Waiting";
display: inline-block;
vertical-align: middle;
width: 100%;
padding-top: 270px; /*spacing*/
text-align: center;
font-size: 140%;
font-weight: bold;
color: #080;
}
Full working example:
jsfiddle
html{
height: 100%;
min-height: 100%;
overflow-y: scroll;
}
body{
width: 100%;
height: 100%;
min-height: 100%;
overflow: hidden;
background-color: #F1FAFC;
background-attachment: fixed;
font-size: 80%;
margin: 0;
}
#bg_loader {
width: 100%;
height: 100%;
z-index: 100000000;
background-image: url(../images/loading/myhhub_loading.gif);
background-repeat: no-repeat;
background-position: center;
}
#bg_loader:before {
content: "";
display: inline-block;
height: 100%;
vertical-align: middle;
}
#bg_loader:after {
content: "Thank You for Waiting";
display: inline-block;
vertical-align: middle;
width: 100%;
padding-top: 270px;
text-align: center;
font-size: 140%;
font-weight: bold;
color: #080;
}
<div id="bg_loader" style="background-image:url(http://www.myhhf.com/images/loading/myhhub_loading_4.gif);"></div>

using a relative and absolute relationship for parent and pseudo elements is known to give you more control over positioning. I added:
#bg_loader {
position: relative;
}
#bg_loader:before{
text-align: center;
position: absolute;
top: calc(50% + 120px);
}
using calc() is the best way to maintain pos. control in my experience,
and using text-align for simplicity sake. this will also 'flex' well in the mobile realm.
check the updated fiddle
and some lite reading

Related

Sprite size issue across web browsers

I have a issue with css sprite size across different web browsers. Some of the icons looks like another icon, laying underneath is displayed. There are sections where the same code shows differently the same icon on the web page.
Is this unit calculation problem? What is - in your - opinion best practise to resize sprite? Thanks for a discussion and your help!
HTML
<h1>Wideo</h1>
CSS
section h1 {
position: relative;
display: inline-block;
font-size: 1.9375rem;
font-style: italic;
letter-spacing: -0.0375rem;
line-height: 1.625rem;
word-break: break-word;
text-align: center;
margin: 0 0 1.25rem 1.5625rem;
}
section h1:before {
position: absolute;
top: 6px;
left: -40px;
font-size: 0.9375rem;
}
SPRITE CSS
section h1:before {
background-position: 0em -75.7em;
width: 2.2em;
height: 2.2em;
}
section h1:before {
content: '';
vertical-align: middle;
display: inline-block;
background-image: url(../svg/sprite.svg);
background-repeat: no-repeat;
background-size: 24em 82em;
}
Best regards,
Kuba

Z-index on nested float not working

I'm trying to achieve a sliding-door effect on a span contained within an h2 element. The h2 element has position set to relative in order to enable z-index, so far so good.
The span is floated to the right within this h2 element and has a z-index set to lower than the h2's in order to slide under it when the view contracts, but keep sticking out under as if only the 'float' matters here.
But here's what I get instead, followed by what I wish to get:
Current result of z-indexed span within z-indexed h2:
Desired result
CSS for containing H2 element:
width: 400px;
height: 24px;
font-size: 13px;
vertical-align: bottom;
position: relative;
z-index: 5;
padding: 2px;
color: white;
text-align: left;
background: url('left-door.gif') top left no-repeat;
CSS for nested span element:
width: 200px;
height: 100%;
float: right;
z-index: 0;
text-align: center;
background: url('right-door.gif') top right no-repeat;
Anyone knows why the span refuses to sit level with the h2 even though the h2's fixed height ought to cap it within (setting clear: both on the h2 did nothing), and to be obscured by it when the view shrinks? It is because it's nested?
(Also, 'vertical-align' has no effect, the text doesn't settle on text-bottom but sits in vertical middle. Btw, just got started with this tinkering, but happen to be in a hurry. thanks.)
Edit: After BHouwens answer, i tried by reducing the text content of the H2 (real code has text), and the span aligns level, only problem is the z-index doesn't seem to work, but from what I remember from a previous question, z-index doesn't work on nested elements, as in they can't be stacked above or below their parent. Thanks for helping to troubleshoot! This has helped me focus better, and now think I'll just add two spans within the h2!
May be, below code will work for you!
h2 {
width: 400px;
height: 24px;
font-size: 13px;
vertical-align: bottom;
position: relative;
display: table-cell;
z-index: 5;
padding: 2px;
color: white;
background: #ff0;
}
span {
width: 200px;
height: 100%;
right: 0px;
top: 0px;
z-index: 0;
text-align: center;
background: #f00;
position: absolute;
}
span .wrap {
display: table-cell;
height: 24px;
width: inherit;
vertical-align: bottom;
text-align: center;
}
<h2>
H2
<span>
<div class="wrap">Span</div>
</span>
</h2>
Made a fiddle with your code only, which achieves your desired result: https://jsfiddle.net/dzpfkoLy/
My guess is you've got some other CSS acting on these elements (you can inspect with dev tools in Chrome or Firefox to see what this could be). Otherwise, if you want to ensure the result you need, you can set your span to
span {
// other span styling
position: absolute;
right: 0;
top: 0;
}
Which will shunt it to the top right of the h2. You can then play with z-index to get whatever kind of ordering you're after.

Safari 8 text-indent bug

We've just launched a new site and are experiencing some problems in older versions of Safari with text-indent. A live link is supplied below, hopefully somebody can help me to debug this issue? I've never experience text-indent issue before.
HTML
<ul>
<li>Prev</li>
<li>Next</li>
<li>Scroll Down</li>
</ul>
CSS
li {
padding: 2px 0;
display: inline-block;
vertical-align: text-top;
font-size: 0.9em;
overflow: hidden;
}
#slide-prev, #slide-next, #scroll-down {
margin-left: 10px;
padding: 0;
}
#slide-prev {
width: 18px;
height: 14px;
background: url(images/ui-sprite.svg) no-repeat 0 -406px;
text-indent: -9999px;
}
#slide-next {
width: 18px;
height: 14px;
background: url(images/ui-sprite.svg) no-repeat -40px -406px;
text-indent: -9999px;
}
#scroll-down {
width: 14px;
height: 14px;
background: url(images/ui-sprite.svg) no-repeat -80px -406px;
text-indent: 9999px;
}
Cross Browser Screenshots
Live Link
A better way to hide text is using this method:
.hide-text{
text-indent: 101%;
white-space: nowrap;
overflow: hidden;
}
.slide-prev {
width: 18px;
height: 14px;
background: url(images/ui-sprite.svg) no-repeat 0 -406px;
}
You can add this class to each of the items to hide the text. In your code above, you are repeating a lot and I also wouldn't recommending using IDs like this in your CSS.
<li>Prev</li>
I have resolved this issue with help from this article.
text-indent only works on block level elements, so it's a case of adding that to the CSS. I have changed my text-indent technique to include white-space and overflow as suggested by #paul-redmond, for a more full proof text-indent solution.
The key was being a block level element however.
CSS
#slide-prev, #slide-next, #scroll-down {
margin-left: 10px;
padding: 0;
display: block;
text-indent: 100%;
white-space: nowrap;
overflow: hidden;
}

CSS to center text - Canvas Wootheme

I'm using Wordpress theme Canvas by Woothemes. I'm trying to center the site description text underneath the logo. Here is the site: http://barkhascustomsourcing.com
Here is the current CSS I am using. It's not responsive, I know, but I need to re-write so it is.
#logo .site-description {
display: inline-block !important;
line-height: 25px;
margin-left: -330px;
padding-bottom: 20px;
padding-top: 20px;
text-align: center;
width: 1000px;
}
Any suggestions?
To make this properly responsive you will have to do a lot more than only adjust this class. Heres how you can implement it for your header section:
#logo { width: 100%; }
#logo > a {
width: 100%;
max-width: 350px;
margin: 0px auto;
display: block;
}
#logo > span.site-description {
width: 100%;
max-width: 1000px;
margin: 0px auto;
display: block !important;
}
By making the outer and inner box following the full width but constraining them with max-width will make them responsive.
Now, this is only a small part of your site. I'm not doing the entire thing for you here, but you get the general gist. It also requires you to look at almost any elements, as resizing will require you to make elements that take up a natural; height and expand as their width decreases. So actually, you cannot use any pixel based widths or heights anywhere, only max-widths and max-heights.
PS: Next time you ask a question, please include what you have tried yourself. Show us that you are not outsourcing your work to us :)
Following things you need to update.
#logo {
margin: 0px auto;
width: 100%;
text-align: center;
}
#logo .site-description {
line-height: 25px;
padding-top: 20px;
padding-bottom: 20px;
text-align: center;
width: 100%;
display: block !important;
}

Center an image inserted with after: pseudo-element; can't use spans

I'm pulling in a tumblr feed using tumblr's code, and using after: to add an image as a separator between posts. I'd like to center the image, but haven't had luck doing so. Since tumblr's generating the content, not me, I don't think I can use span tags, which seems to be the usual answer. Any other ideas?
Page showing feed in use: lumn.net/index.shtml
CSS:
.tumblr_post:after {
content: url(../img/flower.png);
display: block;
position: relative;
margin-top: 42px;
margin-bottom: 24px;
margin-left: auto;
margin-right: auto;
}
Try this:
.tumblr_post:after {
content: url("../img/flower.png");
display: block;
margin: 42px auto 24px;
position: relative;
text-align: center;
width: 100%;
}
While the answer from Zoltan Toth works, it's got a bunch of code that does nothing for the desired effect. This should do the trick and with less code.
.tumblr_post:after {
content: url("../img/flower.png");
display: block;
text-align: center;
}
Here is a working solution on all screens :
.produit-col .img-produit::after{
content: '';
background: url("../img/icone.png") center no-repeat;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
display: none;
}

Resources