本文翻译于bootstrap官网,主要用于查看文档,方便学习,文章比较长!
Bootstrap是一个简洁、直观、强悍的前端开发框架,让web开发更迅速、简单。是全球最流行的前端框架,用于构建响应式、移动设备优先的 WEB 站点。
表单控件样式,布局选项和用于创建各种表单的自定义组件的示例和使用指南
<form>
<div class="form-group">
<label for="exampleInputEmail1">电子邮件地址label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
<small id="emailHelp" class="form-text text-muted">我们不会向任何人透露您的邮件地址small>
div>
<div class="form-group">
<label for="exampleInputPassword1">密码label>
<input type="password" class="form-control" id="exampleInputPassword1">
div>
<div class="form-group form-check">
<input type="checkbox" class="form-check-input" id="exampleCheck1">
<label class="form-check-label" for="exampleCheck1">检查label>
div>
<button type="submit" class="btn btn-primary">提交button>
form>
文本形式的控件(如,
和
)使用
.form-control
该类设置样式。其中包括用于一般外观,焦点状态,大小调整等的样式。
确保探索我们的自定义表单以进一步样式化。
<form>
<div class="form-group">
<label for="exampleFormControlInput1">Email addresslabel>
<input type="email" class="form-control" id="exampleFormControlInput1" placeholder="name@example.com">
div>
<div class="form-group">
<label for="exampleFormControlSelect1">Example selectlabel>
<select class="form-control" id="exampleFormControlSelect1">
<option>1option>
<option>2option>
<option>3option>
<option>4option>
<option>5option>
select>
div>
<div class="form-group">
<label for="exampleFormControlSelect2">Example multiple selectlabel>
<select multiple class="form-control" id="exampleFormControlSelect2">
<option>1option>
<option>2option>
<option>3option>
<option>4option>
<option>5option>
select>
div>
<div class="form-group">
<label for="exampleFormControlTextarea1">Example textarealabel>
<textarea class="form-control" id="exampleFormControlTextarea1" rows="3">textarea>
div>
form>
对于文件输入,将替换.form-control
为.form-control-file
。
<form>
<div class="form-group">
<label for="exampleFormControlFile1">示例文件输入label>
<input type="file" class="form-control-file" id="exampleFormControlFile1">
div>
form>
使用类.form-control-lg
和设置高度.form-control-sm
。
<input class="form-control form-control-lg" type="text" placeholder=".form-control-lg">
<input class="form-control" type="text" placeholder="Default input">
<input class="form-control form-control-sm" type="text" placeholder=".form-control-sm">
<select class="form-control form-control-lg">
<option>Large selectoption>
select>
<select class="form-control">
<option>Default selectoption>
select>
<select class="form-control form-control-sm">
<option>Small selectoption>
select>
readonly
在输入上添加布尔属性,以防止修改输入的值。只读输入看起来较浅(就像禁用的输入一样),但保留了标准光标。
<input class="form-control" type="text" placeholder="只读输入在这里..." readonly>
如果希望表单中的元素以纯文本样式设置,请使用
.form-control-plaintext
该类删除默认的表单字段样式,并保留正确的边距和填充。
<form>
<div class="form-group row">
<label for="staticEmail" class="col-sm-2 col-form-label">电子邮件label>
<div class="col-sm-10">
<input type="text" readonly class="form-control-plaintext" id="staticEmail" value="email@example.com">
div>
div>
<div class="form-group row">
<label for="inputPassword" class="col-sm-2 col-form-label">密码label>
<div class="col-sm-10">
<input type="password" class="form-control" id="inputPassword">
div>
div>
form>
<form class="form-inline">
<div class="form-group mb-2">
<label for="staticEmail2" class="sr-only">Emaillabel>
<input type="text" readonly class="form-control-plaintext" id="staticEmail2" value="email@example.com">
div>
<div class="form-group mx-sm-3 mb-2">
<label for="inputPassword2" class="sr-only">密码label>
<input type="password" class="form-control" id="inputPassword2" placeholder="Password">
div>
<button type="submit" class="btn btn-primary mb-2">确认身份button>
form>
使用设置水平滚动范围输入.form-control-range
。
<form>
<div class="form-group">
<label for="formControlRange">范围输入示例label>
<input type="range" class="form-control-range" id="formControlRange">
div>
form>
默认复选框和收音机时的帮助下提高.form-check
,单一类改善他们的HTML元素的布局和行为都输入类型。复选框用于选择列表中的一个或多个选项,而单选框则用于从多个选项中选择一个选项。
支持禁用的复选框和收音机。该disabled
属性将应用较浅的颜色,以帮助指示输入的状态。
复选框和单选按钮支持基于HTML的表单验证,并提供简洁,可访问的标签。因此,我们的和
如相对于一个同级元素
内的
。这有点冗长,因为您必须指定id和for属性以关联
和
。
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="defaultCheck1">
<label class="form-check-label" for="defaultCheck1">
Default checkbox
label>
div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="defaultCheck2" disabled>
<label class="form-check-label" for="defaultCheck2">
Disabled checkbox
label>
div>
<div class="form-check">
<input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios1" value="option1" checked>
<label class="form-check-label" for="exampleRadios1">
Default radio
label>
div>
<div class="form-check">
<input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios2" value="option2">
<label class="form-check-label" for="exampleRadios2">
Second default radio
label>
div>
<div class="form-check">
<input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios3" value="option3" disabled>
<label class="form-check-label" for="exampleRadios3">
Disabled radio
label>
div>
通过将复选框或单选框添加.form-check-inline
到同一水平行来对它们进行分组.form-check
。
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="inlineCheckbox1" value="option1">
<label class="form-check-label" for="inlineCheckbox1">1label>
div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="inlineCheckbox2" value="option2">
<label class="form-check-label" for="inlineCheckbox2">2label>
div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="inlineCheckbox3" value="option3" disabled>
<label class="form-check-label" for="inlineCheckbox3">3 (disabled)label>
div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio1" value="option1">
<label class="form-check-label" for="inlineRadio1">1label>
div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio2" value="option2">
<label class="form-check-label" for="inlineRadio2">2label>
div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio3" value="option3" disabled>
<label class="form-check-label" for="inlineRadio3">3 (disabled)label>
div>
添加.position-static
到.form-check
其中没有任何标签文本的输入中。请记住,仍要为辅助技术提供某种形式的可访问名称(例如,使用aria-label
)。
<div class="form-check">
<input class="form-check-input position-static" type="checkbox" id="blankCheckbox" value="option1" aria-label="...">
div>
<div class="form-check">
<input class="form-check-input position-static" type="radio" name="blankRadio" id="blankRadio1" value="option1" aria-label="...">
div>
由于引导应用display: block
和width: 100%
几乎所有的表单控件,表格会默认堆栈垂直。可以使用其他类来根据表单更改此布局。
该 可以使用我们的网格类构建更复杂的表单。将它们用于需要多列,可变宽度和其他对齐选项的表单布局。 您也可以互换 通过将 有时,您可能需要使用边距或填充实用程序来创建所需的完美对齐方式。例如,我们删除 一定要使用 如前面的示例所示,我们的网格系统允许您 下面的示例使用flexbox实用工具将内容垂直居中并更改 使用 您可能需要使用间距实用程序手动解决单个表单控件的宽度和对齐问题(如下所示)。最后, 可以使用 将 对于自定义的Bootstrap表单验证消息,您需要将 自定义反馈样式将应用自定义颜色,边框,焦点样式和背景图标,以更好地传达反馈。背景图标 对于无效字段,请使用来确保无效反馈/错误消息与相关表单字段相关联 验证样式可用于以下表单控件和组件: 如果您的表单布局允许,则可以将 开关具有自定义复选框的标记,但是使用 自定义 作为size属性: 创建自定义 推荐使用动画制作自定义文件输入的插件: 通过在文本输入,自定义选择和自定义文件输入的两侧添加文本,按钮或按钮组,可以轻松扩展表单控件。 将任何复选框或单选选项放在输入组的插件中,而不是文本中。 支持多个加载项,并且可以与复选框和广播输入版本混合使用。 轻巧灵活的组件,用于展示英雄单位风格的内容。 列表组是用于显示一系列内容的灵活而强大的组件。修改并扩展它们以支持其中的几乎所有内容。 使用 添加 使用上下文类为具有状态背景和颜色的列表项设置样式。 在某些实用程序的帮助下,将标志添加到任何列表组项以显示未读计数,活动等。 借助flexbox实用程序,几乎可以在其中添加所有HTML,甚至可以为下面的链接列表组添加HTML 。 通过JavaScript启用可选项列表项(每个列表项需要单独激活): 您可以通过以下几种方式激活单个列表项: 淡入淡出效果 显示新标签时,事件将按以下顺序触发: 如果尚未激活任何选项卡,则不会触发 Bootstrap媒体对象的文档和示例,用于构建高度重复的组件,例如博客评论,推文等。 媒体对象可以无限嵌套,尽管我们建议您在某个时候停止。嵌套 媒体对象中的媒体可以使用Flexbox实用程序对齐到内容的顶部(默认),中间或结尾 由于媒体对象的结构要求很少,因此您也可以在列表HTML元素上使用这些类。在您 使用Bootstrap的JavaScript模式插件将对话框添加到您的网站,以获取灯箱,用户通知或完全自定义的内容。 下面是一个静态模态示例(表示其 当背景设置为静态时,在其外部单击时,模式不会关闭。单击下面的按钮尝试。 可以创建一个可滚动的模式,允许通过添加滚动模式的身体 可以根据需要将工具提示和弹出窗口放置在模态中。关闭模态后,其中的所有工具提示和弹出窗口也将自动关闭。 通过嵌套 是否有一堆按钮都以相同的内容触发相同的模式?使用 变更动画 删除动画 模态具有三种可选尺寸,可通过放置在上的修饰符类获得 基础导航 使用flexbox实用程序更改导航的水平对齐方式。默认情况下,导航是左对齐的,但是您可以轻松地将它们更改为居中或右对齐。 通过使用 从上面获取基本导航,并添加 强制您 添加带有一些额外HTML和下拉JavaScript插件的下拉菜单。 Bootstrap强大的响应式导航标题导航栏的文档和示例。包括对品牌,导航等的支持,包括对我们的合拢插件的支持。 Navbars内置了对少数子组件的支持。根据需要从以下选项中进行选择: 响应式轻型导航栏中包含的所有子组件的示例,该导航栏在 该 活动状态-带有 可以在导航栏导航中使用下拉菜单。下拉菜单需要用于定位的缠绕元件,所以一定要使用单独的和嵌套的元素 使用将各种表单控件和组件放置在导航栏中 导航栏可能在的帮助下包含一些文本 配色方案 可以将导航栏包裹在 从固定到顶部,固定到底部或固定在顶部进行选择(滚动页面直到到达顶部,然后停留在顶部)。固定的导航栏使用position: 另请注意 导航栏可以利用 对于永不崩溃 导航栏切换器默认情况下是 有时,您想使用折叠插件来触发页面其他位置的隐藏内容。由于我们的插件适用于 用于显示分页以指示一系列相关内容的文档和示例跨越多个页面存在。 通过.form-group
是一些结构添加到形式最简单的方法。它提供了一个灵活的类,它鼓励标签,控件,可选的帮助文本和表单验证消息的正确分组。默认情况下,它仅适用margin-bottom
,但会.form-inline
根据需要选择其他样式。与,
<form>
<div class="form-group">
<label for="formGroupExampleInput">标签示例label>
<input type="text" class="form-control" id="formGroupExampleInput" placeholder="Example input placeholder">
div>
<div class="form-group">
<label for="formGroupExampleInput2">另一个标签示例label>
<input type="text" class="form-control" id="formGroupExampleInput2" placeholder="Another input placeholder">
div>
form>
表格网格
<form>
<div class="row">
<div class="col">
<input type="text" class="form-control" placeholder="First name">
div>
<div class="col">
<input type="text" class="form-control" placeholder="Last name">
div>
div>
form>
表格行
.row
的.form-row
,覆盖缺省列排水沟更严格和更紧凑的布局,我们的标准网格行的变化。<form>
<div class="form-row">
<div class="col">
<input type="text" class="form-control" placeholder="First name">
div>
<div class="col">
<input type="text" class="form-control" placeholder="Last name">
div>
div>
form>
<form>
<div class="form-row">
<div class="form-group col-md-6">
<label for="inputEmail4">邮箱label>
<input type="email" class="form-control" id="inputEmail4">
div>
<div class="form-group col-md-6">
<label for="inputPassword4">密码label>
<input type="password" class="form-control" id="inputPassword4">
div>
div>
<div class="form-group">
<label for="inputAddress">地址label>
<input type="text" class="form-control" id="inputAddress" placeholder="1234 Main St">
div>
<div class="form-group">
<label for="inputAddress2">地址2label>
<input type="text" class="form-control" id="inputAddress2" placeholder="Apartment, studio, or floor">
div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="inputCity">城市label>
<input type="text" class="form-control" id="inputCity">
div>
<div class="form-group col-md-4">
<label for="inputState">区label>
<select id="inputState" class="form-control">
<option selected>选择option>
<option>...option>
select>
div>
<div class="form-group col-md-2">
<label for="inputZip">Ziplabel>
<input type="text" class="form-control" id="inputZip">
div>
div>
<div class="form-group">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="gridCheck">
<label class="form-check-label" for="gridCheck">
选择我
label>
div>
div>
<button type="submit" class="btn btn-primary">登录button>
form>
水平形式
.row
类添加到表单组并使用这些.col-*-*
类指定标签和控件的宽度,可以使用网格创建水平表单。请务必同时添加,.col-form-label
以使它们与相关的表单控件垂直居中。
padding-top
了堆叠式无线电输入标签上的,以更好地对齐文本基线。<form>
<div class="form-group row">
<label for="inputEmail3" class="col-sm-2 col-form-label">Emaillabel>
<div class="col-sm-10">
<input type="email" class="form-control" id="inputEmail3">
div>
div>
<div class="form-group row">
<label for="inputPassword3" class="col-sm-2 col-form-label">Passwordlabel>
<div class="col-sm-10">
<input type="password" class="form-control" id="inputPassword3">
div>
div>
<fieldset class="form-group">
<div class="row">
<legend class="col-form-label col-sm-2 pt-0">Radioslegend>
<div class="col-sm-10">
<div class="form-check">
<input class="form-check-input" type="radio" name="gridRadios" id="gridRadios1" value="option1" checked>
<label class="form-check-label" for="gridRadios1">
First radio
label>
div>
<div class="form-check">
<input class="form-check-input" type="radio" name="gridRadios" id="gridRadios2" value="option2">
<label class="form-check-label" for="gridRadios2">
Second radio
label>
div>
<div class="form-check disabled">
<input class="form-check-input" type="radio" name="gridRadios" id="gridRadios3" value="option3" disabled>
<label class="form-check-label" for="gridRadios3">
Third disabled radio
label>
div>
div>
div>
fieldset>
<div class="form-group row">
<div class="col-sm-2">Checkboxdiv>
<div class="col-sm-10">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="gridCheck1">
<label class="form-check-label" for="gridCheck1">
Example checkbox
label>
div>
div>
div>
<div class="form-group row">
<div class="col-sm-10">
<button type="submit" class="btn btn-primary">Sign inbutton>
div>
div>
form>
水平表格标签大小
.col-form-label-sm
或.col-form-label-lg
您的或
到正确遵循的大小
.form-control-lg
和.form-control-sm
。<form>
<div class="form-group row">
<label for="colFormLabelSm" class="col-sm-2 col-form-label col-form-label-sm">Emaillabel>
<div class="col-sm-10">
<input type="email" class="form-control form-control-sm" id="colFormLabelSm" placeholder="col-form-label-sm">
div>
div>
<div class="form-group row">
<label for="colFormLabel" class="col-sm-2 col-form-label">Emaillabel>
<div class="col-sm-10">
<input type="email" class="form-control" id="colFormLabel" placeholder="col-form-label">
div>
div>
<div class="form-group row">
<label for="colFormLabelLg" class="col-sm-2 col-form-label col-form-label-lg">Emaillabel>
<div class="col-sm-10">
<input type="email" class="form-control form-control-lg" id="colFormLabelLg" placeholder="col-form-label-lg">
div>
div>
form>
列大小
.col
在.row
或内放置任意数量的.form-row
。他们将在它们之间平均分配可用宽度。您还可以选择列的子集来占用更多或更少的空间,而其余的.cols
用其他特定的列类(例如)平均分配其余部分.col-7
。<form>
<div class="form-row">
<div class="col-7">
<input type="text" class="form-control" placeholder="City">
div>
<div class="col">
<input type="text" class="form-control" placeholder="State">
div>
<div class="col">
<input type="text" class="form-control" placeholder="Zip">
div>
div>
form>
自动调整大小
.col
为.col-auto
以便您的列仅占用所需的空间。换句话说,该列将根据内容自行调整大小。<form>
<div class="form-row align-items-center">
<div class="col-auto">
<label class="sr-only" for="inlineFormInput">Namelabel>
<input type="text" class="form-control mb-2" id="inlineFormInput" placeholder="Jane Doe">
div>
<div class="col-auto">
<label class="sr-only" for="inlineFormInputGroup">Usernamelabel>
<div class="input-group mb-2">
<div class="input-group-prepend">
<div class="input-group-text">@div>
div>
<input type="text" class="form-control" id="inlineFormInputGroup" placeholder="Username">
div>
div>
<div class="col-auto">
<div class="form-check mb-2">
<input class="form-check-input" type="checkbox" id="autoSizingCheck">
<label class="form-check-label" for="autoSizingCheck">
Remember me
label>
div>
div>
<div class="col-auto">
<button type="submit" class="btn btn-primary mb-2">Submitbutton>
div>
div>
form>
<form>
<div class="form-row align-items-center">
<div class="col-sm-3 my-1">
<label class="sr-only" for="inlineFormInputName">Namelabel>
<input type="text" class="form-control" id="inlineFormInputName" placeholder="Jane Doe">
div>
<div class="col-sm-3 my-1">
<label class="sr-only" for="inlineFormInputGroupUsername">Usernamelabel>
<div class="input-group">
<div class="input-group-prepend">
<div class="input-group-text">@div>
div>
<input type="text" class="form-control" id="inlineFormInputGroupUsername" placeholder="Username">
div>
div>
<div class="col-auto my-1">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="autoSizingCheck2">
<label class="form-check-label" for="autoSizingCheck2">
Remember me
label>
div>
div>
<div class="col-auto my-1">
<button type="submit" class="btn btn-primary">Submitbutton>
div>
div>
form>
<form>
<div class="form-row align-items-center">
<div class="col-auto my-1">
<label class="mr-sm-2 sr-only" for="inlineFormCustomSelect">Preferencelabel>
<select class="custom-select mr-sm-2" id="inlineFormCustomSelect">
<option selected>Choose...option>
<option value="1">Oneoption>
<option value="2">Twooption>
<option value="3">Threeoption>
select>
div>
<div class="col-auto my-1">
<div class="custom-control custom-checkbox mr-sm-2">
<input type="checkbox" class="custom-control-input" id="customControlAutosizing">
<label class="custom-control-label" for="customControlAutosizing">Remember my preferencelabel>
div>
div>
<div class="col-auto my-1">
<button type="submit" class="btn btn-primary">Submitbutton>
div>
div>
form>
内联表格
.form-inline
该类可在单个水平行上显示一系列标签,表单控件和按钮。内联表单中的表单控件与其默认状态略有不同。
display: flex
,可以折叠任何HTML空格,并允许您提供带有空格和flexbox实用程序的对齐控件。即使您需要使用来向非屏幕阅读者隐藏它,也请确保始终在每个表单控件中都包含一个
.sr-only
。<form class="form-inline">
<label class="sr-only" for="inlineFormInputName2">Namelabel>
<input type="text" class="form-control mb-2 mr-sm-2" id="inlineFormInputName2" placeholder="Jane Doe">
<label class="sr-only" for="inlineFormInputGroupUsername2">Usernamelabel>
<div class="input-group mb-2 mr-sm-2">
<div class="input-group-prepend">
<div class="input-group-text">@div>
div>
<input type="text" class="form-control" id="inlineFormInputGroupUsername2" placeholder="Username">
div>
<div class="form-check mb-2 mr-sm-2">
<input class="form-check-input" type="checkbox" id="inlineFormCheck">
<label class="form-check-label" for="inlineFormCheck">
Remember me
label>
div>
<button type="submit" class="btn btn-primary mb-2">Submitbutton>
form>
帮助文字
.form-text
(以前在v3中称为.help-block
)创建表单中的块级帮助文本。可以使用任何内联HTML元素和实用程序类(如)灵活地实现内联帮助文本.text-muted
。<label for="inputPassword5">密码label>
<input type="password" id="inputPassword5" class="form-control" aria-describedby="passwordHelpBlock">
<small id="passwordHelpBlock" class="form-text text-muted">
您的密码长度必须为8-20个字符,包含字母和数字,并且不得包含空格,特殊字符或表情符号。
small>
嵌入式文本可以使用任何典型的内嵌HTML元素(可以是一个 ,
或别的东西),有什么比一个实用工具类的更多。
<form class="form-inline">
<div class="form-group">
<label for="inputPassword6">密码label>
<input type="password" id="inputPassword6" class="form-control mx-sm-3" aria-describedby="passwordHelpInline">
<small id="passwordHelpInline" class="text-muted">
必须是8-20个字符长。
small>
div>
form>
禁用表格
disabled
在输入上添加布尔属性,以防止用户交互并使它看起来更浅。<input class="form-control" id="disabledInput" type="text" placeholder="Disabled input here..." disabled>
disabled
属性添加到中以禁用其中的所有控件。
<form>
<fieldset disabled>
<div class="form-group">
<label for="disabledTextInput">禁止输入label>
<input type="text" id="disabledTextInput" class="form-control" placeholder="Disabled input">
div>
<div class="form-group">
<label for="disabledSelect">禁止选择菜单label>
<select id="disabledSelect" class="form-control">
<option>禁止选择option>
select>
div>
<div class="form-group">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="disabledFieldsetCheck" disabled>
<label class="form-check-label" for="disabledFieldsetCheck">
无法选中
label>
div>
div>
<button type="submit" class="btn btn-primary">提交button>
fieldset>
form>
验证方式
自定义样式
novalidate
boolean属性添加到。这将禁用浏览器的默认反馈工具提示,但仍提供对JavaScript中的表单验证API的访问。尝试提交以下表格;我们的JavaScript会拦截“提交”按钮,并将反馈转发给您。尝试提交时,您将看到
:invalid
和:valid
样式应用于表单控件。仅可用于
.custom-select
,而不能使用.form-control
。<form class="needs-validation" novalidate>
<div class="form-row">
<div class="col-md-6 mb-3">
<label for="validationCustom01">First namelabel>
<input type="text" class="form-control" id="validationCustom01" value="Mark" required>
<div class="valid-feedback">
Looks good!
div>
div>
<div class="col-md-6 mb-3">
<label for="validationCustom02">Last namelabel>
<input type="text" class="form-control" id="validationCustom02" value="Otto" required>
<div class="valid-feedback">
Looks good!
div>
div>
div>
<div class="form-row">
<div class="col-md-6 mb-3">
<label for="validationCustom03">Citylabel>
<input type="text" class="form-control" id="validationCustom03" required>
<div class="invalid-feedback">
Please provide a valid city.
div>
div>
<div class="col-md-3 mb-3">
<label for="validationCustom04">Statelabel>
<select class="custom-select" id="validationCustom04" required>
<option selected disabled value="">Choose...option>
<option>...option>
select>
<div class="invalid-feedback">
Please select a valid state.
div>
div>
<div class="col-md-3 mb-3">
<label for="validationCustom05">Ziplabel>
<input type="text" class="form-control" id="validationCustom05" required>
<div class="invalid-feedback">
Please provide a valid zip.
div>
div>
div>
<div class="form-group">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="invalidCheck" required>
<label class="form-check-label" for="invalidCheck">
Agree to terms and conditions
label>
<div class="invalid-feedback">
You must agree before submitting.
div>
div>
div>
<button class="btn btn-primary" type="submit">Submit formbutton>
form>
<script>
// Example starter JavaScript for disabling form submissions if there are invalid fields
(function() {
'use strict';
window.addEventListener('load', function() {
// Fetch all the forms we want to apply custom Bootstrap validation styles to
var forms = document.getElementsByClassName('needs-validation');
// Loop over them and prevent submission
var validation = Array.prototype.filter.call(forms, function(form) {
form.addEventListener('submit', function(event) {
if (form.checkValidity() === false) {
event.preventDefault();
event.stopPropagation();
}
form.classList.add('was-validated');
}, false);
});
}, false);
})();
script>
服务器端
我们建议使用客户端验证,但是如果您需要服务器端验证,则可以使用.is-invalid
和指示无效和有效的表单字段.is-valid
。请注意,.invalid-feedback
这些类也支持。aria-describedby
。id如果该字段已经指向其他表单文本,则此属性允许引用多个。<form>
<div class="form-row">
<div class="col-md-6 mb-3">
<label for="validationServer01">First namelabel>
<input type="text" class="form-control is-valid" id="validationServer01" value="Mark" required>
<div class="valid-feedback">
Looks good!
div>
div>
<div class="col-md-6 mb-3">
<label for="validationServer02">Last namelabel>
<input type="text" class="form-control is-valid" id="validationServer02" value="Otto" required>
<div class="valid-feedback">
Looks good!
div>
div>
div>
<div class="form-row">
<div class="col-md-6 mb-3">
<label for="validationServer03">Citylabel>
<input type="text" class="form-control is-invalid" id="validationServer03" aria-describedby="validationServer03Feedback" required>
<div id="validationServer03Feedback" class="invalid-feedback">
Please provide a valid city.
div>
div>
<div class="col-md-3 mb-3">
<label for="validationServer04">Statelabel>
<select class="custom-select is-invalid" id="validationServer04" aria-describedby="validationServer04Feedback" required>
<option selected disabled value="">Choose...option>
<option>...option>
select>
<div id="validationServer04Feedback" class="invalid-feedback">
Please select a valid state.
div>
div>
<div class="col-md-3 mb-3">
<label for="validationServer05">Ziplabel>
<input type="text" class="form-control is-invalid" id="validationServer05" aria-describedby="validationServer05Feedback" required>
<div id="validationServer05Feedback" class="invalid-feedback">
Please provide a valid zip.
div>
div>
div>
<div class="form-group">
<div class="form-check">
<input class="form-check-input is-invalid" type="checkbox" value="" id="invalidCheck3" aria-describedby="invalidCheck3Feedback" required>
<label class="form-check-label" for="invalidCheck3">
Agree to terms and conditions
label>
<div id="invalidCheck3Feedback" class="invalid-feedback">
You must agree before submitting.
div>
div>
div>
<button class="btn btn-primary" type="submit">Submit formbutton>
form>
支持的元素
和
与
.form-control
与
.form-control
或.custom-select
.form-checks
.custom-checkboxs
和.custom-radios
.custom-file
<form class="was-validated">
<div class="mb-3">
<label for="validationTextarea">Textarealabel>
<textarea class="form-control is-invalid" id="validationTextarea" placeholder="Required example textarea" required>textarea>
<div class="invalid-feedback">
Please enter a message in the textarea.
div>
div>
<div class="custom-control custom-checkbox mb-3">
<input type="checkbox" class="custom-control-input" id="customControlValidation1" required>
<label class="custom-control-label" for="customControlValidation1">Check this custom checkboxlabel>
<div class="invalid-feedback">Example invalid feedback textdiv>
div>
<div class="custom-control custom-radio">
<input type="radio" class="custom-control-input" id="customControlValidation2" name="radio-stacked" required>
<label class="custom-control-label" for="customControlValidation2">Toggle this custom radiolabel>
div>
<div class="custom-control custom-radio mb-3">
<input type="radio" class="custom-control-input" id="customControlValidation3" name="radio-stacked" required>
<label class="custom-control-label" for="customControlValidation3">Or toggle this other custom radiolabel>
<div class="invalid-feedback">More example invalid feedback textdiv>
div>
<div class="mb-3">
<select class="custom-select" required>
<option value="">Choose...option>
<option value="1">Oneoption>
<option value="2">Twooption>
<option value="3">Threeoption>
select>
<div class="invalid-feedback">Example invalid custom select feedbackdiv>
div>
<div class="custom-file mb-3">
<input type="file" class="custom-file-input" id="validatedCustomFile" required>
<label class="custom-file-label" for="validatedCustomFile">Choose file...label>
<div class="invalid-feedback">Example invalid custom file feedbackdiv>
div>
<div class="mb-3">
<div class="input-group is-invalid">
<div class="input-group-prepend">
<span class="input-group-text" id="validatedInputGroupPrepend">@span>
div>
<input type="text" class="form-control is-invalid" aria-describedby="validatedInputGroupPrepend" required>
div>
<div class="invalid-feedback">
Example invalid input group feedback
div>
div>
<div class="mb-3">
<div class="input-group is-invalid">
<div class="input-group-prepend">
<label class="input-group-text" for="validatedInputGroupSelect">Optionslabel>
div>
<select class="custom-select" id="validatedInputGroupSelect" required>
<option value="">Choose...option>
<option value="1">Oneoption>
<option value="2">Twooption>
<option value="3">Threeoption>
select>
div>
<div class="invalid-feedback">
Example invalid input group feedback
div>
div>
<div class="input-group is-invalid">
<div class="custom-file">
<input type="file" class="custom-file-input" id="validatedInputGroupCustomFile" required>
<label class="custom-file-label" for="validatedInputGroupCustomFile">Choose file...label>
div>
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="button">Buttonbutton>
div>
div>
<div class="invalid-feedback">
Example invalid input group feedback
div>
form>
工具提示
.{valid|invalid}-feedback
类交换为.{valid|invalid}-tooltip
类,以在样式化的工具提示中显示验证反馈。确保position: relative
在其上放置父项以进行工具提示定位。在下面的示例中,我们的列类已经具有此功能,但是您的项目可能需要替代设置。<form class="needs-validation" novalidate>
<div class="form-row">
<div class="col-md-6 mb-3">
<label for="validationTooltip01">First namelabel>
<input type="text" class="form-control" id="validationTooltip01" value="Mark" required>
<div class="valid-tooltip">
Looks good!
div>
div>
<div class="col-md-6 mb-3">
<label for="validationTooltip02">Last namelabel>
<input type="text" class="form-control" id="validationTooltip02" value="Otto" required>
<div class="valid-tooltip">
Looks good!
div>
div>
div>
<div class="form-row">
<div class="col-md-6 mb-3">
<label for="validationTooltip03">Citylabel>
<input type="text" class="form-control" id="validationTooltip03" required>
<div class="invalid-tooltip">
Please provide a valid city.
div>
div>
<div class="col-md-3 mb-3">
<label for="validationTooltip04">Statelabel>
<select class="custom-select" id="validationTooltip04" required>
<option selected disabled value="">Choose...option>
<option>...option>
select>
<div class="invalid-tooltip">
Please select a valid state.
div>
div>
<div class="col-md-3 mb-3">
<label for="validationTooltip05">Ziplabel>
<input type="text" class="form-control" id="validationTooltip05" required>
<div class="invalid-tooltip">
Please provide a valid zip.
div>
div>
div>
<button class="btn btn-primary" type="submit">Submit formbutton>
form>
开关
.custom-switch
该类来呈现切换开关。开关也支持该disabled
属性。<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" id="customSwitch1">
<label class="custom-control-label" for="customSwitch1">Toggle this switch elementlabel>
div>
<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" disabled id="customSwitch2">
<label class="custom-control-label" for="customSwitch2">Disabled switch elementlabel>
div>
选择菜单
菜单仅需要一个自定义类
.custom-select
即可触发自定义样式。自定义样式仅限于的初始外观,
由于浏览器的限制,无法修改。
<select class="custom-select">
<option selected>打开此选择菜单option>
<option value="1">Oneoption>
<option value="2">Twooption>
<option value="3">Threeoption>
select>
<select class="custom-select custom-select-lg mb-3">
<option selected>Open this select menuoption>
<option value="1">Oneoption>
<option value="2">Twooption>
<option value="3">Threeoption>
select>
<select class="custom-select custom-select-sm">
<option selected>Open this select menuoption>
<option value="1">Oneoption>
<option value="2">Twooption>
<option value="3">Threeoption>
select>
<select class="custom-select" multiple>
<option selected>Open this select menuoption>
<option value="1">Oneoption>
<option value="2">Twooption>
<option value="3">Threeoption>
select>
<select class="custom-select" size="3">
<option selected>Open this select menuoption>
<option value="1">Oneoption>
<option value="2">Twooption>
<option value="3">Threeoption>
select>
范围
与控制
.custom-range
。轨道(背景)和拇指(值)的样式都在浏览器中显示为相同。由于只有IE
和Firefox
支持从拇指的左侧或右侧“填充”其轨迹,以可视方式指示进度,因此我们目前不支持它。文件浏览器
bs-custom-file-input
<div class="custom-file">
<input type="file" class="custom-file-input" id="customFile">
<label class="custom-file-label" for="customFile">选择文件label>
div>
输入组
将一个附加组件或按钮放在输入的两侧。您也可以在输入的两侧放置一个。记住将放在输入组之外。
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1">@span>
div>
<input type="text" class="form-control" placeholder="Username" aria-label="Username" aria-describedby="basic-addon1">
div>
<div class="input-group mb-3">
<input type="text" class="form-control" placeholder="Recipient's username" aria-label="Recipient's username" aria-describedby="basic-addon2">
<div class="input-group-append">
<span class="input-group-text" id="basic-addon2">@example.comspan>
div>
div>
<label for="basic-url">Your vanity URLlabel>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon3">https://example.com/users/span>
div>
<input type="text" class="form-control" id="basic-url" aria-describedby="basic-addon3">
div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text">$span>
div>
<input type="text" class="form-control" aria-label="Amount (to the nearest dollar)">
<div class="input-group-append">
<span class="input-group-text">.00span>
div>
div>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text">With textareaspan>
div>
<textarea class="form-control" aria-label="With textarea">textarea>
div>
默认情况下,输入组会通过包装,flex-wrap: wrap
以适应输入组中的自定义表单字段验证。您可以使用禁用此功能.flex-nowrap
。<div class="input-group flex-nowrap">
<div class="input-group-prepend">
<span class="input-group-text" id="addon-wrapping">@span>
div>
<input type="text" class="form-control" placeholder="Username" aria-label="Username" aria-describedby="addon-wrapping">
div>
<div class="input-group input-group-sm mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="inputGroup-sizing-sm">Smallspan>
div>
<input type="text" class="form-control" aria-label="Sizing example input" aria-describedby="inputGroup-sizing-sm">
div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="inputGroup-sizing-default">Defaultspan>
div>
<input type="text" class="form-control" aria-label="Sizing example input" aria-describedby="inputGroup-sizing-default">
div>
<div class="input-group input-group-lg">
<div class="input-group-prepend">
<span class="input-group-text" id="inputGroup-sizing-lg">Largespan>
div>
<input type="text" class="form-control" aria-label="Sizing example input" aria-describedby="inputGroup-sizing-lg">
div>
复选框和单选框
<div class="input-group mb-3">
<div class="input-group-prepend">
<div class="input-group-text">
<input type="checkbox" aria-label="Checkbox for following text input">
div>
div>
<input type="text" class="form-control" aria-label="Text input with checkbox">
div>
<div class="input-group">
<div class="input-group-prepend">
<div class="input-group-text">
<input type="radio" aria-label="Radio button for following text input">
div>
div>
<input type="text" class="form-control" aria-label="Text input with radio button">
div>
多个插件
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text">$span>
<span class="input-group-text">0.00span>
div>
<input type="text" class="form-control" aria-label="Dollar amount (with dot and two decimal places)">
div>
<div class="input-group">
<input type="text" class="form-control" aria-label="Dollar amount (with dot and two decimal places)">
<div class="input-group-append">
<span class="input-group-text">$span>
<span class="input-group-text">0.00span>
div>
div>
按钮
<div class="input-group mb-3">
<div class="input-group-prepend">
<button class="btn btn-outline-secondary" type="button" id="button-addon1">Buttonbutton>
div>
<input type="text" class="form-control" placeholder="" aria-label="Example text with button addon" aria-describedby="button-addon1">
div>
<div class="input-group mb-3">
<input type="text" class="form-control" placeholder="Recipient's username" aria-label="Recipient's username" aria-describedby="button-addon2">
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="button" id="button-addon2">Buttonbutton>
div>
div>
<div class="input-group mb-3">
<div class="input-group-prepend" id="button-addon3">
<button class="btn btn-outline-secondary" type="button">Buttonbutton>
<button class="btn btn-outline-secondary" type="button">Buttonbutton>
div>
<input type="text" class="form-control" placeholder="" aria-label="Example text with two button addons" aria-describedby="button-addon3">
div>
<div class="input-group">
<input type="text" class="form-control" placeholder="Recipient's username" aria-label="Recipient's username with two button addons" aria-describedby="button-addon4">
<div class="input-group-append" id="button-addon4">
<button class="btn btn-outline-secondary" type="button">Buttonbutton>
<button class="btn btn-outline-secondary" type="button">Buttonbutton>
div>
div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<button class="btn btn-outline-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdownbutton>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">Actiona>
<a class="dropdown-item" href="#">Another actiona>
<a class="dropdown-item" href="#">Something else herea>
<div role="separator" class="dropdown-divider">div>
<a class="dropdown-item" href="#">Separated linka>
div>
div>
<input type="text" class="form-control" aria-label="Text input with dropdown button">
div>
<div class="input-group">
<input type="text" class="form-control" aria-label="Text input with dropdown button">
<div class="input-group-append">
<button class="btn btn-outline-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdownbutton>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">Actiona>
<a class="dropdown-item" href="#">Another actiona>
<a class="dropdown-item" href="#">Something else herea>
<div role="separator" class="dropdown-divider">div>
<a class="dropdown-item" href="#">Separated linka>
div>
div>
div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<button type="button" class="btn btn-outline-secondary">Actionbutton>
<button type="button" class="btn btn-outline-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="sr-only">Toggle Dropdownspan>
button>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">Actiona>
<a class="dropdown-item" href="#">Another actiona>
<a class="dropdown-item" href="#">Something else herea>
<div role="separator" class="dropdown-divider">div>
<a class="dropdown-item" href="#">Separated linka>
div>
div>
<input type="text" class="form-control" aria-label="Text input with segmented dropdown button">
div>
<div class="input-group">
<input type="text" class="form-control" aria-label="Text input with segmented dropdown button">
<div class="input-group-append">
<button type="button" class="btn btn-outline-secondary">Actionbutton>
<button type="button" class="btn btn-outline-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="sr-only">Toggle Dropdownspan>
button>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">Actiona>
<a class="dropdown-item" href="#">Another actiona>
<a class="dropdown-item" href="#">Something else herea>
<div role="separator" class="dropdown-divider">div>
<a class="dropdown-item" href="#">Separated linka>
div>
div>
div>
自订表格
输入组包括对自定义选择和自定义文件输入的支持。不支持这些浏览器的默认版本。<div class="input-group mb-3">
<div class="input-group-prepend">
<label class="input-group-text" for="inputGroupSelect01">Optionslabel>
div>
<select class="custom-select" id="inputGroupSelect01">
<option selected>Choose...option>
<option value="1">Oneoption>
<option value="2">Twooption>
<option value="3">Threeoption>
select>
div>
<div class="input-group mb-3">
<select class="custom-select" id="inputGroupSelect02">
<option selected>Choose...option>
<option value="1">Oneoption>
<option value="2">Twooption>
<option value="3">Threeoption>
select>
<div class="input-group-append">
<label class="input-group-text" for="inputGroupSelect02">Optionslabel>
div>
div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<button class="btn btn-outline-secondary" type="button">Buttonbutton>
div>
<select class="custom-select" id="inputGroupSelect03" aria-label="Example select with button addon">
<option selected>Choose...option>
<option value="1">Oneoption>
<option value="2">Twooption>
<option value="3">Threeoption>
select>
div>
<div class="input-group">
<select class="custom-select" id="inputGroupSelect04" aria-label="Example select with button addon">
<option selected>Choose...option>
<option value="1">Oneoption>
<option value="2">Twooption>
<option value="3">Threeoption>
select>
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="button">Buttonbutton>
div>
div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="inputGroupFileAddon01">Uploadspan>
div>
<div class="custom-file">
<input type="file" class="custom-file-input" id="inputGroupFile01" aria-describedby="inputGroupFileAddon01">
<label class="custom-file-label" for="inputGroupFile01">Choose filelabel>
div>
div>
<div class="input-group mb-3">
<div class="custom-file">
<input type="file" class="custom-file-input" id="inputGroupFile02">
<label class="custom-file-label" for="inputGroupFile02" aria-describedby="inputGroupFileAddon02">Choose filelabel>
div>
<div class="input-group-append">
<span class="input-group-text" id="inputGroupFileAddon02">Uploadspan>
div>
div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<button class="btn btn-outline-secondary" type="button" id="inputGroupFileAddon03">Buttonbutton>
div>
<div class="custom-file">
<input type="file" class="custom-file-input" id="inputGroupFile03" aria-describedby="inputGroupFileAddon03">
<label class="custom-file-label" for="inputGroupFile03">Choose filelabel>
div>
div>
<div class="input-group">
<div class="custom-file">
<input type="file" class="custom-file-input" id="inputGroupFile04" aria-describedby="inputGroupFileAddon04">
<label class="custom-file-label" for="inputGroupFile04">Choose filelabel>
div>
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="button" id="inputGroupFileAddon04">Buttonbutton>
div>
div>
大屏幕
<div class="jumbotron">
<h1 class="display-4">你好,世界!h1>
<p class="lead">这是一个简单的英雄单元,是一个简单的巨无霸风格的组件,可以引起人们对特色内容或信息的更多关注。p>
<hr class="my-4">
<p>它使用实用程序类来进行排版和间距,以在较大的容器内分隔内容。
p>
<a class="btn btn-primary btn-lg" href="#" role="button">了解更多a>
div>
要使超长距伞成为全角且没有圆角,请添加.jumbotron-fluid
修饰符类并在其中添加.container或.container-fluid
。<div class="jumbotron jumbotron-fluid">
<div class="container">
<h1 class="display-4">Fluid jumbotronh1>
<p class="lead">This is a modified jumbotron that occupies the entire horizontal space of its parent.p>
div>
div>
列表组
<ul class="list-group">
<li class="list-group-item">Cras justo odioli>
<li class="list-group-item">Dapibus ac facilisis inli>
<li class="list-group-item">Morbi leo risusli>
<li class="list-group-item">Porta ac consectetur acli>
<li class="list-group-item">Vestibulum at erosli>
ul>
添加.active
到.list-group-item
以指示当前的活动选择。<ul class="list-group">
<li class="list-group-item active">Cras justo odioli>
<li class="list-group-item">Dapibus ac facilisis inli>
<li class="list-group-item">Morbi leo risusli>
<li class="list-group-item">Porta ac consectetur acli>
<li class="list-group-item">Vestibulum at erosli>
ul>
添加.disabled到.list-group-item
使其显示为禁用状态。请注意,带有的某些元素.disabled
还需要自定义JavaScript才能完全禁用其点击事件(例如,链接)。链接和按钮
或,通过添加
来创建具有悬浮,禁用和活动状态的可操作列表组项目
.list-group-item-action
。<div class="list-group">
<a href="#" class="list-group-item list-group-item-action active">
Cras justo odio
a>
<a href="#" class="list-group-item list-group-item-action">Dapibus ac facilisis ina>
<a href="#" class="list-group-item list-group-item-action">Morbi leo risusa>
<a href="#" class="list-group-item list-group-item-action">Porta ac consectetur aca>
<a href="#" class="list-group-item list-group-item-action disabled" tabindex="-1" aria-disabled="true">Vestibulum at erosa>
div>
.list-group-flush
以删除某些边框和圆角,以在父容器(例如卡片)中边对边呈现列表组项。<ul class="list-group list-group-flush">
<li class="list-group-item">Cras justo odioli>
<li class="list-group-item">Dapibus ac facilisis inli>
<li class="list-group-item">Morbi leo risusli>
<li class="list-group-item">Porta ac consectetur acli>
<li class="list-group-item">Vestibulum at erosli>
ul>
添加.list-group-horizontal
以将所有断点的列表组项目的布局从垂直更改为水平。或者,选择一个响应变体.list-group-horizontal-{sm|md|lg|xl}
以使列表组从该断点开始为水平min-width
。当前水平列表组不能与刷新列表组结合。<ul class="list-group list-group-horizontal">
<li class="list-group-item">Cras justo odioli>
<li class="list-group-item">Dapibus ac facilisis inli>
<li class="list-group-item"/>Morbi leo risusli>
ul>
<ul class="list-group list-group-horizontal-sm">
<li class="list-group-item">Cras justo odioli>
<li class="list-group-item">Dapibus ac facilisis inli>
<li class="list-group-item">Morbi leo risusli>
ul>
<ul class="list-group list-group-horizontal-md">
<li class="list-group-item">Cras justo odioli>
<li class="list-group-item">Dapibus ac facilisis inli>
<li class="list-group-item">Morbi leo risusli>
ul>
<ul class="list-group list-group-horizontal-lg">
<li class="list-group-item">Cras justo odioli>
<li class="list-group-item">Dapibus ac facilisis inli>
<li class="list-group-item">Morbi leo risusli>
ul>
<ul class="list-group list-group-horizontal-xl">
<li class="list-group-item">Cras justo odioli>
<li class="list-group-item">Dapibus ac facilisis inli>
<li class="list-group-item">Morbi leo risusli>
ul>
上下文类
<ul class="list-group">
<li class="list-group-item">Dapibus ac facilisis inli>
<li class="list-group-item list-group-item-primary">A simple primary list group itemli>
<li class="list-group-item list-group-item-secondary">A simple secondary list group itemli>
<li class="list-group-item list-group-item-success">A simple success list group itemli>
<li class="list-group-item list-group-item-danger">A simple danger list group itemli>
<li class="list-group-item list-group-item-warning">A simple warning list group itemli>
<li class="list-group-item list-group-item-info">A simple info list group itemli>
<li class="list-group-item list-group-item-light">A simple light list group itemli>
<li class="list-group-item list-group-item-dark">A simple dark list group itemli>
ul>
带徽章
<ul class="list-group">
<li class="list-group-item d-flex justify-content-between align-items-center">
Cras justo odio
<span class="badge badge-primary badge-pill">14span>
li>
<li class="list-group-item d-flex justify-content-between align-items-center">
Dapibus ac facilisis in
<span class="badge badge-primary badge-pill">2span>
li>
<li class="list-group-item d-flex justify-content-between align-items-center">
Morbi leo risus
<span class="badge badge-primary badge-pill">1span>
li>
ul>
自订内容
<div class="list-group">
<a href="#" class="list-group-item list-group-item-action active">
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-1">List group item headingh5>
<small>3 days agosmall>
div>
<p class="mb-1">Donec id elit non mi porta gravida at eget metus. Maecenas sed diam eget risus varius blandit.p>
<small>Donec id elit non mi porta.small>
a>
<a href="#" class="list-group-item list-group-item-action">
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-1">List group item headingh5>
<small class="text-muted">3 days agosmall>
div>
<p class="mb-1">Donec id elit non mi porta gravida at eget metus. Maecenas sed diam eget risus varius blandit.p>
<small class="text-muted">Donec id elit non mi porta.small>
a>
<a href="#" class="list-group-item list-group-item-action">
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-1">List group item headingh5>
<small class="text-muted">3 days agosmall>
div>
<p class="mb-1">Donec id elit non mi porta gravida at eget metus. Maecenas sed diam eget risus varius blandit.p>
<small class="text-muted">Donec id elit non mi porta.small>
a>
div>
使用选项卡JavaScript插件(单独包含它或通过编译的bootstrap.js
文件包含它)来扩展列表组,以创建本地内容的可选项窗格。<div class="row">
<div class="col-4">
<div class="list-group" id="list-tab" role="tablist">
<a class="list-group-item list-group-item-action active" id="list-home-list" data-toggle="list" href="#list-home" role="tab" aria-controls="home">Homea>
<a class="list-group-item list-group-item-action" id="list-profile-list" data-toggle="list" href="#list-profile" role="tab" aria-controls="profile">Profilea>
<a class="list-group-item list-group-item-action" id="list-messages-list" data-toggle="list" href="#list-messages" role="tab" aria-controls="messages">Messagesa>
<a class="list-group-item list-group-item-action" id="list-settings-list" data-toggle="list" href="#list-settings" role="tab" aria-controls="settings">Settingsa>
div>
div>
<div class="col-8">
<div class="tab-content" id="nav-tabContent">
<div class="tab-pane fade show active" id="list-home" role="tabpanel" aria-labelledby="list-home-list">...div>
<div class="tab-pane fade" id="list-profile" role="tabpanel" aria-labelledby="list-profile-list">...div>
<div class="tab-pane fade" id="list-messages" role="tabpanel" aria-labelledby="list-messages-list">...div>
<div class="tab-pane fade" id="list-settings" role="tabpanel" aria-labelledby="list-settings-list">...div>
div>
div>
div>
只需指定data-toggle="list"
或在元素上即可激活列表组导航而无需编写任何JavaScript 。在上使用这些数据属性.list-group-item
。
<div class="list-group" id="myList" role="tablist">
<a class="list-group-item list-group-item-action active" data-toggle="list" href="#home" role="tab">Homea>
<a class="list-group-item list-group-item-action" data-toggle="list" href="#profile" role="tab">Profilea>
<a class="list-group-item list-group-item-action" data-toggle="list" href="#messages" role="tab">Messagesa>
<a class="list-group-item list-group-item-action" data-toggle="list" href="#settings" role="tab">Settingsa>
div>
<div class="tab-content">
<div class="tab-pane active" id="home" role="tabpanel">...div>
<div class="tab-pane" id="profile" role="tabpanel">...div>
<div class="tab-pane" id="messages" role="tabpanel">...div>
<div class="tab-pane" id="settings" role="tabpanel">...div>
div>
$('#myList a').on('click', function (e) {
e.preventDefault()
$(this).tab('show')
})
$('#myList a[href="#profile"]').tab('show') // Select tab by name
$('#myList a:first-child').tab('show') // Select first tab
$('#myList a:last-child').tab('show') // Select last tab
$('#myList a:nth-child(3)').tab('show') // Select third tab
要使选项卡面板淡入,请添加.fade
到每个.tab-pane
。第一个选项卡窗格还必须.show
使初始内容可见。<div class="tab-content">
<div class="tab-pane fade show active" id="home" role="tabpanel">...div>
<div class="tab-pane fade" id="profile" role="tabpanel">...div>
<div class="tab-pane fade" id="messages" role="tabpanel">...div>
<div class="tab-pane fade" id="settings" role="tabpanel">...div>
div>
方法
$().tab
激活列表项元素和内容容器。Tab应该在DOM中具有data-target或href定位容器节点。<div class="list-group" id="myList" role="tablist">
<a class="list-group-item list-group-item-action active" data-toggle="list" href="#home" role="tab">Homea>
<a class="list-group-item list-group-item-action" data-toggle="list" href="#profile" role="tab">Profilea>
<a class="list-group-item list-group-item-action" data-toggle="list" href="#messages" role="tab">Messagesa>
<a class="list-group-item list-group-item-action" data-toggle="list" href="#settings" role="tab">Settingsa>
div>
<div class="tab-content">
<div class="tab-pane active" id="home" role="tabpanel">...div>
<div class="tab-pane" id="profile" role="tabpanel">...div>
<div class="tab-pane" id="messages" role="tabpanel">...div>
<div class="tab-pane" id="settings" role="tabpanel">...div>
div>
<script>
$(function () {
$('#myList a:last-child').tab('show')
})
script>
.tab(‘show’)
选择给定的列表项并显示其关联的窗格。先前选择的任何其他列表项将变为未选中状态,并且其关联的窗格被隐藏。在实际显示选项卡窗格之前(例如,在shown.bs.tab
事件发生之前)返回到调用者。$('#someListItem').tab('show')
事记
hide.bs.tab
(在当前活动标签上)show.bs.tab
(在待显示的标签上)hidden.bs.tab
(在上一个活动选项卡上,与活动相同hide.bs.tab)shown.bs.tab
(在刚刚显示的新活动标签上,该标签与该show.bs.tab
活动的标签相同)hide.bs.tab
和hidden.bs.tab
事件。$('a[data-toggle="list"]').on('shown.bs.tab', function (e) {
e.target // newly activated tab
e.relatedTarget // previous active tab
})
媒体对象
<div class="media">
<img src="..." class="mr-3" alt="...">
<div class="media-body">
<h5 class="mt-0">Media headingh5>
Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
div>
div>
嵌套
.media
在.media-body
父媒体对象的中。<div class="media">
<img src="..." class="mr-3" alt="...">
<div class="media-body">
<h5 class="mt-0">Media headingh5>
Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
<div class="media mt-3">
<a class="mr-3" href="#">
<img src="..." class="mr-3" alt="...">
a>
<div class="media-body">
<h5 class="mt-0">Media headingh5>
Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
div>
div>
div>
div>
对齐
.media-body
。
顶对齐<div class="media">
<img src="..." class="align-self-start mr-3" alt="...">
<div class="media-body">
<h5 class="mt-0">Top-aligned mediah5>
<p>Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.p>
<p>Donec sed odio dui. Nullam quis risus eget urna mollis ornare vel eu leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.p>
div>
div>
<div class="media">
<img src="..." class="align-self-center mr-3" alt="...">
<div class="media-body">
<h5 class="mt-0">Center-aligned mediah5>
<p>Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.p>
<p class="mb-0">Donec sed odio dui. Nullam quis risus eget urna mollis ornare vel eu leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.p>
div>
div>
<div class="media">
<img src="..." class="align-self-end mr-3" alt="...">
<div class="media-body">
<h5 class="mt-0">Bottom-aligned mediah5>
<p>Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.p>
<p class="mb-0">Donec sed odio dui. Nullam quis risus eget urna mollis ornare vel eu leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.p>
div>
div>
调整
通过修改HTML本身或添加一些自定义flexbox CSS来设置order属性(更改为您选择的整数),可以更改媒体对象中内容的顺序。<div class="media">
<div class="media-body">
<h5 class="mt-0 mb-1">Media objecth5>
Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
div>
<img src="..." class="ml-3" alt="...">
div>
媒体列表
或者
,添加.list-unstyled
删除任何浏览器的默认列表样式,然后应用.media
到你的秒。与往常一样,在需要微调的地方都使用间距实用程序。
<ul class="list-unstyled">
<li class="media">
<img src="..." class="mr-3" alt="...">
<div class="media-body">
<h5 class="mt-0 mb-1">List-based media objecth5>
Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
div>
li>
<li class="media my-4">
<img src="..." class="mr-3" alt="...">
<div class="media-body">
<h5 class="mt-0 mb-1">List-based media objecth5>
Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
div>
li>
<li class="media">
<img src="..." class="mr-3" alt="...">
<div class="media-body">
<h5 class="mt-0 mb-1">List-based media objecth5>
Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
div>
li>
ul>
模态框
原理:
$('#myModal').on('shown.bs.modal', function () {
$('#myInput').trigger('focus')
})
模态组件
position
和display
已被覆盖)。包括模态页眉,模态主体(必需padding
)和模态页脚(可选)。我们要求您在可能的情况下在模式标头中包含dismiss
操作,或提供另一个显式的dismiss
操作。<div class="modal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal titleh5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×span>
button>
div>
<div class="modal-body">
<p>Modal body text goes here.p>
div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Closebutton>
<button type="button" class="btn btn-primary">Save changesbutton>
div>
div>
div>
div>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
Launch demo modal
button>
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal titleh5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×span>
button>
div>
<div class="modal-body">
...
div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Closebutton>
<button type="button" class="btn btn-primary">Save changesbutton>
div>
div>
div>
div>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#staticBackdrop">
Launch static backdrop modal
button>
<div class="modal fade" id="staticBackdrop" data-backdrop="static" data-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="staticBackdropLabel">Modal titleh5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×span>
button>
div>
<div class="modal-body">
...
div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Closebutton>
<button type="button" class="btn btn-primary">Understoodbutton>
div>
div>
div>
div>
滚动长内容
.modal-dialog-scrollable
到.modal-dialog
<div class="modal-dialog modal-dialog-scrollable">
...
div>
垂直居中
将模态添加.modal-dialog-centered
到.modal-dialog
垂直居中。
<div class="modal-dialog modal-dialog-centered">
...
div>
<div class="modal-dialog modal-dialog-centered modal-dialog-scrollable">
...
div>
工具提示和弹出窗口
<div class="modal-body">
<h5>Popover in a modalh5>
<p>This <a href="#" role="button" class="btn btn-secondary popover-test" title="Popover title" data-content="Popover body content is set in this attribute.">buttona> triggers a popover on click.p>
<hr>
<h5>Tooltips in a modalh5>
<p><a href="#" class="tooltip-test" title="Tooltip">This linka> and <a href="#" class="tooltip-test" title="Tooltip">that linka> have tooltips on hover.p>
div>
使用网格
.container-fluid
在模态中利用Bootstrap网格系统.modal-body
。然后,像在其他任何地方一样使用普通的网格系统类。<div class="modal-body">
<div class="container-fluid">
<div class="row">
<div class="col-md-4">.col-md-4div>
<div class="col-md-4 ml-auto">.col-md-4 .ml-autodiv>
div>
<div class="row">
<div class="col-md-3 ml-auto">.col-md-3 .ml-autodiv>
<div class="col-md-2 ml-auto">.col-md-2 .ml-autodiv>
div>
<div class="row">
<div class="col-md-6 ml-auto">.col-md-6 .ml-autodiv>
div>
<div class="row">
<div class="col-sm-9">
Level 1: .col-sm-9
<div class="row">
<div class="col-8 col-sm-6">
Level 2: .col-8 .col-sm-6
div>
<div class="col-4 col-sm-6">
Level 2: .col-4 .col-sm-6
div>
div>
div>
div>
div>
div>
各种模态内容
event.relatedTarget
和HTML data-*
属性(可能通过jQuery)根据所单击的按钮来更改模式的内容。<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo">Open modal for @mdobutton>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@fat">Open modal for @fatbutton>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@getbootstrap">Open modal for @getbootstrapbutton>
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">New messageh5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×span>
button>
div>
<div class="modal-body">
<form>
<div class="form-group">
<label for="recipient-name" class="col-form-label">Recipient:label>
<input type="text" class="form-control" id="recipient-name">
div>
<div class="form-group">
<label for="message-text" class="col-form-label">Message:label>
<textarea class="form-control" id="message-text">textarea>
div>
form>
div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Closebutton>
<button type="button" class="btn btn-primary">Send messagebutton>
div>
div>
div>
div>
$('#exampleModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget) // Button that triggered the modal
var recipient = button.data('whatever') // Extract info from data-* attributes
// If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
// Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
var modal = $(this)
modal.find('.modal-title').text('New message to ' + recipient)
modal.find('.modal-body input').val(recipient)
})
该$modal-fade-transform
变量确定.modal-dialog
模态淡入动画之前的变换状态,该$modal-show-transform
变量确定.modal-dialog
模态淡入动画结束时的变换。
例如,如果要放大动画,可以设置$modal-fade-transform: scale(.8)
。
对于仅显示而不是淡入显示的模态,请.fade
从模态标记中删除该类。<div class="modal" tabindex="-1" aria-labelledby="..." aria-hidden="true">
...
div>
可选尺寸
.modal-dialog
。这些大小会在某些断点处插入,以避免在较窄的视口上出现水平滚动条。
<div class="modal-dialog modal-xl">...div>
<div class="modal-dialog modal-lg">...div>
<div class="modal-dialog modal-sm">...div>
方法
.modal(options)
将您的内容激活为模式。接受可选选项object。$('#myModal').modal({
keyboard: false
})
.modal('toggle')
手动切换模态。在实际显示或隐藏模式之前(即在shown.bs.modalor hidden.bs.modal事件发生之前)返回调用方。$('#myModal').modal('toggle')
.modal('show')
手动打开模式。在实际显示模式之前(即shown.bs.modal事件发生之前)返回调用方。$('#myModal').modal('show')
.modal('hide')
手动隐藏模式。在实际隐藏模式之前(即hidden.bs.modal事件发生之前)返回调用方。$('#myModal').modal('hide')
.modal('handleUpdate')
如果模态的高度在打开时发生变化,则手动重新调整模态的位置(即,如果出现滚动条)。$('#myModal').modal('handleUpdate')
.modal('dispose')
销毁元素的模态。导航
Bootstrap中可用的导航共享从基.nav
类到活动和禁用状态的常规标记和样式。交换修饰符类可在每种样式之间切换。<ul class="nav">
<li class="nav-item">
<a class="nav-link active" href="#">Activea>
li>
<li class="nav-item">
<a class="nav-link" href="#">Linka>
li>
<li class="nav-item">
<a class="nav-link" href="#">Linka>
li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disableda>
li>
ul>
<nav class="nav">
<a class="nav-link active" href="#">Activea>
<a class="nav-link" href="#">Linka>
<a class="nav-link" href="#">Linka>
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disableda>
nav>
水平对齐
.justify-content-center
:<ul class="nav justify-content-center">
<li class="nav-item">
<a class="nav-link active" href="#">Activea>
li>
<li class="nav-item">
<a class="nav-link" href="#">Linka>
li>
<li class="nav-item">
<a class="nav-link" href="#">Linka>
li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disableda>
li>
ul>
.justify-content-end
:<ul class="nav justify-content-end">
<li class="nav-item">
<a class="nav-link active" href="#">Activea>
li>
<li class="nav-item">
<a class="nav-link" href="#">Linka>
li>
<li class="nav-item">
<a class="nav-link" href="#">Linka>
li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disableda>
li>
ul>
垂直
.flex-column
实用程序更改弹性项目方向来堆叠导航。<ul class="nav flex-column">
<li class="nav-item">
<a class="nav-link active" href="#">Activea>
li>
<li class="nav-item">
<a class="nav-link" href="#">Linka>
li>
<li class="nav-item">
<a class="nav-link" href="#">Linka>
li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disableda>
li>
ul>
<nav class="nav flex-column">
<a class="nav-link active" href="#">Activea>
<a class="nav-link" href="#">Linka>
<a class="nav-link" href="#">Linka>
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disableda>
nav>
标签
.nav-tabs
类以生成选项卡式界面。使用它们通过我们的标签JavaScript插件创建可标签区域。<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#">Activea>
li>
<li class="nav-item">
<a class="nav-link" href="#">Linka>
li>
<li class="nav-item">
<a class="nav-link" href="#">Linka>
li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disableda>
li>
ul>
胶囊
使用相同的HTML,.nav-tabs
改为.nav-pills
使用:<ul class="nav nav-pills">
<li class="nav-item">
<a class="nav-link active" href="#">Activea>
li>
<li class="nav-item">
<a class="nav-link" href="#">Linka>
li>
<li class="nav-item">
<a class="nav-link" href="#">Linka>
li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disableda>
li>
ul>
填写并证明
.nav
的内容扩展两个修饰符类之一的全部可用宽度。要用.nav-items
按比例填充所有可用空间,请使用.nav-fill
。请注意,所有水平空间都被占用,但并非每个导航项都具有相同的宽度。<ul class="nav nav-pills nav-fill">
<li class="nav-item">
<a class="nav-link active" href="#">Activea>
li>
<li class="nav-item">
<a class="nav-link" href="#">Much longer nav linka>
li>
<li class="nav-item">
<a class="nav-link" href="#">Linka>
li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disableda>
li>
ul>
使用基于导航的导航时,可以安全地省略
.nav-item
,仅对.nav-link
样式元素进行必要。
<nav class="nav nav-pills nav-fill">
<a class="nav-link active" href="#">Activea>
<a class="nav-link" href="#">Much longer nav linka>
<a class="nav-link" href="#">Linka>
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disableda>
nav>
使用下拉菜单
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#">Activea>
li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">Dropdowna>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">Actiona>
<a class="dropdown-item" href="#">Another actiona>
<a class="dropdown-item" href="#">Something else herea>
<div class="dropdown-divider">div>
<a class="dropdown-item" href="#">Separated linka>
div>
li>
<li class="nav-item">
<a class="nav-link" href="#">Linka>
li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disableda>
li>
ul>
//胶囊下拉菜单
<ul class="nav nav-pills">
<li class="nav-item">
<a class="nav-link active" href="#">Activea>
li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">Dropdowna>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">Actiona>
<a class="dropdown-item" href="#">Another actiona>
<a class="dropdown-item" href="#">Something else herea>
<div class="dropdown-divider">div>
<a class="dropdown-item" href="#">Separated linka>
div>
li>
<li class="nav-item">
<a class="nav-link" href="#">Linka>
li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disableda>
li>
ul>
<ul class="nav nav-tabs" id="myTab" role="tablist">
<li class="nav-item" role="presentation">
<a class="nav-link active" id="home-tab" data-toggle="tab" href="#home" role="tab" aria-controls="home" aria-selected="true">Homea>
li>
<li class="nav-item" role="presentation">
<a class="nav-link" id="profile-tab" data-toggle="tab" href="#profile" role="tab" aria-controls="profile" aria-selected="false">Profilea>
li>
<li class="nav-item" role="presentation">
<a class="nav-link" id="contact-tab" data-toggle="tab" href="#contact" role="tab" aria-controls="contact" aria-selected="false">Contacta>
li>
ul>
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">...div>
<div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab">...div>
<div class="tab-pane fade" id="contact" role="tabpanel" aria-labelledby="contact-tab">...div>
div>
<nav>
<div class="nav nav-tabs" id="nav-tab" role="tablist">
<a class="nav-link active" id="nav-home-tab" data-toggle="tab" href="#nav-home" role="tab" aria-controls="nav-home" aria-selected="true">Homea>
<a class="nav-link" id="nav-profile-tab" data-toggle="tab" href="#nav-profile" role="tab" aria-controls="nav-profile" aria-selected="false">Profilea>
<a class="nav-link" id="nav-contact-tab" data-toggle="tab" href="#nav-contact" role="tab" aria-controls="nav-contact" aria-selected="false">Contacta>
div>
nav>
<div class="tab-content" id="nav-tabContent">
<div class="tab-pane fade show active" id="nav-home" role="tabpanel" aria-labelledby="nav-home-tab">...div>
<div class="tab-pane fade" id="nav-profile" role="tabpanel" aria-labelledby="nav-profile-tab">...div>
<div class="tab-pane fade" id="nav-contact" role="tabpanel" aria-labelledby="nav-contact-tab">...div>
div>
<ul class="nav nav-pills mb-3" id="pills-tab" role="tablist">
<li class="nav-item" role="presentation">
<a class="nav-link active" id="pills-home-tab" data-toggle="pill" href="#pills-home" role="tab" aria-controls="pills-home" aria-selected="true">Homea>
li>
<li class="nav-item" role="presentation">
<a class="nav-link" id="pills-profile-tab" data-toggle="pill" href="#pills-profile" role="tab" aria-controls="pills-profile" aria-selected="false">Profilea>
li>
<li class="nav-item" role="presentation">
<a class="nav-link" id="pills-contact-tab" data-toggle="pill" href="#pills-contact" role="tab" aria-controls="pills-contact" aria-selected="false">Contacta>
li>
ul>
<div class="tab-content" id="pills-tabContent">
<div class="tab-pane fade show active" id="pills-home" role="tabpanel" aria-labelledby="pills-home-tab">...div>
<div class="tab-pane fade" id="pills-profile" role="tabpanel" aria-labelledby="pills-profile-tab">...div>
<div class="tab-pane fade" id="pills-contact" role="tabpanel" aria-labelledby="pills-contact-tab">...div>
div>
<div class="row">
<div class="col-3">
<div class="nav flex-column nav-pills" id="v-pills-tab" role="tablist" aria-orientation="vertical">
<a class="nav-link active" id="v-pills-home-tab" data-toggle="pill" href="#v-pills-home" role="tab" aria-controls="v-pills-home" aria-selected="true">Homea>
<a class="nav-link" id="v-pills-profile-tab" data-toggle="pill" href="#v-pills-profile" role="tab" aria-controls="v-pills-profile" aria-selected="false">Profilea>
<a class="nav-link" id="v-pills-messages-tab" data-toggle="pill" href="#v-pills-messages" role="tab" aria-controls="v-pills-messages" aria-selected="false">Messagesa>
<a class="nav-link" id="v-pills-settings-tab" data-toggle="pill" href="#v-pills-settings" role="tab" aria-controls="v-pills-settings" aria-selected="false">Settingsa>
div>
div>
<div class="col-9">
<div class="tab-content" id="v-pills-tabContent">
<div class="tab-pane fade show active" id="v-pills-home" role="tabpanel" aria-labelledby="v-pills-home-tab">...div>
<div class="tab-pane fade" id="v-pills-profile" role="tabpanel" aria-labelledby="v-pills-profile-tab">...div>
<div class="tab-pane fade" id="v-pills-messages" role="tabpanel" aria-labelledby="v-pills-messages-tab">...div>
<div class="tab-pane fade" id="v-pills-settings" role="tabpanel" aria-labelledby="v-pills-settings-tab">...div>
div>
div>
div>
导航栏
了解:
.navbar
以.navbar-expand{-sm|-md|-lg|-xl}
用于响应式折叠和配色方案类。.d-print
到.navbar
。元素来确保可访问性,或者如果使用更通用的元素(例如使用)来确保可访问性,则在每个导航栏上
role="navigation"
以将其明确标识为辅助技术用户的界标区域。
支持内容:
.navbar-brand
您公司,产品或项目的名称。.navbar-nav
全高和轻量级导航(包括对下拉菜单的支持)。.navbar-toggler
与我们的崩溃插件和其他导航切换行为配合使用。.form-inline
用于任何形式的控件和动作。.navbar-text
用于添加垂直居中的文本字符串。
-.collapse.navbar-collapse
用于通过父断点分组和隐藏导航栏内容。lg
(大)断点处自动折叠:<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Navbara>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon">span>
button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)span>a>
li>
<li class="nav-item">
<a class="nav-link" href="#">Linka>
li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown
a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Actiona>
<a class="dropdown-item" href="#">Another actiona>
<div class="dropdown-divider">div>
<a class="dropdown-item" href="#">Something else herea>
div>
li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disableda>
li>
ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Searchbutton>
form>
div>
nav>
品牌
.navbar-brand
可应用于大部分元素,但是锚效果最好的一些元素可能需要的工具类或自定义样式。
<nav class="navbar navbar-light bg-light">
<a class="navbar-brand" href="#">Navbara>
nav>
<nav class="navbar navbar-light bg-light">
<span class="navbar-brand mb-0 h1">Navbarspan>
nav>
<nav class="navbar navbar-light bg-light">
<a class="navbar-brand" href="#">
<img src="/docs/4.5/assets/brand/bootstrap-solid.svg" width="30" height="30" alt="" loading="lazy">
a>
nav>
<nav class="navbar navbar-light bg-light">
<a class="navbar-brand" href="#">
<img src="/docs/4.5/assets/brand/bootstrap-solid.svg" width="30" height="30" class="d-inline-block align-top" alt="" loading="lazy">
Bootstrap
a>
nav>
导航
Navbar
导航链接以.nav
自己的选项为基础,带有自己的修改器类,并且需要使用切换器类来进行适当的响应式样式。.active-
表示当前页面可以直接应用于.nav-links
或它们的直接父级.nav-items
。<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Navbara>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon">span>
button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)span>a>
li>
<li class="nav-item">
<a class="nav-link" href="#">Featuresa>
li>
<li class="nav-item">
<a class="nav-link" href="#">Pricinga>
li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disableda>
li>
ul>
div>
nav>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Navbara>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon">span>
button>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<div class="navbar-nav">
<a class="nav-link active" href="#">Home <span class="sr-only">(current)span>a>
<a class="nav-link" href="#">Featuresa>
<a class="nav-link" href="#">Pricinga>
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disableda>
div>
div>
nav>
嵌套下拉菜单
.nav-item
和.nav-link
,如下所示。<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Navbara>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon">span>
button>
<div class="collapse navbar-collapse" id="navbarNavDropdown">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)span>a>
li>
<li class="nav-item">
<a class="nav-link" href="#">Featuresa>
li>
<li class="nav-item">
<a class="nav-link" href="#">Pricinga>
li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown link
a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<a class="dropdown-item" href="#">Actiona>
<a class="dropdown-item" href="#">Another actiona>
<a class="dropdown-item" href="#">Something else herea>
div>
li>
ul>
div>
nav>
表单
.form-inline
。<nav class="navbar navbar-light bg-light">
<form class="form-inline">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Searchbutton>
form>
nav>
直接子元素在.navbar
使用flex
布局,并且默认为justify-content: space-between
。根据需要使用其他Flex实用程序来调整此行为。<nav class="navbar navbar-light bg-light">
<a class="navbar-brand">Navbara>
<form class="form-inline">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Searchbutton>
form>
nav>
输入组
<nav class="navbar navbar-light bg-light">
<form class="form-inline">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1">@span>
div>
<input type="text" class="form-control" placeholder="Username" aria-label="Username" aria-describedby="basic-addon1">
div>
form>
nav>
这些导航栏形式也支持各种按钮。这也非常提醒我们可以使用垂直对齐工具来对齐不同大小的元素。<nav class="navbar navbar-light bg-light">
<form class="form-inline">
<button class="btn btn-outline-success" type="button">Main buttonbutton>
<button class="btn btn-sm btn-outline-secondary" type="button">Smaller buttonbutton>
form>
nav>
文本
.navbar-text
。此类调整文本字符串的垂直对齐和水平间距。<nav class="navbar navbar-light bg-light">
<span class="navbar-text">
带有内联元素的导航栏文本
span>
nav>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Navbar w/ texta>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon">span>
button>
<div class="collapse navbar-collapse" id="navbarText">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)span>a>
li>
<li class="nav-item">
<a class="nav-link" href="#">Featuresa>
li>
<li class="nav-item">
<a class="nav-link" href="#">Pricinga>
li>
ul>
<span class="navbar-text">
Navbar text with an inline element
span>
div>
nav>
导航背景颜色
选择.navbar-light``用于浅色背景颜色或.navbar-dark
深色背景颜色。然后,使用.bg-*
实用程序进行自定义。<nav class="navbar navbar-dark bg-dark">
nav>
<nav class="navbar navbar-dark bg-primary">
nav>
<nav class="navbar navbar-light" style="background-color: #e3f2fd;">
nav>
导航栏布局
.container
中间,以使其在页面上居中,也可以在其中添加一个导航栏,仅将固定或静态顶部导航栏的内容居中。<div class="container">
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Navbara>
nav>
div>
当容器在导航栏中时,在低于指定.navbar-expand{-sm|-md|-lg|-xl}
类的断点处删除其水平填充。这样可以确保在导航栏合拢时,我们不会在不必要的情况下在较低视口上加倍填充。<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container">
<a class="navbar-brand" href="#">Navbara>
div>
nav>
位置
fixed
,这意味着它们是从DOM的正常流程中拉出的,并且可能需要自定义CSS(例如padding-top
在上),以防止与其他元素重叠。
.sticky-top
用途position: sticky
,并非所有浏览器都完全支持。
默认:<nav class="navbar navbar-light bg-light">
<a class="navbar-brand" href="#">Defaulta>
nav>
<nav class="navbar fixed-top navbar-light bg-light">
<a class="navbar-brand" href="#">Fixed topa>
nav>
<nav class="navbar fixed-bottom navbar-light bg-light">
<a class="navbar-brand" href="#">Fixed bottoma>
nav>
<nav class="navbar sticky-top navbar-light bg-light">
<a class="navbar-brand" href="#">Sticky topa>
nav>
响应
.navbar-toggler
,.navbar-collapse
和.navbar-expand{-sm|-md|-lg|-xl}
类在内容折叠到按钮后时进行更改。与其他实用程序结合使用,您可以轻松选择何时显示或隐藏特定元素。.navbar-expand的导航栏
,请在导航栏上添加类。对于始终折叠的导航栏,请勿添加任何.navbar-expand
类。切换器
左对齐
的,但是如果它们跟随诸如的兄弟元素.navbar-brand
,它们将自动对齐到最右端。反转标记将反转切换器的位置。以下是不同切换样式的示例。.navbar-brand
最低断点处没有显示:<nav class="navbar navbar-expand-lg navbar-light bg-light">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo01" aria-controls="navbarTogglerDemo01" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon">span>
button>
<div class="collapse navbar-collapse" id="navbarTogglerDemo01">
<a class="navbar-brand" href="#">Hidden branda>
<ul class="navbar-nav mr-auto mt-2 mt-lg-0">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)span>a>
li>
<li class="nav-item">
<a class="nav-link" href="#">Linka>
li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disableda>
li>
ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Searchbutton>
form>
div>
nav>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Navbara>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon">span>
button>
<div class="collapse navbar-collapse" id="navbarTogglerDemo02">
<ul class="navbar-nav mr-auto mt-2 mt-lg-0">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)span>a>
li>
<li class="nav-item">
<a class="nav-link" href="#">Linka>
li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disableda>
li>
ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Searchbutton>
form>
div>
nav>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo03" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon">span>
button>
<a class="navbar-brand" href="#">Navbara>
<div class="collapse navbar-collapse" id="navbarTogglerDemo03">
<ul class="navbar-nav mr-auto mt-2 mt-lg-0">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)span>a>
li>
<li class="nav-item">
<a class="nav-link" href="#">Linka>
li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disableda>
li>
ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Searchbutton>
form>
div>
nav>
折叠内容
id
和data-target
,因此很容易做到!<div class="fixed-top">
<div class="collapse" id="navbarToggleExternalContent">
<div class="bg-dark p-4">
<h5 class="text-white h4">Collapsed contenth5>
<span class="text-muted">Toggleable via the navbar brand.span>
div>
div>
<nav class="navbar navbar-dark bg-dark">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarToggleExternalContent" aria-controls="navbarToggleExternalContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon">span>
button>
nav>
div>
分页
网页可能有不止一个这样的导航部分,它的最好提供一个描述性aria-label
的,以反映其目的。例如,如果使用分页组件在一组搜索结果之间导航,则适当的标签可以为
aria-label="Search results pages"
。<nav aria-label="Page navigation example">
<ul class="pagination">
<li class="page-item"><a class="page-link" href="#">Previousa>li>
<li class="page-item"><a class="page-link" href="#">1a>li>
<li class="page-item"><a class="page-link" href="#">2a>li>
<li class="page-item"><a class="page-link" href="#">3a>li>
<li class="page-item"><a class="page-link" href="#">Nexta>li>
ul>
nav>
使用图标
aria
属性在某些分页链接中使用图标或符号代替文本<nav aria-label="Page navigation example">
<ul class="pagination">
<li class="page-item">
<a class="page-link" href="#" aria-label="Previous">
<span aria-hidden="true">«span