Twitter Bootstrap 3.0我现在如何"徽章重要"

Pet*_*ter 216 twitter-bootstrap twitter-bootstrap-3

在第二版我可以使用

徽章徽章 - 重要

我看到.badge元素不再具有上下文(-success,-primary等)类.

我如何在版本3中实现相同的功能?

例如.我想在我的UI中使用警告徽章和重要徽章

Jen*_*och 252

总之:更换badge-important用两种alert-dangerprogress-bar-danger.

它看起来像这样:Bootply Demo.


您可以将CSS类badgealert-*或组合使用progess-bar-*:

class="badges alert-*"

  <span class="badge alert-info">badge</span> Info
  <span class="badge alert-success">badge</span> Success 
  <span class="badge alert-danger">badge</span> Danger   
  <span class="badge alert-warning">badge</span> Warning
Run Code Online (Sandbox Code Playgroud)

警报文件: http ://getbootstrap.com/components/#alerts

随着class="badges progress-bar-*"(@ clami219的建议)

  <span class="badge progress-bar-info">badge</span> Info
  <span class="badge progress-bar-success">badge</span> Success
  <span class="badge progress-bar-danger">badge</span> Danger
  <span class="badge progress-bar-warning">badge</span> Warning
Run Code Online (Sandbox Code Playgroud)

Progress-Bar Docu: http ://getbootstrap.com/components/#progress-alternatives

  • 这有点像黑客的答案.当然有效,而且很简单.但是您应该将这些上下文警报类与警报一起使用.您可能会在将来签署自己的破损风格.@JasonHuang的答案同样简单,并为您提供更大的控制权. (6认同)
  • 我不喜欢警报装饰徽章的方式,所以我选择使用标签代替.http://getbootstrap.com/components/#labels (4认同)
  • 人们登陆这个页面,因为引导人员做了很多设计考虑,这就抛出了他们之前做过的设计考虑:) - 我只是重新组合现有的css类,以便接近前"徽章重要" .在这里,我们开始使用他们开箱即用的东西:就是这样.我不认为有一种真正的方法来制作CSS.此页面提供了许多方法来解决问题.但是请停止喊"Aaah!不!这不是方法!你做错了什么". (3认同)
  • 促使我做出上述回复的原因是Visual Studio Web Essentials显示上述代码的警告.即,"当使用'alert-danger'时,您还必须指定班级'警报'." (2认同)

bro*_*eib 252

只需在CSS中添加这个单行类,并使用bootstrap label组件.

.label-as-badge {
    border-radius: 1em;
}
Run Code Online (Sandbox Code Playgroud)

比较这labelbadge并排:

<span class="label label-default label-as-badge">hello</span>
<span class="badge">world</span>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

它们看起来是一样的.但是在CSS中,label使用em它可以很好地扩展,并且它仍然具有所有"-color"类.因此标签将更好地扩展到更大的字体大小,并且可以使用标签成功,标签警告等进行着色.以下是两个示例:

<span class="label label-success label-as-badge">Yay! Rah!</span>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

或者事情变得更大:

<div style="font-size: 36px"><!-- pretend an enclosing class has big font size -->
    <span class="label label-success label-as-badge">Yay! Rah!</span>
</div>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


2015年11月16日:看看我们将如何在Bootstrap 4中执行此操作

看起来.badge课程已经完全消失了.但是有一个内置.label-pill(这里)看起来像我们想要的.

.label-pill {
  padding-right: .6em;
  padding-left: .6em;
  border-radius: 10rem;
}
Run Code Online (Sandbox Code Playgroud)

在使用中它看起来像这样:

<span class="label label-pill label-default">Default</span>
<span class="label label-pill label-primary">Primary</span>
<span class="label label-pill label-success">Success</span>
<span class="label label-pill label-info">Info</span>
<span class="label label-pill label-warning">Warning</span>
<span class="label label-pill label-danger">Danger</span>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


2014年4月11日:这里有一个关于为什么交叉授粉警报类.badge不是那么好的更新.我想这张图总结了一下:

在此输入图像描述

那些警报类不是为徽章而设计的.它使它们呈现出预期颜色的"暗示",但最终一致性被抛到窗外,可读性值得怀疑.那些警示被攻击的徽章在视觉上没有凝聚力.

.label-as-badge解决方案仅延长引导设计.我们完整地保留了自举设计师做出的所有决策,即他们对所有可能颜色的可读性和内聚性的考虑,以及颜色选择本身.本.label-as-badge类只增加了圆角,而不是其他.没有引入颜色定义.因此,单行CSS.

