html - Force element to wrap depending on sibling width -
as you'll see in snippet , picture below, have container (grey) contains main element (green) , footer.
the footer contains 2 elements (blue , yellow).
what want footer second element (yellow) wraps if footer (blue + yellow) wider main element (green).
i'm enclosing picture don't want run snippet:
i tried using flex-wrap
on footer works if i'm setting width on footer, i'd prefer not do.
.container { background-color: grey; display: inline-block; } .narrow-child { background-color: green; height: 200px; width: 100px; } .large-child { background-color: green; height: 200px; width: 300px; } .footer { display: flex; flex-wrap: wrap; /*width: 100px;*/ } .footer-sub-element1 { background-color: blue; width: 80px; height: 50px; } .footer-sub-element2 { background-color: yellow; width: 90px; height: 50px; }
<div class="container"> <div class="narrow-child"> </div> <div class="footer"> <div class="footer-sub-element1"> </div> <div class="footer-sub-element2"> want wrap </div> </div> </div> <div class="container"> <div class="large-child"> </div> <div class="footer"> <div class="footer-sub-element1"> </div> <div class="footer-sub-element2"> don't want wrap </div> </div> </div> </div>
the width of .footer
cannot dependent of sibling without use of javascript. solution define width of .container
instead of .child
.
.container { background-color: grey; display: inline-block; } .narrow { width: 100px; } .large { width: 300px; } .child { background-color: green; height: 200px; } .footer { display: flex; flex-wrap: wrap; /*width: 100px;*/ } .footer-sub-element1 { background-color: blue; width: 80px; height: 50px; } .footer-sub-element2 { background-color: yellow; width: 90px; height: 50px; }
<div class="container narrow"> <div class="child"> </div> <div class="footer"> <div class="footer-sub-element1"> </div> <div class="footer-sub-element2"> want wrap </div> </div> </div> <div class="container large"> <div class="child"> </div> <div class="footer"> <div class="footer-sub-element1"> </div> <div class="footer-sub-element2"> don't want wrap </div> </div> </div> </div>
Comments
Post a Comment