Vertically and Horizontally Center Text Responsively - css

I'm trying to make a website using a parallax jquery pluin, and i would like the first 'slide' to be a title which is right in the center of the screen.
I've been struggling to do this - horizontally aligning it is simple however I can't vertically align it.
Is there a way to do this which works and is also responsive? I've included a fiddle, to show you the basic structure - http://jsfiddle.net/SohamK/yKDWS/1/. I have also reproduced the html of the fiddle below:
<div class="container">
<div class="title">
<h1> My Title </h1>
</div>
Any help would be much appreciated!
Thanks!

You can use display property like this:
.container {
width:100%;
height:100%;
display:table;
text-align:center;
}
.title {
display:table-cell;
vertical-align:middle;
}
The demo http://jsfiddle.net/yKDWS/13/

$(function() {
$('h1').css({
'position' : 'absolute',
'left' : '50%',
'top' : '50%',
'margin-left' : -$(this).width()/2,
'margin-top' : -$(this).height()/2
});
});
Try this with jquery

You can use the CSS property display:table, and use vertical-align:middle & text-align:center. This is perfectly center your text vertically and horizontally in your div.
Your div .container will have a display:table, and your div .title will have a display:table-cell with the property vertical-align:middle.
Check Can I Use for display:table browser compatibility.
Full CSS will look like this -
.container {
display:table;
}
.title {
text-align:center;
display:table-cell;
vertical-align:middle;
}

Basically this would always align it to vertical middle
.title
{
margin-top:25%;
}
demo here
Whats the exact markup for you page, it depends on that.
If the above demo solves it, its fine, else markup would be required!! :)

Try this : Demo here
CSS
.container {
width:500px;
height:300px;
background:#333;
display: table;
}
.title {
color:#fff;
padding:0;
display: table-cell;
vertical-align: middle;
text-align:center;
}

Related

How do I make a svg icon and a select input box vertically align in a div?

Here is the code:
http://jsfiddle.net/jj9gLxta/1/
#wrapper {
vertical-align:middle;
display:inline-table;
}
#wrapper select {
margin-left:5px;
}
I'd like to make the svg icon and select box vertically aligned.
BTW the svg icon and the select input may be in other sizes, so I need some css to make it applicable to different designs.
Thanks,
This fiddle should have the desired effect. I removed styling from the wrapper element, and made these changes:
#wrapper select {
display:inline-block;
margin-left:5px;
}
#icon {
display:inline-block;
vertical-align: middle;
}
You could also use something like
display: table;
and also:
display: table-cell;
Like the following example:
http://jsfiddle.net/aq8gv4hj/1/
Better explained here:
https://css-tricks.com/centering-in-the-unknown/

Vertically Center - with unknown height

I have two div's side by side. On the left is an image, on the right are inputs.
The image varies depending on what the user uploads.
How can I vertically centre align the image and the inputs? I would like the inputs to appear vertically centre to the image.
Both the img and inputs have their own container:
<div class="img-container">
<div class="data-container">
Fiddle:
http://jsfiddle.net/vbLht/
Remove float:left and try display CSS Rule
.img-container {
width: 50%;
display:inline-block;
vertical-align:middle;
}
.data-container {
width: 50%;
display:inline-block;
vertical-align:middle;
}
Fiddle
Note: You need to set <!-- --> to your mark-up after inline divs as css inline rule will leave white-space between 2 elements, Though this an awkward way but if you don't want to write that crack then you've to adjust width.
Or
Set your li style to display:table with the above CSS Code
li{
display:table; /*remove float*/
}
.img-container {
width: 50%;
display:inline-block;
vertical-align:middle;
}
.data-container {
width: 50%;
display:inline-block;
vertical-align:middle;
}
Hope this is clear :)
Flexbox makes this very easy.
Just wrap the child elements you want to vertically center in a wrapper div with:
.wpr {
display:flex;
align-items: center; /* align vertical */
}
FIDDLE
Browser support is good in modern browsers - caniuse..
but if CSS3 isn't an option, the display:table approach (As mentioned in the other answer) is fine.

