Centering a background image in the left of two columns
I have a big image, and some text that I would like to lay out in two columns, both 50% width, with the text in the right hand column and the image in the left hand column. I'd like the left hand column to take its height from the text in the right, and for the centre of the image to be in the centre of the left hand column, both vertically, and horizontally.
So something like this:
[ ]|[----Text-----]
[ ]|[-------------]
[ ]|[-------------]
[ Image ]|[-------------]
[ Centre ]|[-------------]
[ ]|[-------------]
[ ]|[-------------]
[ ]|[-------------]
The standard trick to get the height of the left hand column to match that of the right hand column is to put a container div around both columns so the left column can be full height in that, and to allow the right column to sit on top of the left. So something like this.
<div 开发者_JAVA百科class="container">
<div class="right-column">Lorem ipsum dolor sit...</div>
</div>
div.container {
background-image: url(my_big_image.jpeg);
background-position: ?;
float: left;
width: 100%;
}
div.right-column {
float: right;
background: white;
width: 50%;
}
But then how do I get the centre of my image to the centre of the column on the left? It seems I need the container div to include the right column in order to get the height right, but I need to exclude it in order to get the image centered horizontally in the left column.
Ideally, I'd like to do this without using JavaScript!
You could certainly do this with jQuery, but here is a CSS only method:
DEMO: http://jsfiddle.net/skylar/zXqQB/
HTML:
<div id="container">
<div id="text">
<div id="image"></div>
Lorem ipsum dolor sit amet..
</div>
<div class="clear"></div>
</div>
CSS:
#container { position:absolute; }
#image {
position:absolute;
left:0;
width:50%;
height:100%;
background-color: #ccc;
background-image: url(http://www.maniacworld.com/have-a-nice-day.jpg);
background-position: center center;
background-repeat: no-repeat;
}
#text { margin-left:50%;}
.clear { clear:both; }
I know the standard trick of getting two columns to be the same size, but I like to use a different method involving javascript (or in my case, jQuery). jQuery shouldn't be used just for this, but if your site has it, might as well use it. It also involves a third div.
LIVE DEMO http://jsfiddle.net/ZH4mT/5/
(Add/Remove text from the blah
area to see BG image center horizontally and vertically)
HTML
<div class="container">
<div class="left-column"></div>
<div class="right-column">blah blah blah blah blah ... blah blah blah</div>
</div>
CSS (background colors are for demo purposes)
.container {
float: left;
width: 100%;
}
.left-column {
float: left;
background: #ff0 url(http://dummyimage.com/100x100/000/fff) no-repeat;
background-position: 50% 50%;
width: 50%;
}
.right-column {
float: right;
background:#f00;
width: 50%;
}
jQuery
var l = $('.left-column');
var r = $('.right-column');
if (l.height() < r.height())
l.height(r.height());
else
r.height(l.height());
精彩评论