㈠输入框(input) 样式

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

  示例:css部分:input { width: 100%; }  

             html部分:<form>

                              <label for="fname">First Name</label>

                              <input type="text" id="fname" name="fname">

                             </form>

设置了所有 <input> 元素的宽度为 100%

⑵如果只想设置指定类型的输入框可以使用以下属性选择器:

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

㈡输入框填充

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

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>文本框的内边距</title>
<style>
input[type=text] {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
box-sizing: border-box;
}
</style>
</head>
<body> <p>设置文本框的内边距:</p> <form>
<label for="fname">First Name</label>
<input type="text" id="fname" name="fname">
<label for="lname">Last Name</label>
<input type="text" id="lname" name="lname">
</form> </body>
</html>

效果图:

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

㈢输入框(input) 边框

⑴使用 border 属性可以修改 input 边框的大小或颜色,使用 border-radius 属性可以给 input 添加圆角

⑵示例1:文本框圆角边框

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>文本框边框</title>
<style>
input[type=text] {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
box-sizing: border-box;
border: 2px solid red;
border-radius: 4px;
}
</style>
</head>
<body> <p>文本框的边框:</p> <form>
<label for="fname">First Name</label>
<input type="text" id="fname" name="fname">
<label for="lname">Last Name</label>
<input type="text" id="lname" name="lname">
</form> </body>
</html>

效果图:

⑶ 示例二:添加底部边框,使用 border-bottom 属性,添加底部边框

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>底部边框</title>
<style>
input[type=text] {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
box-sizing: border-box;
border: none;
border-bottom: 2px solid red;
}
</style>
</head>
<body> <p>只在文本框底部添加边框:</p> <form>
<label for="fname">First Name</label>
<input type="text" id="fname" name="fname">
<label for="lname">Last Name</label>
<input type="text" id="lname" name="lname">
</form> </body>
</html>

效果图:

㈣输入框(input) 颜色

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

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

㈤输入框(input) 聚焦

⑴默认情况下,一些浏览器在输入框获取焦点时(点击输入框)会有一个蓝色轮廓。

⑵我们可以设置 input 样式为 outline: none; 来忽略该效果。

⑶示例1:使用 :focus 选择器可以设置输入框在获取焦点时的样式:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>:focus选择器</title>
<style>
input[type=text] {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
box-sizing: border-box;
border: 1px solid #555;
outline: none;
} input[type=text]:focus {
background-color: lightblue;
}
</style>
</head>
<body> <p>在这个实例中,我们使用了 :focus 选择器(点击输入框时)来给文本输入框添加背景颜色:</p> <form>
<label for="fname">First Name</label>
<input type="text" id="fname" name="fname" value="John">
<label for="lname">Last Name</label>
<input type="text" id="lname" name="lname" value="Doe">
</form> </body>
</html>

效果图:

⑷示例2:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>CSS transition 属性</title>
<style>
input[type=text] {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
box-sizing: border-box;
border: 3px solid #ccc;
-webkit-transition: 0.5s;
transition: 0.5s;
outline: none;
} input[type=text]:focus {
border: 3px solid #555;
}
</style>
</head>
<body> <p>在这个实例,我们使用 :focus 选择器,在文本框获取焦点时,设置文本框当边框颜色为黑色。</p>
<p>注意,我们使用来 CSS transition 属性来设置边框当颜色 (在 0.5 秒内修改边框当颜色)。</p> <form>
<label for="fname">First Name</label>
<input type="text" id="fname" name="fname" value="John">
<label for="lname">Last Name</label>
<input type="text" id="lname" name="lname" value="Doe">
</form> </body>
</html>

效果图:

㈥输入框(input) 图标

⑴如果想在输入框中添加图标,可以使用 background-image 属性和用于定位的background-position 属性。

⑵注意设置图标的左边距,让图标有一定的空间

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>图标</title>
<style>
input[type=text] {
width: 100%;
box-sizing: border-box;
border: 2px solid #ccc;
border-radius: 4px;
font-size: 16px;
background-color: white;
background-image: url('https://static.runoob.com/images/mix/searchicon.png');
background-position: 10px 10px;
background-repeat: no-repeat;
padding: 12px 20px 12px 40px;
}
</style>
</head>
<body> <p>输入框按钮:</p> <form>
<input type="text" name="search" placeholder="搜索..">
</form> </body>
</html>

效果图:

㈦带动画的搜索框

