修复' html'的大小和身体'元素正是屏幕的元素
我通常需要HTML
和body
元素具有屏幕大小。通常,在我想拥有svg
元素的情况下,适合整个屏幕。
为了实现这一目标,我发现可以使用CSS代码如下。
html,body{margin:0;padding:0;height:100%;}
我人体使用的代码是以下。
html {
height: 100%;
display: flex;
}
body {
flex-grow: 1;
display: flex;
flex-direction: column;
}
两者似乎都很好,但是我最近有以下评论。
html {高度:100%;显示:Flex; }
是无用的声明。 HTML高度将始终计算为拟合内容。另外,100%
表示父母的100%。html
没有父母...还将Flexbox应用于html
是没有用的,因为它只有1个可见的子元素:body> body
。 P>
实际上:
- 我将
的100%
HTML
高度放置,以使其适合屏幕高度。 - 我将Flexbox应用于
html
,以便能够在其子女上使用flex-glow:1
,并让这个孩子填写其父母。
有什么比我的更好的解决方案了吗?
I often need that html
and body
elements have the size of the screen. Typically, in the case when I want to have a svg
element fit the whole screen.
To achieve that, I saw that it was possible to use CSS code as follow.
html,body{margin:0;padding:0;height:100%;}
The code that I personnaly use is the following one.
html {
height: 100%;
display: flex;
}
body {
flex-grow: 1;
display: flex;
flex-direction: column;
}
Both seem to work well, but I recently had the following remark.
html { height: 100%; display: flex; }
is a useless declaration. html height will always be calculated to fit content. Also100%
means 100% of the parent.html
has no parent... also applying flexbox tohtml
is useless as it only has 1 child element that is visible:body
.
Actually:
- I put
100%
ofhtml
height in order to have it fit the screen height. - I apply flexbox to
html
in order to be able to useflex-glow: 1
on its child, and have this child filling its parent.
Is there any better to solution than mine?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我个人使用此方法:
这将使您的身体全高,默认情况下,也将尊重默认利润率
您可以轻松地使用
高度:内部元素上的100%
,而没有问题:I personally use this:
This will make your body full height by default and will also respect default margin
And you can easily use
height:100%
on an inner element without issue:我发现,每当使用需要成为屏幕全高
高度的元素时:100VH
通常是一个不错的起点。 VH =视口高。我将其使用在高度上:100%取决于布局100%并不总是等于页面高度,因此,使用VH,您可以确切知道自己得到的!使用VH,您还可以在CSS中使用Calc(),因此,如果您需要身体来填充页面的整个高度,但是减去标头的高度,例如,您可以做这样的事情:
I find that whenever working with elements that need to be the full height of the screen
height: 100vh
is usually a good place to start. VH = viewport height. I use it over height: 100% as depending on the layout 100% doesn't always equal the page height, so with VH you know exactly what you are getting!With VH you can also then use a calc() in your CSS, so if you needed your body to fill the whole height of the page, but subtract the height of a header for example you could do something like this: