简体   繁体   English

CSS在最小高度容器内垂直对齐?

[英]CSS vertical align inside a min-height container?

I've been doing a lot of searching and can't find a css solution to vertically align my content.我一直在进行大量搜索,但找不到垂直对齐我的内容的 css 解决方案。

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.我可以做的唯一后备是一些 javascript 以编程方式测量内部容器并设置其边距顶部。 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.有关我想要设置样式的标记的示例,请参阅此 js fiddle

I am happy to forget IE6 for this, but need to support IE7+ and the latest webkit+firefox...我很高兴为此忘记了 IE6,但需要支持 IE7+ 和最新的 webkit+firefox...

Is anyone aware of a CSS only solution to this?有没有人知道只有 CSS 才能解决这个问题?

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.据我所知,除非您可以使用表格,否则绝对无法仅使用 CSS 在 IE7 中完成此操作。 For all decent browsers, and IE8, you can use display:table-cell:对于所有不错的浏览器和 IE8,您可以使用 display:table-cell:

http://jsfiddle.net/hGjfY/ 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)将高度设置为 200px 而不是 min-height(表格单元格不支持 min-height,高度被解释为 min-height)

  • add conditional comment to target only IE7, and add min-height:200px;添加条件注释以仅针对 IE7,并添加 min-height:200px; and height:auto;和高度:自动; to the inner div (height doesn't work as expected in IE7 on table cells)到内部 div(高度在表格单元格上的 IE7 中无法按预期工作)

  • load jQuery fix only for IE7, or live with a non-centered view in IE7只为 IE7 加载 jQuery 修复,或者在 IE7 中使用非居中视图

Here is a CSS-only approach using the HTML your provided on jsfiddle.这是使用您在 jsfiddle 上提供的 HTML 的纯 CSS 方法。 It does require two wrapper DIVS around your content, but you already had that in your HTML.它确实需要在您的内容周围使用两个包装器 DIVS,但是您的 HTML 中已经有了它。 This solution does not use tables or table cells, but DIVS that act like tables and table cells using CSS.此解决方案不使用表格或表格单元格,而是使用使用 CSS 的 DIVS 就像表格和表格单元格一样。

Here is your jsfiddle modified and working: http://jsfiddle.net/8Mjc3/2/这是你的 jsfiddle 修改和工作: http : //jsfiddle.net/8Mjc3/2/

HTML HTML

<div class="vertical-center-container">
  <div class="vertical-center-table-cell">
    <!-- INSERT CONTENT TO BE VERTICALLY ALIGNED -->
  </div>
</div>

CSS CSS

.vertical-center-container {
    display: table;
    width:100%;
}
.vertical-center-table-cell {
    display: table-cell;
    vertical-align: middle;
}

This is a modified solution using what I learned about vertical alignment using the Table Cell Method from a Smashing Magazine article .这是一个修改的解决方案,使用我从Smashing Magazine 文章中使用 Table Cell Method 学到的关于垂直对齐的知识。

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 @ptriek 的答案很好,但如果您还想获得中心水平对齐,则需要一个额外的包装器并将其设置为

.outer{ 
    display: table; 
    width: 100%
}
.inner{
    diplay: table-cell;
    vertical-align: middle;
    height: 200px;
    text-align:center;
}

This is my first answer, but hopefully it helps!这是我的第一个答案,但希望它有所帮助!

* {
  font-size: 9px;
}

#container{
    border: 3px solid black;
    text-align:center;
    min-height: 200px;
}

.valign_outer{
}

.valign_inner{
  padding-top: calc( (200px - 9px) * 1.2) ;
  padding-bottom: calc( (200px - 9px) * 1.2);
}

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 )计算为: calc((min-width - font-size) * padding-multiplier )

Make sure the padding multiplier is at least 0.5 on both.确保两者的填充乘数至少为 0.5。

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.当您设置固定高度或 vh 高度时,这很棘手:要么始终调整字体大小以不溢出高度,要么在较小的宽度上,您会得到换行符并且文本可能会溢出高度。

And font-resize is not always best: we may want some CSS locks: see https://fvsch.com/css-locks/而且字体调整并不总是最好的:我们可能需要一些 CSS 锁:参见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所以,文本将占据一定的大小,为了让它居中,我们需要相同的 padding-top 和 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.例如,如果字体为 20px,则将 padding 设置为 20px,但如果字体为 40px,则将 padding 设置为 40px。

Also, we have to account for remaining space.此外,我们必须考虑剩余空间。 If min-height is 200, the text is at least font-size high.如果 min-height 为 200,则文本至少是 font-size 高。 It may be more, if there are line breaks, but if font-size is 16px, at least the text occupies 16px vertically.可能会更多,如果有换行符,但是如果font-size是16px,至少文字垂直占据16px。

So out of 200, we have 200 - 16 = 184 pixels available at most, to divide into our padding-top and padding-bottom.所以在 200 个中,我们最多有 200 - 16 = 184 个像素可用,以分为我们的 padding-top 和 padding-bottom。

You have to make sure the padding is at least ((min-width - font-size) / 2) to account for this scenario!您必须确保填充至少为 ((min-width - font-size) / 2) 以解决这种情况!

Hope it helps!希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM