【问题标题】:grid layout with 100% height and margin in HTML 5 with an HTML 5 CSS reset在 HTML 5 中具有 100% 高度和边距的网格布局以及 HTML 5 CSS 重置
【发布时间】:2022-02-15 15:48:34
【问题描述】:

我正在尝试获取具有网格布局和边距的页面。

我有下面的代码。我正在使用 CSS 重置。

当我向body 添加边距时,它会向主体/页面添加一个滚动条。我不想要那个。我希望整个页面都有一个 5px 边距的网格。

/* 
html5doctor.com Reset Stylesheet
v1.6.1
Last Updated: 2010-09-17
Author: Richard Clark - http://richclarkdesign.com 
Twitter: @rich_clark
*/

html, body, div, span, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
abbr, address, cite, code,
del, dfn, em, img, ins, kbd, q, samp,
small, strong, sub, sup, var,
b, i,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section, summary,
time, mark, audio, video {
    margin:0;
    padding:0;
    border:0;
    outline:0;
    font-size:100%;
    vertical-align:baseline;
    background:transparent;
}

body {
    line-height:1;
}

article,aside,details,figcaption,figure,
footer,header,hgroup,menu,nav,section { 
    display:block;
}

nav ul {
    list-style:none;
}

blockquote, q {
    quotes:none;
}

blockquote:before, blockquote:after,
q:before, q:after {
    content:'';
    content:none;
}

a {
    margin:0;
    padding:0;
    font-size:100%;
    vertical-align:baseline;
    background:transparent;
}

/* change colours to suit your needs */
ins {
    background-color:#ff9;
    color:#000;
    text-decoration:none;
}

/* change colours to suit your needs */
mark {
    background-color:#ff9;
    color:#000; 
    font-style:italic;
    font-weight:bold;
}

del {
    text-decoration: line-through;
}

abbr[title], dfn[title] {
    border-bottom:1px dotted;
    cursor:help;
}

table {
    border-collapse:collapse;
    border-spacing:0;
}

/* change border colour to suit your needs */
hr {
    display:block;
    height:1px;
    border:0;   
    border-top:1px solid #cccccc;
    margin:1em 0;
    padding:0;
}

input, select {
    vertical-align:middle;
}

/* end CSS reset */


html,
body {
  height: 100%;
  background-color: black;
}

body
{
  margin: 5px;
}

.container {
  display: grid;
  grid-template-columns: 1fr min-content min-content;
  grid-template-rows: min-content 1fr;
  gap: 5px 5px;
  grid-auto-flow: row;
  grid-template-areas: "search-box search-options actions" "table-holder table-holder table-holder";
  height: 100%;
}

.table-holder {
  grid-area: table-holder;
  overflow: auto;
}

.search-box {
  grid-area: search-box;
}

.search-options {
  grid-area: search-options;
  padding: 5px 40px 5px 40px;
}

.actions {
  grid-area: actions;
  padding: 5px 40px 5px 40px;
}

div div {
  background-color: yellow;
  padding: 5px;
}
<div class="container">
  <div class="search-box">a</div>
  <div class="search-options">b</div>
  <div class="actions">c</div>
  <div class="table-holder">this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br /></div>
</div>

【问题讨论】:

  • 只为容器添加边距?
  • 为所有元素添加`box-sizing:border-box`
  • @Paulie_D 但它仍然有同样的问题。
  • @M.RMRF 这似乎不起作用。

标签: html css height margin


【解决方案1】:

bodymargin 是溢出的原因。 使用height: calc(100% - 10px) for body 可以消除溢出。

/* 
html5doctor.com Reset Stylesheet
v1.6.1
Last Updated: 2010-09-17
Author: Richard Clark - http://richclarkdesign.com 
Twitter: @rich_clark
*/

html, body, div, span, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
abbr, address, cite, code,
del, dfn, em, img, ins, kbd, q, samp,
small, strong, sub, sup, var,
b, i,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section, summary,
time, mark, audio, video {
    margin:0;
    padding:0;
    border:0;
    outline:0;
    font-size:100%;
    vertical-align:baseline;
    background:transparent;
}

body {
    line-height:1;
}

article,aside,details,figcaption,figure,
footer,header,hgroup,menu,nav,section { 
    display:block;
}

nav ul {
    list-style:none;
}

blockquote, q {
    quotes:none;
}

blockquote:before, blockquote:after,
q:before, q:after {
    content:'';
    content:none;
}

a {
    margin:0;
    padding:0;
    font-size:100%;
    vertical-align:baseline;
    background:transparent;
}

/* change colours to suit your needs */
ins {
    background-color:#ff9;
    color:#000;
    text-decoration:none;
}

/* change colours to suit your needs */
mark {
    background-color:#ff9;
    color:#000; 
    font-style:italic;
    font-weight:bold;
}

del {
    text-decoration: line-through;
}

abbr[title], dfn[title] {
    border-bottom:1px dotted;
    cursor:help;
}

table {
    border-collapse:collapse;
    border-spacing:0;
}

/* change border colour to suit your needs */
hr {
    display:block;
    height:1px;
    border:0;   
    border-top:1px solid #cccccc;
    margin:1em 0;
    padding:0;
}

input, select {
    vertical-align:middle;
}

/* end CSS reset */


html,
body {
  height: 100%;
  background-color: black;
}

body
{
  margin: 5px;
  height: calc(100% - 10px);
}

.container {
  display: grid;
  grid-template-columns: 1fr min-content min-content;
  grid-template-rows: min-content 1fr;
  gap: 5px 5px;
  grid-auto-flow: row;
  grid-template-areas: "search-box search-options actions" "table-holder table-holder table-holder";
  height: 100%;
}

.table-holder {
  grid-area: table-holder;
  overflow: auto;
}

.search-box {
  grid-area: search-box;
}

.search-options {
  grid-area: search-options;
  padding: 5px 40px 5px 40px;
}

.actions {
  grid-area: actions;
  padding: 5px 40px 5px 40px;
}

div div {
  background-color: yellow;
  padding: 5px;
}
<div class="container">
  <div class="search-box">a</div>
  <div class="search-options">b</div>
  <div class="actions">c</div>
  <div class="table-holder">this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br />this content may be small in height or be longer than the page<br /></div>
</div>

【讨论】:

    猜你喜欢
    • 2013-12-21
    • 2013-02-16
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 2010-12-09
    相关资源
    最近更新 更多