CSS - 将图像放置在文本旁边

发布于 2024-07-26 06:28:25 字数 688 浏览 5 评论 0原文

我正在做一个网站,其中图像需要在文本内容旁边呈现 - 一种伪两列布局,因为图像和文本来自单个 html 源。

我找到了一种非常简单的方法来做到这一点,即将图像作为它们自己的段落并浮动它们。 是否还有更简单的方法(就 html 而言)来做到这一点,而无需这些额外的段落,并且仅将额外的 css 归因于图像?

如果浮动图像与文本位于同一段落中,则带图像和不带图像的段落宽度会有所不同。

编辑:基本上,我正在寻找尽可能简单的 HTML 标记来定位图像,例如 这个。 CSS 可能很复杂;)

CSS:
p { width: 500px; }
p.image { float: right; width: 900px; }


Current HTML:
<p class="image"><img src="image.jpg" /></p>
<p>Some text here.</p>


Is the above possible with this HTML?
<p><img src="image.jpg" /></p>

I'm doing a site in which images need to presented next to textual content - a sort of pseudo two-column layout, as the images and text come from a single html source.

I've found quite a simple way to do this by putting the images as their own paragraphs and floating them. Would there still be a more simpler way (in regards to html) to do this without these extra paragraphs and by only attributing extra css to images?

If the floated image is in the same paragraph than the text, then paragraphs with and without images would be different in width.

EDIT: Basically, I'm looking for as simple HTML markup as possible to position images like this. The CSS can be complex ;)

CSS:
p { width: 500px; }
p.image { float: right; width: 900px; }


Current HTML:
<p class="image"><img src="image.jpg" /></p>
<p>Some text here.</p>


Is the above possible with this HTML?
<p><img src="image.jpg" /></p>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

乞讨 2024-08-02 06:28:25

您在找这个吗?

p img { float: right; width: 900px; }

这将匹配 p 标签内的所有 img 标签。
但我建议始终使用类或 id 来匹配 CSS 规则。 仅使用标签名称迟早会导致恼人的陷阱。

编辑

嗯,也许我误解了你的意思。 您想应用 float: right; width: 900px; 到 p 元素,而不是 img 元素...

据我所知,没有办法选择特定元素的父元素。 它始终按照 PARENT -> 方向工作。 孩子,不是孩子 -> 家长。

Are you looking for this?

p img { float: right; width: 900px; }

This would match all img-tags inside p-tags.
But I recommend always using classes or ids to match CSS rules. Just using the tag name can lead to annoying pitfalls, sooner or later.

Edit

Mhm, maybe I got you wrong. You would like to apply float: right; width: 900px; to the p-elements, not the img-elements ...

AFAIK there is no way to select a parent of a specific element. It always works in direction PARENT -> CHILD, not CHILD -> PARENT.

薄荷梦 2024-08-02 06:28:25

不会。将 img 放在 p 内后,您可以将图像向右浮动,但文本不会保留在列中。 它将包裹在图像下方。 您应用于 p 的任何右边距或填充也将应用于 img。

由于您有两条相关信息,我会将它们包装在 div 中,然后将它们放置在 div 中。

.info {width:900px;}
.info img {float:right;}
.info p {margin-right:400px;}

<div class="info">
    <img src="image.jpg" />
    <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p>
</div>

No. With the img inside the p, you can float the image right but the text will not stay in a column. It will wrap underneath the image. Any right margin or padding you apply to the p will also apply to the img.

Since you have two pieces of related information, I would wrap those in a div and then position them within the div.

.info {width:900px;}
.info img {float:right;}
.info p {margin-right:400px;}

<div class="info">
    <img src="image.jpg" />
    <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p>
</div>
生来就爱笑 2024-08-02 06:28:25

回应艾米丽的回答。

没有。 将 img 放在 p 内,您可以
使图像向右浮动,但使文本浮动
不会停留在一个列中。 它会
包裹在图像下方。 任何权利
应用于 p 的边距或填充
也适用于 img。

虽然她是对的,但就她目前而言,有一个解决方法。 虽然它并不理想:

p {position: relative; padding-right: 950px; /* width of the image + 50px margin */ }

img {position: absolute; top: 0; right: 0; }

这应该将图像放在 p 的右上角,同时将文本强制放入 p 元素的左边界和950px 右内边距。 不理想,而且有点混乱,但它允许柱状效果而不添加额外的标签。

...除非您想在末尾添加一个clearfix br(br.clearfix: display: block; clear: Both)段落(强制 p 标签延伸到短段落的图像之外)。

In response to Emily's answer.

No. With the img inside the p, you can
float the image right but the text
will not stay in a column. It will
wrap underneath the image. Any right
margin or padding you apply to the p
will also apply to the img.

While she's right, as far she goes, there is a workaround. Though it's not ideal:

p {position: relative; padding-right: 950px; /* width of the image + 50px margin */ }

img {position: absolute; top: 0; right: 0; }

This should put the image in the top-right corner of the p, while forcing the text into a column between the left boundary of the p element and the 950px right-padding. Not ideal, and a little messy, but it allows for the columnar effect without adding extra tags.

...unless you want to add a clearfix br(br.clearfix: display: block; clear: both) at the end of the paragraph (to force the p tag to extend past the image for short paragraphs).

假装爱人 2024-08-02 06:28:25

是的,刚刚测试了这个,
确保使用严格的文档类型

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 

<style>
p { width: 500px; position:relative;}
p img { position:absolute; margin-left:520px;}
</style>

<p><img src="PastedImage.jpg" />text text text text text text text text text text text text text text text text text text text text text text text text text text text </p>

yes, just tested this,
make sure you use the strict doctype

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 

<style>
p { width: 500px; position:relative;}
p img { position:absolute; margin-left:520px;}
</style>

<p><img src="PastedImage.jpg" />text text text text text text text text text text text text text text text text text text text text text text text text text text text </p>
爱你是孤单的心事 2024-08-02 06:28:25

我在这里编写了这行代码,以帮助我将一些文本准确地定位在我想要的位置。 我可能是一个新手,但它简单、轻松、精确地完成了工作。 和所有 HTML。

<a href="http://elonmusk.com" STYLE="position:absolute; TOP: 24px; LEFT:50px;">put your txt in here and use the Top and Left values to position the text precisely where you want it</a>

您还可以使用 href 字段使文本成为超链接,或者如果这就是您现在想要的,您可以删除 href 字段,但请注意保留“

<a STYLE="position:absolute; TOP: 24px; LEFT:50px;">txt go here - use Top & Left values to position it. ex. of text with no hyperlink</a>

I concocted this line of code here to help me position some text exactly where I wanted it. I might be a novice way but it got the job done simply and easily and precise. and all HTML.

<a href="http://elonmusk.com" STYLE="position:absolute; TOP: 24px; LEFT:50px;">put your txt in here and use the Top and Left values to position the text precisely where you want it</a>

you can also use the href field to make your text a hyperlink or if thats now what you want you can jus delete the href field but be mindful to keep the "

<a STYLE="position:absolute; TOP: 24px; LEFT:50px;">txt go here - use Top & Left values to position it. ex. of text with no hyperlink</a>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文