是的,它更容易被攻击并放入这些.alert-xxxxx类 - 您不必添加任何 CSS行.或者你可以更关心小事情并添加一行.

  • 这很酷,只要你不想在组列表中使用徽章,那么它们就不会浮动......随着时间的推移,我可以看到这种制动因为引导程序被改变了.不错的黑客. (3认同)
  • 如果你想获得特殊列表组浮动只需添加这个!`.list-group-item> .label-as-badge {float:right; } .list-group-item> .label-as-badge + .label-as-badge {margin-right:5px; }` (3认同)
  • 是的,好点@nodrog.带有'徽章'的`list-group`是bootstrap中一个很好的开箱即用功能.对于那些观看的人来说,这就是:http://getbootstrap.com/components/#list-group-badges (2认同)
  • 很好的答案.可能还想在那里输入匹配.badge的填充如下:`.label-as-badge {border-radius:1em; 填充:3px 7px; }` (2认同)

Ada*_*s.H 44

Bootstrap 3删除了徽章的颜色选项.但是,我们可以手动添加这些样式.这是我的解决方案,这里是JS Bin:

.badge {
  padding: 1px 9px 2px;
  font-size: 12.025px;
  font-weight: bold;
  white-space: nowrap;
  color: #ffffff;
  background-color: #999999;
  -webkit-border-radius: 9px;
  -moz-border-radius: 9px;
  border-radius: 9px;
}
.badge:hover {
  color: #ffffff;
  text-decoration: none;
  cursor: pointer;
}
.badge-error {
  background-color: #b94a48;
}
.badge-error:hover {
  background-color: #953b39;
}
.badge-warning {
  background-color: #f89406;
}
.badge-warning:hover {
  background-color: #c67605;
}
.badge-success {
  background-color: #468847;
}
.badge-success:hover {
  background-color: #356635;
}
.badge-info {
  background-color: #3a87ad;
}
.badge-info:hover {
  background-color: #2d6987;
}
.badge-inverse {
  background-color: #333333;
}
.badge-inverse:hover {
  background-color: #1a1a1a;
}
Run Code Online (Sandbox Code Playgroud)


Zim*_*Zim 25

badge确实从Bootstrap 3中删除了上下文类,因此您必须添加一些自定义CSS来创建相同的效果,如...

.badge-important{background-color:#b94a48;}
Run Code Online (Sandbox Code Playgroud)

Bootply

  • 他们为什么要摆脱它们? (20认同)
  • +!我也想知道他们为什么要摆脱它们.您可以使用标签,但它不能正确排列在药丸容器上.至少它没有垂直居中. (4认同)
  • 瘸.向上迁移从2到3是一个彻头彻尾的痛苦. (2认同)
  • @Peter:请参阅https://github.com/twbs/bootstrap/pull/6342.TLDR:徽章旨在用作"未读"计数器,不应用于传达其他状态. (2认同)

cla*_*219 20

为了使颜色更加强烈,另一种可能的方法是:

<span class="badge progress-bar-info">10</span>
<span class="badge progress-bar-success">20</span>
<span class="badge progress-bar-warning">30</span>
<span class="badge progress-bar-danger">40</span>
Run Code Online (Sandbox Code Playgroud)

Bootply


Pow*_*iKi 18

如果使用SASS版本(例如:thomas-mcdonald's),那么您可能希望稍微更动态(尊重现有变量)并使用与标签相同的技术创建所有徽章上下文:

// Colors
// Contextual variations of badges
// Bootstrap 3.0 removed contexts for badges, we re-introduce them, based on what is done for labels
.badge-default {
  @include label-variant($label-default-bg);
}

.badge-primary {
  @include label-variant($label-primary-bg);
}

.badge-success {
  @include label-variant($label-success-bg);
}

.badge-info {
  @include label-variant($label-info-bg);
}

.badge-warning {
  @include label-variant($label-warning-bg);
}

.badge-danger {
  @include label-variant($label-danger-bg);
}
Run Code Online (Sandbox Code Playgroud)

LESS等价物应该是直截了当的.


小智 5

像上面的答案,但这里使用bootstrap 3名称和颜色:

/*css to add back colours for badges and make use of the colours*/
.badge-default {
  background-color: #999999;
}

.badge-primary {
  background-color: #428bca;
}

.badge-success {
  background-color: #5cb85c;
}

.badge-info {
  background-color: #5bc0de;
}

.badge-warning {
  background-color: #f0ad4e;
}

.badge-danger {
  background-color: #d9534f;
}
Run Code Online (Sandbox Code Playgroud)