Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying exact percentage widths in relation to parent DIV in CSS

Tags:

css

I am attempting to create a visual element using DIV elements and CSS which should display data in the format demonstrated below.

[-----50%-----|--25%--|--25%--]

When using the code and CSS I've specified below, my final element always spills onto the next line and the CSS percentage values I'm specifying don't seem to create the layout properly.

Could anybody suggest a better way to do this?

My HTML

<div class="visual-indicator-title">
All Items</div>
<div class="visual-indicator-holder">
<div class="vi-internal-element" style="width: 25%; background-color: #5E9BD1;">
    25%</div>
<div class="vi-internal-element" style="width: 25%; background-color: #AB884D;">
    25%</div>
<div class="vi-internal-element" style="width: 50%;">
    50%</div>
</div>
<div class="visual-legend">
<ul class="inline-block">
    <li>
        <div class="legend-blue">
        </div>
        Sales</li>
    <li><span class="legend-tan"></span>Processed</li>
    <li><span class="legend-grey"></span>Pending Processing</li>
</ul>

My CSS

.visual-indicator-title{
font-size:12px;
font-weight:bold;
color:#777777;
}
.visual-indicator-holder
{
width:100%;
background-color:#666666;
height:28px;
border-radius: 8px;
}
.visual-indicator-holder .vi-internal-element
{
font-size:11px;
text-align:center;
color:#ffffff;
background-color:#777777;
border-radius: 6px;
display:inline-block;
}
like image 313
Nick Avatar asked Nov 04 '11 13:11

Nick


People also ask

How do you inherit the width of a parent div?

width:inherit inherits width that defined by parent. This makes child width 25%, but if I redefine it with width:100% it will define width of child 50%. I see. So inherit copies the literal expression rather than the value.

What are percentages relative to 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.

How do I make DIVS always fit in my parent div?

For width it's easy, simply remove the width: 100% rule. By default, the div will stretch to fit the parent container. Height is not quite so simple.

What does width 100% do in CSS?

width: 100%; will make the element as wide as the parent container. Extra spacing will be added to the element's size without regards to the parent.


4 Answers

The reason this happens is that with inline or inline-block, white space in the element will affect the rendering (adds space). Here is your demo working with white space removed, no changes to the CSS: http://jsfiddle.net/fZXnU/

Removing white space is not trivial though, so you'd be better off floating the elements (which triggers display:block). Working demo with plenty of white space: http://jsfiddle.net/fZXnU/1/

like image 100
Wesley Murch Avatar answered Oct 17 '22 02:10

Wesley Murch


You can use float: left, position: relative, and then define width in percentage as you are.

I modified your code to use float here: http://jsfiddle.net/Z3kdP/.

like image 22
Zack Marrapese Avatar answered Oct 17 '22 01:10

Zack Marrapese


If you remove the white-space between the divs then it works as intended.

http://jsfiddle.net/TeJuU/


EDIT: See this question: How to remove the space between inline-block elements?

You can make font-size: 0 on the parent element if you don't want to edit your html.

http://jsfiddle.net/TeJuU/1/

like image 3
James Montagne Avatar answered Oct 17 '22 03:10

James Montagne


All of those elements have margin and padding with them as well as the percentages creating rounding errors during calculation. So you need to make sure you set, or take into consideration, what margin is doing to this. For rounding errors, it's typical to let the percentages add up to something less than 100% but then add margin: auto to center the whole thing.

like image 2
Rob Avatar answered Oct 17 '22 03:10

Rob