如果小于父容器,则将所有图像居中

时间:2012-09-27 21:34:16

标签: javascript jquery html css smooth-scrolling

如果我有足够的图像,我的代码工作正常:它滚动并且一切都很好。

但是,如果图像的组合宽度小于父div的宽度,则不会滚动(因为它应该),但所有图像都对齐到左边。

如何让它们与中心对齐?

我很确定这很简单,但我找不到解决方案。

如果有人可以帮忙,我将不胜感激。

由于 启

适用于此插件:http://www.smoothdivscroll.com/

我的CSS看起来像这样

#makeMeScrollable
    {
        width:100%;
        height: 55px;
        position: relative;
        margin: 20px 0; 
    }
#makeMeScrollable div.scrollableArea img, #makeMeScrollable div.scrollableArea a
{
position: relative;
float: left;
margin: 0 10px;
padding: 0;

filter: url(stylesheets/filters.svg#grayscale); /* Firefox 3.5+ */ 
filter: gray; /* IE6-9 */ 
-webkit-filter: grayscale(1); /* Google Chrome & Safari 6+ */

/* If you don't want the images in the scroller to be selectable, try the following
    block of code. It's just a nice feature that prevent the images from
    accidentally becoming selected/inverted when the user interacts with the scroller. */
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-o-user-select: none;
user-select: none;
}
#makeMeScrollable div.scrollableArea img:hover, #makeMeScrollable div.scrollableArea a:hover
{
filter: none; 
-webkit-filter: grayscale(0); 
}

html是:

<div id = "makeMeScrollable">
            <img src = "images/test.png"
               id = "1" />

            <img src = "images/test2.png"
               id = "2" />
        </div>

2 个答案:

答案 0 :(得分:0)

在图片上尝试margin:auto,因为这会使块级元素居中。

答案 1 :(得分:0)

试试这个

<div style="display:table-cell; vertical-align:middle; text-align:center">
<img src="img.png">
</div>