㈠输入框(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 属性可以在输入框中添加内边距。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>文本框的内边距</title>
  6. <style>
  7. input[type=text] {
  8. width: 100%;
  9. padding: 12px 20px;
  10. margin: 8px 0;
  11. box-sizing: border-box;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <p>设置文本框的内边距:</p>
  17. <form>
  18. <label for="fname">First Name</label>
  19. <input type="text" id="fname" name="fname">
  20. <label for="lname">Last Name</label>
  21. <input type="text" id="lname" name="lname">
  22. </form>
  23. </body>
  24. </html>

效果图:

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

㈢输入框(input) 边框

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

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

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>文本框边框</title>
  6. <style>
  7. input[type=text] {
  8. width: 100%;
  9. padding: 12px 20px;
  10. margin: 8px 0;
  11. box-sizing: border-box;
  12. border: 2px solid red;
  13. border-radius: 4px;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <p>文本框的边框:</p>
  19. <form>
  20. <label for="fname">First Name</label>
  21. <input type="text" id="fname" name="fname">
  22. <label for="lname">Last Name</label>
  23. <input type="text" id="lname" name="lname">
  24. </form>
  25. </body>
  26. </html>

效果图:

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

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>底部边框</title>
  6. <style>
  7. input[type=text] {
  8. width: 100%;
  9. padding: 12px 20px;
  10. margin: 8px 0;
  11. box-sizing: border-box;
  12. border: none;
  13. border-bottom: 2px solid red;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <p>只在文本框底部添加边框:</p>
  19. <form>
  20. <label for="fname">First Name</label>
  21. <input type="text" id="fname" name="fname">
  22. <label for="lname">Last Name</label>
  23. <input type="text" id="lname" name="lname">
  24. </form>
  25. </body>
  26. </html>

效果图:

㈣输入框(input) 颜色

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

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

㈤输入框(input) 聚焦

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

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

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

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>:focus选择器</title>
  6. <style>
  7. input[type=text] {
  8. width: 100%;
  9. padding: 12px 20px;
  10. margin: 8px 0;
  11. box-sizing: border-box;
  12. border: 1px solid #555;
  13. outline: none;
  14. }
  15. input[type=text]:focus {
  16. background-color: lightblue;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <p>在这个实例中,我们使用了 :focus 选择器(点击输入框时)来给文本输入框添加背景颜色:</p>
  22. <form>
  23. <label for="fname">First Name</label>
  24. <input type="text" id="fname" name="fname" value="John">
  25. <label for="lname">Last Name</label>
  26. <input type="text" id="lname" name="lname" value="Doe">
  27. </form>
  28. </body>
  29. </html>

效果图:

⑷示例2:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>CSS transition 属性</title>
  6. <style>
  7. input[type=text] {
  8. width: 100%;
  9. padding: 12px 20px;
  10. margin: 8px 0;
  11. box-sizing: border-box;
  12. border: 3px solid #ccc;
  13. -webkit-transition: 0.5s;
  14. transition: 0.5s;
  15. outline: none;
  16. }
  17. input[type=text]:focus {
  18. border: 3px solid #555;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <p>在这个实例,我们使用 :focus 选择器,在文本框获取焦点时,设置文本框当边框颜色为黑色。</p>
  24. <p>注意,我们使用来 CSS transition 属性来设置边框当颜色 (在 0.5 秒内修改边框当颜色)。</p>
  25. <form>
  26. <label for="fname">First Name</label>
  27. <input type="text" id="fname" name="fname" value="John">
  28. <label for="lname">Last Name</label>
  29. <input type="text" id="lname" name="lname" value="Doe">
  30. </form>
  31. </body>
  32. </html>

效果图:

㈥输入框(input) 图标

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

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

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

效果图:

㈦带动画的搜索框

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

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>搜索框带动画</title>
  6. <style>
  7. input[type=text] {
  8. width: 130px;
  9. box-sizing: border-box;
  10. border: 2px solid #ccc;
  11. border-radius: 4px;
  12. font-size: 16px;
  13. background-color: white;
  14. background-image: url('https://static.runoob.com/images/mix/searchicon.png');
  15. background-position: 10px 10px;
  16. background-repeat: no-repeat;
  17. padding: 12px 20px 12px 40px;
  18. -webkit-transition: width 0.4s ease-in-out;
  19. transition: width 0.4s ease-in-out;
  20. }
  21. input[type=text]:focus {
  22. width: 100%;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <p>搜索输入框带动画:</p>
  28. <form>
  29. <input type="text" name="search" placeholder="搜索..">
  30. </form>
  31. </body>
  32. </html>

效果图:

 

 ㈧文本框(textarea)样式

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

  1. textarea {
  2. width: 100%;
  3. height: 150px;
  4. padding: 12px 20px;
  5. box-sizing: border-box;
  6. border: 2px solid #ccc;
  7. border-radius: 4px;
  8. background-color: #f8f8f8;
  9. resize: none;
  10. }

㈨下拉菜单(select)样式

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>水果下拉菜单</title>
  6. <style>
  7. select {
  8. width: 100%;
  9. padding: 16px 20px;
  10. border: none;
  11. border-radius: 4px;
  12. background-color: #f1f1f1;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <p>下拉菜单</p>
  18. <form>
  19. <select id="fruit" name="fruit">
  20. <option value="ap">Apple</option>
  21. <option value="cs">Cstrawberry</option>
  22. <option value="ba">Banana </option>
  23. </select>
  24. </form>
  25. </body>
  26. </html>

效果图:

㈩按钮样式

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

 

(十一)响应式表单

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

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. * {
  6. box-sizing: border-box;
  7. }
  8. input[type=text], select, textarea {
  9. width: 100%;
  10. padding: 12px;
  11. border: 1px solid #ccc;
  12. border-radius: 4px;
  13. resize: vertical;
  14. }
  15. label {
  16. padding: 12px 12px 12px 0;
  17. display: inline-block;
  18. }
  19. input[type=submit] {
  20. background-color: #4CAF50;
  21. color: white;
  22. padding: 12px 20px;
  23. border: none;
  24. border-radius: 4px;
  25. cursor: pointer;
  26. float: right;
  27. }
  28. input[type=submit]:hover {
  29. background-color: #45a049;
  30. }
  31. .container {
  32. border-radius: 5px;
  33. background-color: #f2f2f2;
  34. padding: 20px;
  35. }
  36. .col-25 {
  37. float: left;
  38. width: 25%;
  39. margin-top: 6px;
  40. }
  41. .col-75 {
  42. float: left;
  43. width: 75%;
  44. margin-top: 6px;
  45. }
  46. /* 清除浮动 */
  47. .row:after {
  48. content: "";
  49. display: table;
  50. clear: both;
  51. }
  52. /* 响应式布局 layout - 在屏幕宽度小于 600px 时, 设置为上下堆叠元素 */
  53. @media screen and (max-width: 600px) {
  54. .col-25, .col-75, input[type=submit] {
  55. width: 100%;
  56. margin-top: 0;
  57. }
  58. }
  59. </style>
  60. </head>
  61. <body>
  62. <h2>响应式表单</h2>
  63. <p>响应式表带可以根据浏览器窗口的大小重新布局各个元素,我们可以通过重置浏览器窗口大小来查看效果:</p>
  64. <div class="container">
  65. <form action="/action_page.php">
  66. <div class="row">
  67. <div class="col-25">
  68. <label for="fname">First Name</label>
  69. </div>
  70. <div class="col-75">
  71. <input type="text" id="fname" name="firstname" placeholder="Your name..">
  72. </div>
  73. </div>
  74. <div class="row">
  75. <div class="col-25">
  76. <label for="lname">Last Name</label>
  77. </div>
  78. <div class="col-75">
  79. <input type="text" id="lname" name="lastname" placeholder="Your last name..">
  80. </div>
  81. </div>
  82. <div class="row">
  83. <div class="col-25">
  84. <label for="country">Country</label>
  85. </div>
  86. <div class="col-75">
  87. <select id="country" name="country">
  88. <option value="australia">Australia</option>
  89. <option value="canada">Canada</option>
  90. <option value="usa">USA</option>
  91. </select>
  92. </div>
  93. </div>
  94. <div class="row">
  95. <div class="col-25">
  96. <label for="subject">Subject</label>
  97. </div>
  98. <div class="col-75">
  99. <textarea id="subject" name="subject" placeholder="Write something.." style="height:200px"></textarea>
  100. </div>
  101. </div>
  102. <div class="row">
  103. <input type="submit" value="Submit">
  104. </div>
  105. </form>
  106. </div>
  107. </body>
  108. </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. PHP Smarty模板的安装

    最近开发中用到了PHP中smarty模板..作为一个长久以来的前端,开始学习PHP模板..下面将安装教程分享给大家.. 1. 下载Smarty最新版: http://www.smarty.NET/do ...

  2. FTL2

    ABSTACT 1.NAND flash memory  (主要缺点): (1)partial page updates (2)general-purpose cache usually does n ...

  3. 算法 - k-means++

    Kmeans++算法 Kmeans++算法,主要可以解决初始中心的选择问题,不可解决k的个数问题. Kmeans++主要思想是选择的初始聚类中心要尽量的远. 做法: 1.    在输入的数据点中随机选 ...

  4. 第二大矩阵面积--(stack)牛客多校第二场-- Second Large Rectangle

    题意: 给你一幅图,问你第二大矩形面积是多少. 思路: 直接一行行跑stack求最大矩阵面积的经典算法,不断更新第二大矩形面积,注意第二大矩形可能在第一大矩形里面. #define IOS ios_b ...

  5. 使用request+bs4爬取所有股票信息

    爬取前戏 我们要知道利用selenium是非常无敌的,自我认为什么反爬不反爬都不在话下,但是今天我们为什么要用request+bs4爬取所有股票信息呢?因为他比较原始,因此今天的数据,爬取起来也是比较 ...

  6. L2-014. 列车调度(Dilworth定理)

    火车站的列车调度铁轨的结构如下图所示. Figure 两端分别是一条入口(Entrance)轨道和一条出口(Exit)轨道,它们之间有N条平行的轨道.每趟列车从入口可以选择任意一条轨道进入,最后从出口 ...

  7. Robot Framework(三)项目实践出现的问题以及解决方法

    导航: 1.元素定位失败 2.系统自带的确认弹窗 3.ElementNotVisibleException: Message: element not visible 1.元素定位失败(使用frame ...

  8. Codeforces 1221E. Game With String

    传送门 首先每一段连续的 $...$ 都是互不影响的,所以可以一段段考虑 考虑最简单的情况,此时每一段都大于等于 $a$ 并且小于 $2b$ ,那么每一段都只能放一次,胜负直接根据段数即可得到答案 考 ...

  9. ion-icon

    观察默认的使用 关于Ion-icon 图标的自定义,首先,看一看默认内置的图标的显示,是怎么来的 可以看到默认的路径为:/svg/ios-xxx.svg 自定义实现 那么这种路径是哪里来的,明显是an ...

  10. 01 初识HTML

    HTML是什么? 超文本标记语言(Hypertext Markup Language, HTML)是一种用于创建网页的标记语言. 本质上是浏览器可识别的规则,我们按照规则写网页,浏览器根据规则渲染我们 ...