html - 表单对齐折叠成单行

标签 html css alignment css-grid

我将父元素设置为 newTableForm,将 labelColumnmainFormColumn 作为子元素 - 但使用当前的 CSS,所有内容都折叠成一行:

.newTableForm {
  display: grid;
  grid-template-columns: 1fr 3fr;
  grid-template-rows: 30px 30px 30px 40px;
  grid-gap: 10px;
  grid-template-areas: "... main" "labels main" "labels main" "... main";
  border: 2px dashed deeppink;
}

.labelColumn {
  grid-area: labels;
}

.mainFormColumn {
  grid-area: main;
}
<form method='post' action="/admin-post.php" class="newTableForm">
  <h4 class="mainFormColumn">
    Add a new price compare table
  </h4>
  <label for="store" class="labelColumn">Store</label>
  <input type="text" name="store" placeholder="" class="mainFormColumn"/>
  <label for="product-page" class="labelColumn">Product Page Link</label>
  <input type="text" name="product-page" placeholder="Copy address here" class="mainFormColumn" />
  <button class="mainFormColumn">Save new price compare table</button>
</form>

有没有办法取消这里的单列堆叠行为?如果不是带有...(标签部分上方和下方)的空白部分,我将完全删除模板区域

最佳答案

同样,您可以将 grid-template-areas 作为多个网格区域重叠 - 您可以为此使用伪元素:

  • 使用 grid-column: 1mainFormColumnlabelColumn 放入第一列 第二列使用grid-column: 2

  • after 元素将成为最后一行中的第一列,使用grid-row: -2grid-column: 1

  • before 将是第一行中的第一列,使用grid-column: 1

请看下面的演示:

.newTableForm {
  display: grid;
  grid-template-columns: 1fr 3fr;
  grid-template-rows: 30px 30px 30px 40px;
  grid-gap: 10px;
  border: 2px dashed deeppink;
}

.labelColumn {
  grid-column: 1;
}

.mainFormColumn {
  grid-column: 2;
}
.newTableForm:after, .newTableForm:before {
  content: '';
  display: block;
  grid-column: 1;
}

.newTableForm:after {
  grid-row: -2;
}
<form method='post' action="/admin-post.php" class="newTableForm">
  <h4 class="mainFormColumn">
    Add a new price compare table
  </h4>
  <label for="store" class="labelColumn">Store</label>
  <input type="text" name="store" placeholder="" class="mainFormColumn"/>
  <label for="product-page" class="labelColumn">Product Page Link</label>
  <input type="text" name="product-page" placeholder="Copy address here" class="mainFormColumn" />

  <button class="mainFormColumn">Save new price compare table</button>
</form>

关于html - 表单对齐折叠成单行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55325488/

相关文章:

html - 为什么对齐 ="right"不起作用?

javascript - unity3d.com/5 中使用了什么 slider ?

javascript - 使用来自 XMLHttpRequest responsetext 的图像数据来显示图像

javascript - 图像在 chrome 和 mozila 中显示,但在 IE 中不显示?

jquery - 自定义分享按钮,需要将图标字体替换为图片

html - 如何让页面上的象限垂直拉伸(stretch)以匹配水平相邻象限的高度?

html - HTML5 中的行内元素是否允许 block 级元素?

html - 使用非常简单的 html 的意外边距

jquery - 如何将导航栏制作成轮播

css - 如何让主 div 容器居中对齐?