如何移除HTML网站页面中的多余文本框

问题原因
你看到的文本外的框,是CSS写法错误导致的:
- 你给
html和body同时设置了display: flex,默认flex布局为横向排列,此时body作为html的直接子元素(flex项目),不会默认占满视口全宽,会根据内部文本宽度自动收缩,你设置的页面背景会跟着收缩的body边界显示,视觉上就像内容套了个框。 - 你设置的
background-size: 100%只定义了背景宽度,高度会按原图比例自适应,没法铺满整个视口,进一步强化了“框”的视觉效果。
另外你的代码还有几个不影响核心问题但不规范的地方:
- 重复定义了两个
<title>标签,后面的标签会覆盖前面的配置,只需要保留一个即可 - 同个选择器下写了两次
font-family,后面的'Trispace', sans-serif会完全覆盖前面的手写体配置,要给不同元素配不同字体需要分开写选择器 - 写了
textarea的相关样式,但页面里根本没有textarea元素,属于无效代码可以删除
修复方案
直接替换成下面的代码即可,核心修改点:
- 移除
html的flex布局触发,只给body设置flex实现内容居中,避免body宽度随内容收缩 - 给
body加min-height: 100vh,让body始终占满整个浏览器视口高度,背景铺满全页 - 把
background-size改成cover,让背景图自适应铺满视口,不会出现留白 - 加了基础的全局样式重置,清除浏览器默认的内外边距,避免边缘出现多余空白
- 修正了重复title、字体配置冲突的问题
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>live_game</title> <link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <link href="https://fonts.googleapis.com/css2?family=The+Nautigal:wght@700&family=Trispace:wght@200&display=swap" rel="stylesheet"> <style> * { margin: 0; padding: 0; box-sizing: border-box; } html, body { min-height: 100vh; background-color: #288BA8; color: white; font-size: 30px; font-family: 'Trispace', sans-serif; display: flex; justify-content: center; align-items: center; background-image: url("https://images.pexels.com/photos/417173/pexels-photo-417173.jpeg?cs=srgb&dl=pexels-pixabay-417173.jpg&fm=jpg"); background-size: cover; background-position: center; } h1 { font-family: 'The Nautigal', cursive; } </style> </head> <body> <h1>My first website</h1> </body> </html>
如果不需要文本垂直居中,把align-items: center;删掉就行。
内容的提问来源于stack exchange,提问作者live_game3
相关产品推荐
相关产品推荐

