1 |
|
2 | title: "Cards"
|
3 | layout: detail
|
4 | section: components
|
5 | excerpt: "Cards contain content and actions about a single subject."
|
6 | iconId: card
|
7 | path: /catalog/cards/
|
8 | -->
|
9 |
|
10 | # Cards
|
11 |
|
12 | [Cards](https://material.io/components/cards/) contain content and actions about a single subject.
|
13 |
|
14 | For additional information, see the [API](#api).
|
15 |
|
16 | 
|
17 |
|
18 | ## Using cards
|
19 |
|
20 | ### Installation
|
21 |
|
22 | ```
|
23 | npm install @material/card
|
24 | ```
|
25 |
|
26 | ### Styles
|
27 |
|
28 | ```css
|
29 | @use "@material/card";
|
30 |
|
31 | @include card.core-styles;
|
32 | ```
|
33 |
|
34 | In order to remain general-purpose and support e.g. images spanning the full width of the card, MDC Card does not
|
35 | include padding styles on the root element. When adding free-form text content to cards, padding should be set to
|
36 | `16px`:
|
37 |
|
38 | ```css
|
39 | .my-card-content {
|
40 | padding: 16px;
|
41 | }
|
42 | ```
|
43 |
|
44 | **Note: MDC Card's predefined classes for content areas (e.g. `mdc-card__actions`) take care of their own padding.**
|
45 |
|
46 | By default, cards expand horizontally to fill all available space, and vertically to fit their contents.
|
47 | If you'd like to maintain a consistent width and height across cards, you'll need to set it in your styles:
|
48 |
|
49 | ```css
|
50 | .my-card {
|
51 | height: 350px;
|
52 | width: 350px;
|
53 | }
|
54 | ```
|
55 |
|
56 | You can also place cards within layout containers, such as [MDC Layout Grid](../mdc-layout-grid)
|
57 | or CSS Flexbox or Grid.
|
58 |
|
59 | ### JavaScript
|
60 |
|
61 | MDC Card itself does not require JavaScript. However, if you place interactive components inside your cards,
|
62 | you may want to instantiate ripples or other components. For example:
|
63 |
|
64 | ```js
|
65 | import {MDCRipple} from '@material/ripple';
|
66 |
|
67 | const selector = '.mdc-button, .mdc-icon-button, .mdc-card__primary-action';
|
68 | const ripples = [].map.call(document.querySelectorAll(selector), function(el) {
|
69 | return new MDCRipple(el);
|
70 | });
|
71 | ```
|
72 |
|
73 | **Note: If your card includes any [icon button toggles](../mdc-icon-button#icon-button-toggle), you will want to
|
74 | instantiate `MDCIconButtonToggle` instead of `MDCRipple`.**
|
75 |
|
76 | ## Card
|
77 |
|
78 | ### Card example
|
79 |
|
80 | ```html
|
81 | <div class="mdc-card">
|
82 | <!-- ... content ... -->
|
83 | </div>
|
84 | ```
|
85 |
|
86 | **Note: MDC Card is designed to accommodate a wide variety of use cases. See the [Card Contents](#card-contents) section for information on helpers for specific types of content within cards.**
|
87 |
|
88 | ## Other variants
|
89 |
|
90 | ### Outlined card
|
91 |
|
92 | 
|
93 |
|
94 | By default, cards are elevated with no outline. You can render unelevated, outlined cards instead by adding the
|
95 | `mdc-card--outlined` modifier class.
|
96 |
|
97 | ```html
|
98 | <div class="mdc-card mdc-card--outlined">
|
99 | <!-- ... content ... -->
|
100 | </div>
|
101 | ```
|
102 |
|
103 | ### Card contents
|
104 |
|
105 | MDC Card can be used for a wide variety of use cases, but it includes styles for a few common ones.
|
106 |
|
107 | #### Primary action
|
108 |
|
109 | If a majority of the card (or even the entire card) should be actionable, you can add the `mdc-card__primary-action`
|
110 | class to the region to give it MDC Ripple styles. You should also assign `tabindex="0"` to ensure it can also be
|
111 | interacted with via keyboard.
|
112 |
|
113 | ```html
|
114 | <div class="mdc-card">
|
115 | <div class="mdc-card__primary-action" tabindex="0">
|
116 | <!-- content within actionable area -->
|
117 | <div class="mdc-card__ripple"></div>
|
118 | </div>
|
119 | <!-- ... content ... -->
|
120 | </div>
|
121 | ```
|
122 |
|
123 | **Note: We recommend avoiding adding other actionable items within `mdc-card__primary-action`, due to the overlapping
|
124 | effect of multiple nested elements with ripple and states applied at once.**
|
125 |
|
126 | #### Rich media
|
127 |
|
128 | This area is used for showing rich media in cards, and optionally as a container. Use the `mdc-card__media` CSS class
|
129 | and the [optional modifier classes](#css-classes).
|
130 |
|
131 | ```html
|
132 | <div class="my-card__media mdc-card__media mdc-card__media--16-9">
|
133 | <div class="mdc-card__media-content">Title</div>
|
134 | </div>
|
135 | ```
|
136 |
|
137 | ```css
|
138 | .my-card__media {
|
139 | background-image: url("pretty.jpg");
|
140 | }
|
141 | ```
|
142 |
|
143 | #### Actions
|
144 |
|
145 | This area is used to show different actions the user can take, typically at the bottom of a card.
|
146 | It's often used with [buttons](../mdc-button):
|
147 |
|
148 | ```html
|
149 | <div class="mdc-card__actions">
|
150 | <button class="mdc-button mdc-card__action mdc-card__action--button">
|
151 | <div class="mdc-button__ripple"></div>
|
152 | <span class="mdc-button__label">Action 1</span>
|
153 | </button>
|
154 | <button class="mdc-button mdc-card__action mdc-card__action--button">
|
155 | <div class="mdc-button__ripple"></div>
|
156 | <span class="mdc-button__label">Action 2</span>
|
157 | </button>
|
158 | </div>
|
159 | ```
|
160 |
|
161 | It can also be used with [icon buttons](../mdc-icon-button):
|
162 |
|
163 | ```html
|
164 | <div class="mdc-card__actions">
|
165 | <button class="mdc-icon-button mdc-card__action mdc-card__action--icon"
|
166 | aria-pressed="false"
|
167 | aria-label="Add to favorites"
|
168 | title="Add to favorites">
|
169 | <i class="material-icons mdc-icon-button__icon mdc-icon-button__icon--on">favorite</i>
|
170 | <i class="material-icons mdc-icon-button__icon">favorite_border</i>
|
171 | </button>
|
172 | <button class="material-icons mdc-icon-button mdc-card__action mdc-card__action--icon" title="Share">share</button>
|
173 | <button class="material-icons mdc-icon-button mdc-card__action mdc-card__action--icon" title="More options">more_vert</button>
|
174 | </div>
|
175 | ```
|
176 |
|
177 | Be sure to include the `mdc-card__action` class on every action for correct positioning. In addition, _button_ icons
|
178 | should use the `mdc-card__action--button` class, and _icon_ actions should use the `mdc-card__action--icon` class.
|
179 |
|
180 | To have a single action button take up the entire width of the action row, use the `--full-bleed` modifier on the row:
|
181 |
|
182 | ```html
|
183 | <div class="mdc-card__actions mdc-card__actions--full-bleed">
|
184 | <a class="mdc-button mdc-card__action mdc-card__action--button" href="#">
|
185 | <div class="mdc-button__ripple"></div>
|
186 | <span class="mdc-button__label">All Business Headlines</span>
|
187 | <i class="material-icons mdc-button__icon" aria-hidden="true">arrow_forward</i>
|
188 | </a>
|
189 | </div>
|
190 | ```
|
191 |
|
192 | To display buttons _and_ icons in the same row, wrap them in `mdc-card__action-buttons` and `mdc-card__action-icons`
|
193 | elements:
|
194 |
|
195 | ```html
|
196 | <div class="mdc-card__actions">
|
197 | <div class="mdc-card__action-buttons">
|
198 | <button class="mdc-button mdc-card__action mdc-card__action--button">
|
199 | <div class="mdc-button__ripple"></div>
|
200 | <span class="mdc-button__label">Read</span>
|
201 | </button>
|
202 | <button class="mdc-button mdc-card__action mdc-card__action--button">
|
203 | <div class="mdc-button__ripple"></div>
|
204 | <span class="mdc-button__label">Bookmark</span>
|
205 | </button>
|
206 | </div>
|
207 | <div class="mdc-card__action-icons">
|
208 | <button class="material-icons mdc-icon-button mdc-card__action mdc-card__action--icon" title="Share">share</button>
|
209 | <button class="material-icons mdc-icon-button mdc-card__action mdc-card__action--icon" title="More options">more_vert</button>
|
210 | </div>
|
211 | </div>
|
212 | ```
|
213 |
|
214 | ##### Icons
|
215 |
|
216 | We recommend using [Material Icons](https://material.io/tools/icons/) from Google Fonts:
|
217 |
|
218 | ```html
|
219 | <head>
|
220 | <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
|
221 | </head>
|
222 | ```
|
223 |
|
224 | However, you can also use SVG, [Font Awesome](https://fontawesome.com/), or any other icon library you wish.
|
225 |
|
226 | #### Combined example
|
227 |
|
228 | The following is an example incorporating all of the above elements:
|
229 |
|
230 | ```html
|
231 | <div class="mdc-card">
|
232 | <div class="mdc-card__primary-action">
|
233 | <div class="mdc-card__media mdc-card__media--square">
|
234 | <div class="mdc-card__media-content">Title</div>
|
235 | </div>
|
236 | <!-- ... additional primary action content ... -->
|
237 | <div class="mdc-card__ripple"></div>
|
238 | </div>
|
239 | <div class="mdc-card__actions">
|
240 | <div class="mdc-card__action-buttons">
|
241 | <button class="mdc-button mdc-card__action mdc-card__action--button">
|
242 | <div class="mdc-button__ripple"></div>
|
243 | <span class="mdc-button__label">Action 1</span>
|
244 | </button>
|
245 | <button class="mdc-button mdc-card__action mdc-card__action--button">
|
246 | <div class="mdc-button__ripple"></div>
|
247 | <span class="mdc-button__label">Action 2</span>
|
248 | </button>
|
249 | </div>
|
250 | <div class="mdc-card__action-icons">
|
251 | <button class="material-icons mdc-icon-button mdc-card__action mdc-card__action--icon" title="Share">share</button>
|
252 | <button class="material-icons mdc-icon-button mdc-card__action mdc-card__action--icon" title="More options">more_vert</button>
|
253 | </div>
|
254 | </div>
|
255 | </div>
|
256 | ```
|
257 |
|
258 | #### Non-semantic content
|
259 |
|
260 | It can occasionally be useful to add non-semantic elements to a card. For instance, some implementations might do this to add an overlay layer.
|
261 |
|
262 | In this case, it's important to delineate between semantic (real) content and non-semantic content added by the implementation. To achieve this, simply wrap the semantic content in an `mdc-card__content` element. The non-semantic content can remain at the card's top level:
|
263 |
|
264 | ```html
|
265 | <div class="mdc-card">
|
266 | <div class="mdc-card__content">
|
267 | <!-- ... semantic content ... -->
|
268 | </div>
|
269 | <!-- ... non-semantic content ... -->
|
270 | </div>
|
271 | ```
|
272 |
|
273 | ## API
|
274 |
|
275 | ### CSS classes
|
276 |
|
277 | CSS Class | Description
|
278 | --- | ---
|
279 | `mdc-card` | Mandatory. The main card element.
|
280 | `mdc-card--outlined` | Optional. Removes the shadow and displays a hairline outline instead.
|
281 | `mdc-card__primary-action` | Optional. The main tappable area of the card. Typically contains most (or all) card content _except_ `mdc-card__actions`. Only applicable to cards that have a primary action that the main surface should trigger.
|
282 | `mdc-card__ripple` | Optional. The element that shows the ripple styling. This is mandatory if `mdc-card__primary-action` is used. Only applicable to cards that have a primary action that the main surface should trigger.
|
283 | `mdc-card__media` | Optional. Media area that displays a custom `background-image` with `background-size: cover`.
|
284 | `mdc-card__media--square` | Optional. Automatically scales the media area's height to equal its width.
|
285 | `mdc-card__media--16-9` | Optional. Automatically scales the media area's height according to its width, maintaining a 16:9 aspect ratio.
|
286 | `mdc-card__media-content` | Optional. An absolutely-positioned box the same size as the media area, for displaying a title or icon on top of the `background-image`.
|
287 | `mdc-card__actions` | Optional. Row containing action buttons and/or icons.
|
288 | `mdc-card__actions--full-bleed` | Optional. Removes the action area's padding and causes its only child (an `mdc-card__action` element) to consume 100% of the action area's width.
|
289 | `mdc-card__action-buttons` | Optional. A group of action buttons, displayed on the left side of the card (in LTR), adjacent to `mdc-card__action-icons`.
|
290 | `mdc-card__action-icons` | Optional. A group of supplemental action icons, displayed on the right side of the card (in LTR), adjacent to `__action-buttons`.
|
291 | `mdc-card__action` | Optional. An individual action button or icon.
|
292 | `mdc-card__action--button` | Optional. An action button with text.
|
293 | `mdc-card__action--icon` | Optional. An action icon with no text. We recommend using [Material Icons](https://material.io/tools/icons/) from Google Fonts.
|
294 | `mdc-card__content` | Optional. Used to delineate the card's semantic contents from any non-semantic elements (e.g., those used to implement an overlay).
|
295 |
|
296 | ### Sass mixins
|
297 |
|
298 | Mixin | Description
|
299 | --- | ---
|
300 | `fill-color($color)` | Sets the fill color of a card.
|
301 | `outline($color, $thickness)` | Sets the color and thickness of a card's outline (but does _not_ remove its shadow).
|
302 | `shape-radius($radius, $rtl-reflexive)` | Sets the rounded shape to card with given radius size. Set `$rtl-reflexive` to true to flip radius values in RTL context, defaults to false.
|
303 | `media-aspect-ratio($x, $y)` | Maintains the given aspect ratio on a `mdc-card__media` subelement by dynamically scaling its height relative to its width.
|