Resizable table-cell to shrink smaller than content

I have set up 2 div tags, the outer one with display:table and the inner one with display:table-cell. Inside these I have an image.
When I resize the box using jQuery UI's resizable() API, I am unable to shrink it smaller than the image.
Markup:
<div class="resizebox">
<div class="content">
<img src="http://placehold.it/300x60">
</div>
</div>
CSS:
.resizebox {
border:1px solid black;
height:100px;
width:320px;
overflow:hidden;
display:table;
}
.resizebox .content {
display:table-cell;
vertical-align: middle;
text-align: center;
}
jsFiddle
I've added another example under the top resizable box to demonstrate the kind of behavior I'm trying to achieve (while keeping the CSS Table)
You beat me to it. Just using regular 'ol width: 100%;
img {
width:100%;
}
Fiddle Example
Try adding the following css
.resizebox .content img {
width:100%;
max-width:100%;
}
JsFiddle
Feeling a big silly now.
Fixed this by adding these CSS Styles:
.resizebox img {
max-width:100%;
max-height:100%;
}
jsFiddle

Centered DIV w/ width dependant on text, buffered by two divs that should fill the containing DIV

Thank you all for your help so far. I updated the description, concept image, and JSFiddle link to make things a little clearer.
I have been wracking my brains on this seemingly small issue the whole day. My web dev friends are baffled and I could not find a suitable answer in my search of this site and others (though, I could have missed it somewhere along the way).
Here's what I am trying to achieve:
3 non-fixed-width DIVs within one fixed-width container DIV
The center DIV needs to be centered, and no larger than the text it contains.
The left and right DIVs need to fill the remaining space in the container DIV.
Here are some links to help communicate this concept:
This is what I'd like to end up with
Check out this JSFiddle Link
The basic HTML:
<div id="container" >
<div id="left" ></div>
<div id="center" >Text inside center should resize this block</div>
<div id="right" ></div>
</div>
Below, I removed most of the styles I have tried. This CSS currently centers the DIV (if I set it as an inline block), but I need the other divs to fill the left and right space remaining:
#container {
width:750px;
text-align:center;
border:3px solid #E85355;
}
#left {
background-color:#A3CB46;
}
#center {
background-color:#6D6E71;
display:inline-block;
color:#FFFFFF;
}
#right {
background-color:#1DB0CE;
}
I've tried floating, no-wrap, overflow, etc. Thanks a million to whomever can offer some help!
Try the following CSS. It fills the width of the container...
#container {
width:764px;
text-align:center;
}
#container > div {
display: table-cell;
}
#center {
background-color:#CDD7D7;
}
#right, #left {
background-color:#E85355;
width:200px;
}
EDIT: display:table on container, not needed...
Do you need this ?
CSS
#container {
width:764px;
text-align:center;}
#left {
background-color:#E85355;
width:20px;
height:20px;
float:left;
}
#center {
background-color:#CDD7D7;
display:inline-block;}
#right {
background-color:#65A8A6;
width:20px;
height:20px;
float:right;
}
DEMO
Try this:
jsfiddle.net/SHnc9/36/
You can do it with flexbox! Demo: http://dabblet.com/gist/7187048
Markup
<div class='container'>
<div class='box left'></div>
<div class='box center'>enter text here to see this box grow!</div>
<div class='box right'></div>
</div>
CSS
.container {
display: flex;
}
.box {
flex-grow: 1;
}
.center {
flex-grow: 0; /* to get the box to wrap closely around the text */
}
According to caniuse.com http://caniuse.com/#search=flexbox, it's supported in all the major desktop browsers with firefox having partial support which probably means it uses the old syntax / doesn't support some new properties but the demo worked fine when I checked.
Just be sure to use prefixes(or use a prefixfree / unprefix plugin), add the old syntax for old browser versions (add old syntax below the new ones).
Also, use display: inline-block as a fallback.
You may also want to check out flexie.js http://flexiejs.com/.
Essential reading:
http://css-tricks.com/snippets/css/a-guide-to-flexbox/

