是否有必要(或建议)将CSS样式添加到HTML元素?(Is it necessary (or advisable) to add CSS styling to the HTML element?)

编程入门 行业动态 更新时间:2024-10-09 12:31:25
是否有必要(或建议)将CSS样式添加到HTML元素?(Is it necessary (or advisable) to add CSS styling to the HTML element?)

在查看其他人的代码(或许多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

更多推荐

本文发布于:2023-08-07 10:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464311.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有必要   或建议   样式   元素   CSS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!