将列表项图像对齐到html中心

时间:2015-10-19 20:12:02

标签: javascript jquery html css

我正在学习具有响应式设计(仍然是菜鸟)的网页开发,所以请放心!我会尽量尽可能彻底,但如果您需要更多信息,请告诉我们!

所以我正在尝试设计一个对图像具有jquery悬停效果的页面。你会认为我在JS中遇到了麻烦,但我的问题要简单得多,这让它像地狱一样令人沮丧。我希望我的图像在它们与最左边对齐时在中心对齐。图像框是li的,我试图将它们添加到div并将div与中心对齐。

请注意,我还需要它才能响应,所以不能简单地添加边距或填充。

以下是我的html正文:

<body>
<div class="body">

   <div> <img src="images/logo.png" class="image"></div>

            <div class="imgs">
                <ul id="da-thumbs" class="da-thumbs">
                    <li>
                        <a href="http://dribbble.com/shots/502538-Natalie-Justin-Cleaning">
                            <img src="images/7.jpg" />
                            <div><span>Natalie & Justin Cleaning by Justin Younger</span></div>
                        </a>
                    </li>
                    <li>
                        <a href="http://dribbble.com/shots/501695-Cornwall-Map">
                            <img src="images/9.jpg" />
                            <div><span>Cornwall Map by Katharina Maria Zimmermann</span></div>
                        </a>
                    </li>

                </ul>
            </div>
</div>


        <script type="text/javascript">
            $(function() {

                $(' #da-thumbs > li ').each( function() { $(this).hoverdir({
                    hoverDelay : 75
                }); } );

            });
        </script>
</body>

这是我的CSS:

.body {
    width: 100%;
    height: 1000px;
    animation-name: colorChange;
    animation-duration: 10s;
    animation-iteration-count: infinite;
    text-align: center;
}

@keyframes colorChange {
    0% {
        background: #4BB4BF;    
    }
    20% {
        background: #306F7A;    
    }
    40% {
        background: #207DFF;    
    }
    60% {
        background: #1B98E0;    
    }
    80% {
        background: #7EA0E0;    
    }
    100% {
        background: #4BB4BF;    
    }


}

.button {
    padding: 10px;
    margin-top: 40px;
    font-size: 20px;
}



.da-thumbs {
    list-style: none;
    width: 100%;
    height: 100%;
    position: relative;
    margin: 20px auto;
    padding: 0;

}
.da-thumbs li {
    float: left;
    margin: 5px;
    background: #fff;
    padding: 8px;
    position: relative;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}
.da-thumbs li a,
.da-thumbs li a img {
    display: block;
    position: relative;
}
.da-thumbs li a {
    overflow: hidden;
}
.da-thumbs li a div {
    position: absolute;
    background: #333;
    background: rgba(75,75,75,0.7);
    width: 100%;
    height: 100%;
}
.da-thumbs li a div span {
    display: block;
    padding: 10px 0;
    margin: 40px 20px 20px 20px;
    text-transform: uppercase;
    font-weight: normal;
    color: rgba(255,255,255,0.9);
    text-shadow: 1px 1px 1px rgba(0,0,0,0.2);
    border-bottom: 1px solid rgba(255,255,255,0.5);
    box-shadow: 0 1px 0 rgba(0,0,0,0.1), 0 -10px 0 rgba(255,255,255,0.3);
}





<!-- Demo content here --> 





*,
*:after,
*:before {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    padding: 0;
    margin: 0;
}

/* Clearfix hack by Nicolas Gallagher: http://nicolasgallagher.com/micro-clearfix-hack/ */
.clearfix:before,
.clearfix:after {
    content: " "; /* 1 */
    display: table; /* 2 */
}

.clearfix:after {
    clear: both;
}

.clearfix {
    *zoom: 1;
}

/* General Demo Style */
body{
    font-family: Cambria, Palatino, "Palatino Linotype", "Palatino LT STD", Georgia, serif;

    font-weight: 400;
    font-size: 15px;

}
a{
    color: #555;
    text-decoration: none;
}
.container{
    width: 100%;
    position: relative;
    min-height: 750px;
}
.clr{
    clear: both;
    padding: 0;
    height: 0;
    margin: 0;
}

.image {
 max-width: 100%;
    max-height: 100%;
    background-size: cover;
}

.imgs {margin: 0 auto;
align: center;
}

请介绍一下我如何调整这些元素?

小提琴盒: https://jsfiddle.net/eqyfm41r/3/

2 个答案:

答案 0 :(得分:1)

您编辑的代码:

.da-thumbs li {
   display:inline-block;
   width:46%;
   margin: 5px;
   padding: 8px;
   position: relative;
}
.da-thumbs li img{
   box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

JSFiddle:https://jsfiddle.net/1zq95tzp/

你是浮动李的一件事,你也想为图像添加框阴影,而不是李。这样做的原因是li会比图像大,所以盒子阴影会显得远离实际图像的边缘。随着屏幕缩小,图像将叠加。你可能需要最终给出这种造型的图像:

max-width:100%;

这样他们就不会以电话宽度离开页面(我没有看到图片的大小)。希望这会对你有所帮助。

答案 1 :(得分:0)

你不能这样做:

margin-left: auto; margin-right: auto; 

?这将是响应性的,它不是固定的保证金。