Center text vertically centered within a div

I want to center text vertically aligned without using the box property because It does not work in IE9 so I have read it. I have only IE 10 here...
http://jsfiddle.net/J8rL7/6/
I have also tried display:table-cell and vertical-align:middle but this destroyed the whole layout.
Are there any vertical align tricks for my scenario which support IE9+, Chrome/Firefox (latest).
<div id="wrapper" style="margin:auto;background-color:yellow;height:100%;">
<div style="width:50px;height:100%;">
<div class="fluid-column" style="height:80%;background-color:green;">
<div style="display:-webkit-box;-webkit-box-pack:center;-webkit-box-align:center;background-color:#ff99cc;height:25%;">1</div>
<div style="display:-webkit-box;-webkit-box-pack:center;-webkit-box-align:center;background-color:#ff33cc;height:50%;">2</div>
<div style="display:-webkit-box;-webkit-box-pack:center;-webkit-box-align:center;background-color:#ff66cc;height:25%;">3</div>
</div>
<div class="fix-column" style="height:20%;background-color:violet">
<div style="display:-webkit-box;-webkit-box-pack:center;-webkit-box-align:center;background-color:orange;height:50%;">Total</div>
<div style="display:-webkit-box;-webkit-box-pack:center;-webkit-box-align:center;background-color:blue;height:50%;">Test</div>
</div>
</div>
</div>
Let's update this and go old ways, so older IEs should come in the run too:
Let's use specifitie of inline-boxes and use one inline-level empty element to secure vertical-align.
DEMO to test : http://jsfiddle.net/D9gnP/6/ - http://jsfiddle.net/D9gnP/6/show.
body, html {
width:100%;
height:100%;
margin:0;
padding:0;
}
div {
text-align:center;
/* text-indent:-0.5em; to swallow word spacing , should be right value */
}
div span {
display:inline-block;
height:100%;
vertical-align:middle;
width:0;/* no need to have a width, it's got be invisible */
margin:0 -5px;/* this will reduce effect of word spacing to none, it can be a little oversized */
}
If you want to use table-cell, you need to start from the main container drawing the column. and end up with table-cells to use the vertical-align rule.
I added an extra span to get to it :
http://jsfiddle.net/D9gnP/
.fluid-column,
.fix-column{
display:table-row;
width:100%;
}
.fluid-column > div,
.fix-column > div{
display:table;
height:100%;
width:100%;
}
.fluid-column > div > span,
.fix-column > div> span {
display:table-cell;
vertical-align:middle;
}
I just added "text-align: center;" in div tag.
It is done, check below link..
http://jsfiddle.net/J8rL7/15/
http://jsfiddle.net/J8rL7/24/
If you look at http://www.vanseodesign.com/css/vertical-centering/ and the heading: Absolute Positioning and Stretching
It requires adding a span around each text field, and a couple of classes
.vert {
position: relative;
}
.span {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
width: 90%;
height: 30%;
margin: auto;
}
I've used this trick before for vertical alignment:
HTML:
<div id="container">
<div class="center">vertically centered content</div>
</div>
CSS:
#container { white-space:nowrap; height:200px; }
#container:before { content:""; display:inline-block; width:0; height:100%; vertical-align:middle; }
.center { display:inline-block; vertical-align:middle; white-space:normal; }
This creates a pseudo-element before the element with class="center" and uses inline-block so the vertical-align style takes effect.
Here's a jsfiddle so you can check if it works for you: http://jsfiddle.net/Etzpj/
I think that in your case you would need to wrap the text on each cell with another element for this trick to work.
Edit: here i used this trick in your fiddle http://jsfiddle.net/J8rL7/25/

Resources