helpers.md 6.03 KB
Newer Older
Mark Otto's avatar
Mark Otto committed
1
<a id="helpers"></a>
Mark Otto's avatar
Mark Otto committed
2

Mark Otto's avatar
Mark Otto committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Helper classes



<a id="helpers-colors"></a>

### 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>



<a id="helpers-bgs"></a>

### 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>



<a id="helpers-close"></a>

### 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 %}
<button type="button" class="close">
  <span aria-hidden="true">&times;</span>
  <span class="sr-only">Close</span>
</button>
{% endexample %}



<a id="helpers-floats"></a>

### 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
72
73
<div class="pull-left">...</div>
<div class="pull-right">...</div>
Mark Otto's avatar
Mark Otto committed
74
75
{% endexample %}

Mark Otto's avatar
Mark Otto committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
{% highlight scss %}
// Classes
.pull-left {
  float: left !important;
}
.pull-right {
  float: right !important;
}

// Usage as mixins
.element {
  .pull-left();
}
.another-element {
  .pull-right();
}
{% endhighlight %}

Mark Otto's avatar
Mark Otto committed
94
95
96
97
<div class="bs-callout bs-callout-warning">
  <h4>Not for use in navbars</h4>
  <p>To align components in navbars with utility classes, use <code>.navbar-left</code> or <code>.navbar-right</code> instead. <a href="../components/#navbar-component-alignment">See the navbar docs</a> for details.</p>
</div>
Mark Otto's avatar
Mark Otto committed
98
99


Mark Otto's avatar
Mark Otto committed
100
101
102
103
104
105
106
107

<a id="helpers-center"></a>

### 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
108
<div class="center-block">...</div>
Mark Otto's avatar
Mark Otto committed
109
110
{% endexample %}

Mark Otto's avatar
Mark Otto committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
{% highlight scss %}
// Classes
.center-block {
  display: block;
  margin-left: auto;
  margin-right: auto;
}

// Usage as mixins
.element {
  .center-block();
}
{% endhighlight %}


Mark Otto's avatar
Mark Otto committed
126
127
128
129
130

<a id="helpers-clearfix"></a>

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
131
132
133
{% highlight html %}
<div class="clearfix">...</div>
{% endhighlight %}
Mark Otto's avatar
Mark Otto committed
134

Mark Otto's avatar
Mark Otto committed
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
{% highlight scss %}
// Mixin itself
.clearfix() {
  &:before,
  &:after {
    content: " ";
    display: table;
  }
  &:after {
    clear: both;
  }
}

// Usage as a Mixin
.element {
  .clearfix();
}
{% endhighlight %}


Mark Otto's avatar
Mark Otto committed
155
156
157
158
159
160
161
162
163

<a id="helpers-toggles"></a>

### Showing and hiding content

Force an element to be shown or hidden (**including for screen readers**) with the use of `.show` and `.hidden` classes. These classes use `!important` to avoid specificity conflicts, just like the [quick floats](#helper-floats). They are only available for block level toggling. They can also be used as mixins.

Furthermore, `.invisible` 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.

Mark Otto's avatar
Mark Otto committed
164
165
166
167
{% highlight html %}
<div class="show">...</div>
<div class="hidden">...</div>
{% endhighlight %}
Mark Otto's avatar
Mark Otto committed
168

Mark Otto's avatar
Mark Otto committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
{% highlight scss %}
// Classes
.show {
  display: block !important;
}
.hidden {
  display: none !important;
  visibility: hidden !important;
}
.invisible {
  visibility: hidden;
}

// Usage as mixins
.element {
  .show();
}
.another-element {
  .hidden();
}
{% endhighlight %}


Mark Otto's avatar
Mark Otto committed
192
193
194
195
196
197
198

<a id="helpers-screen-readers"></a>

### 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
199
200
201
{% highlight html %}
<a class="sr-only sr-only-focusable" href="#content">Skip to main content</a>
{% endhighlight %}
Mark Otto's avatar
Mark Otto committed
202

Mark Otto's avatar
Mark Otto committed
203
204
205
206
207
208
209
210
211
{% highlight scss %}
// Usage as a Mixin
.skip-navigation {
  .sr-only();
  .sr-only-focusable();
}
{% endhighlight %}


Mark Otto's avatar
Mark Otto committed
212
213
214
215
216
217
218
219

<a id="helpers-image-replacement"></a>

### 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
220
<h1 class="text-hide">Custom heading</h1>
Mark Otto's avatar
Mark Otto committed
221
222
223
{% endexample %}

{% highlight scss %}
Mark Otto's avatar
Mark Otto committed
224
225
226
227
228
// Usage as a Mixin
.heading {
  .text-hide();
}
{% endhighlight %}