How to focus on <div>?
Possible duplicate:
How to center a div into a div?
I want to center the <div>
(I am new to HTML5 and the <center>
no longer exists) and, in any case, avoid using a table. This is what I have CSS for the <div>
in question:
#roundedcorner { -moz-border-radius: 15px; border-radius: 15px; background-image: url(file:///Macintosh%20HD/Users/julesmazur/Desktop/TAN3.0/Photos/bodydivbg.png); width: 960px; left: 50%; font-family: Ubuntu; }
(for anyone curious, Ubuntu is kindly provided by Google Web Fonts).
+7
Jules
source share4 answers
Typical trick margin: auto
. He will center him within what he has ever been.
demonstration
In addition, I have a few comments on your code (since you are a beginner).
left: 50%;
does nothing if you do not change the style of the position, for example,position: relative;
orposition: absolute;
- The
div
container should be bigger than it - otherwise its centering really means nothing. This is because styles such asmargin: auto
refer to the parent container, i.e. He needs space. - For Google web fonts, make sure you have
@font-face
. Also use alternatives. Not all browsers support web fonts, and the more control you have: the better. An example would befont-family: Ubuntu, Arial, Sans;
ordered from most preferred for most backups.
+8
Fakerain brigand
source shareDo not use left
. This will create a gab on the page on the left. Use margin: 0 auto
instead.
<div style="margin:0 auto;width:960px">I'm centered.</div>
+6
kba
source shareYou can center the DOM elements that are absolutely positioned (horizontally and vertically) using:
div { position : absolute; width : 960px; height : 500px; left : 50%; top : 50%; margin-top : -250px; margin-left : -480px; }
Here is a demo: http://jsfiddle.net/KBHjT/
0
Jasper
source share