We can set a image as background image of a <div>
like:
<style>
#test {
background-image: url(./images/grad.gif);
background-repeat: no-repeat;
background-position: center center;
width:80%;
margin-left:10%;
height:200px;
background-color:blue;
}
</style>
<div id="test"></div>
I need to set a table at the center of a <div>
horizontally & vertically. Is there a cross-browser solution using CSS
?
Centering is one of the biggest issues in CSS. However, some tricks exist:
To center your table horizontally, you can set left and right margin to auto:
<style>
#test {
width:100%;
height:100%;
}
table {
margin: 0 auto; /* or margin: 0 auto 0 auto */
}
</style>
To center it vertically, the only way is to use javascript:
var tableMarginTop = Math.round( (testHeight - tableHeight) / 2 );
$('table').css('margin-top', tableMarginTop) # with jQuery
$$('table')[0].setStyle('margin-top', tableMarginTop) # with Mootools
No vertical-align:middle
is possible as a table is a block and not an inline element.
Here is a website that sums up CSS centering solutions: http://howtocenterincss.com/