html - CSS property min-height causing margin issue? -
i trying make div has css-generated triangle attached bottom of it. works when height of div above set using css height
property, when use min-height
instead, results in there being unwanted gap, unable rid of using margin
css properties.
does have idea how rid of issue?
.triangle { width: 0; height: 0; margin-right: auto; margin-left: auto; border-style: solid; border-width: 17px 10.5px 0 10.5px; } .divider1 { min-height: 80px; } .divider2 { height: 80px; }
<div class="divider1" style="background-color: blue"> <h1>this 1 doesn't work</h1> </div> <div class="triangle" style="border-color: blue transparent transparent transparent"></div> <div class="divider2" style="background-color: green"> <h1>but 1 does?</h1> </div> <div class="triangle" style="border-color: green transparent transparent transparent"></div>
it's because h1
element's margin
collapsing in second example. known collapsing margins. resolve this, 1 option remove h1
element's margin
in first example.
for it's worth, following applies:
box model - 8.3.1 collapsing margins
the bottom margin of in-flow block box
height
ofauto
,min-height
of 0 collapses last in-flow block-level child's bottom margin if box has no bottom padding , no bottom border , child's bottom margin not collapse top margin has clearance.a box's own margins collapse if
min-height
property zero, , has neither top or bottom borders nor top or bottom padding, , hasheight
of either0
orauto
, , not contain line box, , of in-flow children's margins (if any) collapse.
as side note, i'd suggest adding triangle via pseudo-element absolutely positioned relative parent element top
value of 100%
.
in doing so, work regardless of whether parent has height
/min-height
, , regardless of element's margin
.
.triangle-after { position: relative; } .triangle-after:after { content: ''; width: 0; height: 0; position: absolute; top: 100%; left: 0; right: 0; margin: auto; border-style: solid; border-width: 17px 10.5px 0 10.5px; } .divider1 { min-height: 80px; } .divider1.triangle-after:after { border-color: blue transparent transparent transparent; } .divider2 { height: 80px; } .divider2.triangle-after:after { border-color: green transparent transparent transparent; }
<div class="divider1 triangle-after" style="background-color: blue"> <h1>both of these work now.</h1> </div> <div class="divider2 triangle-after" style="background-color: green"> <h1>both of these work now.</h1> </div>
Comments
Post a Comment