HTML/CSS – 网格布局不响应式

huangapple go评论68阅读模式
英文:

HTML/CSS - Grid Layout Not Responsive

问题

当我调整textarea元素的大小时,其他元素不具有响应性。选择的textarea和其旁边的textarea之间突然出现了间隙。

lang-css 部分:

@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600&display=swap');
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Poppins', sans-serif;
}

.grid-container {
  position: relative;
  background-color: pink;
  display: grid;
  padding: 20px;
  grid-template-columns: auto auto auto auto auto auto;
  text-align: center;
}

.item {
  background-color: green;
  border: 1px solid black;
  font-size: 40px;
  resize: horizontal;
}

lang-html 部分:

<link rel="stylesheet" href="https://unicons.iconscout.com/release/v4.0.0/css/line.css">

<div class="grid-container">
  <textarea class="item" id="item1">1</textarea>
  <textarea class="item" id="item2">2</textarea>
  <textarea class="item" id="item3">3</textarea>
  <textarea class="item" id="item4">4</textarea>
  <textarea class="item" id="item5">5</textarea>
  <textarea class="item" id="item6">6</textarea>
  <textarea class="item" id="item7">7</textarea>
  <textarea class="item" id="item8">8</textarea>
  <textarea class="item" id="item9">9</textarea>
  <textarea class="item" id="item10">10</textarea>
  <textarea class="item" id="item10">11</textarea>
  <textarea class="item" id="item10">12</textarea>
</div>
英文:

When I resize the textarea element the other elements are not responsive. A sudden gap appears between the selected textarea and the textarea next to it.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

@import url(&#39;https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600&amp;display=swap&#39;);
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: &#39;Poppins&#39;, sans-serif;
}

.grid-container {
  position: relative;
  background-color: pink;
  display: grid;
  padding: 20px;
  grid-template-columns: auto auto auto auto auto auto;
  text-align: center;
}

.item {
  background-color: green;
  border: 1px solid black;
  font-size: 40px;
  resize: horizontal;
}

<!-- language: lang-html -->

&lt;link rel=&quot;stylesheet&quot; href=&quot;https://unicons.iconscout.com/release/v4.0.0/css/line.css&quot;&gt;

&lt;div class=&quot;grid-container&quot;&gt;
  &lt;textarea class=&quot;item&quot; id=&quot;item1&quot;&gt;1&lt;/textarea&gt;
  &lt;textarea class=&quot;item&quot; id=&quot;item2&quot;&gt;2&lt;/textarea&gt;
  &lt;textarea class=&quot;item&quot; id=&quot;item3&quot;&gt;3&lt;/textarea&gt;
  &lt;textarea class=&quot;item&quot; id=&quot;item4&quot;&gt;4&lt;/textarea&gt;
  &lt;textarea class=&quot;item&quot; id=&quot;item5&quot;&gt;5&lt;/textarea&gt;
  &lt;textarea class=&quot;item&quot; id=&quot;item6&quot;&gt;6&lt;/textarea&gt;
  &lt;textarea class=&quot;item&quot; id=&quot;item7&quot;&gt;7&lt;/textarea&gt;
  &lt;textarea class=&quot;item&quot; id=&quot;item8&quot;&gt;8&lt;/textarea&gt;
  &lt;textarea class=&quot;item&quot; id=&quot;item9&quot;&gt;9&lt;/textarea&gt;
  &lt;textarea class=&quot;item&quot; id=&quot;item10&quot;&gt;10&lt;/textarea&gt;
  &lt;textarea class=&quot;item&quot; id=&quot;item10&quot;&gt;11&lt;/textarea&gt;
  &lt;textarea class=&quot;item&quot; id=&quot;item10&quot;&gt;12&lt;/textarea&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

以下是您要求的翻译部分:

"The gap appears because of textareas in the same column and different row. You better use flex instead of grid."

"因为位于同一列但不同行的文本区域,所以出现了间隙。最好使用 flex 而不是 grid。"

"In the snippet bellow, I created the two lines by separating items into two different divs. A more responsive way would be to use just one div, combined with flex-basis and flex-wrap on children."

"在下面的示例中,我通过将项目分为两个不同的 div 来创建了两行。更具响应性的方法是只使用一个 div,并在子元素上结合使用 flex-basisflex-wrap。"

英文:

The gap appears because of textareas in the same column and different row. You better use flex instead of grid.<br/>
In the snippet bellow, I created the two lines by separating items into two different divs. A more responsive way would be to use just one div, combined with flex-basis and flex-wrap on children.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

@import url(&#39;https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600&amp;display=swap&#39;);
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: &#39;Poppins&#39;, sans-serif;
}

.flex-container {
  position: relative;
  background-color: pink;
  display: flex;
  padding: 20px;
  grid-template-columns: auto auto auto auto auto auto;
  text-align: center;
}

.flex-container:not(:last-child) {
  padding-bottom: 0;
}

.flex-container:not(:first-child) {
  padding-top: 0;
}

.item {
  background-color: green;
  border: 1px solid black;
  font-size: 40px;
  resize: horizontal;
}

<!-- language: lang-html -->

&lt;link rel=&quot;stylesheet&quot; href=&quot;https://unicons.iconscout.com/release/v4.0.0/css/line.css&quot;&gt;

&lt;div class=&quot;flex-container&quot;&gt;
  &lt;textarea class=&quot;item&quot; id=&quot;item1&quot;&gt;1&lt;/textarea&gt;
  &lt;textarea class=&quot;item&quot; id=&quot;item2&quot;&gt;2&lt;/textarea&gt;
  &lt;textarea class=&quot;item&quot; id=&quot;item3&quot;&gt;3&lt;/textarea&gt;
  &lt;textarea class=&quot;item&quot; id=&quot;item4&quot;&gt;4&lt;/textarea&gt;
  &lt;textarea class=&quot;item&quot; id=&quot;item5&quot;&gt;5&lt;/textarea&gt;
  &lt;textarea class=&quot;item&quot; id=&quot;item6&quot;&gt;6&lt;/textarea&gt;
&lt;/div&gt;
&lt;div class=&quot;flex-container&quot;&gt;
  &lt;textarea class=&quot;item&quot; id=&quot;item7&quot;&gt;7&lt;/textarea&gt;
  &lt;textarea class=&quot;item&quot; id=&quot;item8&quot;&gt;8&lt;/textarea&gt;
  &lt;textarea class=&quot;item&quot; id=&quot;item9&quot;&gt;9&lt;/textarea&gt;
  &lt;textarea class=&quot;item&quot; id=&quot;item10&quot;&gt;10&lt;/textarea&gt;
  &lt;textarea class=&quot;item&quot; id=&quot;item10&quot;&gt;11&lt;/textarea&gt;
  &lt;textarea class=&quot;item&quot; id=&quot;item10&quot;&gt;12&lt;/textarea&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月24日 03:28:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549464.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定