helpers.md 6.11 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
55
56
57
58
59
60
61
62
63
64
65
### 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 %}

### 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
81
82
83
84
85
86
87
{% 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
88
89
90
91
<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
92

Mark Otto's avatar
Mark Otto committed
93
94
95
96
97
### 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
98
<div class="center-block">...</div>
Mark Otto's avatar
Mark Otto committed
99
100
{% endexample %}

Mark Otto's avatar
Mark Otto committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
{% 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
115
116
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
117
118
119
{% highlight html %}
<div class="clearfix">...</div>
{% endhighlight %}
Mark Otto's avatar
Mark Otto committed
120

Mark Otto's avatar
Mark Otto committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
{% highlight scss %}
// Mixin itself
.clearfix() {
  &:before,
  &:after {
    content: " ";
    display: table;
  }
  &:after {
    clear: both;
  }
}

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

140
### Making content invisible
Mark Otto's avatar
Mark Otto committed
141

142
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.
Mark Otto's avatar
Mark Otto committed
143

Mark Otto's avatar
Mark Otto committed
144
{% highlight html %}
145
<div class="invisible">...</div>
Mark Otto's avatar
Mark Otto committed
146
{% endhighlight %}
Mark Otto's avatar
Mark Otto committed
147

Mark Otto's avatar
Mark Otto committed
148
149
150
151
152
153
{% highlight scss %}
// Classes
.invisible {
  visibility: hidden;
}

154
// Usage as mixin
Mark Otto's avatar
Mark Otto committed
155
.element {
156
  .invisible();
Mark Otto's avatar
Mark Otto committed
157
158
159
}
{% endhighlight %}

Mark Otto's avatar
Mark Otto committed
160
161
162
163
### 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
164
165
166
{% highlight html %}
<a class="sr-only sr-only-focusable" href="#content">Skip to main content</a>
{% endhighlight %}
Mark Otto's avatar
Mark Otto committed
167

Mark Otto's avatar
Mark Otto committed
168
169
170
171
172
173
174
175
{% highlight scss %}
// Usage as a Mixin
.skip-navigation {
  .sr-only();
  .sr-only-focusable();
}
{% endhighlight %}

Mark Otto's avatar
Mark Otto committed
176
177
178
179
180
### 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
181
<h1 class="text-hide">Custom heading</h1>
Mark Otto's avatar
Mark Otto committed
182
183
184
{% endexample %}

{% highlight scss %}
Mark Otto's avatar
Mark Otto committed
185
186
187
188
189
// Usage as a Mixin
.heading {
  .text-hide();
}
{% endhighlight %}