Card
- Card
- Styling
Style Variants
The Card component supports the following style variants:
new tab
Source code
CardVariants.java
package com.vaadin.demo.component.card;
import com.vaadin.flow.component.card.Card;
import com.vaadin.flow.component.card.CardVariant;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.dom.Style.Display;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.theme.lumo.LumoUtility;
@Route("card-variants")
public class CardVariants extends Div {
public CardVariants() {
Card cardDefault = new Card();
Card cardOutlined = new Card();
cardOutlined.addThemeVariants(CardVariant.LUMO_OUTLINED);
Card cardElevated = new Card();
cardElevated.addThemeVariants(CardVariant.LUMO_ELEVATED);
cardDefault.setTitle("Default");
cardDefault.add("This is the default card style.");
cardOutlined.setTitle("Outlined");
cardOutlined.add("Adds a solid outline around the card.");
cardElevated.setTitle("Elevated");
cardElevated.add("This variant works better on a shaded background.");
Div cardVariantsLayout = new Div(cardDefault, cardOutlined, cardElevated);
cardVariantsLayout.addClassNames(LumoUtility.Gap.MEDIUM, LumoUtility.Display.GRID);
cardVariantsLayout.getStyle().set("grid-template-columns", "repeat(auto-fit, minmax(12ch, 1fr))");
add(cardVariantsLayout);
}
}
card-variants.tsx
card-variants.ts
Horizontal
Place all card content on the side of the media element, if provided.
new tab
Source code
CardHorizontal.java
package com.vaadin.demo.component.card;
import com.vaadin.demo.DemoExporter;
import com.vaadin.flow.component.avatar.Avatar;
import com.vaadin.flow.component.card.CardVariant;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.router.Route;
@Route("card-horizontal")
public class CardHorizontal extends Div {
public CardHorizontal() {
Card card = new Card();
card.addThemeVariants(CardVariant.LUMO_HORIZONTAL);
card.setMedia(new Avatar("Lapland"));
card.setTitle(new Div("Lapland"));
card.setSubtitle(new Div("The Exotic North"));
card.add("Lapland is the northern-most region of Finland and an active outdoor destination.");
card.setMaxWidth("300px");
add(card);
}
}
card-horizontal.tsx
card-horizontal.ts
Stretch Media
Stretches the media element as wide – or tall, if combined with the horizontal variant – as the card, if the media element is an image, video, or an icon.
new tab
Source code
CardStretchMedia.java
package com.vaadin.demo.component.card;
import com.vaadin.flow.component.card.Card;
import com.vaadin.flow.component.card.CardVariant;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.Image;
import com.vaadin.flow.component.icon.Icon;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.StreamResource;
import com.vaadin.flow.theme.lumo.LumoIcon;
@Route("card-stretch-media")
public class CardStretchMedia extends Div {
public CardStretchMedia() {
Div layout = new Div();
layout.getStyle().set("display", "grid")
.set("grid-template-columns", "repeat(auto-fill, minmax(200px, 1fr))")
.set("gap", "1em");
// Card with stretched image
Card imageCard = new Card();
imageCard.addThemeVariants(CardVariant.LUMO_STRETCH_MEDIA);
StreamResource imageResource = new StreamResource("lapland.avif",
() -> getClass().getResourceAsStream("/images/lapland.avif"));
Image image = new Image(imageResource, "");
imageCard.setMedia(image);
imageCard.setTitle(new Div("Lapland"));
imageCard.setSubtitle(new Div("The Exotic North"));
imageCard.add("Lapland is the northern-most region of Finland and an active outdoor destination.");
// Card with stretched icon
Card iconCard = new Card();
iconCard.addThemeVariants(CardVariant.LUMO_STRETCH_MEDIA);
Icon icon = LumoIcon.PHOTO.create();
icon.getStyle()
.setColor("var(--lumo-primary-color)")
.setBackgroundColor("var(--lumo-primary-color-10pct)");
iconCard.setMedia(icon);
iconCard.setTitle(new Div("Lapland"));
iconCard.setSubtitle(new Div("The Exotic North"));
iconCard.add("Lapland is the northern-most region of Finland and an active outdoor destination.");
layout.add(imageCard, iconCard);
add(layout);
}
}
card-stretch-media.tsx
card-stretch-media.ts
Cover Media
Similar to the stretch-media variant, but this variant allows the media element to also cover the padding area of the card. This variant overrides the stretch-media variant.
new tab
Source code
CardCoverMedia.java
package com.vaadin.demo.component.card;
import com.vaadin.flow.component.card.Card;
import com.vaadin.flow.component.card.CardVariant;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.Image;
import com.vaadin.flow.component.icon.Icon;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.StreamResource;
import com.vaadin.flow.theme.lumo.LumoIcon;
@Route("card-cover-media")
public class CardCoverMedia extends Div {
public CardCoverMedia() {
Div layout = new Div();
layout.getStyle().set("display", "grid")
.set("grid-template-columns", "repeat(auto-fill, minmax(200px, 1fr))")
.set("gap", "1em");
// Card with cover image
Card imageCard = new Card();
imageCard.addThemeVariants(CardVariant.LUMO_COVER_MEDIA);
StreamResource imageResource = new StreamResource("lapland.avif",
() -> getClass().getResourceAsStream("/images/lapland.avif"));
Image image = new Image(imageResource, "");
imageCard.setMedia(image);
imageCard.setTitle(new Div("Lapland"));
imageCard.setSubtitle(new Div("The Exotic North"));
imageCard.add("Lapland is the northern-most region of Finland and an active outdoor destination.");
// Card with cover icon
Card iconCard = new Card();
iconCard.addThemeVariants(CardVariant.LUMO_COVER_MEDIA);
Icon icon = LumoIcon.PHOTO.create();
icon.getStyle()
.setColor("var(--lumo-primary-color)")
.setBackgroundColor("var(--lumo-primary-color-10pct)");
iconCard.setMedia(icon);
iconCard.setTitle(new Div("Lapland"));
iconCard.setSubtitle(new Div("The Exotic North"));
iconCard.add("Lapland is the northern-most region of Finland and an active outdoor destination.");
layout.add(imageCard, iconCard);
add(layout);
}
}
card-cover-media.tsx
card-cover-media.ts
Combine Variants
You can combine all style variants together.
new tab
Source code
CardCombineVariants.java
package com.vaadin.demo.component.card;
import com.vaadin.flow.component.card.Card;
import com.vaadin.flow.component.card.CardVariant;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.Image;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.StreamResource;
@Route("card-combine-variants")
public class CardCombineVariants extends Div {
public CardCombineVariants() {
Card card = new Card();
card.addThemeVariants(
CardVariant.LUMO_OUTLINED,
CardVariant.LUMO_ELEVATED,
CardVariant.LUMO_HORIZONTAL,
CardVariant.LUMO_COVER_MEDIA
);
StreamResource imageResource = new StreamResource("lapland.avif",
() -> getClass().getResourceAsStream("/images/lapland.avif"));
Image image = new Image(imageResource, "");
image.setWidth("200px");
card.setMedia(image);
card.setTitle(new Div("Lapland"));
card.setSubtitle(new Div("The Exotic North"));
card.add(new Div("Lapland is the northern-most region of Finland and an active outdoor destination."));
add(card);
}
}
card-combine-variants.tsx
card-combine-variants.ts
Style Properties
The following style properties can be used in CSS stylesheets to customize the appearance of this component.
To apply values to these properties globally in your application UI, place them in a CSS block using the html {…}
selector.
See Lumo Style Properties for more information on style properties.
Common Properties
Feature | Property | Default Value |
---|---|---|
Background |
|
|
Box Shadow |
|
|
Border Width |
|
|
Border Color |
|
|
Border Radius |
|
|
Padding |
|
|
Gap |
|
|
Media Aspect Ratio |
|
|
CSS Selectors
The following CSS selectors can be used in stylesheets to target the various parts and states of the component. See the Styling documentation for more details on how to style components.
- Root element
-
vaadin-card