簡體   English   中英

溢出時如何將整個單元格隱藏在 css 的網格中

[英]How do I hide a whole cell in a grid with css when overflowing

我正在嘗試制作一個占據整個視口的網格。 它有長寬比為 1 的正方形單元格。正方形的寬度隨着屏幕的寬度按比例增加,從而改變正方形的高度。 當它們的一部分超出屏幕時,我想完全隱藏底部溢出的單元格。 目前 overflow 屬性只隱藏單元格中超出視口的部分

問題的圖像

<body>
  <div id="projects">
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
    <div><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
  </div>
</body>
</html>
:root {
  --light-color: #e8e6e3;
  --grid-div-min-width: 200px;
}

body {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  margin: 0;
  background: #282c2d;
  min-height: 100vh;
}

#projects {
  color: white;
  text-align: center;

  display: grid;
  width: 100vw;
  max-height: 100vh;
  gap: .2rem;
  grid-template-columns: repeat(auto-fit, minmax(var(--grid-div-min-width), 1fr));
  overflow: hidden;
}

#projects>div {
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: center;
  width: 100%;
  aspect-ratio: 1;
  /*overflow: hidden;*/
}

#projects>div>img {
  position: absolute;
  z-index: -1;
  width: 100%;
  transition: 0.4s;
}

我嘗試添加“隱藏到子元素的溢出”,但它似乎擺脫了縱橫比

說明前面句子結果的圖像

我嘗試像對列所做的那樣手動設置行數,但單元格的內容不適合行

說明前面句子結果的圖像

實現具有占據整個視口的方形單元格的網格的一種方法是使用display: grid並將grid-template-rowsgrid-template-columns屬性設置為1fr ,這意味着每一行和每一列將占據一個等量的空間。 您還可以設置grid-gap屬性以在單元格之間添加一些空間。

