【问题标题】:How can I can I center a row of relatively centered <div>s in a container width of 100%如何在 100% 的容器宽度中将一排相对居中的 <div> 居中
【发布时间】:2014-12-06 05:26:42
【问题描述】:

我正在尝试将一排相对居中的 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);
}

【问题讨论】:

    标签: css hover css-position css-transitions center


    【解决方案1】:

    无浮动

    使用display:inline-block 而不是float:left

    text-align:center 对浮动元素没有任何影响,除了将其内容居中。

    你可以看到它here 我覆盖了你的 css。

    【讨论】:

    • 谢谢filever10!!我一直被困在这上面!
    • 实际上,我意识到解决方案不起作用,因为悬停效果不起作用。你有解决这个问题的办法吗?
    • 我找到了解决悬停效果的方法。我只是将图像的容器作为位置:相对,以便图像和悬停图像都绝对定位并且悬停后不会离开屏幕。您的内联解决方案仍然有效。谢谢!!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    • 2011-01-21
    • 2015-04-07
    • 2013-10-09
    • 2011-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多