Image alignment within a row of divs

巧了我就是萌 提交于 2019-12-25 01:38:45

问题


I have a row of divs which I would like the images to centre vertical and horizontally.

<div id="treatments-wrapper">
                <div class="heading">
                    <h2>Our Treatments</h2>
                </div>
                <div class="treatments-logo">
                    <a href="#"><img src="img/nouveau-logo.png" alt="nouveau logo"></a>
                </div>
                <div class="treatments-logo">
                    <a href="#"><img src="img/nouveau-logo.png" alt="nouveau logo"></a>
                </div>
                <div class="treatments-logo">
                    <a href="#"><img src="img/nouveau-logo.png" alt="nouveau logo"></a>
                </div>
            </div><!--treatments-wrapper-->




 #treatments-wrapper {
    width:960px;
    height:100px;
    float: left;
    margin: 10px 0;
    border-bottom: #373839 solid 10px;
}

.treatments-logo {
    width: auto;
    height: 60px;
    float: left;
    margin: 0 8px;
}

.treatments-logo img {
    display: block;
    margin: auto;
}

.heading {
    width:960px;
    height: 40px;
    background-color: #373839;
    float: left;
    margin:
}

Here it is on jsfiddle

For this example I have used the same image 3 times, but it will be a row of different logos, all with different height and widths. Can anyone suggest something I could try out?


回答1:


try display:table-cell; with vertical-align:middle;

.treatments-logo {
    width: 1%;
    display:table-cell;
    height: 60px;
    vertical-align:middle;
}



回答2:


I think this is what you're after - I've removed the individual div's for each image and made the treatments-logo class the container.

HTML

<div class="treatments-logo">
    <a href="#">
        <img src="https://dl.dropboxusercontent.com/u/37491564/nouveau-logo.png" alt="nouveau logo"/>
    </a>
    <a href="#">
        <img src="https://dl.dropboxusercontent.com/u/37491564/nouveau-logo.png" alt="nouveau logo"/>
    </a>
    <a href="#">
        <img src="https://dl.dropboxusercontent.com/u/37491564/nouveau-logo.png" alt="nouveau logo"/>
    </a>
</div>

I've then changed these two CSS classes to this:

CSS

.treatments-logo {
    height: 60px;
    margin: 0 auto;
    text-align: center;
}
.treatments-logo img {
    display: inline-block;
}

Here's the Fiddle: http://jsfiddle.net/6jGTW/

Hope this helps!



来源:https://stackoverflow.com/questions/20569955/image-alignment-within-a-row-of-divs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!