简体   繁体   English

如何在图像旁边垂直对齐 DIV?

[英]How to vertically align a DIV next to an image?

I have the following html code:我有以下 html 代码:

<div id="personalInfo">
    <img class="photo" alt="" src="...." />

    <div id="details">
        <p>
        <label class="label">Name:</label>
        <label class="detailsLabel"></label>
        </p>

        <p>
        <label class="label">Date of birth:</label>
        <label class="detailsLabel"></label>
        </p>

        <p>
        <label class="label">Employee id:</label>
        <label class="detailsLabel"></label>
        </p>

        <p>
        <label class="label">Status:</label>
        <label class="detailsLabel"></label>
        </p>
   </div>
</div>

and the following css:以及以下 css:

#personalInfo     
{
   width: 35%;
   float: left;
   clear: left;
   margin-top: 5%; 
   margin-left: 2%;
   font-size: 1.3em;
}
#details { margin-left: 5%; } 
.photo { 
   vertical-align: middle; 
   width: 150px; 
   height: 150px; 
   float: left; 
   margin-left: 3%; 
   border: 1px solid #d1c7ac; }
.label { margin-top: 2%; margin-left: 5%; font-weight: bold; }
.detailsLabel { margin-top: 5%; margin-left: 0.5%; }

I need the 'details' div to be vertically aligned to middle relatively to the image.我需要“详细信息” div 相对于图像垂直对齐到中间。 How can I accomplish that?我怎样才能做到这一点?

Thanks !!!谢谢 !!!

Use display: inline-block .使用display: inline-block

#details { 
    display: inline-block; 
    vertical-align:middle;
    border:solid black 1px; 
    width: 300px; 
 } 
.photo { 
   display: inline-block; 
   vertical-align:middle;
   width: 300px; 
   height: 300px; 
   border: 1px solid #d1c7ac; 
}

try this尝试这个

#personalInfo{
   float: left;
   margin-top: 5%; 
   margin-left: 2%;
   font-size: 1.3em;
}
img{float:left;border:1px solid #333}
#details{float:left;padding:10px 0 10px 0}

working example: http://jsfiddle.net/bingjie2680/DSmKu/工作示例: http://jsfiddle.net/bingjie2680/DSmKu/

the idea is to float left both the image and details.这个想法是让图像和细节都向左浮动。 by doing so two elements will have the same height.通过这样做,两个元素将具有相同的高度。 and then you can make the detail div padding top and bottom some space.然后你可以让细节 div 填充顶部和底部一些空间。

basically what I would do is:If you can specify a fixed height (corresponding to your image height) for your outer container(#personalInfo div)..do it.基本上我会做的是:如果你可以为你的外部容器(#personalInfo div)指定一个固定的高度(对应于你的图像高度)..do it。 then I will set this #personalInfo position to relative: After that I will set your #details div position to absolute so that I can shift it to 50% from top and i would set margin-top:-yy where yy is half the height of the #details to offset the item up:然后我将此#personalInfo position 设置为相对:之后我会将您的#details div position 设置为绝对,以便我可以将其从顶部移至 50%,我将设置 margin-top:-yy 其中 yy 是高度的一半的#details 以抵消项目:

give a look here看看这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM