简体   繁体   中英

Border radius on parent div

There are three divs that are inside the parent div with display:flex .

I want to create a border-radius for parent div but something doesn't work.

My code is:

 <div style="height:50px;display:flex;width:500px;border-radius: 20px;"> <div style="height:50px;width:30%;background:red"></div> <div style="height:50px;width:30%;background:blue"></div> <div style="height:50px;width:40%;background:yellow"></div> </div>

The border-radius is invisible. It is possible for the child to have width:0% or width:100% so the border-radius should be applied for the parent container.

How is it possible to to that?

add overflow:hidden to parent div

 <div style="height:50px;display:flex;width:500px;border-radius: 20px;overflow:hidden"> <div style="height:50px;width:30%;background:red"></div> <div style="height:50px;width:30%;background:blue"></div> <div style="height:50px;width:40%;background:yellow"></div> </div>

Try this:

 <div style="height:50px;display:flex;width:500px;border-radius: 20px;overflow: hidden"> <div style="height:50px;width:30%;background:red"></div> <div style="height:50px;width:30%;background:blue"></div> <div style="height:50px;width:40%;background:yellow"></div> </div>

so adding overflow: hidden to the parent should fix the problem

Just gotta add overflow: hidden to the parent div OR apply the border-radius to the first and last children. Sometimes, certain elements don't react nicely to the parent clipping them and respond better to being clipped directly. Here are both methods:

Border-radius on parent:

 body > div { border-radius: 20px; overflow: hidden; }
 <div style="height:50px;display:flex;width:500px;"> <div style="height:50px;width:30%;background:red"></div> <div style="height:50px;width:30%;background:blue"></div> <div style="height:50px;width:40%;background:yellow"></div> </div>

Border-radius on children:

 body > div > div:first-child { border-radius: 20px 0 0 20px; } body > div > div:last-child { border-radius: 0 20px 20px 0; }
 <div style="height:50px;display:flex;width:500px;"> <div style="height:50px;width:30%;background:red"></div> <div style="height:50px;width:30%;background:blue"></div> <div style="height:50px;width:40%;background:yellow"></div> </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM