Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Child padding falls outside the parent element

Tags:

html

css

Applying padding to child elements is making the child draw over the boundaries of its containing parent. Can you please explain the size consideration in margin, padding and content width.

If we increase the padding why don't the parent also resize to the accumulative size of all the children considering the child's padding also?

http://jsfiddle.net/NkXUW/4/

 <div>        <ul>           <li><a>srikanth</a>            </li>            <li><a>sunkist</a>         </li>         <li><a>sunday</a>         </li>     </ul>     </div>    div {      margin-top:90px;     margin-left:90px;     background-color:#676896;    }    ul {      list-style-type:none;    }     ul li {     display:inline-block;    }    a {     background-color:#c34567;     padding:10px 10px 10px 10px;    } 

What are coding practices that we need to consider to over come this problem.?

Ok guys I got lot answers that do work. Can anybody explain the parent size calculation based on child elements. what are characteristics of the child that are considered while calculating the encompassing parent's size. when the whole padding is considered when it not considered ?

like image 745
Srikan Avatar asked Mar 15 '13 04:03

Srikan


People also ask

Is padding inside or outside an element?

Padding is the space that's inside the element between the element and the border. Padding goes around all four sides of the content and you can target and change the padding for each side (just like a margin).

Does padding add to the height of an element?

The box model of an element in CSS—includes the content, padding, border, and margin areas. Any padding added to the element will increase the total computed height of the element, so you may not always end up with the expected height using just the height property if you also add padding to your element.

Why did the width collapse in the percentage width child element in an absolutely positioned parent on Internet Explorer 7?

Its width will be worked out based on the pixel width of its content and will be calculated after the contents are rendered. So at the point, IE encounters and renders your relatively positioned div its parent has a width of 0 hence why it itself collapses to 0.


1 Answers

the reason the child was overdrawing the boundaries of the parent is because the child is a tag of type <a> which by default is display:inline (you can see if that you go in chrome developer tools and see under computed style). an inline element displays like a line of text.. so the way it treats width and height and all that is very different than a block (a div for example is a block by default).

that being said, if you change the display setting of a to display:inline-block you get to keep the inline properties of <a> but at the same time also get the block properties, namely having a padding and width and height that is recognised by its parent node, which will then expand to accommodate it.

So there aren't any best practices about this. The only best practice is to understand what each display property mean (ie inline vs block vs inline-block) and put it to its proper use.

like image 135
abbood Avatar answered Oct 03 '22 00:10

abbood