如何在容器宽度为100%</div>中将一排相对居中的<div>居中

时间:2014-10-10 23:58:50

标签: hover position center relative transitions

我试图将一排相对居中的div居中,这些div包含悬停过渡到另一个图像的图像。

在使悬停过渡工作的过程中,我将每个div的容器定位为position:relative。

我正试图将行放到中心,因为窗口浏览器变小了以使其响应。发生的行为虽然如此奇怪。作为浏览器窗口的图像太小而无法容纳图像行将跳转到第二行,但它将停止屏幕左侧,但不会居中。

有没有人有解决方案或理解为什么会这样?理想情况下,我希望跳到第二行的图像居中。

这是我的代码:

http://jsfiddle.net/4kfbh018/

HTML

<div class="center-logo">
<div class="nbc-container">
<a href="#">
    <div class="nbc press">
        <span></span>
    </div>
</a>
</div>

<div class="npr-container">
<a href="#">
    <div class="npr press">
        <span></span>
    </div>
</a>
</div>

<div class="washington-container">
<a href="#">
    <div class="washington press">
        <span></span>
    </div>
</a>
</div>

<div class="bbc-container">
 <a href="#">
    <div class="bbc press">
        <span></span>
    </div>
 </a>
</div>

<div class="forbes-container">
<a href="#">
    <div class="forbes press">
        <span></span>
    </div>
</a>
</div>

<div class="cnet-container">
<a href="#">
    <div class="cnet press">
        <span></span>
    </div>
</a>
</div>

</div>

CSS

.center-logo {
margin: 0 auto;
height: 99px;
width: 100%;
}

.press {
    top: 0; bottom: 0; left: 0; right: 0; 
}

.press:hover span {
    opacity: 1;
}

.nbc {
    background-image: url('http://www.yodamark.net/files/7114/0570/5786/nbc.jpg');
    height: 79px;
    width: 79px;

}

.nbc-container {
    height: 79px;
    margin: 10px 10px;
    width: 79px;
    float: left;
}

.nbc span {
    height: 79px;
    width: 79px;
    position: absolute;
    top: 0; bottom: 0; left: 0; right: 0; 
    background-image: url('http://www.yodamark.net/files/4114/1168/9420/nbc-b.jpg');
    opacity: 0;
    transition: linear .2s, box-shadow linear .2s;
    -webkit-filter: grayscale(1);
} 


.npr {
    height: 41px;
    width: 109px;
    background-image: url('http://www.yodamark.net/files/6414/0570/5787/npr.jpg');
    height: 41px;
    width: 109px;
}

.npr-container {
    height: 41px;
    margin: 25px 10px 0 10px;
    width: 109px;
    float: left;
}

.npr span {
    position: absolute;
    top: 0; bottom: 0; left: 0; right: 0; 
    background-image: url('http://www.yodamark.net/files/6414/1168/8919/npr-b.jpg');
    height: 41px;
    width: 109px;
    opacity: 0;
    transition: linear .2s, box-shadow linear .2s;
    -webkit-filter: grayscale(1);
} 

.washington {
    background-image: url('http://www.yodamark.net/files/6514/0570/5787/wapo.jpg');
    height: 34px;
    width: 232px;
}

.washington-container {
    float: left;
    height: 41px;
    margin: 25px 10px 0 10px;
    width: 232px;
}

.washington span {
    position: absolute;
    top: 0; bottom: 0; left: 0; right: 0; 
    background-image: url('http://www.yodamark.net/files/6214/1168/8864/wapo-b.jpg');
    height: 34px;
    width: 232px;
    opacity: 0;
    transition: linear .2s, box-shadow linear .2s;
    -webkit-filter: grayscale(1);
} 

.bbc {
    background-image: url('http://www.yodamark.net/files/2914/0570/5785/bbc.jpg');
    height: 35px;
    width: 186px;
}

.bbc-container {    
    height: 35px;
    margin: 25px 10px 0 10px;
    width: 186px;
    float: left;
}

.bbc span {
    position: absolute;
    top: 0; bottom: 0; left: 0; right: 0; 
    background-image: url('http://www.yodamark.net/files/1314/1168/9450/bbc-b.jpg');
    height: 35px;
    width: 186px;
    opacity: 0;
    transition: linear .2s, box-shadow linear .2s;
    -webkit-filter: grayscale(1);
}

.forbes {
    background-image: url('http://www.yodamark.net/files/2614/0570/5786/forbes.jpg');
    height: 38px;
    width: 117px;
}

.forbes-container {
    height: 38px;
    margin: 25px 10px 0 10px;
    width: 117px;
    float: left;
}

.forbes span {
    position: absolute;
    top: 0; bottom: 0; left: 0; right: 0; 
    background-image: url('http://www.yodamark.net/files/1514/1168/9431/forbes-b.jpg');
    height: 38px;
    width: 117px;
    opacity: 0;
    transition: linear .2s, box-shadow linear .2s;
    -webkit-filter: grayscale(1);
}

.cnet {
    background-image: url('http://www.yodamark.net/files/5314/0570/5785/cnet.jpg');
    height: 83px;
    width: 85px;
}

.cnet-container {
    height: 83px;
    margin: 0 10px;
    width: 85px;
    float: left;
}

.cnet span {
    position: absolute;
    top: 0; bottom: 0; left: 0; right: 0; 
    background-image: url('http://www.yodamark.net/files/8014/1168/9440/cnet-b.jpg');
    height: 83px;
    width: 85px;
    opacity: 0;
    transition: linear .2s, box-shadow linear .2s;
    -webkit-filter: grayscale(1);
}

1 个答案:

答案 0 :(得分:0)

No Floats

使用display:inline-block代替float:left

除了将内容集中在其中之外,

text-align:center对浮动元素没有影响。

你可以看到here我在哪里覆盖了你的CSS。