Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying percentage width for CSS table cells

Tags:

I'm trying to create an HTML/CSS-based week calendar using CSS display:table-cell styling on the divs corresponding to each day. When I specify a percent width for the day divs that is greater than 17%, the divs fills the whole screen as expected (since 7*17% > 100%).

jsfiddle here: http://jsfiddle.net/huDxZ/1/

However, when I specify a percent width that is 16% or less, the divs behave entirely differently, taking up only part of the page width.

jsfiddle here: http://jsfiddle.net/DLjnH/

I would like my day divs to each have widths of about 14% so they roughly fill the width of the page and have equal sizes. Unfortunately, a width of 14% looks even worse.

jsfiddle here:http://jsfiddle.net/YB5bY/

What is causing this unexpected behavior? Is there any way around it? I need to explicitly specify widths because eventually I would like the days of the calendar to expand on mouseover.

Please, no solutions involving floats.

Thanks!

like image 327
Dylan Avatar asked Feb 27 '12 11:02

Dylan


People also ask

Can I use percentage values for td width?

Note: Using a percentage as the size unit for a width means how wide will this element be compared to its parent element, which in this case is the <body> element.

How do I make a percentage in CSS?

The <percentage> CSS data type represents a percentage value. It is often used to define a size as relative to an element's parent object. Numerous properties can use percentages, such as width , height , margin , padding , and font-size . Note: Only calculated values can be inherited.

What is width percentage in CSS?

The width property in CSS specifies the width of the element's content area. This “content” area is the portion inside the padding, border, and margin of an element (the box model). .wrap { width: 80%; } In the example above, elements that have a class name of . wrap will be 80% as wide as their parent element.


2 Answers

CSS rules percentage values is always relative to the value of the same property of the parent.
Try adding an explicit width to the container:

#calendar {     display: table;     height:900px;     width: 100%; } 

The you can use 14% for the ".day" elements:

DEMO

like image 125
Didier Ghys Avatar answered Sep 21 '22 04:09

Didier Ghys


I cannot tell you the reason but if you force the container div to have 100% width the behaviors of the two cases are identical.

#calendar {     display: table;     height:900px;     width: 100%; } 

Updated version: http://jsfiddle.net/DLjnH/1/

like image 22
Rui Carneiro Avatar answered Sep 24 '22 04:09

Rui Carneiro