简体   繁体   English

图像和Div-并排

[英]Image and Div - side by side

I would like to create a set up where there is an image on the left and and div on the right. 我想创建一个设置,其中左侧有一个图像,右侧有div。 I am using float left to accomplish this but I would like the right div to have the same height as the image on the left and for the text to be centered vertically. 我正在使用float left来完成此操作,但我希望right div具有与左侧图像相同的高度,并使文本垂直居中。

here is a jsfiddle 这是一个jsfiddle

<img class="half" src="http://placekitten.com/200/300">
<section class="articlename">Gangstas Drinkin</section>  

...

.half{float:left;width:50%;}
.articlename{width:50%;background-color:black;float:left;color:white;}

So to recap i want the cat to be 50% of the page and the other 50% to be black with the text and the right side needs to match the height of the image. 因此,回顾一下,我希望猫占页面的50%,其他50%的文本为黑色,并且右侧需要与图像的高度匹配。

Is this possible? 这可能吗?

http://jsfiddle.net/Dyt2X/23/ http://jsfiddle.net/Dyt2X/23/
http://fiddle.jshell.net/Dyt2X/23/show/ http://fiddle.jshell.net/Dyt2X/23/show/
Note: remove the white space between the tags to avoid inline-block elements to add unwanted space. 注意:删除标签之间的空白,以避免内联块元素添加不必要的空间。

.wrap {
    display:inline-block;
    background-color:#000;
}
.half,
.articlename {
    display:inline-block;
    width:50%;
    vertical-align:middle;
}
.articlename {
    color:#fff;
    text-align: center;
}

You could do something like this: http://codepen.io/the_ruther4d/pen/695e1228a4efa13ee3e5551687c66247/ 您可以执行以下操作: http : //codepen.io/the_ruther4d/pen/695e1228a4efa13ee3e5551687c66247/

Using relative positioning on a wrapper element we can use the absolute position method of vertically centering the articlename. 通过在包装元素上使用相对定位,我们可以使用将商品名称垂直居中的绝对位置方法。

Make the child div inline or inline-block 使子div inlineinline-block

A simple example is below: 下面是一个简单的示例:

html: HTML:

<div >
  <img src="myimg.png" />
  <div class="child-div1">My div content</div>
</div>

css: CSS:

.child-div1 {
    display: inline-block;
}

Hope that helps. 希望能有所帮助。

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

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