Centering an Absolute Image using % - css

I am having some problems using percentages to horizontally center an absolutely positioned image. It it for a fluid site and so I need the erasers.png image to be centered in the background. I need to be able to control its size in percentages, so I dont think I can make it a background image.
Is there a way to do this? Or is there a better way to accomplish the same thing?
http://jsfiddle.net/vzPUw/6/

Absolutely position a wrapper for the image (instead of the image), apply text-align:center to the wrapper, and set its left and right properties to 0px. The wrapper will be the same with as the header, but will be taken out of the flow, while the image can continue to behave as an inline element, so that centering works, but will be isolated to the context of the wrapper.
HTML:
<span id="erasersWrapper">
<img id="toperasers" src="http://dl.dropbox.com/u/14080718/Final/images/erasers.png" alt="Erasers">
</span>
CSS:
#erasersWrapper{
position:absolute;
left:0px;
right:0px;
text-align:center;
}

Related

Css width the same as height with unknown height

I'm trying to create one of those little fade effects that Google uses at the end of text instead of an ellipsis or simply cutting the text off with overflow:hidden.
I do this by creating a :before element, that I position over the right hand side.
Here's the mixin I use:
.OverflowFadeRight(#color)
{
position:relative;
&:before {
content:"";
height:100%;
position:absolute;
top:0;
right:0;
width:4.8rem;
.GradientLTR(transparent; #color);
}
}
This code works, but what I would like to do is set the width to the same as height so it's always proportional, which is 100% the height of the parent.
I've seen techiques which set height based on width, but can it be done this way round?
You need to use object-fit: contain to achieve this result.
Turns out that creating a square with height:100% using pseudo elements may not be possible.
The way to create a 'responsive square' is to use the img tag.
It allows to set the height and it will proportionally auto-adjust its width.
The alternative to that is to use percentage off the width.
Here's a demo using either one.
<!-- Empty image 1x1 pixels as gif base64 data -->
<div class="OverflowFadeRight2">Real square with img
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" alt="">
</div>

Absolute positioning of div without overlapping div above

I have a div that I wish to position at the bottom of the webpage. I can achieve this using position:absolute. However, I don't want it to overlap the div above when the window is made smaller. This was achieved by changing it to position:relative however as expected it does not stay on the bottom of the page on bigger screens.
Is there a way in which this is possible?
Current CSS
position:relative;
bottom:0;
background-image:url('.......');
background-repeat:repeat-x;
background-position:bottom;
width:100%;
An example of what I was explaining.
As for me, the best idea is through creating a container DIV for all page content (stretch it to fit all screen using popular practices). Then you can put your footer to the bottom of this container by setting position: absolute and bottom: 0, and don't forget to set padding-bottom: height of your footer to the container. This will prevent overlapping your footer by content of the page.
Try giving min-height to DIV above footer DIV.
When the window becomes smaller, use media queries for that particular resolution or a resolution lesser than that and apply a display:none; to that div with the class that has a position absolute, if you do not want it to display or z-index:0; or z-index:-1; if you want to show it below the contents div.
Hope this helps.
You could set a margin-bottom of the height of the absolute element on the upper div. This way, your absolute positioned element will overflow with the margin instead of the element itself.
The way I see it, you should revert back to position: absolute, then try giving it a low z-index value, such as z-index: -1

CSS take height of absolutely positioned child

I've got a container that's set to a max-width:780px and height is undeclared. Inside the container, there's an image slideshow. Everything on the page is responsive, so as the width decreases, the image (who's width is set to 100%) adjust's the heights container.
The slideshow change's the images to display:static; and position:absolute; which no longer "holds open" the container because it's not seen as content of the container
Is there any creative solution out there to take the height of a child element that's absolutely positioned?
Example below has NO height declared on the main container.. nothing's holding it open.
http://dhut.ch/test/santos/
Thank you!
Are the images all the same dimensions? If yes, you can use a percentage padding-top on the element that contains the images.
So if your images are all, say, 760px wide by 500px tall, that's 500/760 = .65789
Which as percentage would translate into something like:
#main {
position: relative;
max-width: 760px;
padding-top: 65.789%;
}
The reason this works is because with padding if it's set with a percentage, it is calculated as a percentage of the width. As the element shrinks in width, the height will shrink proportionately and the box will remain in the same ratio of width to height. The images, positioned absolutely, won't be adding to the height of the box.
This'll work as long as your images are all the same aspect ratio and you're not expecting that ratio to change. If you'll be using a lot of random images, this isn't for you.
I recently had a similar problem with an image that I needed to absolute position at the top of a Zurb Foundation templated page in order to pull it out of the flow and reset its dimensions (Image had to stretch to edges of wrapper, instead be enclosed by its parent .row padding). However, of course, this meant that all the fluid responsive elements below it popped right up over the top of the image. Setting a margin-top or positioning the sibling elements below meant a rigid top space that didn't resize with the width of the browser.
To get around it, I placed a duplicate of the image right after the absolute positioned image and set its visibility: hidden; I had to add a little bit of extra margin bottom to make up for the difference in height, but the end result is everything on the page flowing exactly to the height of the image in use.
I've also used the padding trick described by unexplainedBacn above, and it's a great trick as well. It takes a little bit of math, but I voted that answer up. Great solution.
I think you'd better change your approach. For sliders, the best practices is to float child elements of the container, and also use one of the known techniques to prevent parent's great collapse. So, I suggest that you remove the position: absolute CSS rule from images and float them inside your <div id='main'>, then use any of these methods to force it to encompass it's children:
div#main {overflow: hidden;}
div#main:after {content: ''; display: block; clear: both; visibility: hidden;}
Add a <div style='clear: both;'> to the end of your main div container.
Remove the absolute position. I would avoid inline styling as well.

HTML: I want to create a DIV thats horizontal centered and reaches from the top to the bottom

I want to create a page with a horizontal centered content block that reaches from teh top to the bottom of the browser window. I already figured out that tables are not the right way to design a layout. A block that reaches from top to bottom is not the problem:
<div style="position:absolute;top:0px;width:800px;height:100%;background-color: #fff;">
</div>
But I'm not able to make this Div centered. I tried
"margin:auto"
But no effect. Th centers the text in the Div, but not the Div itself on th screen.
To center a div you need two things, a width, and automatic horizontal margins. Like this:
#myDiv {
width:800px; /* or whatever */
margin:0 auto;
}
There is no need for absolute positioning, just these two rules will do the trick.
to center an Absolutely Positioned div add left: 50%; margin-left: -400px;
where the negative margin value is half the width of the div
Try not to use position:absolute for layouts unless necessary. This sample shows best practice for horizontally centering your content.
If you need a solution that will continuously work to restrain the content area height within the viewable area, try my jQuery solution: http://jsfiddle.net/BumbleB2na/Z75hA/

Centering of a div, but the normal CSS does not work

I want to center a div, but the general way is
#selector{position:relative;margin:0px auto;}
What exactly is wrong below
#crp{top:40%; position:absolute; margin:auto;}
The Div below is not nested but a standalone. The #crp is going to the extreme right.
<div id="crp">...something goes here....</div>
Thanks
Jean
It is because its position is absolute, it does not work according to margins anymore, it is expecting you to fix its position.
Just like you are setting its vertical position with the top property, you have to set its horizontal position as well.
To center a div using margin:0 auto; you must specify width of the element.
You could also use text-align:center; on the parent container.
Also remove any absolute positioning because it breaks standard element flow.

Resources