.grid {
  display: grid;
  grid-template-rows: repeat(auto-fill, 1fr);
  grid-template-columns: repeat(auto-fill, 1fr);
  grid-gap: 10px;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.cell {
  /* Add styles for the cells here */
}

下一個,

<div class="grid">
  <div class="cell"><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
  <div class="cell"><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
  <div class="cell"><img src="https://baptiste-crepin.fr/repo-bg/Placeholder.png"></div>
  <!-- Add more cells here -->
</div>

這將創建一個網格,其中包含方形單元格並占據整個視口,任何溢出的單元格都將被隱藏。

使用 css 網格,我如何得到 SHORT<div> 文本在較早的空間上換行而不是在移動設備上稍微溢出?</div><div id="text_translate"><p> 我的 (iPhone) 手機上出現以下問題。 我的文本(用空格分隔的相對較短的單詞(不是 &amp;nbsps))在換行之前略微溢出。 我希望它包裹起來而不會溢出。 換句話說,我希望它在換行到下一行之前選擇一個更早的空間來終止一行(換行符)。</p><p> 我正在使用 CSS 網格來執行此操作。</p><p> 我很抱歉 CSS 有點冗長。 在我絕望中,我使用了各種我不完全理解的重置。 我的代碼在這里: <a href="https://codepen.io/Photog/pen/rNGEbKL" rel="nofollow noreferrer">https://codepen.io/Photog/pen/rNGEbKL</a></p><p> CSS</p><pre> * { font-size: 100%; } /* reset / initialize */ *, *:before, *:after { padding: 0; margin: 0; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } /* Define variables for colors */:root { --bgnormal: black; --fgnormal: white; } /* ****************************************** */ /* Eric Meyer's reset.css: http://meyerweb.com/eric/tools/css/reset/reset.css */ /* http://meyerweb.com/eric/tools/css/reset/ v2.0 | 20110126 License: none (public domain) */ /* alphabetized */ a, abbr, acronym, address, applet, article, aside, audio, b, big, blockquote, body, canvas, caption, center, cite, code, dd, del, details, dfn, div, dl, dt, em, embed, fieldset, figcaption, figure, footer, form, h1, h2, h3, h4, h5, h6, header, hgroup, html, i, iframe, img, ins, kbd, label, legend, li, mark, menu, nav, object, ol, output, p, pre, q, ruby, s, samp, section, small, span, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, time, tr, tt, u, ul, var, video { margin: 0; padding: 0; border: 0; font-size: 100%; font: inherit; vertical-align: baseline; } /* HTML5 display-role reset for older browsers */ article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section { display: block; } body { line-height: 1; } ol, ul { list-style: none; } blockquote, q { quotes: none; } blockquote:before, blockquote:after, q:before, q:after { content: ''; content: none; } table { border-collapse: collapse; border-spacing: 0; } /* ******************************************** */ body { font-size: calc(0.75em + 1vmin); background-color: var(--bgnormal); } article,aside,div,figcaption,figure,footer,form,h1,h2,h3,h4,h5,h6,header,hr,li,main,nav,p,section,ul { font-family: Helvetica, Verdana, "Lucida Sans", Tahoma, Arial, "Century Gothic", sans-serif; font-weight: normal; font-size: 1em; font-size: 1rem; line-height: 140%; color: var(--fgnormal); background-color: var(--bgnormal); } /* ********************* */.grid-container { display: inline-grid; grid-template-columns: 20% 20% 10% 10% 20% 20%; grid-template-rows: auto; align-items: start; grid-gap: 1em; padding-left: 1em; padding-right: 3em; white-space: normal; width: 96vw; /* 96% of viewport */ }.grid-item-1 { grid-row: 1; grid-column: 1 / span 6; }.grid-item-2 { grid-row: 2; grid-column: 1 / span 6; }.grid-item-3 { grid-row: 3; grid-column: 1 / span 6; }.grid-item-4 { grid-row: 4; grid-column: 1 / span 6; } [class^='grid-item'] { display: grid; align-items: center; justify-content: center; font-size: 1em; color: var(--fgnormal); white-space: normal; }</pre><p> HTML</p><pre> &lt;div class="grid-container"&gt; &lt;div class="grid-item-2"&gt; If&amp;nbsp;you're interested in purchasing, displaying or discussing my photographs, please contact me. I&amp;nbsp;usually respond within three days. I'd&amp;nbsp;be delighted to hear from you and know what you think. &lt;br&gt;&lt;br&gt; I am available for critique sessions, workshops and presentations where I share my experience, creative process and techniques as a photographer and curator. &lt;/div&gt; &lt;/div&gt; &lt;!-- grid-container --&gt;</pre></div>

[英]Using css grid, how do I get SHORT<div> text to wrap on an earlier space instead of slightly overflowing on mobile?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 如何阻止此 CSS 橫幅在復制時溢出? CSS 網格項目單元格擴展而不是溢出 如何在 CSS 中隱藏溢出的內容? 如何使表格單元格中的div溢出的CSS重要? 如何在空或使用 display:none 時使 CSS 網格單元折疊? 如何在 css 網格單元中獲取滾動列表 如何阻止我的最后一個網格項目溢出? 如何防止DIV溢出表單元格? 使用 css 網格,我如何得到 SHORT<div> 文本在較早的空間上換行而不是在移動設備上稍微溢出?</div><div id="text_translate"><p> 我的 (iPhone) 手機上出現以下問題。 我的文本(用空格分隔的相對較短的單詞(不是 &amp;nbsps))在換行之前略微溢出。 我希望它包裹起來而不會溢出。 換句話說,我希望它在換行到下一行之前選擇一個更早的空間來終止一行(換行符)。</p><p> 我正在使用 CSS 網格來執行此操作。</p><p> 我很抱歉 CSS 有點冗長。 在我絕望中,我使用了各種我不完全理解的重置。 我的代碼在這里: <a href="https://codepen.io/Photog/pen/rNGEbKL" rel="nofollow noreferrer">https://codepen.io/Photog/pen/rNGEbKL</a></p><p> CSS</p><pre> * { font-size: 100%; } /* reset / initialize */ *, *:before, *:after { padding: 0; margin: 0; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } /* Define variables for colors */:root { --bgnormal: black; --fgnormal: white; } /* ****************************************** */ /* Eric Meyer's reset.css: http://meyerweb.com/eric/tools/css/reset/reset.css */ /* http://meyerweb.com/eric/tools/css/reset/ v2.0 | 20110126 License: none (public domain) */ /* alphabetized */ a, abbr, acronym, address, applet, article, aside, audio, b, big, blockquote, body, canvas, caption, center, cite, code, dd, del, details, dfn, div, dl, dt, em, embed, fieldset, figcaption, figure, footer, form, h1, h2, h3, h4, h5, h6, header, hgroup, html, i, iframe, img, ins, kbd, label, legend, li, mark, menu, nav, object, ol, output, p, pre, q, ruby, s, samp, section, small, span, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, time, tr, tt, u, ul, var, video { margin: 0; padding: 0; border: 0; font-size: 100%; font: inherit; vertical-align: baseline; } /* HTML5 display-role reset for older browsers */ article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section { display: block; } body { line-height: 1; } ol, ul { list-style: none; } blockquote, q { quotes: none; } blockquote:before, blockquote:after, q:before, q:after { content: ''; content: none; } table { border-collapse: collapse; border-spacing: 0; } /* ******************************************** */ body { font-size: calc(0.75em + 1vmin); background-color: var(--bgnormal); } article,aside,div,figcaption,figure,footer,form,h1,h2,h3,h4,h5,h6,header,hr,li,main,nav,p,section,ul { font-family: Helvetica, Verdana, "Lucida Sans", Tahoma, Arial, "Century Gothic", sans-serif; font-weight: normal; font-size: 1em; font-size: 1rem; line-height: 140%; color: var(--fgnormal); background-color: var(--bgnormal); } /* ********************* */.grid-container { display: inline-grid; grid-template-columns: 20% 20% 10% 10% 20% 20%; grid-template-rows: auto; align-items: start; grid-gap: 1em; padding-left: 1em; padding-right: 3em; white-space: normal; width: 96vw; /* 96% of viewport */ }.grid-item-1 { grid-row: 1; grid-column: 1 / span 6; }.grid-item-2 { grid-row: 2; grid-column: 1 / span 6; }.grid-item-3 { grid-row: 3; grid-column: 1 / span 6; }.grid-item-4 { grid-row: 4; grid-column: 1 / span 6; } [class^='grid-item'] { display: grid; align-items: center; justify-content: center; font-size: 1em; color: var(--fgnormal); white-space: normal; }</pre><p> HTML</p><pre> &lt;div class="grid-container"&gt; &lt;div class="grid-item-2"&gt; If&amp;nbsp;you're interested in purchasing, displaying or discussing my photographs, please contact me. I&amp;nbsp;usually respond within three days. I'd&amp;nbsp;be delighted to hear from you and know what you think. &lt;br&gt;&lt;br&gt; I am available for critique sessions, workshops and presentations where I share my experience, creative process and techniques as a photographer and curator. &lt;/div&gt; &lt;/div&gt; &lt;!-- grid-container --&gt;</pre></div> 圖像溢出網格單元
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM