如何将标头推到容器的一部分旁边?
我有一些 HTML:
<div id="thing">
<div id="contentheader">
<h3>Header</h3>
</div>
<div id="contentcontainer">
<div id="image">
<img alt="balt" src="imagesrc">
</div>
<div id="body">
<p>hegl gegl</p>
</div>
</div>
</div>
我需要将“contentheader”中的 h3 向下推到“contentcontainer”中的图像旁边,同时让正文文本位于其旁边。除了图像之外,所有内容都是可变宽度的。
也许图像会更好地展示:
如您所见,灰色对应于“thing”,绿色对应于“thing ”带有“contentcontainer”,蓝色带有“contentheader”。
编辑 HTML 将是一个很大的麻烦。除了图像固定宽度之外,我也无法制作任何其他内容。只用 CSS 可以做到吗? (如果能够用浮动和其他东西来做到这一点那就太棒了,但我不知道它是否可行)
I've got some HTML:
<div id="thing">
<div id="contentheader">
<h3>Header</h3>
</div>
<div id="contentcontainer">
<div id="image">
<img alt="balt" src="imagesrc">
</div>
<div id="body">
<p>hegl gegl</p>
</div>
</div>
</div>
I need to push the h3 in 'contentheader' down alongside the image in 'contentcontainer' while having the body text sit alongside it. Everything is of variable width save the image.
Perhaps an image will demonstrate better:
As you can see, grey corresponds with 'thing', green with 'contentcontainer' and blue with 'contentheader'.
Editing the HTML would be a major hassle. I also can't make anything other than the image fixed-width. Is it possible to do it with just CSS? (It'd be awesome to be able to do it with floats and stuff but I don't know if it's doable)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为您不会用 CSS 找到完美的解决方案。您可以使用定位,但如果您的标题很长且超过一行,则可能会遇到问题。
如果您愿意使用 JavaScript,则以下非框架代码片段可以使用。
您可以使用 jQuery 或其他 JavaScript 框架将此代码重新创建为更简洁的单行代码。
I don't think you're going to find a perfect solution with CSS. You could use positioning but you would probably run into issues if you had a long title that ran more than one line.
If you're open to using javascript the following non-framework snippet would work.
You could recreate this code as a neater, one-liner using jQuery or another javascript framework.
当然,这是基本设置的 Css:
http://jsfiddle.net/Nkapr/
询问您是否有任何问题。
Sure, heres the Css for a rudimentary setup:
http://jsfiddle.net/Nkapr/
Ask if you have any questions.
这里的问题是 HTML 结构,它的编写并没有真正考虑到您的目标(这真是太糟糕了!)
如果您所追求的只是将 H3 容器“contentheader”向下推,使其与“”中的其余内容保持一致您可以在“contentcontainer”上设置负上边距以将其向上拉,然后为“contentcontainer”中需要向下的元素添加正上边距(在本例中为“image”)给人的印象是 h3 部分实际上与其余内容位于同一位置。这有点像黑客,但如果您无法更改 HTML,它可能会起作用。
The problem here is the HTML structure, it's not been written really with your goal in mind (which is a bummer!)
If all you're after is pushing the H3 container 'contentheader' down in line with the rest of the stuff inside 'contentcontainer' you could set a negative top margin on 'contentcontainer' to pull it upwards, and then add a positive top margin to the elements in 'contentcontainer' which need to go down (in this case 'image') giving the impression that the h3 section actually sits in with the rest of the content. It's a bit of a hack but it might do the trick if you can't alter the HTML.
Thirtydot 在评论部分的回答解决了我的问题。
Thirtydot's answewr in the comments section solved my issue.