I'm trying to align a button in the middle of a scalable/responsive div. The problem is, the button isn't actually centered here. What am I missing? fiddle
div{
text-align: center;
}
button{
position: absolute;
top: 50%;
left: 40%;
}
When you use position: absolute; for centering elements, you need to negate the margin which are half of the size of the absolute positioned element. So say if your button is 100px in width you need to negate margin-left: -50px;
So in your case, am hardcoding 250px of the width so I negated -125px, same goes for height as well, if the height of the element is say 30px use margin-top: -15px;
button{
position: absolute;
top: 50%;
left: 50%;
margin-left: -125px;
width: 250px;
/* Use height as well if you need so, along with margin-top which
will have 1/2 of the negative value of the elements height */
}
Demo
The other way to achieve this is to use display: table-cell; with vertical-align set to middle
On the other hand, if you do not want to use negative margin you can use calc() as well
button{
position: absolute;
top: 50%; /* Use calc here too if you define height */
left: calc(50% - 125px); /* Half of the elements width */
width: 250px;
}
Demo 2
Try this code:
DEMO
button{
position: absolute;
top: 50%;
left:25%;
width:50%
}
You have to reduce the height/ width of the button from it's position as well.
Try this
button{
position: absolute;
top: 50%;
left: 50%;
margin-left: -112px;
margin-top: -25px;
}
Fiddle
DEMO
Here is your updated fiddle http://jsfiddle.net/pMxty/122/
a Bit of jQuery resolves it.
HTML
<div>
<button>Load my random useless data</button>
</div>
css
div {
text-align: center;
}
button {
position: absolute;
top: 50%;
}
jQuery
function place()
{
var a = $("div").outerWidth() / 2,
b = $("button").outerWidth() / 2;
$("button").css({
"left": (a - b)
});
}
$(document).ready(function(){
place();
$(window).resize(function(){
place();
});
});
Related
What's the proper way to position an HTML element according to a center handle?
In this example:
XXXXXXXXX
|
|
123px
Assume the element should be position at absolute position left: 123px; but the text should be centered at that point, not start at it. The element text is dynamic, so I have no way of setting a static negative margin-left on it.
Is there a pure CSS way to achieve this? The JS way of measuring offsetWidth and then setting left after calculating width / 2 won't neccesarily work in my case due to various limitations.
One posibility is to set a transform translateX -50%
p {
position: relative;
display: inline-block;
left: 100px;
transform: translateX(-50%);
}
<p>ONE</p>
<br>
<p>TWO, LONGER</p>
<br>
<p>THREE, the longest</p>
It's fairly easy to achieve that and there are several ways to do it. Since you didn't post any HTML construct for your example, I'll just make up some.
The trick is to have an inline-block parent element which has the desired offset (123px) and inside that element you'll have another inline-block element with a left margin of -50%. Position both relative and you'll have the effect you are looking for.
#container {
position: relative;
}
#line {
width: 100px;
height: 100px;
left: 123px;
position: absolute;
border-left: 1px solid red;
}
#text {
left: 123px;
top: 50px;
display: inline-block;
position: relative;
}
#text p {
position: relative;
background: green;
margin-left: -50%;
display: inline-block;
color: #fff;
text-align: center;
padding: 10px;
box-sizing: border-box;
}
<div id="container">
<div id="line">
<-- 123px
</div>
<div id="text">
<p>
This is some dynamic text<br>the div has no absolute set width.
</p>
</div></div>
There are other ways as mentioned, probably depends on your general layout/HTML structure. I would definitely take a look at the flex-box properties, this might also be suitable here.
If you want to play around with it, here's a fiddle.
Some of various ways to do this with css:
If your element is a block:
.element{
width: 200px; /* Full width */
left: 50%;
margin-left: -100px; /* Half width */
position: absolute;
display: block;
}
or, if you're using css3:
.element{
width: 200px; /* Full width */
left: calc(50% - 100px);
position: absolute;
display: block;
}
You can also have a non-absolute approach, but the parent element position should be relative:
.element-parent{
position: relative;
}
.element-parent .element{
margin: 0 auto;
}
If you use text-oriented element (inline-block), this works with IE 7+:
.element-parent{
text-align: center;
}
.element-parent .element{
display: inline-block;
}
I've got a fixed container which is vertically and horizontally centred on the page, and an element within that container. Ideally I would like to have this element positioned in the very top left of the window, however I'm struggling to make it work.
This JS Bin illustrates the problem.
https://jsbin.com/nodonatifo/edit?html,css,output
Initially I thought I would just be able to do something like this on the element.
#container {
width: 300px;
height: 400px;
background-color: #55ffdd;
/* Center on page */
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
#element-actual {
background-color: red;
width: 100px;
height: 100px;
position: fixed;
top: 0px;
left: 0px;
}
<div id="container">
<div id="element-actual"></div>
</div>
However that just fixes the element in the top left corner of the parent container, rather than the window.
Is this possible with my current styles?
#container {
width: 300px;
height: 400px;
position: fixed;
top: 50%;
left: 50%;
background-color: #55ffdd;
margin-top: -200px;
margin-left: -150px;
}
If you use translate property then its children div will place relatively to the parent div only even when it is position:fixed so you can use the above code to place #container in center and you red div will be placed relatively to the window not the parent div :)
As Gaurav Aggarwal already pointed out, the fixed element will still be relative to the parent's transformed positioning. If you want the container element to be dynamically positioned (even if it has unknown dimensions), then you could use the following approach and avoid using transform: translate(-50%, -50%) for vertical/horizontal centering.
This method essentially positions the container element to fill the height/width of the window element with top: 0/right: 0/bottom: 0/left: 0, and then centers it vertically/horizontally using margin: auto.
Example Here
#container {
width: 300px;
height: 400px;
position: fixed;
top: 0; right: 0;
bottom: 0; left: 0;
margin: auto;
background-color: #55ffdd;
}
#element-actual {
background-color: red;
width: 100px;
height: 100px;
position: fixed;
top: 0;
left: 0;
}
<div id="container">
<div id="element-actual"></div>
</div>
Easy, add this to the child:
position: sticky;
I have an h2 which is absolutely postioned inside a parent div which is also absolutely positioned. The parent div has a max width of 350px and what I would like to do is center the h2 inside it. I don't want to set left:0 and right:0 on the h2 as this will stretch to fill the 350px max-width instead I want the h2 to grow in width if more content gets added. Absolutely positioning the h2 is a requirement.
Codepen: http://codepen.io/styler/pen/mAyIt
CSS
.tt {
max-width: 350px;
min-height: 45px;
text-align: center;
position: absolute;
left: 0;
right: 0;
background: #8F9924;
.tt-content {
border: 2px solid black;
background: #ACC95F;
position: absolute;
bottom: 0;
padding: 5px 10px;
}
}
HTML
<div class="tt">
<h2 class="tt-content">This is the content.</h2>
</div>
You might find that this helps.
.tt-content {
...
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
reference: http://css-tricks.com/quick-css-trick-how-to-center-an-object-exactly-in-the-center/
A fork of your pen: http://codepen.io/jakeparis/pen/CGjni
Center an absolutely positioned element like this:
Assume 500px width element:
#heading {
margin-left: 50%;
left: -250px;/* negative half-width */
}
UPDATE: Sorry, should have paid better attention to the question.
If you need dynamic width, then consider relative positioning. Then just use text-align:center; on the parent.
UPDATE 2: You'll also need this on your H2 element: display:inline-block;
This will make the div width stay with the child content.
try this
h2{
width: 100%;
position: absolute;
left: 50%;
margin-left: -50%;
}
I'm trying to vertically center text inside a div that is positioned absolutely.
I have tried table-cell approach with no luck. This is a responsive layout, so I'm trying to avoid setting fixed heights and prefer not to use Javascript either.
Thanks
Link to jsbin demo
HTML & CSS:
<div class="page-banner" style="background: url(http://www.bimga.com.php53-3.ord1-1.websitetestlink.com//wp-content/uploads/BIMGA_Website_InteriorPage_Banners_About.jpg) no-repeat scroll 0 0 / cover transparent">
<img style="visibility:hidden" src="http://www.bimga.com.php53-3.ord1-1.websitetestlink.com//wp-content/uploads/BIMGA_Website_InteriorPage_Banners_About.jpg">
<div class="left">
<div class="page-banner-text">this text needs to be verticall centered</div>
</div>
</div>
<style type="text/css">
.page-banner {
margin-bottom: 35px;
list-style: none;
width: 100%;
padding-left: 0;
position: relative;
}
.page-banner img {
width: 100%;
height: auto;
}
.page-banner .left {
background-color: rgba(10, 65, 142, .75);
position: absolute;
z-index: 1;
left: 0;
top: 0;
height: 100%;
text-align: center;
width: 50%;
}
</style>
We could use a transform like so:
Have a jsBin!
CSS
.page-banner-text {
top: 50%;
transform: translateY(-50%);
position: absolute;
}
More information on this technique.
What you can do is, set the text position to absolute.
Then give it a top: 50%; and give it a top margin of minus half its height.
I would not prefer using position absolute and top: 50% for better multi browser support (espesially on older IE versions) so I would prefer adding line-height: x em; in your .page banner class. Em because you have defined the height by % so it needs to always be on the center no matter the actual pixel height.
.page-banner .left:after {
content: "Background text";
position: absolute;
top: 40%;
left: 35%;
z-index: -1;
}
I have the following bootstrap example http://jsbin.com/EKEHeCIX/3/edit.
As you can see in the code above my form resides in the top of content. But I need to reside it in the middle.
Is it possible to make this without explicity setting of top in pixels? Because need to get it work for any screens (not tablets or phones)
You can do this without setting the top pixels.
Use this:
width: 50%;
height: 50%;
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
With the position on absolute, you can make this responsive for any screensize.
You might want to take a look at this:
Absolute Centering
This can help you out with alot of centering/styling issues!
If you know the exact width and height of your element you could do something like:
.centered {
position: fixed;
top: 50%;
left: 50%;
margin-top: -150px;
margin-left: -220px;
}
Setting margin-top as 50% of element's height and margin-left as the 50% of element's width.
And obviously you must add class="centered" to your element.
Here is your example: http://jsbin.com/EKEHeCIX/6
If you don't know the size of the element or it has a mutable size, you could (using jQuery) implement the css code with javascript:
$(function() {
var $obj = $('.centered');
$obj.css({
marginTop: -($obj.outerHeight() / 2),
marginLeft: -($obj.outerWidth() / 2)
});
});
Demo with javascript: http://jsbin.com/EKEHeCIX/7
You can use this trick :
.form-signin {
width: 400px;
height: 230px;
position: absolute;
/* top:50px for header size */
/* bottom:60px for footer size */
top: 50px; bottom: 60px; left: 0; right: 0;
margin: auto;
}
JSBin updated