本文共 2790 字,大约阅读时间需要 9 分钟。
css创建一个表单在页面上
You learned to create a simple form with Flexbox in the . Today, you’ll understand how to create the same thing with CSS Grid.
在中,您学习了使用Flexbox创建简单表单的方法。 今天,您将了解如何使用CSS Grid创建相同的东西。
Here’s what we’re building:
这是我们正在构建的:
From the picture above, we know the form contains two elements:
从上面的图片中,我们知道表单包含两个元素:
Here’s the HTML:
这是HTML:
To build the form with CSS Grid, you need to set the parent’s display
property to grid
.
要使用CSS Grid构建表单,您需要将父级的display
属性设置为grid
。
form { display: grid; }
Here’s what you get:
这是您得到的:
Why did we get two rows?
为什么我们得到两行?
We get two rows because we did not specify the number of columns for the grid. Browsers will always default to one column.
我们得到两行,因为我们没有指定网格的列数。 浏览器将始终默认为一列。
For this form, we need to set two columns.
对于此表单,我们需要设置两列。
For the first column, we can use the fr
unit. For the second column, we can use auto
.
对于第一列,我们可以使用fr
单位。 对于第二列,我们可以使用auto
。
form { display: grid; grid-template-columns: 1fr auto; }
With this, you have completed form’s layout. Here’s a Codepen for you to play around with:
这样,您就完成了表单的布局。 这是一个Codepen,供您试用:
by Zell Liew () on .
Zell Liew( )在上 。
We will simulate elements of unequal height by substituting the button
’s text with an SVG. .
我们将通过用SVG替换button
的文本来模拟高度不等的元素。 。
Notice the input
’s height increases to fit the large SVG icon too! Once again, we don’t have to write any extra code. This happens because grid items are stretched vertically to fill up any available space.
请注意, input
的高度也会增加以适合较大的SVG图标! 再一次,我们不必编写任何额外的代码。 发生这种情况是因为网格项目在垂直方向上被拉伸以填充任何可用空间。
If you want to change this behavior, you can change the align-items
property to either start
, end
, or center
.
如果要更改此行为,可以将align-items
属性更改为start
, end
或center
。
Here’s a Codepen for you to play around with:
这是一个Codepen,供您试用:
by Zell Liew () on .
Zell Liew( )在上的 。
CSS Grid makes it easy to create layouts. It doesn’t have to be used for macro layouts. It can also be used for micro layouts like the form example you’ve seen here.
CSS Grid使创建布局变得容易。 它不必用于宏布局。 它也可以用于微型布局,例如您在此处看到的表单示例。
Have fun with CSS Grid!
玩CSS网格吧!
Thanks for reading. Did this article help you in any way? If you did, . You might help someone out. Thank you!
谢谢阅读。 本文对您有任何帮助吗? 如果这样做, 。 您可能会帮助某人。 谢谢!
This article was originally posted on .Sign up for my if you want more articles to help you become a better front-end developer.
本文最初发布在 。 如果您想要更多文章来帮助您成为更好的前端开发人员,请注册我的 。
翻译自:
css创建一个表单在页面上
转载地址:http://cbwzd.baihongyu.com/