如何使用 CSS 使字段集适合其内容的大小?
我有一个包含一些内容的字段集,并且内容相对于其原始位置移动。请参阅此 jsFiddle。
当我进入 Stack Overflow 并搜索我的问题时,我很高兴:我认为 这个问题就是答案。然而,这个问题只涉及宽度(将字段集的宽度缩小到内容的大小,将显示更改为内联,或类似的东西)。
如何将字段集缩小到内容的大小? 即,我希望当字段集的内容向上移动时,字段集的高度也缩小。
I have a fieldset with some contents, and the contents are moved relative to their original position. See this jsFiddle.
I was pleased when I got on Stack Overflow and searched for my problem: I thought this question was the answer. However, that question only deals with width (to shrink the width of the fieldset to the size of the contents, change the display to inline, or something similar).
How do I shrink the fieldset to the size of the contents? Namely, I want the height of the fieldset to shrink when the contents of the fieldset are moved up.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许您可以将
display:inline-block;
添加到字段集的 css、f.ex。在你的例子中:这能达到你的意思吗?不过,较旧的浏览器应坚持使用
display:inline;
。编辑:好的,用新的小提琴我明白你想要实现什么。我想在这种情况下你必须使用 javascript 来调整高度 - 如果它适合你。
下面是一个示例:
只需将
id="myfieldset"
添加到您的字段集即可使其在您的示例中工作。链接到此解决方案: http://jsfiddle.net/rMrLe/
Maybe you could add
display:inline-block;
to the fieldset's css, f.ex. in you example:Does this achieve what you mean? Older browser should stick to
display:inline;
though.Edit: ok, with the new fiddle I see what you want to achieve. I guess you have to use javascript to adjust the height in this case - if it is suitable for you.
Here's an example:
Just add
id="myfieldset"
to your fieldset to make it work in your example.Link to this solution: http://jsfiddle.net/rMrLe/
您可以将其设为
内联块
:http://jsfiddle.net/XDMfN/ 49/或
内联
(对于 IE):http://jsfiddle.net/XDMfN/50/You can make it an
inline-block
: http://jsfiddle.net/XDMfN/49/Or
inline
(for IE): http://jsfiddle.net/XDMfN/50/这是不可能的,因为
position:relative
使元素正常渲染,然后移动,因此fieldset
“认为”元素处于正常渲染状态。动态解决此问题的唯一真正选择是使用 Javascript,如 jsFiddle 所示,它使用 jQuery更改
height
CSS 属性。This isn't possible because
position: relative
makes the element render normally, then shift, so thefieldset
"thinks" the element is in its normal-rendered state.The only real option to fix this dynamically is to use Javascript, as seen in this jsFiddle which uses jQuery to change the
height
css property.