helpers.md 6.36 KB
Newer Older
Mark Otto's avatar
Mark Otto committed
1
2
3
4
---
layout: page
title: Helper classes
---
Mark Otto's avatar
Mark Otto committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

### Contextual colors

Convey meaning through color with a handful of emphasis utility classes. These may also be applied to links and will darken on hover just like our default link styles.

{% example html %}
<p class="text-muted">Fusce dapibus, tellus ac cursus commodo, tortor mauris nibh.</p>
<p class="text-primary">Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
<p class="text-success">Duis mollis, est non commodo luctus, nisi erat porttitor ligula.</p>
<p class="text-info">Maecenas sed diam eget risus varius blandit sit amet non magna.</p>
<p class="text-warning">Etiam porta sem malesuada magna mollis euismod.</p>
<p class="text-danger">Donec ullamcorper nulla non metus auctor fringilla.</p>
{% endexample %}

<div class="bs-callout bs-callout-info">
  <h4>Dealing with specificity</h4>
  <p>Sometimes emphasis classes cannot be applied due to the specificity of another selector. In most cases, a sufficient workaround is to wrap your text in a <code>&lt;span&gt;</code> with the class.</p>
</div>
Mark Otto's avatar
Mark Otto committed
23
24
25
26
<div class="bs-callout bs-callout-warning">
  <h4>Conveying meaning to assistive technologies</h4>
  <p>Using color to add meaning only provides a visual indication, which will not be conveyed to users of assistive technologies – such as screen readers. Ensure that information denoted by the color is either obvious from the content itself (the contextual colors are only used to reinforce meaning that is already present in the text/markup), or is included through alternative means, such as additional text hidden with the <code>.sr-only</code> class.</p>
</div>
Mark Otto's avatar
Mark Otto committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

### Contextual backgrounds

Similar to the contextual text color classes, easily set the background of an element to any contextual class. Anchor components will darken on hover, just like the text classes.

{% example html %}
<div class="bg-primary">Nullam id dolor id nibh ultricies vehicula ut id elit.</div>
<div class="bg-success">Duis mollis, est non commodo luctus, nisi erat porttitor ligula.</div>
<div class="bg-info">Maecenas sed diam eget risus varius blandit sit amet non magna.</div>
<div class="bg-warning">Etiam porta sem malesuada magna mollis euismod.</div>
<div class="bg-danger">Donec ullamcorper nulla non metus auctor fringilla.</div>
{% endexample %}

<div class="bs-callout bs-callout-info">
  <h4>Dealing with specificity</h4>
  <p>Sometimes contextual background classes cannot be applied due to the specificity of another selector. In some cases, a sufficient workaround is to wrap your element's content in a <code>&lt;div&gt;</code> with the class.</p>
</div>

Mark Otto's avatar
Mark Otto committed
45
46
47
48
49
<div class="bs-callout bs-callout-warning">
  <h4>Conveying meaning to assistive technologies</h4>
  <p>As with <a href="#helper-classes-colors">contextual colors</a>, ensure that any meaning conveyed through color is also conveyed in a format that is not purely presentational.</p>
</div>

Mark Otto's avatar
Mark Otto committed
50
51
52
53
54
### Close icon

Use a generic close icon for dismissing content like modals and alerts. **Be sure to include screen reader text when you can** as we've done with `.sr-only`.

{% example html %}
Mark Otto's avatar
Mark Otto committed
55
<button type="button" class="close" aria-label="Close">
Mark Otto's avatar
Mark Otto committed
56
57
58
59
60
61
62
63
64
65
  <span aria-hidden="true">&times;</span>
  <span class="sr-only">Close</span>
</button>
{% endexample %}

### Quick floats

Float an element to the left or right with a class. `!important` is included to avoid specificity issues. Classes can also be used as mixins.

{% example html %}
Mark Otto's avatar
Mark Otto committed
66
67
<div class="pull-left">...</div>
<div class="pull-right">...</div>
Mark Otto's avatar
Mark Otto committed
68
69
{% endexample %}

Mark Otto's avatar
Mark Otto committed
70
71
72
73
74
75
76
77
78
79
80
{% highlight scss %}
// Classes
.pull-left {
  float: left !important;
}
.pull-right {
  float: right !important;
}

// Usage as mixins
.element {
Mark Otto's avatar
Mark Otto committed
81
  @include pull-left;
Mark Otto's avatar
Mark Otto committed
82
83
}
.another-element {
Mark Otto's avatar
Mark Otto committed
84
  @include pull-right;
Mark Otto's avatar
Mark Otto committed
85
86
87
}
{% endhighlight %}

