In an effort to make my web sites more responsive, I m trying to learn how to define elements fluidly. Today I came across a situation that I finally fixed, but I don t understand why the fix worked, and I d like to.
I can t link to the site (NDA), but I ve put up some sample pages with the elements in question:
WRONG: http://cruxwire.com/wrong.html RIGHT: http://cruxwire.com/right.html
What I have is three divs floated left. I m trying to add padding to them (as a percentage) and am using target/context=result, and then *100 (because it s a percentage.)
My copy of Responsive Web Design by Ethan Marcotte says "When setting flexible padding on an element, your context is the width of the element itself." I gave the divs a width of 20%. Since the parent element is 945px, the pixel width of each div is 189px. I wanted to add padding of 20px, so 20/189=0.1058201058201058. I added a padding to each div of 10.58201058201058%.
This ends up giving each div a padding of 100px, not 20px. I eventually realized that the padding was being calculated based on the width of the parent element, not the div itself.
My solution was to put an extra div around each existing div, so that I could apply the width to one and the padding to the other, and that solved the problem.
Why is the padding calculating relative to its parent element rather than its own element?
How can I do this without having to add the additional divs?
You can see the code on the pages linked to in this post, and I ve added it below as well.
WRONG:
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>WRONG</title>
<style>
#main { width:945px; margin:0 auto; padding:40px 0; background-color:blue; }
#slideshow { background-color:green; }
.threecolumn { float:left; width:20%; padding:10.58201058201058%; background-color:yellow; } /* 20px/189px */
.slide { position:relative; margin:0 1%; background-color:red; }
p { background-color:white; margin:0; padding:0; }
</style>
</head>
<body>
<div id="main">
<div id="slideshow">
<h1>WRONG</h1>
<div class="threecolumn slide">
<p>According to Firebug, this element has 100px padding.</p>
</div>
<div class="threecolumn slide">
<p>According to Firebug, this element has 100px padding.</p>
</div>
<div class="threecolumn slide">
<p>According to Firebug, this element has 100px padding.</p>
</div>
<div style="clear:both;"></div>
</div>
</div>
</body>
</html>
RIGHT:
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>RIGHT</title>
<style>
#main { width:945px; margin:0 auto; padding:40px 0; background-color:blue; }
#slideshow { background-color:green; }
.threecolumn { float:left; width:20%; margin:0 1%; background-color:yellow; }
.slide { position:relative; padding:10.58201058201058%; background-color:red; } /* 20px/189px */
p { background-color:white; margin:0; padding:0; }
</style>
</head>
<body>
<div id="main">
<div id="slideshow">
<h1>RIGHT</h1>
<div class="threecolumn">
<div class="slide">
<p>According to Firebug, this element has 20px padding.</p>
</div>
</div>
<div class="threecolumn">
<div class="slide">
<p>According to Firebug, this element has 20px padding.</p>
</div>
</div>
<div class="threecolumn">
<div class="slide">
<p>According to Firebug, this element has 20px padding.</p>
</div>
</div>
<div style="clear:both;"></div>
</div>
</div>
</body>
</html>