473,738 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Specifying width as a percent

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 to 100% (which I
interpret as saying 'make this width the same width as the parent
(which is the entire screen)) the div is made just wide enough to fit
it's contents, and if I decrease the value for the width, the width of
the div stretches?!

<html>
<body>
<br /><br /><br />
<p>out of div</p>

<div style = "
background: transparent url('../main/images/header_center.g if')
repeat-x center left;
height: 100px;
display: table-cell;
vertical-align: middle;
width: 3%"/>
<p style="text-align:center">
yeah
</p>
</div>

Thanks

Taras

Apr 4 '07 #1
5 11024
Scripsit Taras_96:
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?
What bottom div? What are you doing, and why? URL?
<br /><br /><br />
That's absurd. How many times can you break a line. You're supposed to use
CSS for vertical spacing.
display: table-cell;
That's pointless in WWW authoring., because IE doesn't support it.
width: 3%"/>
Here you close the div element, by XHTML rules, which you seem to be sort-of
following (why?).
</div>
And here you close it again. Please fix the markup first, validate it, check
your CSS, post the URL telling what you wish to accomplish.

--
Jukka K. Korpela ("Yucca")
http://www.cs.tut.fi/~jkorpela/

Apr 4 '07 #2
On 2007-04-04, Taras_96 <ta******@gmail .comwrote:
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 to 100% (which I
interpret as saying 'make this width the same width as the parent
(which is the entire screen)) the div is made just wide enough to fit
it's contents, and if I decrease the value for the width, the width of
the div stretches?!
First, note that table cells' computed width for a styled auto width is
the shrink-to-fit width, while for block boxes (like the first paragraph
block) the default for auto width is all the available width.

In other words, block boxes like paragraphs fill all the width available
if they're width:auto, but table cells shrink-wrap their contents if
they're width:auto.

Second, if a percentage width is unintelligible, because the containing
block's width depends on its contents, then the browser ignores it, and
treats it as auto.

Third, table cells (in the sense of elements with display: table-cell)
have to be inside table rows (in the sense of elements with display:
table) which have to be inside tables (in the sense of...), and the spec
instructs browsers to supply anonymous table row and table boxes where
required.

Your div is display: table-cell and its parent is body, which is
display: block. So the browser inserts an anonymous table box and an
anonymous table-row box, making your tree of _boxes_[1]:

block box for body
anon table box
anon table row box
table cell box with width 100%

The table cell's containing block is the anonymous table box, whose
width is auto and shrink-to-fit. Your request for 100% (or 3%, whichever
you meant) is therefore ignored, and the cell gets its shrink-to-fit
width.

So if you want your table to take the full width of the viewport, you'll
need to put in two more divs, respectively display: table-row and display:
table, and then set width: 100% on the one that's display: table. At
this point it will be unnecessary also to set width on the display:
table-cell div.

Something like this:

<style type="text/css">
.table { display: table; width: 100%; }
.table-row { display: table-row; }
.table-cell
{
display: table-cell;
background-color: green;
}
</style>
...
<body>
<div class="table">
<div class="table-row">
<div class="table-cell">
Hello
</div>
</div>
</div>
</body>

[1] Note that this is NOT the DOM tree. If you stick a <tdelement
where it doesn't belong, the parser will usually construct <trand
<tableand those will usually appear in the DOM tree. But a div with
display: table-cell will not affect the DOM tree, only the tree of
generated boxes.
><html>
<body>
<br /><br /><br />
<p>out of div</p>

<div style = "
background: transparent url('../main/images/header_center.g if')
repeat-x center left;
height: 100px;
display: table-cell;
vertical-align: middle;
width: 3%"/>
<p style="text-align:center">
yeah
</p>
</div>
Apr 4 '07 #3
>
What bottom div? What are you doing, and why? URL?
I meant the div at the bottom of the page, sorry.

URL: http://tarasdi.110mb.com/contained_divs_and_widths.html
That's absurd. How many times can you break a line. You're supposed to use
CSS for vertical spacing.
I was doing it as a quick hack.
>
That's pointless in WWW authoring., because IE doesn't support it.
I know to avoid it now, thanks.
>
Here you close the div element, by XHTML rules, which you seem to be sort-of
following (why?).
sorry, typo
your CSS, post the URL telling what you wish to accomplish.
I was trying to get text horizontally centered on the page, and
vertically centered in the div it was contained within. I was doing it
using the display:table-cell (as described here), but this would cause
the div to shrink in size, which resulted in the text no longer being
horizontally centered. I noticed that when I decreased the percentage
associated with the width property, the width would actually increase.
I'm still confused about this behaviour.

I've uploaded the page at http://tarasdi.110mb.com/contained_divs_and_widths.html

Apr 6 '07 #4
Excellent post, cleared up a few things for me.
First, note that table cells' computed width for a styled auto width is
the shrink-to-fit width, while for block boxes (like the first paragraph
block) the default for auto width is all the available width.
That makes sense, but do you see any reason why when I decrease the
percentage the width increases (if you look at
http://tarasdi.110mb.com/contained_divs_and_widths.html you'll see
that the div is quite wide when the width is set at 3%)?

