元素居中网格中的最后一行左对齐

元素居中网格中的最后一行左对齐,第1张

元素居中网格中的最后一行左对齐

这是一个非常简单的Javascript(以及CSS中的一些小更改)解决方案:

对我来说很好。

CSS:

.container {  margin: 0 auto;  max-width:960px;  background-color: gold;}.block {  background-color: #ddd;  border:1px solid #999;  display: block;  float: left;  height: 100px;  margin: 4px 2px;  width: 100px;}

Javascript:

$(document).ready(function(){    setContainerWidth();});$(window).resize(function(){   setContainerWidth();});function setContainerWidth(){    $('.container').css('width', 'auto'); //reset    var windowWidth = $(document).width();    var blockWidth = $('.block').outerWidth(true);    var maxBoxPerRow = Math.floor(windowWidth / blockWidth);    $('.container').width(maxBoxPerRow * blockWidth);}

jQuery是必需的:)



欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5149309.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-17
下一篇2022-11-17

发表评论

登录后才能评论

评论列表(0条)

    保存