修复' html'的大小和身体'元素正是屏幕的元素

发布于 2025-01-28 03:55:43 字数 917 浏览 2 评论 0原文

我通常需要HTMLbody元素具有屏幕大小。通常,在我想拥有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. Also 100% means 100% of the parent. html has no parent... also applying flexbox to html is useless as it only has 1 child element that is visible: body.

Actually:

  • I put 100% of html height in order to have it fit the screen height.
  • I apply flexbox to html in order to be able to use flex-glow: 1 on its child, and have this child filling its parent.

Is there any better to solution than mine?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

农村范ル 2025-02-04 03:55:43

我个人使用此方法:

html {
  display: grid;
  min-height: 100%;
}

这将使您的身体全高,默认情况下,也将尊重默认利润率

html {
  display: grid;
  min-height: 100%;
  background: blue;
}

body {
  background: red;
}

您可以轻松地使用高度:内部元素上的100%,而没有问题:

html {
  display: grid;
  min-height: 100%;
  background: blue;
}

body {
  background: red;
}

.box {
  height: 100%;
  border: 5px solid green;
  box-sizing: border-box;
}
<div class="box"></div>

I personally use this:

html {
  display: grid;
  min-height: 100%;
}

This will make your body full height by default and will also respect default margin

html {
  display: grid;
  min-height: 100%;
  background: blue;
}

body {
  background: red;
}

And you can easily use height:100% on an inner element without issue:

html {
  display: grid;
  min-height: 100%;
  background: blue;
}

body {
  background: red;
}

.box {
  height: 100%;
  border: 5px solid green;
  box-sizing: border-box;
}
<div class="box"></div>

五里雾 2025-02-04 03:55:43

我发现,每当使用需要成为屏幕全高高度的元素时:100VH通常是一个不错的起点。 VH =视口高。我将其使用在高度上:100%取决于布局100%并不总是等于页面高度,因此,使用VH,您可以确切知道自己得到的!

使用VH,您还可以在CSS中使用Calc(),因此,如果您需要身体来填充页面的整个高度,但是减去标头的高度,例如,您可以做这样的事情:

<header style="height: 64px">
<section style="height: calc(100vh - 64px)"

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:

<header style="height: 64px">
<section style="height: calc(100vh - 64px)"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文