简体   繁体   English

将多行文字对齐到图像的右侧

[英]Align multi-line text to the right of an image

I want to make it so that there is an image, and to the right of the image, there is text which should take up just two lines. 我要使其具有图像,并且在图像的右侧,应该只占两行的文本。 So, something like this.. The HTML: 所以,像这样。HTML:

<img>
<p>text one</p>
<p>text two</p>

The CSS: CSS:

img {
    width: 200px;
    height: 200px;
    display: inline-block;
    vertical-align: top;
}

p {
    display: inline-block;
}

The issue with this is that the two 'p' tags are on the same line. 问题是两个“ p”标签在同一行上。 I tried placing a 我尝试放置一个

<br />

in between them but that sends the second 'p' below the image. 在它们之间,但是会在图像下方发送第二个“ p”。 Is there any way for me to make the two 'p' tags each on their own line? 我有什么办法可以使两个“ p”标签分别位于自己的行上? Note: I do not want to put the text inside a div and then give the div a width of, say 200px because I want the text to be exactly two lines regardless of the screen size. 注意:我不想将文本放在div内,然后给div设置200px的宽度,因为无论屏幕大小如何,我都希望文本恰好是两行。 (The text is generated by the end user so if the text is long and the div is only 200px then the two 'p' tags might take up more than just two lines). (文本是由最终用户生成的,因此,如果文本较长且div仅200px,则两个'p'标签可能会占用不止两行)。

Here is a fiddle: http://jsfiddle.net/dgh8kf5x/1/ 这是一个小提琴: http : //jsfiddle.net/dgh8kf5x/1/

 div { display: inline-block; } img { width: 200px; height: 200px; display: inline-block; vertical-align: top; } 
 <img> <div> <p>Line 1</p> <p>Line 2</p> </div> 

The key is to use DIVs to surround the two areas you wish to be side-by-side. 关键是要使用DIV围绕您希望并排的两个区域。 Then, use float:left to allow the DIVs to go side-by-side. 然后,使用float:left允许DIV并排运行。

DIVs are absolutely vital to understanding positioning. DIV对于理解定位绝对至关重要。

jsFiddle Demo jsFiddle演示

HTML: HTML:

<div id="imgDiv">
    <img src="http://placekitten.com/g/200/200" />
</div>
<div id="txtDiv">
    <p>text one</p>
    <p>text two</p>
</div>

css: CSS:

#imgDiv{height:200px;width:200px;float:left;}
#txtDiv{height:200px;width:200px;float:left;}

Please Read: 请阅读:

http://css-tricks.com/absolute-relative-fixed-positioining-how-do-they-differ/ http://css-tricks.com/absolute-relative-fixed-positioining-how-do-they-differ/

http://www.bennadel.com/blog/2541-most-css-floats-can-be-replaced-with-relative-and-absolute-positioning.htm http://www.bennadel.com/blog/2541-most-css-floats-can-be-replaced-with-relative-and-absolute-positioning.htm

Why Float is better than position:relative and absolute while we can make layout quickly with position? 为什么浮动可以比位置更好:相对和绝对,而我们可以通过位置快速进行布局?

But, above all, understand that you should always surround groups of elements with DIVs. 但是,最重要的是,您应该始终将DIV包围在元素组中。 They cost absolutely no (none, nada, zero) height/width overhead, but allow you to do all kinds of great positioning stuff. 它们的高度/宽度开销绝对没有(没有,nada,为零),但是允许您执行各种出色的定位工作。

you can update your code like: 您可以像这样更新代码:

The HTML: HTML:

<img>
<p>text one</p>
<p>text two</p>

The CSS: CSS:

img {
width: 200px;
height: 200px;`enter code here`
vertical-align: top;
float:left;

} }

Demo 演示版

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

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