简体   繁体   English

CSS 网格中的神秘差距

[英]Mystery gaps in CSS Grid

In the snippet below, there are gaps above and beneath ' main ' area which suppose to be an empty cells with the same height as the grid cells.在下面的代码片段中,“ main ”区域的上方和下方都有空隙,这些空隙假设是一个与网格单元高度相同的空单元。 Also there is an empty space at the very bottom.最底部也有一个空的空间。

I've tried setting grid-auto-flow to different values, but there is no effect except for the very bottom gap when it set to column .我尝试将grid-auto-flow设置为不同的值,但是当它设置为column时,除了最底部的间隙之外没有任何效果。

What causes that?是什么原因造成的? and how to fix it?以及如何解决?

 .header { grid-area: hd; }.footer { grid-area: ft; }.content { grid-area: main; }.sidebar { grid-area: sd; } * {box-sizing: border-box;}.wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; max-width: 940px; margin: 0 auto; }.wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; }.wrapper { display: grid; grid-auto-rows: minmax(auto, auto); grid-template-areas: "hd hd ft" "sd. ft" "sd main ft" "sd. ft"; }
 <div class="wrapper"> <div class="header">Header</div>     <div class="sidebar">Sidebar</div>     <div class="content">Content</div>     <div class="footer">Footer</div> </div>

Here's what's happening:这是正在发生的事情:

First, let's see the gaps (they don't appear in many cases):首先,让我们看看差距(在很多情况下它们不会出现):

在此处输入图像描述

 .wrapper { display: grid; grid-auto-rows: minmax(auto, auto); grid-template-areas: "hd hd ft" "sd. ft" "sd main ft" "sd. ft"; }.header { grid-area: hd; }.footer { grid-area: ft; }.content { grid-area: main; }.sidebar { grid-area: sd; } * { box-sizing: border-box; }.wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; max-width: 940px; margin: 0 auto; }.wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; }
 <div class="wrapper"> <div class="header">Header</div>     <div class="sidebar">Sidebar</div>     <div class="content">Content</div>     <div class="footer">Footer</div> </div>

What you're seeing is the rendering of &nbsp;你看到的是&nbsp;的渲染。 ( non-breaking space ) characters in the HTML code. HTML 代码中的(不间断空格)字符。

在此处输入图像描述

As white space characters, they're invisible, which makes them hard to detect.作为空白字符,它们是不可见的,这使得它们难以被发现。 But once you remove them, the layout works as expected.但是一旦删除它们,布局就会按预期工作。

在此处输入图像描述

 .wrapper { display: grid; grid-auto-rows: minmax(auto, auto); grid-template-areas: "hd hd ft" "sd. ft" "sd main ft" "sd. ft"; }.header { grid-area: hd; }.footer { grid-area: ft; }.content { grid-area: main; }.sidebar { grid-area: sd; } * { box-sizing: border-box; }.wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; max-width: 940px; margin: 0 auto; }.wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; }
 <div class="wrapper"> <div class="header">Header</div> <div class="sidebar">Sidebar</div> <div class="content">Content</div> <div class="footer">Footer</div> </div>

Lastly, why doesn't the faulty layout display in many cases?最后,为什么很多情况下不显示错误的布局?

When you copy HTML code as rendered on a web page (eg, copy the code directly from the question), the &nbsp;当您复制 web 页面上呈现的 HTML 代码时(例如,直接从问题中复制代码), &nbsp; characters, being HTML code, have already been rendered.字符,即 HTML 代码,已经被渲染。 So only plain (collapsible) white space gets copied and the layout will appear to be working fine.所以只有普通的(可折叠的)空白被复制,布局看起来工作正常。

Also, if you copy the HTML code from some code editors in some browsers (eg, the Stack Snippet editor on Edge), the &nbsp;此外,如果您从某些浏览器中的某些代码编辑器(例如,Edge 上的 Stack Snippet 编辑器)复制 HTML 代码, &nbsp; characters don't get copied, either.字符也不会被复制。 I needed to copy the code from the jsFiddle editor in Chrome to finally see the problem.我需要从 Chrome 中的 jsFiddle 编辑器中复制代码才能最终看到问题所在。

Also, if you hit the "Tidy" button in the editor using the original code, spaces will be added between the lines.此外,如果您使用原始代码在编辑器中点击“整理”按钮,则会在行之间添加空格。

**I tested the code on Chrome and Firefox and the result was ** **我在 Chrome 和 Firefox 上测试了代码,结果是 **

在此处输入图像描述

在此处输入图像描述

Always add the <!DOCTYPE> declaration to your HTML documents, so that the browser knows what type of document to expect.始终将<!DOCTYPE>声明添加到 HTML 文档中,以便浏览器知道预期的文档类型。

 .header { grid-area: hd; }.footer { grid-area: ft; }.content { grid-area: main; }.sidebar { grid-area: sd; } * {box-sizing: border-box;}.wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; max-width: 940px; margin: 0 auto; }.wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; }.wrapper { display: grid; grid-auto-rows: minmax(auto, auto); grid-template-areas: "hd hd ft" "sd. ft" "sd main ft" "sd. ft"; }
 <!DOCTYPE html> <html> <head> </head> <body> <div class="wrapper"> <div class="header">Header</div> <div class="sidebar">Sidebar</div> <div class="content">Content</div> <div class="footer">Footer</div> </div> </body> </html>

The empty cells have to be inserted, you'll not get it by default, just adding the HTML and body tag fixed the bottom gap issue:必须插入空单元格,默认情况下不会得到它,只需添加 HTML 和 body 标签即可修复底部间隙问题:

 .header { grid-area: hd; }.footer { grid-area: ft; }.content { grid-area: main; }.sidebar { grid-area: sd; }.empty-cell1 { grid-area: ec1; }.empty-cell2 { grid-area: ec2; } * {box-sizing: border-box;}.wrapper { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; max-width: 940px; margin: 0 auto; }.wrapper > div { border: 2px solid #ffa94d; border-radius: 5px; background-color: #ffd8a8; padding: 1em; color: #d9480f; }.wrapper { display: grid; grid-template-areas: "hd hd ft" "sd ec1 ft" "sd main ft" "sd ec2 ft"; }
 <!DOCTYPE html> <html> <head> </head> <body> <div class="wrapper"> <div class="header">Header</div> <div class="sidebar">Sidebar</div> <div class="content">Content</div> <div class="footer">Footer</div> <div class="empty-cell1"></div> <div class="empty-cell2"></div> </div> </body> </html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM