简体   繁体   中英

Flex-basis unless no child exists

I have the following layout using the flex css, and my left container is set to hav the width 20%. I want it to collapse if .col.left-col is empty (som divs is rendered there .my-left-div)

If i change .grid--container .left-col to .grid--container .my-left-div the width of the .left-col is no more 20% but more, is that possible to solve without javascript?

---------------------------------
|         top                   |
---------------------------------
|       |            |          |
|  left |   middle   |  right   |
|       |            |          |
|       |            |          |
|       |            |          |
|       |            |          |
|       |------------------------
|       |          bottom       |
---------------------------------

the html is like this.

  .grid--container { display: flex; width: 100%; justify-content: left; } .grid--container .left-col { flex: 0; flex-basis: 20%; } .grid--container .top { margin-top: 28px; } .grid--container .right-col { flex: 1; padding-left: 20px; } .grid--container .wrapper { display: flex; } .grid--container .wrapper > div { flex: 1; } 
  <div class="grid--container"> <div class="col left-col"> left </div> <div class="col right-col"> <div class="top"> top </div> <div class="wrapper"> <div class="middle"> middle </div> <div class="right"> right </div> </div> <div class="bottom"> bottom </div> </div> </div> 

Solved:

By using the :empty selector.

  .grid--container { display: flex; width: 100%; justify-content: left; } .grid--container .left-col { flex: 0; flex-basis: 20%; } .grid--container .left-col:empty { flex-basis: 0%; } .grid--container .top { margin-top: 28px; } .grid--container .right-col { flex: 1; padding-left: 20px; } .grid--container .wrapper { display: flex; } .grid--container .wrapper > div { flex: 1; } 
  <div class="grid--container"> <div class="col left-col"> left </div> <div class="col right-col"> <div class="top"> top </div> <div class="wrapper"> <div class="middle"> middle </div> <div class="right"> right </div> </div> <div class="bottom"> bottom </div> </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