In an attempt to make my websites more responsive, I am trying to learn how to identify elements that are fluid. Today I came across a situation that I finally fixed, but I donβt understand why the fix works, and I would like to.
I canβt link to the site (NDA), but I put a few sample pages with the appropriate elements:
WRONG: http://cruxwire.com/wrong.html RIGHT: http://cruxwire.com/right.html
What I have consists of three sections. I try to add an addition to them (in percent), and I use target / context = result, and then * 100 (because it's a percentage.)
My copy of responsive web design by Ethan Marcott says: "When you set up a flexible complement to an element, your context is the width of the element itself." I gave divs a width of 20%. Since the parent is 945px, the pixel width of each div is 189px. I wanted to add a 20px add-on, so 20/189 = 0.1058201058201058. I added an addition to each div from 10.58201058201058%.
This causes each div to fill 100px, not 20px. In the end, I realized that the padding is calculated based on the width of the parent element, not the div itself.
My solution was to add an extra div around each existing div so that I could apply width to one and padding to the other, and this solved the problem.
I have two questions:
Why is padding computed relative to its parent and not its own?
How can I do this without adding extra divs?
You can see the code on the pages associated with this post, and I added it also below.
Thanks Kim
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; } .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; } 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>