> For the complete documentation index, see [llms.txt](https://docs.2checkout.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.2checkout.com/2checkout-apis/2checkout-apis/2pay.js-payments-solution-integration-guide/use-cases/how-to-customize-and-style-the-2pay.js-payment-form.md).

# How to customize and style the 2Pay.js payment form

### Overview <a href="#overview" id="overview"></a>

The 2Pay.js payment form can be customized and styled by providing an object as the second argument when calling the create method from component service in the TwoPayClient JavaScript SDK.

Before moving on to the payment form customization, let's take a look at how the elements work and how they are rendered by the browser.

To implement the documentation below, you need to have a 2Pay.js client already set up and a payment form already working inside your web page. If not, read this article to see the [basic implementation of 2Pay.js](https://verifone.cloud/docs/2checkout/API-Integration/2Pay.js-payments-solution/2Pay.js-Payments-Solution-Integration-Guide/Basic-Implementation-for-2Pay.js-Payments-Solution/).

### Merchant’s Web Page and the 2Pay.js Form <a href="#merchant__2019s-web-page-and-the-2pay__002ejs__00a0form" id="merchant__2019s-web-page-and-the-2pay__002ejs__00a0form"></a>

As you probably noticed, the payment form renders inside an iFrame so the merchant’s page CSS will have to be written separately from the payment form. For example, you have a **Full name** field which is on the merchant’s page and the credit card, CVV and expiration date fields that are in the iFrame. However, the entire form will have to look and behave the same and the only way to customize the iFrame is by using the style configuration.

### Payment Form Elements and Behavior <a href="#payment-form-elements-and-behavior" id="payment-form-elements-and-behavior"></a>

Rendering the payment form with the default styling (no styles applied) will consist of the following structure.

#### Generic elements <a href="#generic-elements" id="generic-elements"></a>

You can use generic elements as high-level HTML elements.

```html
<body>
  <div>
    <form class="form">
      <div class="row">
        <div class="col">
        
          <!-- Credit card number field content goes here -->
          
        </div>
      </div>
      <div class="row">
        <div class="col">
        
          <!-- Expiration date field content goes here -->
        
        </div>
        <div class="col">
        
          <!-- CVV field content goes here -->
        
        </div>
      </div>
    </form>
  </div>
</body>
```

**The Payment Form \<body> Element**

The payment form is rendered inside an iFrame and it needs to have an HTML, a \<body> and a \<head> tag.

CSS rules such as font, color, and background can be applied to the \<body> by providing styles in the style object, as shown in the example below.

```css
let component = jsPaymentClient.components.create('card', {
    margin                      : 0,
    fontFamily                  : 'Helvetica, sans-serif',
    fontSize                    : '1rem',
    fontWeight                  : '400',
    lineHeight                  : '1.5',
    color                       : '#212529',
    textAlign                   : 'left',
    backgroundColor             : '#ffffff'
});
```

**HTML Elements \<div class=”row”> and \<div class=”col”>**

The purpose of these two elements is to offer a way to divide the payment form content. For example, using rows and columns by applying CSS rules for them. Below is an example of how the payment form can display with the **credit card number** field on a row and **expiration date** field and **CVV** field on another row with two columns.

```css
var component = twoPayClient.components.create('card', {
    '.row': {
        display : 'flex',
        flexWrap: 'wrap'
    },
    '.col': {
        flexBasis: '0',
        flexGrow : '1',
        maxWidth : '100%',
        padding  : '0',
        position : 'relative',
        width    : '100%'
    },
});
```

**Card number field**&#x20;

The **card number** field will be rendered together with several other HTML elements that are represented and explained below.

```html
 <body>
  <div>
    <form class="form">
      <div class="row">
        <div class="col">
        
          <!-- Credit card number field START -->
          <div class="field-container card-number">
            <div class="field-wrapper form-group is-error is-empty is-required">
              <label for="card-number" class="label control-label">Card number</label>
              
              <div class="input-wrapper">
                <input type="text" autocomplete="cc-number" id="card-number" name="card-number" class="field form-control">
                <i class="card-icon"></i>
                <i class="lock-icon"></i>
                <!-- default state of the error or valid icon -->
                <i class="" style="display: none;"></i>
              </div>
            </div>
            
            <span class="validation-message">Enter a valid card number.</span>
          </div>
          <!-- Credit card number field END -->
    ...
```

**The field container \<div class=”field-container card-number”>**

It holds all the card number field elements and it can customizable, as shown in the example below.

```css
 var component = twoPayClient.components.create('card', {
    '.field-container': {
        paddingBottom: '14px'
    }
});
```

**The field wrapper \<div class=”field-wrapper”>**

It contains the label, input, and icons and offers support for different states (validation or transition):

* default → Contains the base class names “field-wrapper form-group is-error is-empty is-required“. Usually, you will apply only some spacing to this element:

```css
var component = twoPayClient.components.create('card', {
    '.field-wrapper': {
        paddingRight: '25px'
    }
});
```

* focus → Offers support for when the input is focused by adding the class name “is-focused” to this element.
* error → Offers support for when there is a validation error and adds the class name “is-error“ to this element.
* valid → Offers support for when the field validation passes and adds the class name ”is-valid” to this element.

**The field’s label \<label class="label control-label">**

Styling the field’s label:

```css
var component = twoPayClient.components.create('card', {
    label: {
        display     : 'inline-block',
        marginBottom: '9px',
        color       : '#313131',
        fontSize    : '14px',
        fontWeight  : '300',
        lineHeight  : '17px'
    }
});
```

Styling the field’s label on focus:

```css
var component = twoPayClient.components.create('card', {
    '.is-focused label': {
        color: '#83ddeb'
    }
}); 
```

Styling the field’s label on error:&#x20;

```css
var component = twoPayClient.components.create('card', {
    '.is-error label': {
        color: '#D9534F'
    }
});
```

Styling the field’s label when valid:&#x20;

```css
var component = twoPayClient.components.create('card', {
    '.is-valid label': {
        color: '#1BB43F'
    }
});
```

**The input wrapper \<div class="input-wrapper">**

Offers support for absolute positioning of the icons:

```css
 var component = twoPayClient.components.create('card', {
    '.input-wrapper': {
        position: 'relative'
    }
});
```

**The input \<input type=”text” class=”field form-control”>**

This is the actual input field where the user types in the data.

Styling the input:

```css
var component = twoPayClient.components.create('card', {
    'input': {
        fontSize: '18px'
    }
});
```

&#x20;Styling the input on focus:

```css
var component = twoPayClient.components.create('card', {
    'input:focus': {
        border         : '1px solid #5D5D5D',
        backgroundColor: '#FFFDF2'
    }
});
```

Styling the input on error:

```css
 var component = twoPayClient.components.create('card', {
    '.is-error input': {
        border: '1px solid #D9534F'
    }
});
```

&#x20;Styling the input when valid:

```css
var component = twoPayClient.components.create('card', {
    '.is-valid input': {
        border: '1px solid #1BB43F'
    }
});
```

**The card icon \<i class="card-icon">\</i> or \<i class="card-type-icon">\</i> (card number specific element)**

This element represents the card icon which appears by default in the card number field. The **“card-icon”** class name changes into “**card-type-icon“** after the credit card auto-detection returns a card type.

This element’s style can also be customized:

```css
var component = twoPayClient.components.create('card', {
    '.card-icon': {
      left: '10px'
    },
    '.card-type-icon': {
      right: '30px'
    }
});
```

**The lock icon \<i class="lock-icon">\</i> (available for card number and CVV fields)**

This element can be customized the same way as the card icon or it can be hid.

```css
var component = twoPayClient.components.create('card', {
    '.lock-icon': {
      display: 'none'
    }
});
```

**The lock icon \<i class="error-icon">\</i> or \<i class="valid-icon">\</i>**

This element represents the error of valid icons. When an error is present, the element will have the class name **“error-icon“**, or if the field is valid, the element will have the **“valid-icon“** class name. There is no class name on default state.

The example below shows how to style the error and valid icons:

```css
var component = twoPayClient.components.create('card', {
    '.valid-icon': {
      right: '-25px'
    },
    '.error-icon': {
      right: '-25px'
    }
});
```

**The validation message element \<span class="validation-message">**

This element displays only whether an error is present for the current field.

Here's how you can customize the validation message element:

```css
var component = twoPayClient.components.create('card', {
    '.validation-message': {
        color       : '#D9534F',
        fontSize    : '10px',
        fontStyle   : 'italic',
        marginTop   : '6px',
        marginBottom: '-5px',
        display     : 'block',
        lineHeight  : '1'
    }
});
```

> Expand table

| ![](https://verifone.cloud/sites/default/files/verifone_2checkout/absolute/Template%253ACustom/Custom_Editor_Templates/Block-Note/Note-Icon.png) | <p>The above styling can be applied to all payment form fields with some exceptions:</p><ul><li>card-icon, card-type-icon – are only available for the card number field;</li><li>lock-icon – is only available for the card number and CVV fields;</li><li>you cannot pass any URL references in the style object. The following example will NOT work:</li></ul> |
| ------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |

```css
// Non working example
var component = twoPayClient.components.create('card', {
    '.valid-icon': {
        backgroundImage: 'url("https://my-web-site.com/some-valid-image.png")'
    }
});
```

### The Style Object <a href="#the-style-object" id="the-style-object"></a>

#### Default style object <a href="#default-style-object" id="default-style-object"></a>

The default style object used, if no other style object has been provided, is the one below:

```css
{
  margin: 0,
  fontFamily: 'Helvetica, sans-serif',
  fontSize: '1rem',
  fontWeight: '400',
  lineHeight: '1.5',
  color: '#212529',
  textAlign: 'left',
  backgroundColor: '#ffffff',
  '*': {
    'boxSizing': 'border-box'
  },
  '.no-gutters': {
    marginRight: 0,
    marginLeft: 0
  },
  '.row': {
    display: 'flex',
    flexWrap: 'wrap'
  },
  '.col': {
    flexBasis: '0',
    flexGrow: '1',
    maxWidth: '100%',
    padding: '0',
    position: 'relative',
    width: '100%'
  },
  'div': {
    display: 'block'
  },
  '.field-container': {
    paddingBottom: '14px'
  },
  '.field-wrapper': {
    paddingRight: '25px'
  },
  '.input-wrapper': {
    position: 'relative'
  },
  label: {
    display: 'inline-block',
    marginBottom: '9px',
    color: '#313131',
    fontSize: '14px',
    fontWeight: '300',
    lineHeight: '17px'
  },
  'input': {
    overflow: 'visible',
    margin: 0,
    fontFamily: 'inherit',
    display: 'block',
    width: '100%',
    height: '42px',
    padding: '10px 12px',
    fontSize: '18px',
    fontWeight: '400',
    lineHeight: '22px',
    color: '#313131',
    backgroundColor: '#fff',
    backgroundClip: 'padding-box',
    border: '1px solid #CBCBCB',
    borderRadius: '3px',
    transition: 'border-color .15s ease-in-out,box-shadow .15s ease-in-out',
    outline: 0
  },
  'input:focus': {
    border: '1px solid #5D5D5D',
    backgroundColor: '#FFFDF2'
  },
  '.is-error input': {
    border: '1px solid #D9534F'
  },
  '.is-error input:focus': {
    backgroundColor: '#D9534F0B'
  },
  '.is-valid input': {
    border: '1px solid #1BB43F'
  },
  '.is-valid input:focus': {
    backgroundColor: '#1BB43F0B'
  },
  '.validation-message': {
    color: '#D9534F',
    fontSize: '10px',
    fontStyle: 'italic',
    marginTop: '6px',
    marginBottom: '-5px',
    display: 'block',
    lineHeight: '1'
  },
  '.card-expiration-date': {
    paddingRight: '.5rem'
  },
  '.is-empty input': {
    color: '#EBEBEB'
  },
  '.lock-icon': {
    top: 'calc(50% - 7px)',
    right: '10px'
  },
  '.valid-icon': {
    top: 'calc(50% - 8px)',
    right: '-25px'
  },
  '.error-icon': {
    top: 'calc(50% - 8px)',
    right: '-25px'
  },
  '.card-icon': {
    top: 'calc(50% - 10px)',
    left: '10px',
    display: 'none'
  },
  '.is-empty .card-icon': {
    display: 'block'
  },
  '.is-focused .card-icon': {
    display: 'none'
  },
  '.card-type-icon': {
    right: '30px',
    display: 'block'
  },
  '.card-type-icon.visa': {
    top: 'calc(50% - 14px)'
  },
  '.card-type-icon.mastercard': {
    top: 'calc(50% - 14.5px)'
  },
  '.card-type-icon.amex': {
    top: 'calc(50% - 14px)'
  },
  '.card-type-icon.discover': {
    top: 'calc(50% - 14px)'
  },
  '.card-type-icon.jcb': {
    top: 'calc(50% - 14px)'
  },
  '.card-type-icon.dankort': {
    top: 'calc(50% - 14px)'
  },
  '.card-type-icon.cartebleue': {
    top: 'calc(50% - 14px)'
  },
  '.card-type-icon.diners': {
    top: 'calc(50% - 14px)'
  },
  '.card-type-icon.elo': {
    top: 'calc(50% - 14px)'
  }
}
```

#### Allowed CSS Properties <a href="#allowed-css-properties" id="allowed-css-properties"></a>

```css
[
    // orphan properties
    'bottom',
    'clear',
    'clip',
    'color',
    'cursor',
    'direction',
    'display',
    'float',
    'height',
    'left',
    'letterSpacing',
    'opacity',
    'position',
    'right',
    'top',
    'verticalAlign',
    'visibility',
    'width',
    'zIndex',

    // align
    'alignContent',
    'alignItems',
    'alignSelf',

    // background
    'backgroundBlendMode',
    'backgroundClip',
    'backgroundColor',
    'backgroundPosition',
    'backgroundRepeat',
    'backgroundSize',
    'background',

    // border
    'borderBottomColor',
    'borderBottomLeftRadius',
    'borderBottomRightRadius',
    'borderBottomStyle',
    'borderBottomWidth',
    'borderBottom',
    'borderCollapse',
    'borderColor',
    'borderLeftColor',
    'borderLeftStyle',
    'borderLeftWidth',
    'borderLeft',
    'borderRadius',
    'borderRightColor',
    'borderRightStyle',
    'borderRightWidth',
    'borderRight',
    'borderSpacing',
    'borderStyle',
    'borderTopColor',
    'borderTopLeftRadius',
    'borderTopRightRadius',
    'borderTopStyle',
    'borderTopWidth',
    'borderTop',
    'borderWidth',
    'border',

    // box
    'boxDecorationBreak',
    'boxShadow',
    'boxSizing',

    // break
    'breakAfter',
    'breakBefore',
    'breakInside',

    // flex
    'flex',
    'flexBasis',
    'flexDirection',
    'fontFeatureSettings',
    'flexFlow',
    'flexGrow',
    'flexShrink',
    'flexWrap',

    // font
    'fontFamily',
    'fontKerning',
    'fontLanguageOverride',
    'fontSizeAdjust',
    'fontSize',
    'fontStretch',
    'fontStyle',
    'fontSynthesis',
    'fontVariant',
    'fontVariantAlternates',
    'fontVariantCaps',
    'fontVariantEastAsian',
    'fontVariantLigatures',
    'fontVariantNumeric',
    'fontVariantPosition',
    'fontWeight',
    'font',

    // justify
    'justifyContent',
    'justifyItems',
    'justifySelf',

    // line
    'lineBreak',
    'lineHeight',

    // list
    'listStylePosition',
    'listStyleType',
    'listStyle',

    // margin
    'marginBottom',
    'marginLeft',
    'marginRight',
    'marginTop',
    'margin',

    // max
    'maxHeight',
    'maxWidth',

    // min
    'minHeight',
    'minWidth',

    // outline
    'outlineColor',
    'outlineOffset',
    'outlineStyle',
    'outlineWidth',
    'outline',

    // overflow
    'overflow',
    'overflowWrap',
    'overflowX',
    'overflowY',

    // padding
    'paddingBottom',
    'paddingLeft',
    'paddingRight',
    'paddingTop',
    'padding',

    //text
    'textAlign',
    'textAlignLast',
    'textDecoration',
    'textDecorationColor',
    'textDecorationLine',
    'textDecorationSkip',
    'textDecorationStyle',
    'textIndent',
    'textOverflow',
    'textShadow',
    'textTransform',
    'textUnderlinePosition',

    // transform
    'transform',
    'transformOrigin',
    'transformStyle',
    'transition',
    'transitionDelay',
    'transitionDuration',
    'transitionProperty',
    'transitionTimingFunction',

    // word
    'wordBreak',
    'wordSpacing',
    'wordWrap'
]
```

#### Allowed CSS Pseudo-classes <a href="#allowed-css-pseudo__002dclasses" id="allowed-css-pseudo__002dclasses"></a>

```css
 [
    ':hover',
    ':focus',
    ':active',
    ':disabled',
    '::placeholder',
    '::after',
    '::before'
]
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.2checkout.com/2checkout-apis/2checkout-apis/2pay.js-payments-solution-integration-guide/use-cases/how-to-customize-and-style-the-2pay.js-payment-form.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
