utilities.md 10.9 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 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
## Spacing

16
Assign `margin` or `padding` to an element or a subset of its 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`.
17

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
The classes are named using the format: `{property}-{sides}-{size}`

Where *property* is one of:
* `m` - for classes that set `margin`
* `p` - for classes that set `padding`

Where *sides* is one of:
* `t` - for classes that set `margin-top` or `padding-top`
* `b` - for classes that set `margin-bottom` or `padding-bottom`
* `l` - for classes that set `margin-left` or `padding-left`
* `r` - for classes that set `margin-right` or `padding-right`
* `x` - for classes that set both `*-left` and `*-right`
* `y` - for classes that set both `*-top` and `*-bottom`
* `a` - for classes that set a `margin` or `padding` on all 4 sides of the element

Where *size* is one of:
* `0` - for classes that eliminate the `margin` or `padding` by setting it to `0`
* `1` - (by default) for classes that set the `margin` or `padding` to `$spacer-x` or `$spacer-y`
* `2` - (by default) for classes that set the `margin` or `padding` to `$spacer-x * 1.5` or `$spacer-y * 1.5`
* `3` - (by default) for classes that set the `margin` or `padding` to `$spacer-x * 3` or `$spacer-y * 3`

Here are some representative examples of these classes:
40

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
```scss
.m-t-0 {
  margin-top: 0 !important;
}

.m-l-1 {
  margin-left: $spacer-x !important;
}

.p-x-2 {
  padding-left: ($spacer-x * 1.5) !important;
  padding-right: ($spacer-x * 1.5) !important;
}

