只要您的的网站可以用户登录,那么不可能不用到表单!表单主要功能是用来与用户做交流的一个网页控件,JavaScript发明之初最大的作用也就是用来进行表单操作。所以表单是每一个前端开发者必须要熟练掌握的东西。
良好的表单设计能够让网页与用户更好的沟通。表单中常见的元素主要包括: 文本输入框、下拉选择框、单选按钮、复选按钮、文本域和按钮等。其中每个控件所起的作用都各不相同,而且不同的浏览器对表单控件渲染的风格都各有不同。传统的HTML表单就不在这样进行讲解了,下面就开始Bootstrap表单的学习吧!
一. 基础表单
在Bootstrap对于基础表单没有做太多的定制效果,仅仅是对于一些元素的 margin、padding和border进行了一些细化设置。
当然,也不会那么简单,在Bootstrap框架中,通过定制了一个类名 form-control,也就是说,使用了类名 form-control,将会实现一些设计上的定制效果。从源码中(2551行,嗯,善用搜索)可以得出结论如下:
- 宽度变成了100%
- 设置了一个浅灰色( #ccc)的边框
- 具有4px的圆角
- 设置阴影效果,并且元素得到焦点之时,阴影和边框效果会有所变化
- 设置了placeholder(输入框的提示文字)的颜色为 #999
这个类一般用于输入框,对于其他的类型使用,嗯,你可以试试自己尝试一下。
<form role="form"> <div class="form-group"><!-- form-group这个类在在这里用于控制表单之间的间隔,之后会有另外介绍 --> <label for="exampleInputEmail1">邮箱:</label> <input type="email" class="form-control" id="exampleInputEmail1" placeholder="请输入您的邮箱地址"> </div> <div class="form-group"> <label for="exampleInputPassword1">密码</label> <input type="password" class="form-control" id="exampleInputPassword1" placeholder="请输入您的邮箱密码"> </div></form>
一般来说效果是这样的:
内联表单
还是上面那一段代码:我们只需要给 form加上下面 .form-inline这个类就可以实现表单元素在一行排列:
它的实现原理很简单,其实就是在添加了这个类后,将内部的表单元素设置为了内联块(inline-block)。
我们继续改变屏幕大小会发现小到一定层度之后又变成了原来的样式,那是因为这个类是处于媒体查询中的!这样是为了更好的在移动设备上体验,Bootstrap本来就是一个响应式框架不是吗?
如果你想更简洁一点还可以对于 label添加一个 .sr-only类,把label标签进行隐藏。(注意是隐藏,而不是人为删除,设置label标签有助于屏幕阅读器理解此处的含义)
水平表单
Bootstrap框架默认的表单是 垂直显示风格,除了上面的内联表达之外,很多时候我们需要的是 水平表单风格。在Bootstrap框架中要实现水平表单效果,必须满足以下两个条件:
- 在
- 配合Bootstrap框架的栅格系统。(栅格布局会在以后的章节中详细讲解)
- 例如:使用 将我们的input进行包裹,具体情况到栅格布局的时候再说
可以实现下面的效果,(同样处于媒体查询中)
二. 表单控件
输入框input
单行文本框在html编写时,我们都需要设置 type属性为 text,在Bootstrap中也必须正确的添加type的类型,因为在这里是使用CSS的属性选择器如: input[type="text"]来进行样式设置的!
为了让控件在各种表单风格中样式不出错,需要添加类名“form-control”,如:
下拉选择框select
Bootstrap框架中的下拉选择框使用和原始的一致,但是Bootstrap框架为这些元素提供统一的样式风格。如:
<form role="form"> <div class="form-group"> <select class="form-control"> <option>踢足球</option> <option>游泳</option> <option>慢跑</option> <option>跳舞</option> </select> </div></form>
想要实现多行选择,只需要在 select中设置multiple属性的值为multiple。
需要注意的是,经过测试多行选择是固定高度的(82px),也就是说只有一个选项时,也占据那么高的高度,在选项多时,将出现滚动条
文本域textarea
文本域和原始使用方法一样,设置 rows可定义其高度,设置 cols可以设置其宽度。但如果textarea元素中添加了类名 form-control类名,则无需设置cols属性。因为Bootstrap框架中的 form-control样式的表单控件宽度为100%或auto。
如:
复选框checkbox和单选按钮radio
Bootstrap对于这两个按钮进行了一些优化,要想达到最佳显示效果,有着如下说明:
- 不管是 checkbox还是 radio都使用 label包起来了。
- checkbox按钮连同 label标签放置在一个名为 .checkbox的容器内, radio连同 label标签放置在一个名为 .radio的容器内。
- 在Bootstrap框架中,主要借助 .checkbox和 .radio样式,来处理复选框、单选按钮与标签的对齐方式。
- 只需要将 checkbox换成 checkbox-inline就可以了,你可以添加在label上也可以添加在外部的容器上。
表单控件大小
在Bootstrap中除了使用传统的控制大小(height,padding等等)方式外,还特意定制了两个类名用来控制大小,如下:
- input-sm:让控件比正常大小更小
- input-lg:让控件比正常大小更大
这两个类适用于表单中的 input,textarea和select控件
表单控件状态
焦点状态
焦点状态是通过伪类 :focus来实现。在Bootstrap框架中表单控件的焦点状态删除了 outline的默认样式,重新添加阴影效果。.
禁用状态
Bootstrap实现禁用和普通的表单禁用状态的实现方法一样的,就是在相应的表单控件上添加属性 disabled,只不过Bootstrap做了一些样式风格的处理:
其次,除了单独给相应的表单控件添加disabled外,还可以直接添加到域中
,在这种情况,整个域中的表单全部都将会被禁用,此外还有一个 小bug:如果该域中存在
HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.