首页 > 其他分享 >body{height:100%}和html,body{height:100%}有什么区别?为什么html要设置height:100%呢,html不就是整个窗口吗?

body{height:100%}和html,body{height:100%}有什么区别?为什么html要设置height:100%呢,html不就是整个窗口吗?

时间:2024-12-02 09:43:34浏览次数:4  
标签:body 100% element html height viewport

body{height:100%} and html, body {height:100%} have a crucial difference related to how the browser calculates the height of the body element. The html element represents the root of the document, while the body element contains the visible content.

Here's the breakdown:

  • body { height: 100% }: This sets the body's height to 100% of its parent's height. If the parent (html) doesn't have an explicitly defined height, the body will not take up the full viewport height. It might only be as tall as its content. This is because the html element, by default, doesn't necessarily have a height of 100% of the viewport.

  • html, body { height: 100% }: This sets both the html and body elements to 100% height. Crucially, setting the html element's height to 100% makes it fill the entire viewport height. Then, the body, having its height set to 100% of its parent (html), will also fill the entire viewport height. This is the key to making the body occupy the full browser window.

To your question, "Isn't html the entire window already?", the answer is subtly no. The html element contains the entire window's content, but its height isn't automatically the viewport height unless explicitly set. It adjusts to the content within it unless told otherwise.

Why is this important?

This is crucial for layouts where you want elements within the body to have percentage-based heights. If the body doesn't have a defined height relative to the viewport, percentage heights for child elements won't work as expected. They'll be calculated relative to the body's height, which might be smaller than the viewport.

In summary:

To ensure the body element takes up the full viewport height, you must set both the html and body elements' height to 100%. Setting only the body's height to 100% is insufficient if the html element's height isn't also explicitly set.

标签:body,100%,element,html,height,viewport
From: https://www.cnblogs.com/ai888/p/18581025

相关文章

  • 写html代码时,如果缺少了结束标签,你如何检测及如何预防?
    在HTML开发中,缺少结束标签会导致各种问题,从样式错乱到JavaScript功能失效,甚至页面崩溃。以下是如何检测和预防HTML缺少结束标签的方法:检测方法:浏览器开发者工具:现代浏览器都内置了开发者工具(通常按F12键打开)。控制台(Console)会报告HTML解析错误,并指出缺少结束标......
  • 光标自动定位到起始位置 contenteditable="true" ,v-html绑定内容,div可编辑时,光标移到
    出现这个问题原因:(1)通过打断点可以看到,当你输入的时候触发input事件,提交值给父组件中的v-model;(2)但因为在子组件中又监听了v-model的值,所以整体形成了闭环;(3)还需要重点说明的是光标问题,contenteditable与v-html所在的元素值的改变如果不是通过输入而是通过赋值实现,光标就会跑到最......
  • 【洛谷】P1002 [NOIP2002 普及组] 过河卒
    #include<iostream>usingnamespacestd;constints1[]={0,-2,-1,1,2,2,1,-1,-2};constints2[]={0,1,2,2,1,-1,-2,-2,-1}; //马可以走到的位置,上下对应longlongf[80][80],s[80][80];intmain(){ longlongi,b1,b2,m1,m2; cin>>b1>......
  • HTML-css样式
    HTML样式-csscss样式的使用方法:内部样式:在HTML元素中使用“style”属性内部样式表:在HTML文档头部中<head>区域中使用<style>元素来包含css外部引用:使用外部css文件,使用link元素包含最好使用外部引用css文件内部样式:<pstyle="color:blue;margin-left:20px;">这是一个段落......
  • hot100-一刷-02双指针(共4道题)
    283.移动零题目链接题目描述代码实现分析:快慢指针,快指针指向不为0的数,慢指针指向左边当前已经处理好的序列的尾部代码:classSolution{publicvoidmoveZeroes(int[]nums){intslow=0;intfast=0;intn=nums.length;wh......
  • 了解HTML
    今天来了解一下htmlHTML(HyperTextMarkupLanguage)即超文本标记语言,是用于创建网页内容和结构的标准标记语言。它主要通过一系列的标签来描述网页的元素,如标题、段落、链接、图像等。以下是一些常见的HTML标签:<html>:表示HTML文档的根标签。<head>:包含文档的元数据,如标......
  • Html Form 验证异步的提交
    一、需求需要在Form的submit按钮点击或输入框回车的时候计算一个耗时的验证。 二、解决方案在Form的onsubmit事件中开启验证,并返回false阻止提交;在验证过程中,根据验证结果决定是否提交。 三、代码<formaction="/Index/login"method="post"onsubmit="returnSubmit(......
  • 【LTspice学习笔记】:三线制 Pt100 测温电路
         本文是一个大三自动化专业本科生就恒流源三线制Pt100测温电路相关仿真设计的实验学习与记录过程,并对 Pt100电阻的一些原理特性以及电位计的导入做简单介绍。一、PT100介绍        PT100电阻是指在0°C时,PT100温度传感器的电阻值为100欧姆,随着温度......
  • [HTML] HTMLCollection vs NodeList
    HTMLCollectionThe HTMLCollection interfacerepresentsagenericcollection(array-likeobjectsimilarto arguments)ofelements(indocumentorder)andoffersmethodsandpropertiesforselectingfromthelist.An HTMLCollection intheHTMLDOMislive......
  • hot100-一刷-01哈希(共3道题)
    1.两数之和题目链接题目描述代码实现分析:暴力的话就是两个for循环依次寻找相加为target的两个数。用一个map记录已经遍历过的数,其中key就用这个数的字面值,value就存它的下标。判断是否相加为taget的时候,只需要看map中是否有target-nums[i]就可以,说明当前的nums[i]和之前......