在页面上水平和垂直居中转盘

时间:2014-11-22 20:01:18

标签: html css

任何人都知道为什么旋转木马没有水平居中?我使用slick.js作为轮播。

已将display: table; margin: 0 auto;设置为水平居中但不起作用。

jsfiddle .. http://jsfiddle.net/bobbyrne01/4qpejg98/1/

enter image description here

html ..

<div class="centerHorizontally parent">
    <div class="halfWidth child">
        <div class="slider multiple-items">
            <div>
                <div class="centerHorizontally">
                    <img src="http://placehold.it/50x50" />
                </div>
            </div>
            <div>
                <div class="centerHorizontally">
                    <img src="http://placehold.it/50x50" />
                </div>
            </div>
            <div>
                <div class="centerHorizontally">
                    <img src="http://placehold.it/50x50" />
                </div>
            </div>
            <div>
                <div class="centerHorizontally">
                    <img src="http://placehold.it/50x50" />
                </div>
            </div>
            <div>
                <div class="centerHorizontally">
                    <img src="http://placehold.it/50x50" />
                </div>
            </div>
            <div>
                <div class="centerHorizontally">
                    <img src="http://placehold.it/50x50" />
                </div>
            </div>
            <div>
                <div class="centerHorizontally">
                    <img src="http://placehold.it/50x50" />
                </div>
            </div>
        </div>
    </div>
</div>

css

html, body {
    height: 100%;
}
.parent {
    width: 100%;
    height: 100%;
    display: table;
    text-align: center;
}
.parent > .child {
    display: table-cell;
    vertical-align: middle;
}
.slider {
    width: 50%;
    height: 10%;
}
.slick-prev:before, .slick-next:before {
    color:#808080;
}
.centerHorizontally {
    display: table;
    margin: 0 auto;
}
.halfWidth {
    width: 50%;
}

javascript

$('.multiple-items').slick({
    infinite: false,
    slidesToShow: 5,
    slidesToScroll: 5,
    dots: true
});

1 个答案:

答案 0 :(得分:2)

您必须将margin: 0 auto添加到.slider div。

.slider {
    margin: 0 auto;
}

小提琴:http://jsfiddle.net/4qpejg98/2/