473,406 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Horizontally centering a DIV (2)

I am trying to center a DIV horizonally, but I want the DIV fluid so
that it is just wide enough to encompass its contents.

I've tried margin:auto with no joy :(

I've set up a demo here:
http://www.thebunnyshed.co.uk/centertest.htm

ps. google screwed up my first post somehow.

May 22 '07 #1
4 2996
lister wrote:
I am trying to center a DIV horizonally, but I want the DIV fluid so
that it is just wide enough to encompass its contents.

I've tried margin:auto with no joy :(

I've set up a demo here:
http://www.thebunnyshed.co.uk/centertest.htm

ps. google screwed up my first post somehow.
You have left-margin:auto and right-margin:auto instead of margin-left:
auto and margin-right:auto ... you also need to set a width or the div
you are using for a container will take up the width of the entire outer
container....this worked for me:

<div style=" width:30em; margin-left:auto; margin-right:auto;
background-color:yellow; ">

--
biff
May 22 '07 #2
biff wrote :
lister wrote:
>I am trying to center a DIV horizonally, but I want the DIV fluid so
that it is just wide enough to encompass its contents.

I've tried margin:auto with no joy :(

I've set up a demo here:
http://www.thebunnyshed.co.uk/centertest.htm

ps. google screwed up my first post somehow.

you also need to set a width

The OP wants the div to stretch laterally, to expand horizontally to
fit/meet the width of its inline content ("just wide enough to encompass
its contents"). So, inline-block has to be a serious option here.

"inline-block
This value causes an element to generate a block box, which itself
is flowed as a single inline box, similar to a replaced element. The
inside of an inline-block is formatted as a block box, and the element
itself is formatted as an inline replaced element."
http://www.w3.org/TR/CSS21/visuren.html#propdef-display

or the div
you are using for a container will take up the width of the entire outer
container....
I believe his best chance is to use

div {display: inline-block; display: -moz-inline-block; margin-left:
auto; margin-right: auto;}

Not tested.

Gérard
--
Using Web Standards in your Web Pages (Updated Apr. 2007)
http://developer.mozilla.org/en/docs...your_Web_Pages
May 23 '07 #3
On 2007-05-23, Gérard Talbot <ne***********@gtalbot.orgwrote:
biff wrote :
>lister wrote:
>>I am trying to center a DIV horizonally, but I want the DIV fluid so
that it is just wide enough to encompass its contents.

I've tried margin:auto with no joy :(

I've set up a demo here:
http://www.thebunnyshed.co.uk/centertest.htm

ps. google screwed up my first post somehow.

you also need to set a width


The OP wants the div to stretch laterally, to expand horizontally to
fit/meet the width of its inline content ("just wide enough to encompass
its contents"). So, inline-block has to be a serious option here.

"inline-block
This value causes an element to generate a block box, which itself
is flowed as a single inline box, similar to a replaced element. The
inside of an inline-block is formatted as a block box, and the element
itself is formatted as an inline replaced element."
http://www.w3.org/TR/CSS21/visuren.html#propdef-display

or the div
>you are using for a container will take up the width of the entire outer
container....

I believe his best chance is to use

div {display: inline-block; display: -moz-inline-block; margin-left:
auto; margin-right: auto;}
Inline-block is the best way to do this, or would be if it were
supported by more browsers. But the way to centre it is not with auto
margins, but with text-align: center on the container.

See CSS 2.1 10.3.9-- auto left and right margins on inline blocks are
treated as zero. You only get that centering behaviour with block boxes.

Inline blocks are like inlines from the point of view of their
container, but like blocks from the point of view of their descendents.

So you center them like inlines, with text-align: center on their
container, rather than like blocks, which you centre with auto margins.

e.g.

body { text-align: center; }
div
{
display: inline-block;
display: -moz-inline-box; /* seems to work here */
border: 2px solid green;
}

<body>
<div>hello</div>
</body>

works in Opera.

The problem with inline-block is that IE (I heard) doesn't do the one
feature you need here, which is shrink-to-fit width. I don't know about
-moz-inline-box (it's -moz-inline-box, not -moz-inline-block) but it
seems to work OK in this example.
May 23 '07 #4
Ben C wrote :
>div {display: inline-block; display: -moz-inline-block; margin-left:
auto; margin-right: auto;}

Inline-block is the best way to do this, or would be if it were
supported by more browsers. But the way to centre it is not with auto
margins, but with text-align: center on the container.
You're right.
See CSS 2.1 10.3.9-- auto left and right margins on inline blocks are
treated as zero. You only get that centering behaviour with block boxes.

Inline blocks are like inlines from the point of view of their
container, but like blocks from the point of view of their descendents.
Correct!
So you center them like inlines, with text-align: center on their
container, rather than like blocks, which you centre with auto margins.

e.g.

body { text-align: center; }
div
{
display: inline-block;
display: -moz-inline-box; /* seems to work here */
border: 2px solid green;
}

<body>
<div>hello</div>
</body>

works in Opera.

The problem with inline-block is that IE (I heard) doesn't do the one
feature you need here, which is shrink-to-fit width. I don't know about
-moz-inline-box (it's -moz-inline-box, not -moz-inline-block)
Yes again! Great post from you, Ben :)
but it
seems to work OK in this example.
GĂ©rard
--
Using Web Standards in your Web Pages (Updated Apr. 2007)
http://developer.mozilla.org/en/docs...your_Web_Pages
May 24 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Jeff Thies | last post by:
I have a series of blocks that are float left that I need centered on the page. <div class="center" align="center"> <div style="width: 100 px;float: left">thumbnail 1</div> <div style="width:...
15
by: red | last post by:
How do I center two side by side divs ? I've been writing css pages for a while but there's one thing tha still eludes me. I can center a div with margin auto. I can place two divs side by side...
10
by: jlub | last post by:
What I'm trying to do is have two different sets of input elements which occupy the same space on the page, only one of which is visible at a time. You switch between the two with a bit of...
5
by: shanti.miller | last post by:
I've got a grid that uses paging and displays a maximum of 10 records at a time. Sometimes the fields have a lot of data in them and the datagrid takes up way too much horizontal space. I've...
3
by: John Pote | last post by:
1. Horizontal centering a <divin browser window. The current trend seems to be to place page content in a fixed width area in the middle of the browser window. How is this achieved? If I use a...
5
by: Markus Ernst | last post by:
Hello This is a test example: http://www.markusernst.ch/anthracite/ http://www.markusernst.ch/anthracite/living_divani.html After googling and experimenting for several hours, I ended up...
3
by: boien | last post by:
I have a div container that i have created now i just want to make sure that its horizontally centered in any browser and in any resolution, so that when ever its on 800x600 or 1280x1024 its STILL...
2
by: lister | last post by:
I want to center a fluid DIV horizontally. I've tried making the margins auto, but this doesn't work. Can anyone help? CSS drives me nuts! demo here:...
1
by: =?Utf-8?B?ZnJhbmt5?= | last post by:
Hello, I've created a table that has two rows that are span across three columns. The third row has three columns, each with an image. The last row is also span accross three columns. The span...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.