CSS 在最小高度容器内垂直对齐?
我做了很多搜索,但找不到 css 解决方案来垂直对齐我的内容。
我在这里看到的所有解决方案和谷歌上的热门文章都要求我有一个内容不能超过的固定高度容器。我不能保证,因为内容是动态生成的。
我唯一可以做的工作是使用一些 javascript 以编程方式测量内部容器并设置其边距顶部。这确实很恶心,但似乎是唯一有效的方法。 :(
请参阅 this js fiddle 了解我想要设置样式的标记的示例。
我很高兴忘记 IE6为此,但需要支持 IE7+ 和最新的 webkit+firefox...
有人知道仅 CSS 的解决方案吗?
I've been doing a lot of searching and can't find a css solution to vertically align my content.
All of the solutions I see here on SO and the top articles on google require that I have a fixed height container which the content cannot exceed. I cannot guarantee that as the content is dynamically generated.
The only fallback I have that I can make work is some javascript to programmatically measure the inner container and set its margin-top. This is truly icky but the only way that seems to work. :(
See this js fiddle for an example of the markup I want to style.
I am happy to forget IE6 for this, but need to support IE7+ and the latest webkit+firefox...
Is anyone aware of a CSS only solution to this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
据我所知,绝对没有办法仅使用 CSS 在 IE7 中完成此操作,除非您可以使用表格。对于所有合适的浏览器和 IE8,您可以使用 display:table-cell:
http://jsfiddle.net/hGjfY/
和
添加条件注释以仅针对 IE7,并添加 min-height:200px;和高度:自动;到内部 div(表格单元格上的高度在 IE7 中无法按预期工作)
仅针对 IE7 加载 jQuery 修复,或在 IE7 中使用非居中视图
As far as I know there is absolutely no way to get this done in IE7 with just CSS, unless you can live with a table. For all decent browsers, and IE8, you can use display:table-cell:
http://jsfiddle.net/hGjfY/
and
set height to 200px instead of min-height (min-height is not supported on table cells, height is interpreted as min-height)
add conditional comment to target only IE7, and add min-height:200px; and height:auto; to the inner div (height doesn't work as expected in IE7 on table cells)
load jQuery fix only for IE7, or live with a non-centered view in IE7
这是使用您在 jsfiddle 上提供的 HTML 的纯 CSS 方法。它确实需要两个围绕您的内容的包装器 DIVS,但您已经在 HTML 中拥有了它。此解决方案不使用表格或表格单元格,而是使用类似于使用 CSS 的表格和表格单元格的 DIVS。
这是您的 jsfiddle 修改后并正在运行: http://jsfiddle.net/8Mjc3/2/
HTML
CSS
这是一个修改后的解决方案,使用了我从 Smashing 杂志文章。
Here is a CSS-only approach using the HTML your provided on jsfiddle. It does require two wrapper DIVS around your content, but you already had that in your HTML. This solution does not use tables or table cells, but DIVS that act like tables and table cells using CSS.
Here is your jsfiddle modified and working: http://jsfiddle.net/8Mjc3/2/
HTML
CSS
This is a modified solution using what I learned about vertical alignment using the Table Cell Method from a Smashing Magazine article.
@ptriek 答案很好,但如果您还想获得中心水平对齐,那么您需要一个额外的包装器并将其设置为
The @ptriek answer is good but if you also want to get the center horizontal alignment than you need to have an extra wrapper and set it as
这是我的第一个答案,但希望对您有所帮助!
您可以设置任何字体大小。
您可以设置任何最小高度。
您可以选择最适合您的填充倍数。
您可以针对不同的媒体查询重做此计算。
计算如下:calc( (min-width - font-size) * padding-multiplier )
确保两者的填充乘数至少为 0.5。
这是逻辑:
当您设置固定高度或 vh 高度时,这是很棘手的:要么字体大小总是调整大小以不溢出高度,要么在较小的宽度上,您会得到换行符并且文本可能会溢出高度。
并且 font-resize 并不总是最好的:我们可能需要一些 CSS 锁:请参阅 https://fvsch.com/ css-locks/
所以,我们无法确定容器的高度。但是,我们可以定义最小高度。
因此,文本将占据一定的大小,为了使其居中,我们需要相同的 padding-top 和 padding-bottom
现在,可以根据字体大小更好地定义填充。例如,如果字体为 20px,则可以将内边距设置为 20px,但如果字体为 40px,则将内边距设置为 40px。
此外,我们还必须考虑剩余空间。如果 min-height 为 200,则文本至少为 font-size 高。如果有换行,可能会更多,但如果 font-size 是 16px,则文本至少在垂直方向上占据 16px。
因此,在 200 个像素中,我们最多有 200 - 16 = 184 个可用像素,可分为 padding-top 和 padding-bottom。
您必须确保填充至少为 ((min-width - font-size) / 2) 才能解决这种情况!
希望有帮助!
This is my first answer, but hopefully it helps!
You can set any font-size.
You can set any min-height.
You can choose the padding-multiplier that best suits you.
You may redo this calculations for different Media Queries.
The calculation is: calc( (min-width - font-size) * padding-multiplier )
Make sure the padding multiplier is at least 0.5 on both.
This is the logic:
When you set a fixed height or a vh height, it is tricky: either the font-size always resizes not to overflow the height, or on smaller widths, you would get line-breaks and text may overflow the height.
And font-resize is not always best: we may want some CSS locks: see https://fvsch.com/css-locks/
So, we cannot determine the height of the container. But, we can define the min-height.
So, the text is going to occupy some size, and to center it, we want the same padding-top and padding-bottom
Now, that padding is better defined based on the font-size. If font is 20px, make the padding 20px maybe, but if font is 40px, make padding 40px, for example.
Also, we have to account for remaining space. If min-height is 200, the text is at least font-size high. It may be more, if there are line breaks, but if font-size is 16px, at least the text occupies 16px vertically.
So out of 200, we have 200 - 16 = 184 pixels available at most, to divide into our padding-top and padding-bottom.
You have to make sure the padding is at least ((min-width - font-size) / 2) to account for this scenario!
Hope it helps!