I've been trying to center both horizontally and vertically a specific div, and I got the vertical part to center, just not the horizontal part.
I'm using translate(-50%, -50%), which should be centering in a center, but is not working...
The part that says "Or login to your acccount" is the part I'm trying to vertically center.
Link to the site
Relevant CSS:
.logindiv {
position: relative;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
you need to do 2 things:
add a wrapper(this will be the green div), let's call login-wrap, and give it this properties:
.login-wrap {
float: left;
height: 100%;
position: relative;
width: 40%;
}
change relative to absolute here
.logindiv {
left: 50%;
position: absolute;
top: 50%;
transform: translate(-50%, -50%);
}
Try adding margins for horizontal center positioning
.logindiv {
margin-right: auto;
margin-left: auto;
}
transform is working. Try this one. You have to define your transform style for all the browsers.
.logindiv {
left: 50%;
transform: translate(-23%, -38%);
-ms-transform: translate(-23%, -38%); /* IE 9 */
-webkit-transform: translate(-23%, -38%) /* safari*/
width: 382px;
position: relative;
}
Here is your original code:
.logindiv {
position: relative;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
By changing the position to absolute, you can move the logindiv around with the transform: translate() (which does X first, then Y)
I chose 60%, -70% as that looked centered with the left on my screen, but mess around with it. This was the only way I could find to let it resize the same way as the welcome text on the left.
New code:
.logindiv {
position: absolute;
top: 50%;
left: 50%;
transform: translate(60%, -70%);
}
Related
When working with hero images or full screen anything, I typically see text or images with the following bit of CSS:
.item {
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
What is this code actually doing?
The reason why transform: translate(-50%, -50%) is required is because you want the center of the element to line up with the center of its parent. In simple terms, it can be boiled down to translateX(-50%) translateY(-50%), which means:
move me leftwards by 50% of my width, along the x-axis, and
move me upwards by 50% of my height, along the y-axis
This effectively moves the center of the element to its original top left corner. Remember then when you set left: 50%; top 50% on the element, you are moving its top left corner to the center of its parent (which means it is not visually centered at all). By moving the element back leftwards and upwards by half of its width and height respectively, you are sure that its center now aligns with the parent's center, making it visually horizontally + vertically centered.
As a proof of concept, see the code snippet below: hover over the parent to cause the child element's "ghost" to reposition itself by means of transform: translate(-50%, -50%):
body {
margin: 0;
padding: p;
}
.parent {
background-color: #ccc;
width: 100vw;
height: 100vh;
position: relative;
}
.child {
background-color: rgba(0,0,255,0.5);
width: 50px;
height: 50px;
position: absolute;
top: 50%;
left: 50%;
}
.child::before {
background-color: rgba(255, 0, 0, 0.5);
position: absolute;
top: 0;
left: 0;
width: 50px;
height: 50px;
content: '';
transition: all .5s ease-in-out;
}
body:hover .child::before {
transform: translate(-50%, -50%);
}
<div class="parent">
<div class="child"></div>
</div>
TL;DR version
Let's say there is a .container and an .item inside.
This code below is positioning .item relatively to .container; meaning .item top left corner is in the center of its container
.item {
top: 50%;
left: 50%;
}
While the below is positioning .item relatively to its own width and height; meaning minus 50% of its width and height.
.item {
transform: translate(-50%, -50%);
}
If the two code snippets below are combined, then the expected center will show up.
When working with hero images or full screen anything, I typically see text or images with the following bit of CSS:
.item {
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
What is this code actually doing?
The reason why transform: translate(-50%, -50%) is required is because you want the center of the element to line up with the center of its parent. In simple terms, it can be boiled down to translateX(-50%) translateY(-50%), which means:
move me leftwards by 50% of my width, along the x-axis, and
move me upwards by 50% of my height, along the y-axis
This effectively moves the center of the element to its original top left corner. Remember then when you set left: 50%; top 50% on the element, you are moving its top left corner to the center of its parent (which means it is not visually centered at all). By moving the element back leftwards and upwards by half of its width and height respectively, you are sure that its center now aligns with the parent's center, making it visually horizontally + vertically centered.
As a proof of concept, see the code snippet below: hover over the parent to cause the child element's "ghost" to reposition itself by means of transform: translate(-50%, -50%):
body {
margin: 0;
padding: p;
}
.parent {
background-color: #ccc;
width: 100vw;
height: 100vh;
position: relative;
}
.child {
background-color: rgba(0,0,255,0.5);
width: 50px;
height: 50px;
position: absolute;
top: 50%;
left: 50%;
}
.child::before {
background-color: rgba(255, 0, 0, 0.5);
position: absolute;
top: 0;
left: 0;
width: 50px;
height: 50px;
content: '';
transition: all .5s ease-in-out;
}
body:hover .child::before {
transform: translate(-50%, -50%);
}
<div class="parent">
<div class="child"></div>
</div>
TL;DR version
Let's say there is a .container and an .item inside.
This code below is positioning .item relatively to .container; meaning .item top left corner is in the center of its container
.item {
top: 50%;
left: 50%;
}
While the below is positioning .item relatively to its own width and height; meaning minus 50% of its width and height.
.item {
transform: translate(-50%, -50%);
}
If the two code snippets below are combined, then the expected center will show up.
I'm trying to vertically align the first div at the center of the browser and everything below it below that div.
I may be able to wrap these two divs in another div and centering that div may work, but I can't change this html structure and have to achieve with two divs only. The first div is a dynamic container where different html will be displayed. The second div is static.
.center {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
}
<div class="center">
I'm at the center of the Browser!
</div>
<div>I'm just below the center!</div>
Add width and margin: auto 0 to the css class and apply it to the div elements.
like this:
.center {
margin: 0 auto;
width: 200px;
}
<div class="center">
I'm at the center of the Browser!
</div>
<div class="center">I'm just below the center!</div>
This should work (I added the color only to make it easier to see the result)
.center {
width:750px;
margin-left: auto;
margin-right: auto;
background-color:Red;
}
<div class="center">
I'm at the center of the Browser!
</div>
<div>I'm just below the center!</div>
Summary:
You can use this code that is in the jsfiddle.
I wrote two classes named .center-x and .center-y. You can use these classes when you want to center elements by x and y axes.
The code:
.box-1 {
background: #00adef;
padding: 5px 10px;
}
.box-2 {
background: #ccc;
padding: 5px 10px;
position: absolute;
top: 100%;
width: 170px;
}
.center-y {
position: absolute;
top: 50%;
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
}
.center-x {
position: absolute;
left: 50%;
-webkit-transform: translateX(-50%);
-ms-transform: translateX(-50%);
transform: translateX(-50%);
}
.center-x.center-y {
-webkit-transform: translateY(-50%) translateX(-50%);
-ms-transform: translateY(-50%) translateX(-50%);
transform: translateY(-50%) translateX(-50%);
}
<div class="box-1 center-y center-x">
I'm at the center of the Browser!
<div class="box-2 center-x">I'm just below the center!</div>
</div>
And if you want to center by vertical, just remove the .center-x class from .box-1. Else if you want to center by horizontal, just remove the .center-x class from .box-1.
If the browser supports viewport units, you can use this way:
.center {
position: relative;
left: 0;
margin-top:50vh;
}
See it working: http://jsfiddle.net/fgpqkrr4/
I want to create a simple full-screen overlay with loader and text in the center. I have some problems with the text. I want the image to be over ABOVE the text. Can you help me with this?
<div id="loadingOverlay" class="loader-overlay">
<div class="loader-content loader-center">
<img src="http://www.mysarkarinaukri.com/images/loadingBar.gif" class="loader-center" alt=""/>
<div class="loader-center loader-text">Loading, please wait...</div>
</div>
</div>
http://jsfiddle.net/bLz7wgvs/2/
[edit]
Sorry for my English. I meant "above", not "under"...
It should look like:
[-------------loader here-------------]
Loader text (plz wait, etc.) in one line, both centered horizontally and vertically
Edit
I didn't know you wanted to let the image appear above the text. I've changed your code a little bit: http://jsfiddle.net/bLz7wgvs/7/
CSS:
.loader-overlay {
-ms-opacity: 0.9;
background: #444;
display: block;
height: 100%;
left: 0;
opacity: 0.9;
position: fixed;
top: 0;
vertical-align: middle;
width: 100%;
z-index: 100000;
}
.loader-content {
margin-left: auto;
margin-top: auto;
width: 50%;
}
.loader-center {
-moz-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
-o-transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%);
left: 50%;
display: block;
position: fixed;
top: 50%;
transform: translate(-50%, -55%);
}
.loader-text {
color: #FFF;
font-size: 18px;
height: 50%;
}
Try this,
.loader-overlay {
-ms-opacity: 0.9;
background: #444;
display: block;
height: 100%;
left: 0;
opacity: 0.9;
position: fixed;
top: 0;
vertical-align: middle;
width: 100%;
z-index: 100000;
}
.loader-content {
height: auto;
margin-left: auto;
margin-top: auto;
width: auto;
}
.loader-center {
-moz-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
-o-transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%);
left: 50%;
position: fixed;
top: 50%;
transform: translate(-50%, -50%);
z-index:1;
}
.loader-text {
color: #FFF;
font-size: 18px;
z-index: 1;
}
Ive added z-index to the image and loader text :)
You just need to add a z-index to the image (and the associated class required to the HTML element):
.loader-img {
z-index: 10;
}
http://jsfiddle.net/bLz7wgvs/4/
Otherwise, you can just replace the order of the elements in the DOM if you have control over this. Siblings will appear above other siblings if they appear after them in the DOM tree As stated in the spec:
Within each stacking context, the following layers are painted in
back-to-front order:
the background and borders of the element forming the stackingcontext.
the child stacking contexts with negative stack levels (most negative first).
the in-flow, non-inline-level, non-positioned descendants.
the non-positioned floats.
the in-flow, inline-level, non-positioned descendants, including inline tables and inline blocks.
the child stacking contexts with stack level 0 and the positioned
descendants with stack level 0.
the child stacking contexts with positive stack levels (least positive first).
So you can just change the markup to:
<div id="loadingOverlay" class="loader-overlay">
<div class="loader-content loader-center">
<div class="loader-center loader-text">Loading, please wait...</div>
<img src="http://www.mysarkarinaukri.com/images/loadingBar.gif" class="loader-center loader-img" alt=""/>
</div>
</div>
http://jsfiddle.net/bLz7wgvs/6/
The problem is when using <table> (or a <div> with display:table), how can I center this table element vertically?
I used this method of vertical centering:
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
overflow: auto;
When using this method in Chrome (any Webkit or Blink), it works well, but not in Firefox.
When running this fiddle (http://jsfiddle.net/Mu4yd) in Chrome, the table is vertically centered, while in Firefox, it sticks at the top.
Is there something that should be added to make it work in Firefox? Or, is there any other alternative method to vertically center a table in both browsers (at least)?
There is a responsive-friendly and flexible solution. If you don't know the width or height of the div, you can use this CSS3 code:
Vertical align center with transform - fiddle
.valign{
transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
position: absolute;
top: 50%;
left: 50%;
}
You can center it using the following method
#tablelo{
background-color: black;
height: 100px;
width: 100px;
display: table;
margin: -50px 0 0 -50px;
position: absolute;
top: 50%; left: 50%;
overflow: auto;
}
Fiddle Demo