Documentation
Feedback
Guides
Learning Center

Learning Center
Learning Center
CSS Handles and the power of customizing blocks
View in Portuguese
This content was migrated from the VTEX Learning Center and is no longer being actively maintained. For the most accurate and up-to-date information, please check the official documentation.

Introduction

Taking a quick look at your online store, you'll notice that components have similar styles, even without applying any customization.

All of them share pre-established values for font, background color, main color, button format, etc. For instance, the Info Card.

This is due to the style.json, the file responsible for declaring generic customization values for every Store Framework store.

{"base64":"  ","img":{"width":1920,"height":1004,"type":"png","mime":"image/png","wUnits":"px","hUnits":"px","length":234037,"url":"https://user-images.githubusercontent.com/19495917/90165970-e0274600-dd6f-11ea-908d-a9f50b1b90a1.png"}}

To create a unique identity for your store's components, you can overwrite these values using CSS customizations.

Analyzing the recipe employed to customize stores using CSS, we can notice that several steps will be needed in order to apply your own style, such as:

  1. Create a new file in the CSS folder, naming it vtex.{AppName}.css.

  2. Use the CSS Handle of the component that will be customized in this file in the following format:


    _10
    .{CSSHandle} {
    _10
    {CSSProperty}: {DesiredValue};
    _10
    {CSSProperty}: {DesiredValue};
    _10
    }

  3. Lacking CSS Handles, apply permitted CSS Selectors, such as :global(vtex-{componentName}).

  4. To apply CSS to a specific block and not to every block of that type, use the blockClass resource, which appears next to the CSS Handles when inspecting your code. The blockClass must be declared as a prop in the block in question, and thus be referenced in the style file as shown below:


    _10
    .{CSSHandle}--{blockClass} {
    _10
    {CSSProperty}: {DesiredValue};
    _10
    {CSSProperty}: {DesiredValue};
    _10
    }

Adding Info Card to the Minimum Boilerplate

Before actually customizing an info card, it's necessary to add one, since our theme is really simple. With that in mind, go to home.jsonc file and add two info cards to its blocks:


_17
{
_17
"store.home": {
_17
"blocks": [
_17
"rich-text",
_17
+ "info-card#clearance",
_17
+ "info-card#vintage"
_17
]
_17
},
_17
"rich-text": {
_17
"props": {
_17
"text": "VTEX Store Framework",
_17
"textAlignment": "CENTER",
_17
"textPosition": "CENTER",
_17
"font": "t-heading-1"
_17
}
_17
}
_17
}

Now, let's define those blocks in the same file, right below the rich-text definition. You can use the following code block to insert on the file:


_25
"info-card#clearance": {
_25
"props": {
_25
"id": "info-card-clearance",
_25
"isFullModeStyle": false,
_25
"textPosition": "left",
_25
"imageUrl": "https://storecomponents.vteximg.com.br/arquivos/banner-infocard2.png",
_25
"headline": "Clearance Sale",
_25
"callToActionText": "DISCOVER",
_25
"callToActionUrl": "/sale/d",
_25
"textAlignment": "center"
_25
}
_25
},
_25
"info-card#vintage": {
_25
"props": {
_25
"id": "info-card-vintage",
_25
"isFullModeStyle": false,
_25
"textPosition": "right",
_25
"imageUrl": "https://appliancetheme.vteximg.com.br/arquivos/cozinha-rosa-min.png",
_25
"headline": "Vintage",
_25
"callToActionText": "DISCOVER",
_25
"callToActionUrl": "/sale/d",
_25
"textAlignment": "center",
_25
"blockClass": "vintage"
_25
}
_25
}

By doing that, you're expected to see the home page similar to the following image:

{"base64":"  ","img":{"width":1752,"height":912,"type":"png","mime":"image/png","wUnits":"px","hUnits":"px","length":1888099,"url":"https://user-images.githubusercontent.com/19495917/90164957-66db2380-dd6e-11ea-88ea-c3a19b741b5b.png"}}

Customizing the Info Card you've just added

To uncover a component's CSS Handles, such as the Info Card, simply access your documentation's Customization section.

