简体   繁体   English

使用CSS使居中文本列表与图像对齐

[英]making a list of centered text aligned next to image with CSS

I'd like to make a (non-numbered or bulleted) list of items, where each item has some text that is adjacent to an image. 我想列出(未编号或项目符号)项目列表,其中每个项目都有一些与图像相邻的文本。 It's important that the text be vertically aligned to the center of the image, and that there will enough top and bottom padding so that each pair of text and image do not vertically overlap. 重要的是,文本必须与图像的中心垂直对齐,并且顶部和底部应有足够的填充,以使每对文本和图像都不会垂直重叠。 I tried using something like this: 我试过使用这样的东西:

img.floatRight { float: right; margin-bottom: 2px; border: 0px; vertical-align: text-middle}
.myitem { font-size: 12pt; margin-bottom: 50px; margin-top: 130px; vertical-align: middle}

in body of HTML page: 在HTML页面的正文中:

<!-- item 1 -->
<img src="item1.jpg" class="floatRight"><p class="myitem">Description of item 1</p>
<!-- item 2 -->
<img src="item2.jpg" class="floatRight"><p class="myitem">Description of item 2</p>

the problem is that the items overlap -- so the images are not vertically aligned which each other, which i'd like them to be. 问题是这些项目重叠-因此图像彼此之间不是垂直对齐的,我希望它们是垂直的。 the margin-top and margin-bottom don't seem to add the right level of padding to achieve this. 上边距和下边距似乎没有添加正确的填充水平来实现此目的。 also, the vertical-align parameter does not seem to vertically align the text to the center of the image. 同样,vertical-align参数似乎不能使文本垂直对齐到图像的中心。

finally, I'm not sure if I should use p class="" or div class="" here... again, it's important not to have overlap between each items' 最后,我不确定是否应该在此处使用p class =“”或div class =“” ...同样,重要的是每个项目之间不要有重叠

any ideas on how to fix this? 有想法该怎么解决这个吗? thanks very much. 非常感谢。

I would start with putting the images inside the p.myitem block and giving it a: 我将从将图像放入p.myitem块中并给它一个开始:

p.myitem {
    overflow: hidden;
}

That should take care of the overlap. 那应该注意重叠。

About the vertical align of the images and the text, that can be tricky using css. 关于图像和文本的垂直对齐,使用CSS可能会比较棘手。 If the vertical-align solution does not work and you know the height of the images and it is only one line of text, the easiest solution is giving the paragraph a line-height equal to the images. 如果垂直对齐解决方案不起作用,并且您知道图像的高度并且仅一行文本,则最简单的解决方案是使段落的行高等于图像的高度。

Vertical aligning things without using tables is a pain in the butt! 在不使用桌子的情况下进行垂直对齐会很麻烦! You're better off using a table for this. 您最好为此使用表格。 Are your images all using the same width? 您的图像都使用相同的宽度吗? If not, you can remove the fixed width in td.item_photo{} and td.item_photo img{}. 如果没有,您可以在td.item_photo {}和td.item_photo img {}中删除固定宽度。 Also you can change table.items width from auto to 100% or a px amount (eg: 400px) if you want. 您也可以根据需要将table.items的宽度从auto更改为100%或px数量(例如:400px)。

I hope this helps solve your problem. 我希望这有助于解决您的问题。

table{margin:0;padding:0;border-collapse:collapse}

table.items{width:auto}
td.item_photo{vertical-align:middle;text-align:left;width:200px;padding:0 0 15px 15px}
td.item_text{vertical-align:middle;text-align:left;padding:0 0 15px 0;font-size:12px;line-height:16px}
td.item_photo img{display:block;width:200px}

<table class="items">
    <tr>
        <td class="item_text">Item 1 Description</td>
        <td class="item_photo"><img src="/images/item1.jpg" /></td>
    </tr>
    <tr>
        <td class="item_text">Item 2 Description</td>
        <td class="item_photo"><img src="/images/item2.jpg" /></td>
    </tr>
</table>

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

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