Mark Otto's avatar
Mark Otto committed
88
89
90
91
92
### Center content blocks

Set an element to `display: block;` and center via `margin`. Available as a mixin and class.

{% example html %}
Mark Otto's avatar
Mark Otto committed
93
<div class="center-block">...</div>
Mark Otto's avatar
Mark Otto committed
94
95
{% endexample %}

Mark Otto's avatar
Mark Otto committed
96
97
98
99
100
101
102
103
104
105
{% highlight scss %}
// Classes
.center-block {
  display: block;
  margin-left: auto;
  margin-right: auto;
}

// Usage as mixins
.element {
106
  @include center-block;
Mark Otto's avatar
Mark Otto committed
107
108
109
}
{% endhighlight %}

Mark Otto's avatar
Mark Otto committed
110
111
Easily clear `float`s by adding `.clearfix` **to the parent element**. Utilizes [the micro clearfix](http://nicolasgallagher.com/micro-clearfix-hack/) as popularized by Nicolas Gallagher. Can also be used as a mixin.

Mark Otto's avatar
Mark Otto committed
112
113
114
{% highlight html %}
<div class="clearfix">...</div>
{% endhighlight %}
Mark Otto's avatar
Mark Otto committed
115

Mark Otto's avatar
Mark Otto committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
{% highlight scss %}
// Mixin itself
.clearfix() {
  &:before,
  &:after {
    content: " ";
    display: table;
  }
  &:after {
    clear: both;
  }
}

// Usage as a Mixin
.element {
131
  @include clearfix;
Mark Otto's avatar
Mark Otto committed
132
133
134
}
{% endhighlight %}

135
### Hidden content
Mark Otto's avatar
Mark Otto committed
136

137
Hide any HTML element with the `[hidden]` attribute. Previously, v3.x included a `.hidden` class that forced toggled content. However, we removed it due to conflicts with jQuery's `hide()` function. It's taken from [PureCSS](http://purecss.io).
Mark Otto's avatar
Mark Otto committed
138

139
Furthermore, `.invisible` can be used to toggle the visibility of an element, meaning its `display` is not modified and the element can still affect the flow of the document.
Mark Otto's avatar
Mark Otto committed
140

Mark Otto's avatar
Mark Otto committed
141
{% highlight html %}
142
<input type="text" hidden>
Mark Otto's avatar
Mark Otto committed
143
{% endhighlight %}
Mark Otto's avatar
Mark Otto committed
144

145
### Invisible content
Mark Otto's avatar
Mark Otto committed
146

147
The `.invisible` class can be used to toggle only the visibility of an element, meaning its `display` is not modified and the element can still affect the flow of the document.
148
149

{% highlight html %}
150
<div class="invisible">...</div>
Mark Otto's avatar
Mark Otto committed
151
152
{% endhighlight %}

153
154
155
156
157
158
159
160
161
162
{% highlight scss %}
// Classes
.invisible {
  visibility: hidden;
}

// Usage as mixin
.element {
  .invisible();
}
Mark Otto's avatar
Mark Otto committed
163
{% endhighlight %}
164

Mark Otto's avatar
Mark Otto committed
165
166
167
168
### Screen readers and keyboard navigation

Hide an element to all devices **except screen readers** with `.sr-only`. Combine `.sr-only` with `.sr-only-focusable` to show the element again when it's focused (e.g. by a keyboard-only user). Necessary for following [accessibility best practices](../getting-started/#accessibility). Can also be used as mixins.

Mark Otto's avatar
Mark Otto committed
169
170
171
{% highlight html %}
<a class="sr-only sr-only-focusable" href="#content">Skip to main content</a>
{% endhighlight %}
Mark Otto's avatar
Mark Otto committed
172

Mark Otto's avatar
Mark Otto committed
173
174
175
{% highlight scss %}
// Usage as a Mixin
.skip-navigation {
176
177
  @include sr-only;
  @include sr-only-focusable;
Mark Otto's avatar
Mark Otto committed
178
179
180
}
{% endhighlight %}

Mark Otto's avatar
Mark Otto committed
181
182
183
184
185
### Image replacement

Utilize the `.text-hide` class or mixin to help replace an element's text content with a background image.

{% example html %}
Mark Otto's avatar
Mark Otto committed
186
<h1 class="text-hide">Custom heading</h1>
Mark Otto's avatar
Mark Otto committed
187
188
189
{% endexample %}

{% highlight scss %}
Mark Otto's avatar
Mark Otto committed
190
191
// Usage as a Mixin
.heading {
192
  @include text-hide;
Mark Otto's avatar
Mark Otto committed
193
194
}
{% endhighlight %}