在查看其他人的代码(或许多CSS重置)时,我看到使用基本样式(如height: 100% )解决的html元素,有时我会完全忽略它。 在我的实验中没有区别,但我不确定我是否遗漏了一些东西。
在这篇文章中,他们给出了一个例子
html,body{ min-height: 101%; }保持滚动条可见(但没有其他确定的答案)。 除了像这样的黑客,是否有任何具体的理由来设置html元素的样式?
When looking at other people's code (or many CSS resets), I see the html element addressed with basic styling (like height: 100%) and sometimes I see it ignored completely. In my experimentation there is no difference, but I am not sure if I am missing something.
In this post they give the example of
html,body{ min-height: 101%; }to keep scrollbars visible (but no other definitive answer). Other than a hack like this, is there any specific reason to style the html element?
最满意答案
我能想到的主要原因是,为了以%为单位指定height ,父元素需要明确设置高度。
假设你有一个容器<div> ,你需要100%高度和响应 。 只需应用height:100%将无效,除非您为其父<body>指定高度。
因此我们将为<body>应用height:100% - 现在,这不起作用,因为<body>的父级没有明确设置高度 - 这是我们的<html>元素。
因此我们申请
html{ height:100%; }...!
如果您的设计没有响应,即如果您以像素为单位设置固定尺寸,则不需要这样做
Well the major reason i can think of is that, for specifying height in % the elements parent needs to have a height set explicitly.
Assume you've a container <div> which you need to be of 100% height and responsive. simply applying height:100% won't work unless you specify a height for it's parent <body>.
Hence we'll apply height:100% for the <body> - Now, this won't work since <body>'s parent doesn't have a height set explicitly - which is our <html> element.
Hence we apply
html{ height:100%; }...!
This is not required if your design is not responsive , i.e if you're setting fixed dimensions in pixels
更多推荐
发布评论