473,396 Members | 1,785 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,396 software developers and data experts.

Assigning minimal width to block-level elements

Hi,

I'm trying to accomplish what I (perhaps naively) assumed would be a
relatively easy task. I am trying to center a box in the middle of the
display. That's all. Something like this here:
http://bioinfo.usask.ca/~lukem/css/

The crux of the problem I'm having is that I'm unable to stop a
block-level element from filling up the entire available width without
making it a float, and once it's a float, I'm uable to center it.

Is there a way to tell a block-level element that it should only be as
wide as it has to be? I've looked in all of the documentation I'm aware
of and I can't seem to find a way, so it seems to me that the answer is
probably no. Am I missing something?

Thanks,

Luke
Jul 20 '05 #1
9 3171
Luke McCarthy wrote:

The crux of the problem I'm having is that I'm unable to stop a
block-level element from filling up the entire available width
Assign a width. If the box holds text, use em units to keep it
flexible. Note that, if the width is larger than the user's window,
this could result in horizontal scroll bars.

Better solution to center things is to assign equal left and right
margins. Search this group with Google; centering is a *very*
frequent topic.
Is there a way to tell a block-level element that it should only be as
wide as it has to be?


display: table-cell ;
only works in Opera afaik

--
Brian
follow the directions in my address to email me

Jul 20 '05 #2
On Mon, 06 Oct 2003 23:30:21 GMT, Luke McCarthy wrote:
The crux of the problem I'm having is that I'm unable to stop a
block-level element from filling up the entire available width without
making it a float, and once it's a float, I'm uable to center it.
The problem you're running in to is a fundamental misunderstanding (or
non-understanding) of the CSS Box model. I'd suggest reading it pretty
carefully, since this describes in detail how items are supposed to be laid
out (bugs not withstanding).

Basically, Margin + Border + Padding + must equal the width of the
containing block. In your case, the containing block is the BODY element,
thus the content expands to the width of the body.

You need to constrain some of the attributes of the box in order to do what
you want. Either with margins, a containing div, or some other mechanism
(such as absolute or relative positioning).
Is there a way to tell a block-level element that it should only be as
wide as it has to be? I've looked in all of the documentation I'm aware
of and I can't seem to find a way, so it seems to me that the answer is
probably no. Am I missing something?


Again, you need to really understand the box model. I can't stress this
enough. It's really easy to gloss over it and think you understand. There
are some subtleties to it that aren't obvious at first.
Jul 20 '05 #3
On Mon, 6 Oct 2003 21:06:28 -0500, Erik Funkenbusch wrote:
Basically, Margin + Border + Padding + must equal the width of the
containing block. In your case, the containing block is the BODY element,
thus the content expands to the width of the body.


That should be Margin + Border + Padding + Content must equal the width of
the containing block.
Jul 20 '05 #4
In article <1n****************@funkenbusch.com>, "Erik Funkenbusch"
<er**@despam-funkenbusch.com> wrote:
Basically, Margin + Border + Padding + must equal the width of the
containing block. In your case, the containing block is the BODY
element, thus the content expands to the width of the body.

You need to constrain some of the attributes of the box in order to do
what you want. Either with margins, a containing div, or some other
mechanism (such as absolute or relative positioning).


I've tried slapping a containing div around the div I'm trying to center,
but that didn't seem to accomplish anything, as that containing div just
expanded to fill the whole page.

Setting margins or padding or making the width absolute requires that I
know how big the text inside the box is going to be in advance. In this
case, I don't. It's a dynamically generated interface and I want to
display error messages in a box. Unfortunately, the length of
those messages is going to vary and I was hoping I could vary the size of
the box as well.
Is there a way to tell a block-level element that it should only be as
wide as it has to be? I've looked in all of the documentation I'm
aware of and I can't seem to find a way, so it seems to me that the
answer is probably no. Am I missing something?


Again, you need to really understand the box model. I can't stress this
enough. It's really easy to gloss over it and think you understand.
There are some subtleties to it that aren't obvious at first.


I've read about the box model (including relevant sections of the document
at http://www.w3.org/TR/REC-CSS2/. As I said above, it doesn't seem to me
that there is a way to contstrain the width of the box based on the
required width of its content. Is that true, or is there a subtlety that
I'm missing?

Cheers,

Luke
Jul 20 '05 #5
Luke McCarthy wrote:

I've tried slapping a containing div around the div I'm trying to
center, but that didn't seem to accomplish anything, as that
containing div just expanded to fill the whole page.

Setting margins or padding or making the width absolute requires
that I know how big the text inside the box is going to be in
advance. In this case, I don't.
Ok. But how is a browser supposed to know how to shape the box?
Consider a paragraph of 2 lines at full width of the containing block.
If you just say, "center that," the question is, how? Make small
margins on either side, and make it 3 lines long? 4 lines long with
bigger margins?
It's a dynamically generated interface and I want to display error
messages in a box. Unfortunately, the length of those messages is
going to vary
If you know the message will only be a few words long, then test a box
with the longest message possible, using different widths set in em
units, and different font settings. Use the smallest size that will
fit that message, and set that as box width. But if that width is
large enough that it might overflow a browser window, then you'll have
to rethink things.
it doesn't seem to me that there is a way to contstrain the width
of the box based on the required width of its content. Is that
true, or is there a subtlety that I'm missing?


display: table-cell
afaik, only works in Opera.

--
Brian
follow the directions in my address to email me

Jul 20 '05 #6
Luke McCarthy wrote:
In article <1n****************@funkenbusch.com>, "Erik Funkenbusch"
<er**@despam-funkenbusch.com> wrote:
Basically, Margin + Border + Padding + must equal the width of the
containing block. In your case, the containing block is the BODY
element, thus the content expands to the width of the body.

