首页 > 开发 > CSS > 正文

CSS 表单

2024-07-11 08:33:31
字体:
来源:转载
供稿:网友

CSS 表单

一个表单案例,我们使用 CSS 来渲染 HTML 的表单元素:

CSS 实例


input[type=text], select {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}

input[type=submit] {
width: 100%;
background-color: #4CAF50;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
border-radius: 4px;
cursor: pointer;
}

input[type=submit]:hover {
background-color: #45a049;
}

div {
border-radius: 5px;
background-color: #f2f2f2;
padding: 20px;
}

 


输入框(input) 样式

使用 width 属性来设置输入框的宽度:

CSS 实例


input {
width: 100%;
}

 

以上实例中设置了所有 元素的宽度为 100%,如果你只想设置指定类型的输入框可以使用以下属性选择器:

input[type=text]
– 选取文本输入框
input[type=password]
– 选择密码的输入框
input[type=number]
– 选择数字的输入框


输入框填充

使用

padding
属性可以在输入框中添加内边距。

CSS 实例


input[type=text] {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
box-sizing: border-box;
}

 

注意我们设置了

box-sizing
属性为
border-box
。这样可以确保浏览器呈现出带有指定宽度和高度的输入框是把边框和内边距一起计算进去的。
更多内容可以阅读

如果你只想添加底部边框可以使用

border-bottom
属性:

CSS 实例


input[type=text] {
border: none;
border-bottom: 2px solid red;
}

 


输入框(input) 颜色

可以使用

background-color
属性来设置输入框的背景颜色,
color
属性用于修改文本颜色:

CSS 实例


input[type=text] {
background-color: #3CBC8D;
color: white;
}

 


输入框(input) 聚焦

默认情况下,一些浏览器在输入框获取焦点时(点击输入框)会有一个蓝色轮廓。我们可以设置 input 样式为

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表