473,770 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3010
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....th is 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.orgwrot e:
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
15031
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: 100 px;float: left">thumbnail 2</div> <div style="width: 100 px;float: left">thumbnail 3</div> </div>
15
13507
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 with float. But I can't center two side by side divs. If I float them and give them auto margins, the auto margins are ignored. If I wrap the two floated divs in another div, the two divs have no
10
3915
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 javascript. How I've done it is to have one of the two sets be absolutely positioned. Here is a short version: <html> <body> <script> function toggleAdvanced() {
5
2859
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 tried setting the height and it doesn't do anything. I can set the width to some very large number like 3000px and that works ok, but it's a waste when the data doesn't require that much space. Is there a way to limit the height of the grid or of...
3
4211
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 top level <divthen I can place it with CSS attribute 'left:', but this is a fixed offset. Is it possible to have a <divcentered in the browser window? 2. Verticle centering in <div>
5
4825
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 doing this demo with tables. The main problems are the vertical centering of the info area, and of the text inside the squares.
3
2744
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 centered horizontally no matter what, instead of sticking to the left side of the browser window in different res' or window adjusting of any kind. Thank you kindly
2
8556
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: http://www.thebunnyshed.co.uk/centertest.htm ps. is there a simpler / cleaner way to create the above layout? I do my best to stay away from DIV soup, but it still ends up horrible :-(
1
2287
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 rows are centering their data. however, the row with three columns, each with images (myimages1-3) are not centering with the rest of the table. Any idea why? Thanks in advance!
0
9618
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10101
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9906
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6712
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.