.p-a-3 {
  padding: ($spacer-y * 3) ($spacer-x * 3) !important;
}
```

Additionally, Bootstrap also includes an `.m-x-auto` class which sets the horizontal margins to `auto`.
61
62


63
## Text alignment
64
65
66
67
68
69
70
71

Easily realign text to components with text alignment classes.

{% example html %}
<p class="text-justify">Justified text.</p>
<p class="text-nowrap">No wrap text.</p>
{% endexample %}

72
73
74
75
76
77
78
79
80
81
82
83
84
For left, right, and center alignment, responsive classes are available that use the same viewport width breakpoints as the grid system.

{% example html %}
<p class="text-xs-left">Left aligned text on all viewport sizes.</p>
<p class="text-xs-center">Center aligned text on all viewport sizes.</p>
<p class="text-xs-right">Right aligned text on all viewport sizes.</p>

<p class="text-sm-left">Left aligned text on viewports sized SM (small) or wider.</p>
<p class="text-md-left">Left aligned text on viewports sized MD (medium) or wider.</p>
<p class="text-lg-left">Left aligned text on viewports sized LG (large) or wider.</p>
<p class="text-xl-left">Left aligned text on viewports sized XL (extra-large) or wider.</p>
{% endexample %}

85
## Text transform
86
87
88
89
90
91

Transform text in components with text capitalization classes.

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

95
## Font weight and italics
96

97
Quickly change the weight (boldness) of text or italicize text.
98
99

{% example html %}
100
101
<p class="font-weight-bold">Bold text.</p>
<p class="font-weight-normal">Normal weight text.</p>
102
103
104
<p class="font-italic">Italicized text.</p>
{% endexample %}

105
## Contextual colors and backgrounds
106

Mark Otto's avatar
Mark Otto committed
107
108
109
110
111
112
113
114
115
116
117
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 %}

118
119
120
121
122
123
124
125
126
127
128
Contextual text classes also work well on anchors with the provided hover and focus states.

{% example html %}
<a href="#" class="text-muted">Muted link</a>
<a href="#" class="text-primary">Primary link</a>
<a href="#" class="text-success">Success link</a>
<a href="#" class="text-info">Info link</a>
<a href="#" class="text-warning">Warning link</a>
<a href="#" class="text-danger">Danger link</a>
{% endexample %}

Mark Otto's avatar
Mark Otto committed
129
130
131
132
133
134
135
136
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>
petetnt's avatar
petetnt committed
137
<div class="bg-inverse">Cras mattis consectetur purus sit amet fermentum.</div>
Mark Otto's avatar
Mark Otto committed
138
139
{% endexample %}

140
141
142
{% callout info %}
#### Dealing with specificity

143
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.
144
145
146
147
{% endcallout %}

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

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

152
## Close icon
Mark Otto's avatar
Mark Otto committed
153

154
Use a generic close icon for dismissing content like modals and alerts. **Be sure to include text for screen readers**, as we've done with `aria-label`.
Mark Otto's avatar
Mark Otto committed
155
156

{% example html %}
Mark Otto's avatar
Mark Otto committed
157
<button type="button" class="close" aria-label="Close">
Mark Otto's avatar
Mark Otto committed
158
159
160
161
  <span aria-hidden="true">&times;</span>
</button>
{% endexample %}

162
## Responsive floats
Mark Otto's avatar
Mark Otto committed
163

164
165
166
These utility classes float an element to the left or right, or disable floating, based on the current viewport size using the [CSS `float` property](https://developer.mozilla.org/en-US/docs/Web/CSS/float). `!important` is included to avoid specificity issues. These use the same viewport width breakpoints as the grid system.

Two similar non-responsive mixins (`pull-left` and `pull-right`) are also available.
Mark Otto's avatar
Mark Otto committed
167
168

{% example html %}
169
170
171
172
173
174
175
176
<div class="pull-xs-left">Float left on all viewport sizes</div>
<div class="pull-xs-right">Float right on all viewport sizes</div>
<div class="pull-xs-none">Don't float on all viewport sizes</div>

<div class="pull-sm-left">Float left on viewports sized SM (small) or wider</div>
<div class="pull-md-left">Float left on viewports sized MD (medium) or wider</div>
<div class="pull-lg-left">Float left on viewports sized LG (large) or wider</div>
<div class="pull-xl-left">Float left on viewports sized XL (extra-large) or wider</div>
Mark Otto's avatar
Mark Otto committed
177
178
{% endexample %}

Mark Otto's avatar
Mark Otto committed
179
{% highlight scss %}
180
// Related simple non-responsive mixins
Mark Otto's avatar
Mark Otto committed
181
.element {
Mark Otto's avatar
Mark Otto committed
182
  @include pull-left;
Mark Otto's avatar
Mark Otto committed
183
184
}
.another-element {
Mark Otto's avatar
Mark Otto committed
185
  @include pull-right;
Mark Otto's avatar
Mark Otto committed
186
187
188
}
{% endhighlight %}

189
## Center content
Mark Otto's avatar
Mark Otto committed
190
191
192
193

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

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

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

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

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

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

236
## Invisible content
Mark Otto's avatar
Mark Otto committed
237

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

{% highlight html %}
241
<div class="invisible">...</div>
Mark Otto's avatar
Mark Otto committed
242
243
{% endhighlight %}

244
{% highlight scss %}
Mark Otto's avatar
Mark Otto committed
245
// Class
246
247
248
249
.invisible {
  visibility: hidden;
}

Mark Otto's avatar
Mark Otto committed
250
// Usage as a mixin
251
252
253
.element {
  .invisible();
}
Mark Otto's avatar
Mark Otto committed
254
{% endhighlight %}
255

256
## Screen readers and keyboard users
Mark Otto's avatar
Mark Otto committed
257

Heinrich Fenkart's avatar
Heinrich Fenkart committed
258
259
260
261
262
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). Can also be used as mixins.

{% comment %}
Necessary for following [accessibility best practices](../getting-started/#accessibility).
{% endcomment %}
Mark Otto's avatar
Mark Otto committed
263

Mark Otto's avatar
Mark Otto committed
264
265
266
{% highlight html %}
<a class="sr-only sr-only-focusable" href="#content">Skip to main content</a>
{% endhighlight %}
Mark Otto's avatar
Mark Otto committed
267

Mark Otto's avatar
Mark Otto committed
268
{% highlight scss %}
Mark Otto's avatar
Mark Otto committed
269
// Usage as a mixin
Mark Otto's avatar
Mark Otto committed
270
.skip-navigation {
271
272
  @include sr-only;
  @include sr-only-focusable;
Mark Otto's avatar
Mark Otto committed
273
274
275
}
{% endhighlight %}

276
## Image replacement
Mark Otto's avatar
Mark Otto committed
277
278
279

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
280
{% highlight html %}
Mark Otto's avatar
Mark Otto committed
281
<h1 class="text-hide">Custom heading</h1>
Mark Otto's avatar
Mark Otto committed
282
{% endhighlight %}
Mark Otto's avatar
Mark Otto committed
283
284

{% highlight scss %}
Mark Otto's avatar
Mark Otto committed
285
// Usage as a mixin
Mark Otto's avatar
Mark Otto committed
286
.heading {
287
  @include text-hide;
Mark Otto's avatar
Mark Otto committed
288
289
}
{% endhighlight %}
290

291
## Responsive embeds
292
293
294
295
296
297
298
299
300
301
302
303
304

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

305
Aspect ratios can be customized with modifier classes.
306
307

{% highlight html %}
308
309
310
311
312
<!-- 21:9 aspect ratio -->
<div class="embed-responsive embed-responsive-21by9">
  <iframe class="embed-responsive-item" src="..."></iframe>
</div>

313
314
315
316
317
318
319
320
321
<!-- 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>
322
323
324
325
326

<!-- 1:1 aspect ratio -->
<div class="embed-responsive embed-responsive-1by1">
  <iframe class="embed-responsive-item" src="..."></iframe>
</div>
327
{% endhighlight %}