[英]CSS Grid with input fields: alignment question
I am using CSS Grid (12 columns) with inputs fields.我正在使用带有输入字段的 CSS 网格(12 列)。 Due to the way I am creating the form (the form design is in JSON and created on the fly) the input fields are not the direct children of the grid but are wrapped in a div.
由于我创建表单的方式(表单设计在 JSON 中并动态创建),输入字段不是网格的直接子项,而是包含在 div 中。 If I do a grid-column: span of the div I also want the input field to span the columns - but not over the grid-gap .
如果我做一个grid-column: span of the div 我还希望输入字段跨越列 - 但不要超过grid-gap 。 I have tried multiple ways of doing this and the best seems to be width: 100% on the input BUT this ignores the grid gap.
我尝试了多种方法,最好的方法似乎是宽度:输入上的 100%但这忽略了网格间隙。 Any idea as to how I can get the input field to be the same width as the div?
关于如何使输入字段与 div 的宽度相同的任何想法?
Sorry if I explained it badly - here is a screenshot of what I mean:对不起,如果我解释得不好 - 这是我的意思的截图:
here the code:这里的代码:
.container { display: grid; grid-template-columns: repeat(12, 1fr); grid-gap: 20px; background-color: black; }.fill-width { width: 100%; }.width-4 { grid-column: span 4; }
<div class="container"> <input id="field1" class="width-4" placeholder="field1 (4)" /> <input id="field2" class="width-4" placeholder="field2 (4)" /> <input id="field3" class="width-4" placeholder="field3 (4)" /> <div class="width-4"> <input id="field5" placeholder="field5 (4)" /> </div> <input id="field6" class="width-4" placeholder="field6 (4)" /> <input id="field7" class="width-4" placeholder="field7 (4)" /> <div class="width-4"> <input id="field1" class="fill-width" placeholder="field1 (4)" /> </div> <div class="width-4"> <input id="field2" class="fill-width" placeholder="field2 (4)" /> </div> <div class="width-4"> <input id="field3" class="fill-width" placeholder="field3 (4)" /> </div> </div>
and the codepen example: https://codepen.io/ursus_the_bear/pen/mdRQNXM和 codepen 示例: https://codepen.io/ursus_the_bear/pen/mdRQNXM
Nothing wrong that you are doing, but let me explain why this happens and the solution.您所做的没有错,但让我解释一下为什么会发生这种情况以及解决方案。
input elements have some css which the browser adds.输入元素有一些浏览器添加的 css。 like border, max-width and stuff.
像边框,最大宽度和东西。 you need to get rid of those, for your inputs to follow your stylesheet strictly.
您需要摆脱这些,以便您的输入严格遵循您的样式表。
add this to the CSS for this to work like a charm:将此添加到 CSS 以使其像魅力一样工作:
* { /* all element selector */
box-sizing: border-box; /* border, within the width of the element */
}
input {
display: block;
width: 100%
}
if * {box-sizing: border-box; }
if
* {box-sizing: border-box; }
* {box-sizing: border-box; }
looks new to you, you can remove the margin and border from input to get the perfect result. * {box-sizing: border-box; }
对您来说看起来很新,您可以从输入中删除边距和边框以获得完美的结果。
input {
display: block;
width: 100%;
border: none;
margin: 0;
}
You are almost there.你快到了。 To ensure that the inputs do not overflow into the grid-gap, you could look into the
box-sizing
property.为确保输入不会溢出到网格间隙中,您可以查看
box-sizing
属性。 But since another answer has already demonstrated that, I'll provide an alternative approach.但是由于另一个答案已经证明了这一点,我将提供另一种方法。
You could try using flexbox.您可以尝试使用 flexbox。 I made the divs in the
container
class flex parents, and then gave the inputs in those divs a flex-grow
of 1. This ensures that the flex child takes up all the extra space in the flex parent.我在
container
class flex 父级中创建了 div,然后将这些 div 中的输入的flex-grow
设置为 1。这样可以确保 flex 子级占用 flex 父级中的所有额外空间。 To make sure that the inputs do not overflow, I gave them a max-width
of 100%.为了确保输入不会溢出,我给了它们 100% 的
max-width
。 I also removed the width
declaration for the fill-width
class:我还删除了
fill-width
class 的width
声明:
.container { display: grid; grid-template-columns: repeat(12, 1fr); grid-gap: 20px; background-color: black; }.width-4 { grid-column: span 4; } /* New styles */.container div { display: flex; flex-flow: row nowrap; }.container div input { flex-grow: 1; max-width: 100%; }
<div class="container"> <input id="field1" class="width-4" placeholder="field1 (4)" /> <input id="field2" class="width-4" placeholder="field2 (4)" /> <input id="field3" class="width-4" placeholder="field3 (4)" /> <div class="width-4"> <input id="field5" placeholder="field5 (4)" /> </div> <input id="field6" class="width-4" placeholder="field6 (4)" /> <input id="field7" class="width-4" placeholder="field7 (4)" /> <div class="width-4"> <input id="field1" class="fill-width" placeholder="field1 (4)" /> </div> <div class="width-4"> <input id="field2" class="fill-width" placeholder="field2 (4)" /> </div> <div class="width-4"> <input id="field3" class="fill-width" placeholder="field3 (4)" /> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.