Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a div slightly off-center

Tags:

html

css

Usually I would do this by either setting the margin to auto, using position:absolute, or via padding but unfortunately these will not work in this case. I need a div to be about 15 pixels off-center horizontally from the page. The tricky bit is it needs to scale correctly as the page widens. It seems to me that I would need to do this horizontal adjustment based on the center point, rather than the left hand side. How could I achieve this? Thanks.

like image 210
keybored Avatar asked Oct 10 '22 14:10

keybored


1 Answers

Use a container div, which is centered, then its content you can give margin-left:npx - like so:

HTML

<div id="container">
    <span id="green">&nbsp;</span><br />
    <span id="blue">&nbsp;</span>
</div>

CSS

#container{width:100px; margin:auto auto; background-color:red;}
#container span{display:block; width:100%; }
#green{background-color:green; margin-left:10px;}
#blue{background-color:blue; margin-left:-10px;}

See the example coded up here - http://jsfiddle.net/Xpk8A/1/

like image 170
Alex Avatar answered Oct 13 '22 11:10

Alex