gpt4 book ai didi

css - 如何居中砌体容器

转载 作者:技术小花猫 更新时间:2023-10-29 11:54:38 26 4
gpt4 key购买 nike

我正在尝试将砖石容器置于页面的中央。目前,它向左对齐。我的 CSS 中有 margin auto 和 isFitWidth: true 在 JS 中,但似乎都没有做任何事情。我也试过将 display:block 放在我的 CSS 中。

这是 HTML;

<div id="masonry_container" class="group">

<div class="masonry_item">
<a href="http://storyville.jonmarkoff.com/storyvillewp"target="_blank">
<img src="images/storyville_home.png" alt="Storyville Entertainment"/>
<h3>Storyville Entertainment</h3></a>
</div><!--masonry_item-->


<div class="masonry_item">
<a href="http://www.ducklingfarm.com"target="_blank">
<img src="images/udof_home.jpg" alt="Ugly Duckling Organic Farm"/>
<h3>Ugly Duckling Organic Farm</h3></a>
</div> <!--masonry_item-->


<div class="masonry_item">
<a href="http://www.underdonk.com"target="_blank">
<img src="images/underdonk_home.png" alt="underdonk"/>
<h3>Underdonk</h3></a>
</div> <!--masonry_item-->

<div class="masonry_item">
<a href="http://www.jaeeunlee.com" target="_blank">
<img src="images/jaeeunlee_home.png" alt="jaeeunlee"/>
<h3>www.jaeeunlee.com</h3></a>
</div> <!--masonry_item-->

<div class="masonry_item">
<img src="images/goindoor_hospitals.png" alt="goindoor"/>
<h3>Goindoor</h3>
</div> <!--masonry_item-->

<div class="masonry_item">
<img src="images/cakes_home.jpg" alt="wonderfully whimsical cakes"/>
<h3>Wonderfully Whimsical Cakes</h3>
</div> <!--masonry_item-->

</div><!--#masonry_container .group-->

CSS;

.group {
display: inline-block;
clear:both;
}

.group:after {
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}

#masonry_container {
margin:50px auto;
width:100%;
position:relative;
z-index:2001;
}

.masonry_item {
width:300px;
margin:0 0 20px 0px;
padding:20px;
}

.masonry_item:hover{
outline:1px solid white;
}

#masonry_container img {
width:100%;
}

和JS;

var container = document.querySelector('#masonry_container');
var msnry = new Masonry( container, {
// options
isFitWidth: true,
itemSelector: '.masonry_item'
});

非常感谢您的帮助!

最佳答案

我今天想自己解决这个问题,我想我会分享一个可能的解决方案。

根据 Masonry 自己的选项页面 "isFitWidth": true 似乎是关键

http://masonry.desandro.com/options.html#isfitwidth

这是他们的代码笔示例..

http://codepen.io/desandro/pen/nGLvx

这是我简化的基本方法..

fiddle

https://jsfiddle.net/Hastig/xtw113wx/2/ - 代码播放

https://jsfiddle.net/Hastig/xtw113wx/2/embedded/result/ - 全屏

html

<div class="masonry-container js-masonry"  data-masonry-options='{ "isFitWidth": true }'>
<div class="image-div">
<img class="image" src="" style="width: 200px; height: 100px;">
</div>
<!-- ..lots more divs in jsfiddle.. -->
</div>

CSS

.masonry-container {
margin: 0 auto; /* this is the css that keeps the container centered in page */
}

.image-div {
float: left;
width: 230px;
margin: 5px;
font-size: 0;
}

.image {
width: 230px;
height: auto;
}

关于css - 如何居中砌体容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20390689/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com