Customize the look of your forms
This feature is available through the early access program. To request access, send an email with your GitHub organization name and repository name from your official address to aem-forms-ea@adobe.com. For example, if the repository URL is https://github.com/adobe/abc, the organization name is adobe and the repository name is abc.
Forms are crucial for user interaction on websites, allowing them to input data. You can use Cascading Style Sheets (CSS) to style fields of a form, enhancing the visual presentation of your forms, and improving the user experience.
The Adaptive Forms Block produces a consistent structure for all form fields. The consistent structure makes it easier to develop CSS Selectors to select and style form fields based on field type and field names.
This document outlines the HTML Structure for various form components, helps you build an understanding of how to create CSS Selectors for various form fields to style form fields of an Adaptive Forms Block.
By the end of the article:
- You build an understanding of the structure of the default CSS file included with Adaptive Forms Block.
- You build an understanding of the HTML structure of the form components provided by the Adaptive Forms Block, including general components and specific components like dropdowns, radio groups, and checkbox groups.
- You learn how to style form fields based on field type and field names using CSS Selectors, allowing for consistent or unique styling based on requirements.
Understanding Form Field Types
Before diving into styling, let鈥檚 review the common form field types supported by the Adaptive Forms Block:
- Input Fields: These include text inputs, email inputs, password inputs, and more.
- Checkbox Groups: Used for selecting multiple options.
- Radio Groups: Used for selecting only one option from a group.
- Dropdowns: Also known as select boxes, used for selecting one option from a list.
- Panels/Containers: Used to group related form elements together.
Basic Styling Principles
Understanding is crucial before styling specific form fields:
- : CSS Selectors allow you to target specific HTML elements for styling. You can use element selectors, class selectors, or ID selectors.
- : CSS properties define the visual appearance of elements. Common properties for styling form fields include color, background-color, border, padding, margin, and more.
- : The CSS box model describes the structure of HTML elements as a content area surrounded by padding, borders, and margins.
- Flexbox/Grid: CSS and are powerful tools for creating responsive and flexible designs.
Styling a form for Adaptive Forms Block
The Adaptive Forms Block offers a standardized HTML structure, simplifying the process of selecting and styling form components:
-
Update default styles: You can modify the default styles of a form by editing the
/blocks/form/form.css file
. This file provides comprehensive styling for a form, supporting multi-step wizard forms. It emphasizes using custom CSS variables for easy customization, maintenance, and uniform styling across forms. -
CSS Styling for Forms: To ensure that your styles are applied correctly, wrap your form-specific CSS within the
main .form form
selector. This ensures that your styles target only the form elements within the main content area, avoiding conflicts with other parts of the website.
Example:code language-css main .form form input { /* Add styles specific to input fields inside the form */ } main .form form button { /* Add styles specific to buttons inside the form */ } main .form form label { /* Add styles specific to labels inside the form */ }
Components Structure
The Adaptive Forms Block offers a consistent HTML structure for various form elements, ensuring easier styling and management. You can manipulate the components using CSS for styling purposes.
General Components (except dropdowns, radio groups, and checkbox groups):
All form fields, except for dropdowns, radio groups, and checkbox groups, has the following HTML structure:
code language-html |
---|
|
- Classes: The div element has several classes for targeting specific elements and styling. You require the
{Type}-wrapper
orfield-{Name}
classes to develop a CSS Selector to style a form field: - {Type}: Identifies the component by field type. For example, text (text-wrapper), number (number-wrapper), date (date-wrapper).
- {Name}: Identifies the component by name. The name of the field can have only alphanumeric characters, the multiple consecutive dashes in the name are replaced with a single dash
(-)
, and starting and ending dashes in a field name are removed. For example, first-name (field-first-name field-wrapper). - {FieldId}: It is a unique identifier for the field, automatically generated.
- {Required}: It is a boolean indicating if the field is required.
- Label: The
label
element provides a descriptive text for the field and associates it with the input element using thefor
attribute. - Input: The
input
element defines the type of data to be entered. For example, text, number, email. - Description (Optional): The
div
with classfield-description
provides additional information or instructions for the user.
Example of HTML Structure
code language-html |
---|
|
code language-css |
---|
|
.{Type}-wrapper
: Targets the outerdiv
element based on the field type. For example,.text-wrapper
targets all text fields..field-{Name}
: Further selects the element based on the specific field name. For example,.field-first-name
targets the 鈥淔irst Name鈥 text field. While this selector can be used for targeting elements with the field-{Name} class, it鈥檚 important to be cautious. In this specific case, it wouldn鈥檛 be useful for styling input fields because it would target not only the input itself but also the label and description elements. It鈥檚 recommended to use more specific selectors like the ones you have for targeting text input fields (.text-wrapper input).
Example CSS Selectors for General Components
code language-css |
---|
|
Dropdown Component
For dropdown menus, the select
element is used instead of an input
element:
code language-html |
---|
|
Example HTML Structure
code language-html |
---|
|
The following CSS lists some example CSS Selectors for dropdown components.
code language-css |
---|
|
- Target the Wrapper: The first selector (
.drop-down-wrapper
) targets the outer wrapper element, ensuring styles apply to the entire dropdown component. - Flexbox Layout: Flexbox arranges the label, dropdown, and description vertically for a clean layout.
- Label Styling: The label stands out with bolder font weight and a slight margin.
- Dropdown Styling: The
select
element receives a border, padding, and rounded corners for a polished look. - Background Color: A consistent background color is set for visual harmony.
- Arrow Customization: Optional styles hide the default dropdown arrow and create a custom arrow using a Unicode character and positioning.
Radio Groups
Similar to dropdown components, radio groups have their own HTML structure and CSS structure:
code language-html |
---|
|
Example HTML Structure
code language-html |
---|
|
- Targeting the Fieldset
code language-css |
---|
|
This selector targets any fieldset with the class radio-group-wrapper. This would be useful for applying general styles to the entire radio group.
- Targeting Radio Button Labels
code language-css |
---|
|
- Target all radio button labels within a specific fieldset based on its name
code language-css |
---|
|
Checkbox Groups
code language-html |
---|
|
Example HTML Structure
code language-html |
---|
|
- Targeting the outer wrapper: These selectors target the outermost containers of both radio and checkbox groups, allowing you to apply general styles to the entire group structure. This is useful for setting spacing, alignment, or other layout-related properties.
code language-css |
---|
|
- Targeting Group Labels: This selector targets the
.field-label
element within both radio and checkbox group wrappers. This allows you to style the labels specifically for these groups, potentially making them stand out more.
code language-css |
---|
|
- Targeting Individual Inputs and Labels: These selectors provide more granular control over individual radio buttons, checkboxes, and their associated labels. You can use these to adjust sizing, spacing, or apply more distinct visual styles.
code language-css |
---|
|
- Customizing the Appearance of Radio Buttons and Checkboxes: This technique hides the default input and uses
:before
and:after
pseudo-elements to create custom visuals that change appearance based on the 鈥榗hecked鈥 state.
code language-css |
---|
|
Panel/Container components
code language-html |
---|
|
Example HTML Structure
code language-html |
---|
|
-
The fieldset element acts as the panel container with the class panel-wrapper and additional classes for styling based on the panel name (field-login).
-
The legend element (
) serves as the panel title with the text 鈥淟ogin Information鈥 and the class field-label. The data-visible=鈥渇alse鈥 attribute can be used with JavaScript to control the visibility of the title.
-
Inside the fieldset, multiple.{Type}-wrapper elements (.text-wrapper and .password-wrapper in this case) represent individual form fields within the panel.
-
Each wrapper contains a label, input field, and description, similar to the previous examples.
- Targeting the Panel:
code language-css |
---|
|
- The
.panel-wrapper
selector styles all elements with the class panel-wrapper, creating a consistent look for all panels.
- Targeting the Panel Title:
code language-css |
---|
|
- The
.panel-wrapper legend
selector styles the legend element within the panel, making the title stand out visually.
- Targeting Individual Fields within the Panel:
code language-css |
---|
|
- The
.panel-wrapper .{Type}-wrapper
selector targets all wrappers with the.{Type}-wrapper
class within the panel, allowing you to style the spacing between form fields.
- Targeting Specific Fields (Optional):
code language-css |
---|
|
- These optional selectors allow you to target specific field wrappers within the panel for unique styling, such as highlighting the username field.
Repeatable Panel
code language-html |
---|
|
Example HTML Structure
code language-html |
---|
|
Each panel has the same structure as the single panel example, with additional attributes:
-
data-repeatable=鈥渢rue鈥: This attribute indicates that the panel can be repeated dynamically using JavaScript or a framework.
-
Unique IDs and names: Each element within the panel has a unique ID (for example, name-1, email-1) and name attribute based on the index of the panel (for example, name=鈥渃ontacts[0].name鈥). This allows for proper data collection when multiple panels are submitted.
- Targeting All Repeatable Panels:
code language-css |
---|
|
The selector styles all panels that can be repeated, ensuring a consistent look and feel.
- Targeting Individual Fields within a Panel:
code language-css |
---|
|
This selector styles all field wrappers within a repeatable panel, maintaining consistent spacing between fields.
- Targeting Specific Fields (within a Panel):
code language-css |
---|
|
File attachment
code language-html |
---|
|
Example HTML Structure
code language-html |
---|
|
- The class attribute uses the provided name for the file attachment (claim_form).
- The id and name attributes of the input element match the file attachment name (claim_form).
- The files-list section is initially empty. It is populated dynamically with JavaScript when files are uploaded.
- Targeting the Entire File Attachment Component:
code language-css |
---|
|
This selector styles the entire file attachment component, including the legend, drag area, input field, and list.
- Targeting Specific Elements:
code language-css |
---|
|
These selectors allow you to style various parts of the file attachment component individually. You can adjust the styles to match your design preferences.
Styling components
You can style form fields based on their specific type ({Type}-wrapper
) or individual names (field-{Name}
). This allows for more granular control and customization of your form鈥檚 appearance.
Styling Based on Field Type
You can use CSS Selectors to target specific field types and apply styles consistently.
code language-html |
---|
|
Example HTML Structure
code language-html |
---|
|
-
Each field is wrapped in a
div
element with several classes:{Type}-wrapper
: Identifies the type of field. For example,form-text-wrapper
,form-number-wrapper
,form-email-wrapper
.field-{Name}
: Identifies the field by its name. For exampleform-name
,form-age
,form-email
.field-wrapper
: A generic class for all field wrappers.
-
The
data-required
attribute indicates whether the field is required or optional. -
Each field has a corresponding label, input element, and potential additional elements like placeholders and descriptions.
code language-css |
---|
|
Styling Based on Field name
You can also target individual fields by name to apply unique styles.
code language-html |
---|
|
Example HTML Structure
code language-html |
---|
|
code language-css |
---|
|
This CSS targets all input elements that are located within an element that has the class field-otp
. Your form鈥檚 HTML structure follows conventions of the Adaptive Forms Block, this implies there鈥檚 a container marked with the class 鈥渇ield-otp鈥 holds the field with the name 鈥渙tp鈥.
See also
{{universal-editor-see-also}}