/    Sign up×
Community /Pin to ProfileBookmark

I’m working on a site here and need it to be centered in the browser. How would I center these divs? Would I have to set a 50% margin to each of the divs? Can I put my whole layout inside a div and center that div?

[url]http://192.168.68.31/dani/default.htm[/url]

to post a comment
CSS

4 Comments(s)

Copy linkTweet thisAlerts:
@ryanbutlerDec 20.2006 — I'll assume this is a fixed width, if so, then place a container div which surrounds you entire layout and assign the following CSS rule:

[CODE]#container{
width:740px;
margin:0 auto;
}[/CODE]


[CODE]<div id="container">
..rest of divs and layout
</div>[/CODE]


In modern browsers, assigning a fixed width to a container div, setting its left and right margins to auto and surrounding the container div inside the entire layout will center it in the browser.

If you're approaching IE 5, then its another ball game to get that browser to play ball, although relatively easy in most circumstances.
Copy linkTweet thisAlerts:
@scottrickmanDec 20.2006 — if you set the css for the body tag to text-align: center then IE will center the content (including older versions I think) then set margin-left: auto; margin-right: auto on the container div for newer IE, FireFox, etc.
Copy linkTweet thisAlerts:
@ryanbutlerDec 20.2006 — Since IE 6 plays ball with left and right margins set to auto, why include it in the main style sheet? Place it in another style sheet specific for IE 5 and then use a conditional comment to check for it in the page.
Copy linkTweet thisAlerts:
@scottrickmanDec 20.2006 — or use a css hack to include (or not) the IE5 specific style sheet, same results different methods
×

Success!

Help @jmilin5931 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...