utilities.md 11.3 KB
Newer Older
Mark Otto's avatar
Mark Otto committed
1
---
Mark Otto's avatar
Mark Otto committed
2
layout: docs
3
title: Utility classes
4
group: components
Mark Otto's avatar
Mark Otto committed
5
---
Mark Otto's avatar
Mark Otto committed
6

7
Bootstrap includes dozens of utilities—classes with a single purpose. They're designed to reduce the frequency of highly repetitive declarations in your CSS down while allowing for quick and easy development.
8

Mark Otto's avatar
Mark Otto committed
9
10
11
12
13
## Contents

* Will be replaced with the ToC, excluding the "Contents" header
{:toc}

14
15
16
17
18
19
20
## Spacing

Assign `margin` or `padding` to an element or a subset of it's sides with shorthand classes. Includes support for individual properties, all properties, and vertical and horizontal properties. All classes are multiples on the global default value, `1rem`.

### Margin

{% highlight scss %}
Mark Otto's avatar
Mark Otto committed
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
.m-a-0 { margin:        0 !important; }
.m-t-0 { margin-top:    0 !important; }
.m-r-0 { margin-right:  0 !important; }
.m-b-0 { margin-bottom: 0 !important; }
.m-l-0 { margin-left:   0 !important; }
.m-x-0 { margin-right:  0 !important; margin-left:   0 !important; }
.m-y-0 { margin-top:    0 !important; margin-bottom: 0 !important; }

.m-a { margin:        $spacer !important; }
.m-t { margin-top:    $spacer-y !important; }
.m-r { margin-right:  $spacer-x !important; }
.m-b { margin-bottom: $spacer-y !important; }
.m-l { margin-left:   $spacer-x !important; }
.m-x { margin-right:  $spacer-x !important; margin-left: $spacer-x !important; }
.m-y { margin-top:    $spacer-y !important; margin-bottom: $spacer-y !important; }
.m-x-auto { margin-right: auto !important; margin-left: auto !important; }

.m-a-md { margin:        ($spacer * 1.5) !important; }
.m-t-md { margin-top:    ($spacer-y * 1.5) !important; }
.m-r-md { margin-right:  ($spacer-y * 1.5) !important; }
.m-b-md { margin-bottom: ($spacer-y * 1.5) !important; }
.m-l-md { margin-left:   ($spacer-y * 1.5) !important; }
.m-x-md { margin-right:  ($spacer-x * 1.5) !important; margin-left:   ($spacer-x * 1.5) !important; }
.m-y-md { margin-top:    ($spacer-y * 1.5) !important; margin-bottom: ($spacer-y * 1.5) !important; }

.m-a-lg { margin:        ($spacer * 3) !important; }
.m-t-lg { margin-top:    ($spacer-y * 3) !important; }
.m-r-lg { margin-right:  ($spacer-y * 3) !important; }
.m-b-lg { margin-bottom: ($spacer-y * 3) !important; }
.m-l-lg { margin-left:   ($spacer-y * 3) !important; }
.m-x-lg { margin-right:  ($spacer-x * 3) !important; margin-left:   ($spacer-x * 3) !important; }
.m-y-lg { margin-top:    ($spacer-y * 3) !important; margin-bottom: ($spacer-y * 3) !important; }
53
54
55
56
57
{% endhighlight %}

### Padding

{% highlight scss %}
Mark Otto's avatar
Mark Otto committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
.p-a-0 { padding:        0 !important; }
.p-t-0 { padding-top:    0 !important; }
.p-r-0 { padding-right:  0 !important; }
.p-b-0 { padding-bottom: 0 !important; }
.p-l-0 { padding-left:   0 !important; }
.p-x-0 { padding-left:   0 !important; padding-right: 0 !important; }
.p-y-0 { padding-top:    0 !important; padding-bottom: 0 !important; }

.p-a { padding:        $spacer !important; }
.p-t { padding-top:    $spacer-y !important; }
.p-r { padding-right:  $spacer-x !important; }
.p-b { padding-bottom: $spacer-y !important; }
.p-l { padding-left:   $spacer-x !important; }
.p-x { padding-right:  $spacer-x !important; padding-left:   $spacer-x !important; }
.p-y { padding-top:    $spacer-y !important; padding-bottom: $spacer-y !important; }