I've tried to read the w3 specs on calculating widths, but it's pretty
complicated (http://www.w3.org/TR/CSS21/
visudet.html#Co mputing_widths_ and_margins), is there any documents out
there that are a bit clearer/explain this bit of the specs a bit
better?

Jukka has pointed out that IE doesn't suppourt table-cell for the
display, so is there some other way of vertically aligning things
within the containing block?

Apr 6 '07 #5
On 2007-04-06, Taras_96 <ta******@gmail .comwrote:
Excellent post, cleared up a few things for me.
>First, note that table cells' computed width for a styled auto width is
the shrink-to-fit width, while for block boxes (like the first paragraph
block) the default for auto width is all the available width.

That makes sense, but do you see any reason why when I decrease the
percentage the width increases (if you look at
http://tarasdi.110mb.com/contained_divs_and_widths.html you'll see
that the div is quite wide when the width is set at 3%)?
Good question. I can't see any justification for that. If you remove the
width: 3% the table-cell div shrinks-to-fit the word "hello" as you
expect. As soon as you put a percentage width on the table-cell div, its
width becomes the whole width of the viewport!

I'm tempted to say it's a bug in Firefox, but Opera and Konqueror both
do the same thing.
I've tried to read the w3 specs on calculating widths, but it's pretty
complicated (http://www.w3.org/TR/CSS21/
visudet.html#Co mputing_widths_ and_margins), is there any documents out
there that are a bit clearer/explain this bit of the specs a bit
better?
Probably. I agree that the spec doesn't explain it very well.

In this case, though, that 3% width is a percentage width of an
auto-width container (the containing block is the anonymous table), and
therefore should be ignored completely. The spec is very clear on this.
But even with a strict doctype on your example (always use the strict
doctype, just start every page with

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">

)

FF, Konqueror and Opera all compute this huge width. I don't know why.
It seems wrong to me.
Jukka has pointed out that IE doesn't suppourt table-cell for the
display, so is there some other way of vertically aligning things
within the containing block?
None that are entirely satisfactory, but something might be possible
depending on your requirements.

See http://www.student.oulu.fi/~laurirai/www/css/middle/
Apr 6 '07 #6

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

Similar topics

2
3178
by: Alex Shi | last post by:
In php, is there a way obtain the width of a charactor of a certain font? Alex -- ================================================== Cell Phone Batteries at 30-50%+ off retail prices! http://www.pocellular.com ==================================================
5
2034
by: Olav Tollefsen | last post by:
Take a look at this page: http://www.webinnovation.no/test/test.htm Why is the left table with the text "Menu" not 160 pixels as stated in the HTML code? Olav
8
39527
by: brett | last post by:
How can I find the height and width of a webpage? Say I want to make sure someone's webpage is within an 800X600 viewing area. Width is the most important but if I can get width, I should also be able to get height. I don't need to modify the page in anyway. Just get the width and height. I can reference the page in an iframe, cfhttp (CFMX) or something if it needs to be on my server. Thanks,
7
1969
by: Johnny | last post by:
June 2, 2005 Greetings, I have a whole lot of questions that all have to do with the same topic: Width. By "width" I mean how wide a particular Web page is, how wide a table is, how wide a particular cell or column is, how wide a particular font is, and so on.
1
366
by: Ken Varn | last post by:
This is probably a common question, but bear with me. I am new to this stuff. I want to convert a Unit Width and/or Height to an actual pixel Int32 Width and Height. I was just using the Value attribute of the Unit, but was not sure if this is how it should be done. Not knowing what would happen if someone specified % or other Unit types. -- -----------------------------------
6
4805
by: Stephan Bourdon | last post by:
Hi, Your opinion please on the following subject: Is it acceptable to set the width and height of an image in ems or percents in CSS? The advantage for me is that images will scale up or down when the user changes the font size in the browser. But what of the disadvantages? In (X)HTML, the img-element will have a pixel-value for the width and height-attributes.
2
2077
by: brixton | last post by:
Hi, I have a three-column-page where the columns' width are specified in percent instead of fixed pixels (I have to have this due to a school assignment). It works great in Firefox but IE interprets the width differently and the last column gets pushed down since it takes too much space. The code looks somewhat like this: div#first { float:left; width:18%; }
4
20054
by: fjm | last post by:
Hello everyone, I have a question regarding css where I would like to know if it is possible to break up a page into thirds for the height. I need to have a page that will be 100% in height and 100% in width. The height will be broken down into thirds; 20% for the header image, 75% for the content and the remaining 5% for the footer image. I am not a real big css buff and have always used tables for everything until recently when I...
4
1379
by: nse111 | last post by:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Hope this works</title> <link href="./Art/stylesheets/cloud_style.css" rel="stylesheet" type="text/css" /> </head> <body> <CODE = PHP>
0
8787
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
9473
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
9334
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
9208
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
8208
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
6053
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
4824
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
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.