标记中前一个元素周围的浮动元素

发布于 2024-12-27 04:18:42 字数 1272 浏览 0 评论 0原文

我有一些相当基本的标题、图像和一些文本标记(示例):

<div>
  <h1>
  <img>
  <p>
</div>

< strong>我想将图像浮动到前面的标题后面的段落的左侧,而不更改 HTML。

理想情况下,我只需将图像浮动到左侧:

img {
  float: left;
}

但是因为标题出现在图像之前标记,标题将跨越整个容器:

/-----------------------------------------\
| Header Header Header                    |
| |-------|                               |
| |-Image-|  Paragraph text               |
| |-------|  Paragraph text               |
| |-------|  Paragraph text               |
\-----------------------------------------/

我希望它看起来像:

/-----------------------------------------\
| |-------|  Header Header Header         |
| |-------|                               |
| |-Image-|  Paragraph text               |
| |-------|  Paragraph text               |
| |-------|  Paragraph text               |
\-----------------------------------------/

我迄今为止的努力( Dabblet)

我可以将

向右浮动,但图像的宽度会有所不同,因此我无法为元素指定恒定的宽度。我更愿意保留上述标记顺序,因为它在语义上更有意义,并且当显示无样式时。

I've got some fairly basic markup for a header, an image, and some text (example):

<div>
  <h1>
  <img>
  <p>
</div>

I'd like to float the image left of both the preceding header and the succeeding paragraph, without changing the HTML.

Ideally, I'd simply float the image left:

img {
  float: left;
}

But because the header appears before the image in the markup, the header will span the entire container:

/-----------------------------------------\
| Header Header Header                    |
| |-------|                               |
| |-Image-|  Paragraph text               |
| |-------|  Paragraph text               |
| |-------|  Paragraph text               |
\-----------------------------------------/

I'd like it to look like:

/-----------------------------------------\
| |-------|  Header Header Header         |
| |-------|                               |
| |-Image-|  Paragraph text               |
| |-------|  Paragraph text               |
| |-------|  Paragraph text               |
\-----------------------------------------/

My efforts so far (Dabblet)

I can float the <h1> to the right, but the width of the image will vary, so I can't specify a constant width for the element. I would prefer to keep the above markup order because it makes more sense semantically, and when the displayed unstyled.

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

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

发布评论

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

评论(2

暮年慕年 2025-01-03 04:18:42

这是一种可能的方法

标记:

<div>
    <h2>This is my title</h2>
    <img src="http://dummyimage.com/320x200/ff00ff/fff&text=woooo!" alt="sample" />
    <p>I'd like to float the image left of both the preceding header and the succeeding paragraph, without changing the HTML. I'd like to float the image left of both the preceding header and the succeeding paragraph, without changing the HTML. I'd like to float the image left of both the preceding header and the succeeding paragraph, without changing the HTML. I'd like to float the image left of both the preceding header and the succeeding paragraph, without changing the HTML.</p>
</div>

CSS:

div { position: relative; }
img { float: left; margin-right: 20px; }
p { position: relative; top: 24px; }
h2 { position: absolute; left: 340px; font-weight: bold; }

视觉示例(在 Firefox 中):

example

Here is one possible way.

Markup:

<div>
    <h2>This is my title</h2>
    <img src="http://dummyimage.com/320x200/ff00ff/fff&text=woooo!" alt="sample" />
    <p>I'd like to float the image left of both the preceding header and the succeeding paragraph, without changing the HTML. I'd like to float the image left of both the preceding header and the succeeding paragraph, without changing the HTML. I'd like to float the image left of both the preceding header and the succeeding paragraph, without changing the HTML. I'd like to float the image left of both the preceding header and the succeeding paragraph, without changing the HTML.</p>
</div>

CSS:

div { position: relative; }
img { float: left; margin-right: 20px; }
p { position: relative; top: 24px; }
h2 { position: absolute; left: 340px; font-weight: bold; }

Visual example (in Firefox):

example

怎言笑 2025-01-03 04:18:42

到目前为止,这是我的解决方案。

您可以在这里找到它: http://jsfiddle.net/gHNdJ/

希望这就是您正在寻找的内容。

注意:如果您仍然想保留这样的html,第一个答案的解决方案是迄今为止最好的。但我不喜欢使用定位。

Here is so far my solution.

You can find it here : http://jsfiddle.net/gHNdJ/

Hope this is what you are searching for.

Note: if you still want to keep the html like that, the solution of the first answer is the best so far. but I'm not like using positioning.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文