简体   繁体   English

jQuery —设置子项以填充父项的剩余宽度

[英]Jquery — Set child to fill remaining width of parent

I am building a responsive layout. 我正在建立一个响应式布局。 I would like to know if anyone knows a way through JS/jQuery which I can set children to fill the remaining space of a parent only if there is space to fill (to the right). 我想知道是否有人通过JS / jQuery知道只有当有空间要填充时(右边),我才能设置子级来填充父级的剩余空间。

Layout when above 900px 高于900像素时的版式

900px以上

Layout when below 900px 低于900像素时的布局

在此处输入图片说明

HTML for reference HTML供参考

 .row { width: 100%; } .colm-span-2 { position: relative; float: left; margin: 15px auto; padding: 0px 15px 0px 15px; background-color: #FFFFFF; background-clip: content-box; } .colm-span-2 { width: 33.3333333333%; } @media screen and (max-width: 900px) { .colm-span-2 { width: 50%; } } 
 <div class="row"> <div class="colm-span-2"> <div class="bg-white"> <div class="cont-span-1"> <h2>Test</h2> <p>Lorem ipsum</p> </div> </div> </div> <div class="colm-span-2"> <div class="bg-cta-green"> <div class="cont-span-1"> <h2>Test</h2> <p>Lorem ipsum</p> </div> </div> </div> <div class="colm-span-2"> <div class="bg-dark-purple"> <div class="cont-span-1"> <h2>Test</h2> <p>Lorem ipsum</p> </div> </div> </div> </div> 

I would suggest adding: 我建议添加:

.colm-span-2:last-child {
    width: 100%;
}

to your media query. 您的媒体查询。 This will change the final .colm-span-2 to have a 100% width. 这会将最终的.colm-span-2更改为100%的宽度。

With flexbox and :last-child (assuming you have rows) this is possible 使用flexbox和:last-child (假设您有行)可以实现

 * { margin: 0; padding: 0; box-sizing: border-box; } ::before, ::after { box-sizing: inherit; } .bg-white { background: whitesmoke; } .bg-cta-green { background: green; } .bg-dark-purple { background: rebeccapurple; } .row { display: flex; } .colm-span-2 { flex: 0 0 33%; margin: .25em; border: 1px solid grey; } .colm-span-2:last-child { flex: 1; } 
 <div class="row"> <div class="colm-span-2"> <div class="bg-white"> <div class="cont-span-1"> <h2>Test</h2> <p>Lorem ipsum</p> </div> </div> </div> </div> <div class="row"> <div class="colm-span-2"> <div class="bg-white"> <div class="cont-span-1"> <h2>Test</h2> <p>Lorem ipsum</p> </div> </div> </div> <div class="colm-span-2"> <div class="bg-cta-green"> <div class="cont-span-1"> <h2>Test</h2> <p>Lorem ipsum</p> </div> </div> </div> <div class="colm-span-2"> <div class="bg-dark-purple"> <div class="cont-span-1"> <h2>Test</h2> <p>Lorem ipsum</p> </div> </div> </div> </div> <div class="row"> <div class="colm-span-2"> <div class="bg-white"> <div class="cont-span-1"> <h2>Test</h2> <p>Lorem ipsum</p> </div> </div> </div> <div class="colm-span-2"> <div class="bg-cta-green"> <div class="cont-span-1"> <h2>Test</h2> <p>Lorem ipsum</p> </div> </div> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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