使用了 CSS transition 属性,该属性设置了输入框在获取焦点时会向右延展。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>搜索框带动画</title>
<style>
input[type=text] {
width: 130px;
box-sizing: border-box;
border: 2px solid #ccc;
border-radius: 4px;
font-size: 16px;
background-color: white;
background-image: url('https://static.runoob.com/images/mix/searchicon.png');
background-position: 10px 10px;
background-repeat: no-repeat;
padding: 12px 20px 12px 40px;
-webkit-transition: width 0.4s ease-in-out;
transition: width 0.4s ease-in-out;
} input[type=text]:focus {
width: 100%;
}
</style>
</head>
<body> <p>搜索输入框带动画:</p> <form>
<input type="text" name="search" placeholder="搜索..">
</form> </body>
</html>

效果图:

 

 ㈧文本框(textarea)样式

 使用 resize 属性来禁用文本框可以重置大小的功能(一般拖动右下角可以重置大小)。

textarea {
width: 100%;
height: 150px;
padding: 12px 20px;
box-sizing: border-box;
border: 2px solid #ccc;
border-radius: 4px;
background-color: #f8f8f8;
resize: none;
}

㈨下拉菜单(select)样式

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>水果下拉菜单</title>
<style>
select {
width: 100%;
padding: 16px 20px;
border: none;
border-radius: 4px;
background-color: #f1f1f1;
}
</style>
</head>
<body> <p>下拉菜单</p> <form>
<select id="fruit" name="fruit">
<option value="ap">Apple</option>
<option value="cs">Cstrawberry</option>
<option value="ba">Banana </option>
</select>
</form> </body>
</html>

效果图:

㈩按钮样式

input[type=button], input[type=submit], input[type=reset] {
background-color: #4CAF50;
border: none;
color: white;
padding: 16px 32px;
text-decoration: none;
margin: 4px 2px;
cursor: pointer;
}
 
提示: 使用 width: 100% 设置全宽按钮

 

(十一)响应式表单

响应式表带可以根据浏览器窗口的大小重新布局各个元素,我们可以通过重置浏览器窗口大小来查看效果

<!DOCTYPE html>
<html>
<head>
<style>
* {
box-sizing: border-box;
} input[type=text], select, textarea {
width: 100%;
padding: 12px;
border: 1px solid #ccc;
border-radius: 4px;
resize: vertical;
} label {
padding: 12px 12px 12px 0;
display: inline-block;
} input[type=submit] {
background-color: #4CAF50;
color: white;
padding: 12px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
float: right;
} input[type=submit]:hover {
background-color: #45a049;
} .container {
border-radius: 5px;
background-color: #f2f2f2;
padding: 20px;
} .col-25 {
float: left;
width: 25%;
margin-top: 6px;
} .col-75 {
float: left;
width: 75%;
margin-top: 6px;
} /* 清除浮动 */
.row:after {
content: "";
display: table;
clear: both;
} /* 响应式布局 layout - 在屏幕宽度小于 600px 时, 设置为上下堆叠元素 */
@media screen and (max-width: 600px) {
.col-25, .col-75, input[type=submit] {
width: 100%;
margin-top: 0;
}
}
</style>
</head>
<body> <h2>响应式表单</h2>
<p>响应式表带可以根据浏览器窗口的大小重新布局各个元素,我们可以通过重置浏览器窗口大小来查看效果:</p> <div class="container">
<form action="/action_page.php">
<div class="row">
<div class="col-25">
<label for="fname">First Name</label>
</div>
<div class="col-75">
<input type="text" id="fname" name="firstname" placeholder="Your name..">
</div>
</div>
<div class="row">
<div class="col-25">
<label for="lname">Last Name</label>
</div>
<div class="col-75">
<input type="text" id="lname" name="lastname" placeholder="Your last name..">
</div>
</div>
<div class="row">
<div class="col-25">
<label for="country">Country</label>
</div>
<div class="col-75">
<select id="country" name="country">
<option value="australia">Australia</option>
<option value="canada">Canada</option>
<option value="usa">USA</option>
</select>
</div>
</div>
<div class="row">
<div class="col-25">
<label for="subject">Subject</label>
</div>
<div class="col-75">
<textarea id="subject" name="subject" placeholder="Write something.." style="height:200px"></textarea>
</div>
</div>
<div class="row">
<input type="submit" value="Submit">
</div>
</form>
</div> </body>
</html>

效果图:

⑴屏幕宽度大于 600px 时的效果⑩

 ⑵屏幕宽度小于 600px 时的效果

 

参考:菜鸟教程:https://www.runoob.com/css/css-form.html

