html - 将两个图像与文本对齐

标签 html css

<分区>

我想对齐本站顶部的两张图片http://paulcwebster.com 旁边有文字。

到目前为止,我能够将两个图像并排对齐,但我遇到了以下问题:

1) 图片之间没有任何空格的文本。

2) 对于不同浏览器上的图像,文本太短或太长。

3) 文字大小与图片位置不符

4) 之后无法将文本“Recent Publications”移到左侧(我已将 align="left"放在它自己的 div 中)

这是我的代码:

<div >
 <img src="gfx/FrontImage1.jpg" width="180" height="180" alt="Image1"  style="float:left">  
 <img src="gfx/FrontImage3.jpg" width="180" height="180" alt="Image2" style="float:left"> 


 <span ><p>Paul Christopher Webster is a freelance writer and documentary film director based in Toronto, Canada.  He has reported from 20 countries since 1992.</p>
Paul's work in film has appeared on the Arte, BBC, CBC, Deutsche Welle, Discovery, National Geographic, Slice, SWR and Vision Television networks. His work as a writer has been published in dozens of magazines, journals and newspapers across Canada, the U.S, and Europe. </p>
<p>He has won four national magazine awards for his writing, and Tier One Journalism Award from the Canadian Institutes of Health Research. His work on documentary films has garnered awards from the Canadian Association of Journalists, Hot Docs, the Canadian Academy of Film and Television, and PARISCIENCE, the international festival of scientific films.
<p>Paul&rsquo;s work focuses on themes in business, science, and politics. For samples of his work in these categories, please click on the links to articles below.
</span>
</div>

最佳答案

首先,我建议重组您的 HTML 标记。放一个<div>或您要控制的每个元素周围的其他 block 级元素。像这样:

<div class="container">
    <div class="photos">
      <img src="gfx/FrontImage1.jpg" width="180" height="180" alt="Image1">
      <img src="gfx/FrontImage3.jpg" width="180" height="180" alt="Image2">
    </div>
    <div class="text">
      Your text goes here...
    </div>
</div>

然后,编写一些 CSS 以您想要的方式控制这些元素:

.container {
    overflow:hidden; // this keeps the floated objects inside the container
    padding:5px;
    border:solid 1px black;
}

.container .photos {
    float:left;
    margin-right:10px; // this puts some padding to the right of the photos
}

我在这里假设您知道如何包含样式表以将上述 CSS 应用到您的页面。这应该可以解决问题 1) 和 4)。确实没有任何方法可以确保文本的大小始终与图像的高度匹配,因为它可以流动和变化,具体取决于浏览器的宽度和查看者使用的字体大小,所以我看不到解决问题 2) 和 3) 的方法。

注意:我看到您使用的是 <span>元素包含您的 <p>元素。这是无效的标记,因为 <span>是一个内联元素,一个 <p>是 block 级元素。内联元素不能包含 block 级元素。

关于html - 将两个图像与文本对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15993811/

相关文章:

javascript - Ajax 提交 ajax 加载的表单

javascript - 使用 jquery 根据模式验证输入框并在出现错误时显示 div

javascript - 当我将 html 和正文高度设置为 100% 时,Smooth Scroll 不起作用

javascript - Protractor 获取元素的比例或缩放宽度和高度

html - 标题栏不包含下拉菜单 (CSS)

javascript - 如何为选项以外的html组合框赋值?

css - 如何在动态高度的 flexbox 中进行 div 拉伸(stretch)?

css - 使用 $.get() 和 .html() 调用时,jQuery UI 按钮丢失 CSS 样式

html - html网站中的页脚显示

jquery - 将div附加到dom时如何隐藏它的一部分