.p-a-md { padding:        ($spacer * 1.5) !important; }
.p-t-md { padding-top:    ($spacer-y * 1.5) !important; }
.p-r-md { padding-right:  ($spacer-y * 1.5) !important; }
.p-b-md { padding-bottom: ($spacer-y * 1.5) !important; }
.p-l-md { padding-left:   ($spacer-y * 1.5) !important; }
.p-x-md { padding-right:  ($spacer-x * 1.5) !important; padding-left:   ($spacer-x * 1.5) !important; }
.p-y-md { padding-top:    ($spacer-y * 1.5) !important; padding-bottom: ($spacer-y * 1.5) !important; }

.p-a-lg { padding:        ($spacer * 3) !important; }
.p-t-lg { padding-top:    ($spacer-y * 3) !important; }
.p-r-lg { padding-right:  ($spacer-y * 3) !important; }
.p-b-lg { padding-bottom: ($spacer-y * 3) !important; }
.p-l-lg { padding-left:   ($spacer-y * 3) !important; }
.p-x-lg { padding-right:  ($spacer-x * 3) !important; padding-left:   ($spacer-x * 3) !important; }
.p-y-lg { padding-top:    ($spacer-y * 3) !important; padding-bottom: ($spacer-y * 3) !important; }
89
90
91
{% endhighlight %}


92
## Text alignment
93
94
95
96
97
98
99
100
101
102
103

Easily realign text to components with text alignment classes.

{% example html %}
<p class="text-left">Left aligned text.</p>
<p class="text-center">Center aligned text.</p>
<p class="text-right">Right aligned text.</p>
<p class="text-justify">Justified text.</p>
<p class="text-nowrap">No wrap text.</p>
{% endexample %}

104
## Text transform
105
106
107
108
109
110
111
112
113

Transform text in components with text capitalization classes.

{% example html %}
<p class="text-lowercase">Lowercased text.</p>
<p class="text-uppercase">Uppercased text.</p>
<p class="text-capitalize">Capitalized text.</p>
{% endexample %}

114
## Contextual colors and backgrounds
115

Mark Otto's avatar
Mark Otto committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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 %}

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 %}

137
138
139
{% callout info %}
#### Dealing with specificity

140
Sometimes contextual 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 `<div>` with the class.
141
142
143
144
{% endcallout %}

{% callout warning %}
#### Conveying meaning to assistive technologies
Mark Otto's avatar
Mark Otto committed
145

146
Ensure that any meaning conveyed through color is also conveyed in a format that is not purely presentational.
147
{% endcallout %}
Mark Otto's avatar
Mark Otto committed
148

149
## Close icon
Mark Otto's avatar
Mark Otto committed
150
151
152
153

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
154
<button type="button" class="close" aria-label="Close">
Mark Otto's avatar
Mark Otto committed
155
156
157
158
159
  <span aria-hidden="true">&times;</span>
  <span class="sr-only">Close</span>
</button>
{% endexample %}

160
## Floats
Mark Otto's avatar
Mark Otto committed
161
162
163
164

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
165
166
<div class="pull-left">Float left</div>
<div class="pull-right">Float right</div>
Mark Otto's avatar
Mark Otto committed
167
168
{% endexample %}

Mark Otto's avatar
Mark Otto committed
169
170
171
172
173
174
175
176
177
178
179
{% highlight scss %}
// Classes
.pull-left {
  float: left !important;
}
.pull-right {
  float: right !important;
}

// Usage as mixins
.element {
Mark Otto's avatar
Mark Otto committed
180
  @include pull-left;
Mark Otto's avatar
Mark Otto committed
181
182
}
.another-element {
Mark Otto's avatar
Mark Otto committed
183
  @include pull-right;
Mark Otto's avatar
Mark Otto committed
184
185
186
}
{% endhighlight %}

187
## Center content
Mark Otto's avatar
Mark Otto committed
188
189
190
191

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
192
<div class="center-block">Centered block</div>
Mark Otto's avatar
Mark Otto committed
193
194
{% endexample %}

Mark Otto's avatar
Mark Otto committed
195
{% highlight scss %}
Mark Otto's avatar
Mark Otto committed
196
// Class
Mark Otto's avatar
Mark Otto committed
197
198
199
200
201
202
.center-block {
  display: block;
  margin-left: auto;
  margin-right: auto;
}

Mark Otto's avatar
Mark Otto committed
203
// Usage as a mixin
Mark Otto's avatar
Mark Otto committed
204
.element {
205
  @include center-block;
Mark Otto's avatar
Mark Otto committed
206
207
208
}
{% endhighlight %}

