博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
css创建一个表单在页面上_如何使用CSS Grid创建简单表单
阅读量:2521 次
发布时间:2019-05-11

本文共 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:

这是我们正在构建的:

使用CSS Grid构建表单 (Building the form with CSS Grid)

From the picture above, we know the form contains two elements:

从上面的图片中,我们知道表单包含两个元素:

  1. An email field

    电子邮件字段
  2. A submit button

    提交按钮

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.

对于此表单,我们需要设置两列。

  1. The first column should expand to fill up any available space

    第一列应展开以填充所有可用空间
  2. The second column should be sized according to its contents

    第二列的大小应根据其内容而定

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( )在上 。

当元素的高度不相等时 (When elements are of unequal height)

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属性更改为startendcenter

Here’s a Codepen for you to play around with:

这是一个Codepen,供您试用:

by Zell Liew () on .

Zell Liew( )在上的 。

结语 (Wrapping up)

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/

你可能感兴趣的文章
冒泡排序
查看>>
react中<link>和<navlink>区别
查看>>
C# 生成随机数
查看>>
Psutil模块的应用
查看>>
session概述
查看>>
MATLAB 单变量函数一阶及N阶求导
查看>>
如何在网页端启动WinForm 程序
查看>>
[转载] Java并发编程:Lock
查看>>
MySQL之索引
查看>>
JAVA设计模式之单例模式
查看>>
优秀博客
查看>>
词法分析程序
查看>>
Java反射
查看>>
[ACM_模拟][ACM_数学] LA 2995 Image Is Everything [由6个视图计算立方体最大体积]
查看>>
1040 有几个PAT
查看>>
BZOJ 1412 [ZJOI2009]狼和羊的故事 | 网络流
查看>>
原型模式
查看>>
Hadoop RPC源码阅读-交互协议
查看>>
WASAPI、DirectSound/DS、WaveOut、Kernel Streaming/KS
查看>>
Perl按行分割文件
查看>>