473,799 Members | 3,009 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Center DIV?

Hi,

I've posted this question a few months ago but didn't get a truly satisfying
answer then. So I'd like to post my question again:

How can I center a DIV? The content in the DIV should be left-aligned, but
the DIV itself should be centered, like:

----------------------------------
| |
| =============== ==== |
| | Foo | |
| | Another foo | |
| | Still a foo-bar | |
| =============== ==== |
| |
| |
----------------------------------

There should be no fixed widths. The width of the DIV should be the width of
the content.

TIA,
Axel Dahmen
Jul 21 '05
32 14683
> That wasn't part of the original question, you can't expect a correct
answer if you omit that. If you then start a new thread instead of
making this clear in the original thread, whilst complaining that your
earlier question wasn't answered properly you can expect the response
you got.
Here's a quote from that previous thread:

"And actually I'm not really satisfied with providing a fixed width. I'd
prefer to have the with of the div be the width required by the content. Is
there a solution without providing a width?"

That question (which was an immediate follow-up) remained unanswered...
The correct answer (a FAQ in this group btw) is that the proper method
to achieve what you want is to use an inline-block element, like so:

<div style="text-align:center">
<div style="display: inline-block;text-align:left">
Foo<br>
Another foo<br>
Still a foo-bar<br>
</div>
</div>

This is currently supported by Opera (buggy), iCab and Safari, it can be
hacked to work in IE, but it's not supported in Gecko based browsers.

For a single instance of the inner box you could use a css table, but
this is not supported by IE at all.

Although it's probably possible to create something that works in all
modern browsers + IE, the solution would be so convoluted that you are
better of scrapping the idea, or use html tables.


That's exactly what I wanted to know. Thank you. :)

Axel
Jul 21 '05 #31
Gus Richter wrote:
Axel Dahmen wrote:
<div style="display: table; width: 0%; margin-left: auto; margin-right:
auto; border: 2px solid black;">
blabla<br>
ölrkgölgkad<br>
blabla<br>
</div>


I think this is it! It seems a correct solution. I guess it's a bug
for IE
not using the correct layout if the "width: 0%" is missing. But as
width is
ignored for "display: table" according to
http://www.w3.org/TR/2003/WD-CSS21-2...o-table-layout,

it doesn't break the code anyway.

Thank you for helping me with this great idea! :)

Regards,
Axel

Works great for me except for one small item that has to be noted. On an
interword space a new line is generated, so &nbsp; must be used instead.


Add white-space:nowrap; instead and use the normal interword space.

--
Gus
Jul 21 '05 #32
merci!
Jul 21 '05 #33

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

Similar topics

7
3245
by: Alex | last post by:
Hi Everone, I need some advice on how to setup 4 columns where the outside two are absolute (120px) and the inner two (side by side) are relevent (Fluid) and change with the screen. Here's my rough layout:
2
7916
by: Claude Shea | last post by:
Is DB2 Development Center an addon product that I have to download or purchase separately? I can't seem to find it in any of my downloads or on any of my CDs. Is it available for all versions of DB2? I currently have two versions of DB2. My production environment is DB2 UDB WSUE V8.1 on a Redhat Linux 7.2 . Currently in the process of creating a new production environment which is DB2 UDB Workgroups V8.2 which is installed on Suse 9...
1
1810
by: Henry Reardon | last post by:
I have been having problems with the Development Center for several days now. It seemed to be working fine when I initially installed DB2 (LUW) Version 8 (FP7) and upgraded to FP8, except that I kept getting SQL1131 whenever I tried to prepared a stored procedure. As a result, I completely deleted DB2 and reinstalled version 8 and upgraded to FP8. At that point, I experimented a bit with some of the Environment Settings, particularly the...
4
6288
by: Joey | last post by:
Does anyone know how to center this sample webpage in Firefox? If so, will you provide the example as it applies to this sample webpage? I have read numerous posts and articles on how to center content with CSS...none are working for me. The best I can do at this point is the "text-align: center" for my div tag, but that only works in IE. I want to be able to center all the content within the div (effectively center the site content) for...
1
3488
by: judacris | last post by:
I've seen the threads here about molding 2 divs in a centered fashion. but I can't seem to solve this thing. my blogger blog is functioning well on my site for now, but the blog feed (left) and the sidebar (right) doesn't center here's the css i'm modifying to center it. i apologise for the mess. <html> <head> <title>cris-kun's 'Blog - www.puff-x.com</title> <$BlogMetaData$>
24
3631
by: GloStix | last post by:
I'm trying to center this banner, it's in a div that has the same width so it's not exactly "centering" but it's screwed up, It works in safari but in Firefox it's messed up. I uploaded a Video to show what I'm talking about. http://screencast.com/t/i5SLg3cAczH (*WARNING, TAKES A WHILE TO LOAD*) *HTML* <html> <head> <title>James Mann - Portfolio</title>
2
16368
by: yangtono | last post by:
Hi, I am creating a table to list some data. The table is using a sorting and highlight function that I found from the net. I can't attach image here, basically html will wrap the heading, therefore I notice the second line of the header is left align instead of centering. I believe it's because of the sort.htc script that will output an arrow when user click on the header to sort. The code is as below: <TABLE width="100%">
14
2437
by: gaijinco | last post by:
I was a hobbist web coder for years but I had to sidestep for a while. Now I'm trying to return to it and I'm trying to clarify how am I supposed to do somethings with CSS v.s. HTML and I'm specially having difficulties with center alignment of elements like images and tables. How is the "right" way to do it? (books that I have checked seemed to dodge the issue) I also have a doubt. html-atributes width and height are preferred over...
13
3325
by: Stevo | last post by:
I've found that for IE6+, if you add the property text-align:center to a DIV, then *anything* inside it gets centered. That can be a table, an object/embed, another DIV, an image, or some text. Firefox and Safari on the other hand don't treat text-align in that way. In my tests on those browsers, they only centers images and text. Any DIVs, object/embeds or tables remain default unaligned and generally appear on the left. Considering...
10
2611
by: dougancil | last post by:
I have a web page that I converted from a psd file to html with Photoshop and imported into Dreamweaver. The problem I'm having is that some viewers comment on the fact that the page is left justified. I inserted a div tag into the html with a wrap around selection and tried to modify the css rules so that this would align properly, and the page is still aligning left justified. Here is the code that I have for my css: #site_layout {...
0
9541
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
10252
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...
1
10231
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7565
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
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
5463
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...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4141
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
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.