Mark Otto's avatar
Mark Otto committed
209
210
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
211
212
213
{% highlight html %}
<div class="clearfix">...</div>
{% endhighlight %}
Mark Otto's avatar
Mark Otto committed
214

Mark Otto's avatar
Mark Otto committed
215
216
217
218
219
220
221
222
223
224
225
226
227
{% highlight scss %}
// Mixin itself
.clearfix() {
  &:before,
  &:after {
    content: " ";
    display: table;
  }
  &:after {
    clear: both;
  }
}

Mark Otto's avatar
Mark Otto committed
228
// Usage as a mixin
Mark Otto's avatar
Mark Otto committed
229
.element {
230
  @include clearfix;
Mark Otto's avatar
Mark Otto committed
231
232
233
}
{% endhighlight %}

234
## Hidden content
Mark Otto's avatar
Mark Otto committed
235

236
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
237

238
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
239

Mark Otto's avatar
Mark Otto committed
240
{% highlight html %}
241
<input type="text" hidden>
Mark Otto's avatar
Mark Otto committed
242
{% endhighlight %}
Mark Otto's avatar
Mark Otto committed
243

244
## Invisible content
Mark Otto's avatar
Mark Otto committed
245

246
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.
247
248

{% highlight html %}
249
<div class="invisible">...</div>
Mark Otto's avatar
Mark Otto committed
250
251
{% endhighlight %}

252
{% highlight scss %}
Mark Otto's avatar
Mark Otto committed
253
// Class
254
255
256
257
.invisible {
  visibility: hidden;
}

Mark Otto's avatar
Mark Otto committed
258
// Usage as a mixin
259
260
261
.element {
  .invisible();
}
Mark Otto's avatar
Mark Otto committed
262
{% endhighlight %}
263

264
## Screen readers
Mark Otto's avatar
Mark Otto committed
265
266
267

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

Mark Otto's avatar
Mark Otto committed
272
{% highlight scss %}
Mark Otto's avatar
Mark Otto committed
273
// Usage as a mixin
Mark Otto's avatar
Mark Otto committed
274
.skip-navigation {
275
276
  @include sr-only;
  @include sr-only-focusable;
Mark Otto's avatar
Mark Otto committed
277
278
279
}
{% endhighlight %}

280
## Image replacement
Mark Otto's avatar
Mark Otto committed
281
282
283

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

Mark Otto's avatar
Mark Otto committed
284
{% highlight html %}
Mark Otto's avatar
Mark Otto committed
285
<h1 class="text-hide">Custom heading</h1>
Mark Otto's avatar
Mark Otto committed
286
{% endhighlight %}
Mark Otto's avatar
Mark Otto committed
287
288

{% highlight scss %}
Mark Otto's avatar
Mark Otto committed
289
// Usage as a mixin
Mark Otto's avatar
Mark Otto committed
290
.heading {
291
  @include text-hide;
Mark Otto's avatar
Mark Otto committed
292
293
}
{% endhighlight %}
294

295
## Responsive embeds
296
297
298
299
300
301
302
303
304
305
306
307
308

Allow browsers to determine video or slideshow dimensions based on the width of their containing block by creating an intrinsic ratio that will properly scale on any device.

Rules are directly applied to `<iframe>`, `<embed>`, `<video>`, and `<object>` elements; optionally use an explicit descendant class `.embed-responsive-item` when you want to match the styling for other attributes.

**Pro-Tip!** You don't need to include `frameborder="0"` in your `<iframe>`s as we override that for you.

{% example html %}
<div class="embed-responsive embed-responsive-16by9">
  <iframe class="embed-responsive-item" src="//www.youtube.com/embed/zpOULjyy-n8?rel=0" allowfullscreen></iframe>
</div>
{% endexample %}

309
Aspect ratios can be customized with modifier classes.
310
311

{% highlight html %}
312
313
314
315
316
<!-- 21:9 aspect ratio -->
<div class="embed-responsive embed-responsive-21by9">
  <iframe class="embed-responsive-item" src="..."></iframe>
</div>

317
318
319
320
321
322
323
324
325
326
<!-- 16:9 aspect ratio -->
<div class="embed-responsive embed-responsive-16by9">
  <iframe class="embed-responsive-item" src="..."></iframe>
</div>

<!-- 4:3 aspect ratio -->
<div class="embed-responsive embed-responsive-4by3">
  <iframe class="embed-responsive-item" src="..."></iframe>
</div>
{% endhighlight %}