标记中前一个元素周围的浮动元素
我有一些相当基本的标题、图像和一些文本标记(示例):
<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 |
\-----------------------------------------/
我可以将
向右浮动,但图像的宽度会有所不同,因此我无法为元素指定恒定的宽度。我更愿意保留上述标记顺序,因为它在语义上更有意义,并且当显示无样式时。
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 |
\-----------------------------------------/
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一种可能的方法。
标记:
CSS:
视觉示例(在 Firefox 中):
Here is one possible way.
Markup:
CSS:
Visual example (in Firefox):
到目前为止,这是我的解决方案。
您可以在这里找到它: 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.