我需要图像水平对齐并在页面上居中

时间:2014-11-07 00:33:46

标签: html css

我不确定如何水平对齐图像并让它们在不使用表格的情况下居中在页面上。我用div尝试了这个但是不能让它在页面上居中。我也希望他们能够做出回应,这样他们在缩小尺寸时总是处于中间位置。

这就是我在JSFiddle中所得到的:http://jsfiddle.net/DrSRT/403/

CSS

/*Black and White
--------------------------------------------------*/
.bw {
  -webkit-transition: all .5s ease;
  -moz-transition: all .5s ease;
  -o-transition: all .5s ease;
  -ms-transition: all .5s ease;
      transition: all .5s ease;
}

.bw:hover {
  -webkit-filter: grayscale(100%);
}


*/container
-----------------------------------------*/
.footersocial {
width:100%;
float:center;
}

HTML

<div id="footersocial"><div class="bw pic"><a href="https://www.facebook.com/PeopleAndPlatforms" target="_blank"><img style="float: left; margin: 2px;" src="http://test.peopleandplatforms.com/wp-content/uploads/2014/11/Footer_facebook_hover.png" alt="facebook"></a></div>
<div class="bw pic"><a href="http://twitter.com/peoplenplatform" target="_blank"><img style="float: left; margin: 2px;" src="http://test.peopleandplatforms.com/wp-content/uploads/2014/11/Footer_twitter_hover.png" alt="Twitter"></a></div>
<div class="bw pic"><img style="float: left; margin: 2px;" src="http://test.peopleandplatforms.com/wp-content/uploads/2014/11/Footer_pinterest_hover.png" alt="pinterest"></div>
<div class="bw pic"><a href="http://www.linkedin.com/company/people-&-platforms" target="_blank"><img style="float: left; margin: 2px;" src="http://test.peopleandplatforms.com/wp-content/uploads/2014/11/Footer_linkedin_hover.png"  alt="linkedin"></a></div>
<div class="bw pic"><a href="https://plus.google.com/u/1/112443165541377795854" target="_blank"><img style="float: left; margin: 2px;" src="http://test.peopleandplatforms.com/wp-content/uploads/2014/11/Footer_gplus_hover.png"  alt="googleplus"></a></div>
</div>

2 个答案:

答案 0 :(得分:1)

这里我做了一个改变并添加一些CSS的例子:

http://jsfiddle.net/DrSRT/405/

大多数情况下,我使用.footersocialmargin: 0 auto;对齐到中心,但为此您需要固定宽度。现在要使其响应,请在更改屏幕宽度后使用@media screen并更改.footersocial宽度。一旦较小,图像也会变小,因为它们的宽度是%。

示例:

@media screen and (max-width: 720px) {
    .footersocial {
        width: 100px;
    }
}

媒体查询W3S文档:http://www.w3schools.com/cssref/css3_pr_mediaquery.asp

答案 1 :(得分:0)

以下是使用表格的一种解决方案:http://jsfiddle.net/Lkynvdxp/

HTML:

<div id="footersocial">
    <a href="#">
        <img src="http://test.peopleandplatforms.com/wp-content/uploads/2014/11/Footer_facebook_hover.png" />
    </a>
    <a href="#">
        <img src="http://test.peopleandplatforms.com/wp-content/uploads/2014/11/Footer_twitter_hover.png" />
    </a>
    <a href="#">
        <img src="http://test.peopleandplatforms.com/wp-content/uploads/2014/11/Footer_linkedin_hover.png" />
    </a>
    <a href = "#">
        <img src="http://test.peopleandplatforms.com/wp-content/uploads/2014/11/Footer_gplus_hover.png" />
    </a>
</div>

CSS:

#footersocial {
    display: table;
    margin: 0 auto;
}

#footersocial > a {
    display: inline-block;
    position: relative;
    width: 34px;
    height: 33px;
}

#footersocial > a > img {
    height: 100%;
}

#footersocial > a:before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    border-radius: 4px;
    background-color: rgba(0, 0, 0, 0.2);
    opacity: 0;
    transition: opacity 0.5s linear;
} 

#footersocial > a:hover:before {
    opacity: 1;
}

以下是使用text-align: center的另一种解决方案:http://jsfiddle.net/k7ye3t0p/

CSS:

#footersocial {
    text-align: center;
}

/*  The rest is the same as in the first solution */