You need to constrain some of the attributes of the box in order to
do what you want. Either with margins, a containing div, or some
other mechanism (such as absolute or relative positioning).


I've tried slapping a containing div around the div I'm trying to
center, but that didn't seem to accomplish anything, as that
containing div just expanded to fill the whole page.

Setting margins or padding or making the width absolute requires that
I know how big the text inside the box is going to be in advance. In
this case, I don't. It's a dynamically generated interface and I
want to display error messages in a box. Unfortunately, the length of
those messages is going to vary and I was hoping I could vary the
size of the box as well.

[snip]

I wonder if what you are doing resembles what I was trying to do?

Take this photograph page:
http://www.barry.pearson.name/photog...99_38_11_3.htm

The key bit of HTML for the photograph is:

<div class="outer">
<div class="middle">
<div class="inner"><img src="ch99_38_11_3.jpg" height="514" alt="Fashion
models, Xian, China" width="700">
</div>
</div>
</div>

I had all sorts of problems with different browers, mostly because of
limitations in IE. I wanted the inner & middle divs to "cling" to the image,
and the outer one to position them all. I ended up with:

http://www.barry.pearson.name/assets/style_eggshell.css

div.outer { display: table; width: 10%; margin-left: auto; margin-right:
auto; }

Have a look at the HTML & CSS and see if any of it works for you.

--
Barry Pearson
http://www.Barry.Pearson.name/photography/
http://www.BirdsAndAnimals.info/
http://www.ChildSupportAnalysis.co.uk/
Jul 20 '05 #7
In article <gx********************@rwcrnsc51.ops.asp.att.net> , Brian
wrote:
Is there a way to tell a block-level element that it should only be as
wide as it has to be?


display: table-cell ;
only works in Opera afaik


display:table also works on Mozilla

--
Lauri Raittila <http://www.iki.fi/lr> <http://www.iki.fi/zwak/fonts>
Saapi lähettää meiliä, jos aihe ei liity ryhmään, tai on yksityinen
tjsp., mutta älä lähetä samaa viestiä meilitse ja ryhmään.

Jul 20 '05 #8
On Tue, 07 Oct 2003 15:38:28 GMT, Luke McCarthy wrote:
I've tried slapping a containing div around the div I'm trying to center,
but that didn't seem to accomplish anything, as that containing div just
expanded to fill the whole page.
Well, the containing div would have to have it's style set appropriately,
or be constrained within something else.
Setting margins or padding or making the width absolute requires that I
know how big the text inside the box is going to be in advance. In this
case, I don't. It's a dynamically generated interface and I want to
display error messages in a box. Unfortunately, the length of
those messages is going to vary and I was hoping I could vary the size of
the box as well.


Actually, you could just as easily generate the width in your
cgi/asp/php/jsp/whatever you're using via the style attribute. Count the
number of characters then add style="width:[char count] em;" to your div.
While it's not usually a good thing to mix style mechanisms, it's certainly
allowed.

You could also set the width to the largest possible text, and then center
that.
Jul 20 '05 #9
Indeed,

For a similar situation, though very different (I'm allowed
to say that, right?), I have a hidden span. Into this I plunk
the text that I'm preparing to display, find the resultant size,
and set that as the visible div's width. Every so often there
seems to be a pixel difference in my IE 5.5 which I haven't
resolved yet, but basically the technique itself seems to
be fine for my purposes. Truly not what I wanted but IE's
resizing of narrow cells with overflow:auto set seems
questionable to me. Your milage may vary.

Csaba Gabor from New York
Jul 20 '05 #10

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

Similar topics

12
by: Tyrone Slothrop | last post by:
OK, I give up! I am trying to convert a JavaScript rollover button script to CSS. It is composed of background colors and font colors with the current page displaying the "over" state. I...
3
by: phil_gg04 | last post by:
Dear CSS Experts, I want to make a textarea fill all the available width. This is something I've done before but mostly by trial and error; now I want to understand what is going on. So I try:...
24
by: Mike L | last post by:
This is for a Win form, in C# 2005. I want to load a datagrid, make some columns width 0, and then clean out the record I added. I get the error message, ""Index was out of range. Must be...
1
by: Robert | last post by:
Probably something simple i'm overlooking, taking the below html and css style settings, when you view in a browser (IE or Mozilla for example), the table is a tiny bit less wide than the div. Yet...
11
by: Gérard Talbot | last post by:
Hello, <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <title></title> <style type="text/css"> body {background-color: white; color: black;}...
3
by: McKirahan | last post by:
I have a Web page that uses some JavaScript and CSS. I have specified a three column table with a fixed-width left and right side; the center colum expands to fill the page. Within this...
5
by: Taras_96 | last post by:
Hi everyone, Can anyone tell me why the top paragraph block stretches across the screen (as you would expect), while the bottom div doesn't stretch across the entire screen? When I set the width...
7
by: Peter Parker | last post by:
Could someone show me how to limit caption width to image width dynamically (image width is not known in advance) if that's possible? I was thinking of using Javascript to get the image width which...
5
by: BobLaughland | last post by:
Hi There, I am trying to get some fields to align on a web page, like this, To: From: Subject: Each of the fields above have a corresponding asp:Textbox control next to it that I cannot...
1
pradeepjain
by: pradeepjain | last post by:
I have a 3 column layout left<->content<->right<->.I made a css and html in such a manner that when there is no left sidebar the content region starts from left side and takes whole width of left on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
0
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,...
0
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,...

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.