According to the description of CSS Handles and to the store customization recipe using CSS, we can implement a customized Info Card example.

  1. On the vtex.store-components.css file, found in /styles/css, let's edit its title and call-to-action button configurations by adding the following code:


    _21
    /* /styles/css/vtex.store-components.css */
    _21
    .infoCardHeadline {
    _21
    font-family: serif;
    _21
    font-size: 2.25rem;
    _21
    font-weight: normal;
    _21
    color: gray;
    _21
    border: 2px solid black;
    _21
    padding: 24px;
    _21
    }
    _21
    _21
    .infoCardCallActionContainer :global(.vtex-button) {
    _21
    color: white;
    _21
    background-color: gray;
    _21
    border: transparent;
    _21
    }
    _21
    _21
    .infoCardCallActionContainer :global(.vtex-button):hover {
    _21
    color: gray;
    _21
    background-color: blue;
    _21
    border: transparent;
    _21
    }

    You can check the effect of these changes by running the vtex link command.

    {"base64":"  ","img":{"width":1753,"height":902,"type":"png","mime":"image/png","wUnits":"px","hUnits":"px","length":1887834,"url":"https://user-images.githubusercontent.com/19495917/90165063-82dec500-dd6e-11ea-8b0d-802fa5afc17f.png"}}

  2. Next, we'll add a specific style to the vintage info card. In order to do that, we first need to add blockClass, as shown below:


    _13
    "info-card#vintage": {
    _13
    "props": {
    _13
    "id": "info-card-vintage",
    _13
    "isFullModeStyle": false,
    _13
    "textPosition": "right",
    _13
    "imageUrl": "https://appliancetheme.vteximg.com.br/arquivos/cozinha-rosa-min.png",
    _13
    "headline": "Vintage",
    _13
    "callToActionText": "DISCOVER",
    _13
    "callToActionUrl": "/sale/d",
    _13
    "textAlignment": "center",
    _13
    + "blockClass": "vintage"
    _13
    }
    _13
    }

  3. Thereafter, declare a background-color for this specific info card in your CSS file:


    _10
    /* /styles/css/vtex.store-components.css */
    _10
    .infoCardContainer--vintage {
    _10
    background-color: #edcfd1;
    _10
    }

    The result should be similar to the image below:

    {"base64":"  ","img":{"width":1919,"height":907,"type":"png","mime":"image/png","wUnits":"px","hUnits":"px","length":1896774,"url":"https://user-images.githubusercontent.com/19495917/90165339-e4069880-dd6e-11ea-89bf-80e63a25ffb4.png"}}

  4. Now let's finish by adding some more style to our Info Card. On the same CSS file that was used before, define a maximum width (max-width) of 1260px for all info cards, a margin of 0 auto and a padding of 0.

    You can do it based on the Info Card Handles.


    _10
    /* /styles/css/vtex.store-components.css */
    _10
    .infoCardContainer {
    _10
    max-width: 1260px;
    _10
    margin: 0 auto;
    _10
    padding: 0;
    _10
    }

    By doing that, the expected result is:

    {"base64":"  ","img":{"width":1920,"height":975,"type":"png","mime":"image/png","wUnits":"px","hUnits":"px","length":1232809,"url":"https://user-images.githubusercontent.com/19495917/90165563-38aa1380-dd6f-11ea-9343-843ccc83d2f7.png"}}

  5. Change the component's title color to black and the font weight to bold:


    _10
    .infoCardHeadline {
    _10
    font-family: serif;
    _10
    font-size: 2.25rem;
    _10
    - font-weight: normal;
    _10
    - color: gray;
    _10
    + font-weight: bold;
    _10
    + color: black;
    _10
    border: 2px solid black;
    _10
    padding: 24px;
    _10
    }

    After doing that, you should have:

    {"base64":"  ","img":{"width":1919,"height":977,"type":"png","mime":"image/png","wUnits":"px","hUnits":"px","length":1238660,"url":"https://user-images.githubusercontent.com/19495917/90165764-8d4d8e80-dd6f-11ea-92f6-cadce9318dff.png"}}

  6. Change the call-to-action buttons background color during hover to white:


    _10
    .infoCardCallActionContainer :global(.vtex-button):hover {
    _10
    color: gray;
    _10
    - background-color: blue;
    _10
    + background-color: white;
    _10
    border: transparent;
    _10
    }

Make sure that you test also the hovering action in order to validate if the color is correct for this action!


Answer sheet

See answer sheet for home.jsonc
home.jsonc

_39
// minimum-boilerplate-theme/store/blocks/home.jsonc
_39
{
_39
"store.home": {
_39
"blocks": ["rich-text", "info-card#clearance", "info-card#vintage"]
_39
},
_39
"rich-text": {
_39
"props": {
_39
"text": "VTEX Store Framework",
_39
"textAlignment": "CENTER",
_39
"textPosition": "CENTER",
_39
"font": "t-heading-1"
_39
}
_39
},
_39
"info-card#clearance": {
_39
"props": {
_39
"id": "info-card-clearance",
_39
"isFullModeStyle": false,
_39
"textPosition": "left",
_39
"imageUrl": "https://storecomponents.vteximg.com.br/arquivos/banner-infocard2.png",
_39
"headline": "Clearance Sale",
_39
"callToActionText": "DISCOVER",
_39
"callToActionUrl": "/sale/d",
_39
"textAlignment": "center"
_39
}
_39
},
_39
"info-card#vintage": {
_39
"props": {
_39
"id": "info-card-vintage",
_39
"isFullModeStyle": false,
_39
"textPosition": "right",
_39
"imageUrl": "https://appliancetheme.vteximg.com.br/arquivos/cozinha-rosa-min.png",
_39
"headline": "Vintage",
_39
"callToActionText": "DISCOVER",
_39
"callToActionUrl": "/sale/d",
_39
"textAlignment": "center",
_39
"blockClass": "vintage"
_39
}
_39
}
_39
}

See answer sheet for vtex.store-components.css
vtex.store-components.css

_31
/* styles/css/vtex.store-components.css */
_31
.infoCardHeadline {
_31
font-family: serif;
_31
font-size: 2.25rem;
_31
font-weight: bold;
_31
color: black;
_31
border: 2px solid black;
_31
padding: 24px;
_31
}
_31
_31
.infoCardCallActionContainer :global(.vtex-button) {
_31
color: white;
_31
background-color: gray;
_31
border: transparent;
_31
}
_31
_31
.infoCardCallActionContainer :global(.vtex-button):hover {
_31
color: gray;
_31
background-color: white;
_31
border: transparent;
_31
}
_31
_31
.infoCardContainer--vintage {
_31
background-color: #edcfd1;
_31
}
_31
_31
.infoCardContainer {
_31
max-width: 1260px;
_31
margin: 0 auto;
_31
padding: 0;
_31
}

On this page