什么是CSS 表单?的更多相关文章

  1. js 节点 document html css 表单节点操作

    js 节点 document html css 表单节点操作 节点操作:访问.属性.创建 (1)节点的访问:firstChild.lastChild.childNodes.parentChild(父子 ...

  2. CSS表单设计

    今天我们开始学习<十天学会web标准(div+css)>的css表单设计,包含以下内容和知识点: 改变文本框和文本域样式 用图片美化按钮 改变下拉列表样式 用label标签提升用户体验 一 ...

  3. Web标准:九、CSS表单设计

    Web标准:九.CSS表单设计 知识点: 1.改变文本框和文本域样式 2.用图片美化按钮 3.改变下拉列表样式 4.用label标签提升用户体验   1)改变文本框和文本域样式 文本框标签:<i ...

  4. css 表单标签两端对齐

    来自:http://demo.doyoe.com/css3/justify/justify-form.htm  侵删 <!DOCTYPE html> <html lang=" ...

  5. CSS 表单

    输入框前有图片 老板让你实现在输入框前有图片的功能.老板觉得用图片代替文字更有说服力. 要实现这样的功能很简单,它的原理是将图片放在内边距内. 代码 1 2 3 4 5 6 7 8 9 10 11 1 ...

  6. bootstrap -- css -- 表单控件

    若干css样式 .form-control { display: block; width: 100%; height: 34px; padding: 6px 12px; font-size: 14p ...

  7. CSS表单与数据表(下)

    2.表单 表单是用户输入内容的地方.表单涉及的控件很多,而且一直很难给它们应用样式.无法控制样式的部分,可以通过自定义控件来解决. 2.1 简单的表单 2.1.1 fieldset与legend fi ...

  8. CSS表单与数据表(上)

    表单在现代Web应用中占据着重要地位. 表单可以很简单,也可以非常复杂,要横跨几个页面. 除了从用户哪里获得数据,Web应用还需要以容易看懂的方式展示数据.表格是展示复杂数据的最佳方式. 1.设计数据 ...

  9. Bootstrap CSS 表单

    表单布局 Bootstrap 提供了下列类型的表单布局: 垂直表单(默认) 内联表单 水平表单 垂直或基本表单 基本的表单结构是 Bootstrap 自带的,个别的表单控件自动接收一些全局样式.下面列 ...

随机推荐

  1. Centos7 安装多版本php 并添加swoole拓展

    服务器默认安装了php7 直接使用lnmp工具包安装php5.6 使用之前的lnmp安装包,切换到root sudo su - 运行 选择5.6 安装完成 没有安装swoole拓展 由官方https: ...

  2. 2019 徐州icpc网络赛 E. XKC's basketball team

    题库链接: https://nanti.jisuanke.com/t/41387 题目大意 给定n个数,与一个数m,求ai右边最后一个至少比ai大m的数与这个数之间有多少个数 思路 对于每一个数,利用 ...

  3. @Transient注解的使用(不被序列化和作为临时变量存储)

    转自:https://blog.csdn.net/sinat_29581293/article/details/51810805 java 的transient关键字的作用是需要实现Serilizab ...

  4. 初入JavaWeb(半成品)

    2019-10-21 20:51:03 初次进行Javaweb的开发. 要求: 1登录账号:要求由6到12位字母.数字.下划线组成,只有字母可以开头:(1分) 2登录密码:要求显示“• ”或“*”表示 ...

  5. 01:keepalive高可用集群

    1.1 keepalived高可用软件介绍 1.keepalived--监控检查 注:keepalive软件有两种功能:监控检查.VRRP冗余协议 1. keepalive的作用是检测web服务器的状 ...

  6. 从入门到自闭之Python入门

    python是一门解释型编程语言 变量名命名的规则: 变量名由字母,数字,下划线组成 变量名不能以数字开头 变量名要具有可描述性 变量名要区分大小写 变量名禁止使用python关键字 变量名不能使用中 ...

  7. 在Windows下安装BIND作为DNS服务器(模拟网站比较有用)

    本文参考了CU下的一篇帖子,感谢:) 1.下载BIND http://ftp.isc.org/isc/bind9/9.4.3/BIND9.4.3.zip 2.安装    下载回来是zip的压缩包,解压 ...

  8. java之设计模式汇总

    1.单例模式 就是一个类只产生一个对象 对应数据库连接 定时执行者服务(ScheduledExecutorService) 在整个项目中应该只有一个对象 2.工厂模式 定义一个用于创建对象的接口 让子 ...

  9. 我爬的entityFramework的坑

    老师使用的是mysql的数据库,但是我只有sqlserver的数据库,于是就照猫画虎,想连自己的sqlserver,结果一连跳了几个坑: 坑一:appsetting中的字符串连接是后面还有个s, 坑二 ...

  10. 使用maven构建dubbo服务的可执行jar包+Dubbo 程序实例

    https://blog.csdn.net/zsg88/article/details/76100482 https://blog.csdn.net/zsg88/article/details/762 ...