Customize the look of your forms
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 the HTML structure of 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’s 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 fundamental CSS concepts is crucial before styling specific form fields:
- Selectors: CSS Selectors allow you to target specific HTML elements for styling. You can use element selectors, class selectors, or ID selectors.
- Properties: CSS properties define the visual appearance of elements. Common properties for styling form fields include color, background-color, border, padding, margin, and more.
- Box Model: The CSS box model describes the structure of HTML elements as a content area surrounded by padding, borders, and margins.
- Flexbox/Grid: CSS Flexbox and Grid layouts 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. For instructions on adding the Adaptive Forms Block to your project, refer to create a form. -
Customization: Use the default
forms.css
as a base and customize it to modify the look and feel of your form components, making it visually appealing and user-friendly. The file’s structure encourages organization and maintains styles for forms, promoting consistent designs across your website.
Breakdown of forms.css’s structure
-
Global variables: Defined at the
:root
level, these variables (--variable-name
) store values used throughout the style sheet for consistency and ease of updates. These variables define colors, font sizes, padding, and other properties. You can declare your own Global variables or modify existing ones to change the form’s style. -
Universal selector styles: The
*
selector matches every element in the form, ensuring styles are applied to all components by default, including setting thebox-sizing
property toborder-box
. -
Form styling: This section focuses on styling form components using selectors to target specific HTML elements. It defines styles for input fields, text areas, checkboxes, radio buttons, file inputs, form labels, and descriptions.
-
Wizard styling (if applicable): This section is dedicated to styling the wizard layout, a multi-step form where each step is displayed one at a time. It defines styles for the wizard container, fieldsets, legends, navigation buttons, and responsive layouts.
-
Media queries: These provide styles for different screen sizes, adjusting layout and styling accordingly.
-
Miscellaneous styling:: This section covers styles for success or error messages, file upload areas, and other elements you might encounter in a 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 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 “First Name” text field. While this selector can be used for targeting elements with the field-{Name} class, it’s important to be cautious. In this specific case, it wouldn’t be useful for styling input fields because it would target not only the input itself but also the label and description elements. It’s 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 |
---|
|
HTML Structure Example
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 |
---|
|
HTML Structure Example
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 /* Targets radio group wrappers */ .radio-group-wrapper { margin-bottom: 20px; /* Adds space between radio groups */ } /* Targets checkbox group wrappers */ .checkbox-group-wrapper { margin-bottom: 20px; /* Adds space between checkbox groups */ }
-
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 .radio-group-wrapper legend, .checkbox-group-wrapper legend { font-weight: bold; /* Makes the group label bold */ }
-
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 /* Styling radio buttons */ .radio-group-wrapper input[type="radio"] { margin-right: 5px; /* Adds space between the input and its label */ } /* Styling radio button labels */ .radio-group-wrapper label { font-size: 15px; /* Changes the label font size */ } /* Styling checkboxes */ .checkbox-group-wrapper input[type="checkbox"] { margin-right: 5px; /* Adds space between the input and its label */ } /* Styling checkbox labels */ .checkbox-group-wrapper label { font-size: 15px; /* Changes the label font size */ }
-
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 ‘checked’ state.code language-css /* Hide the default radio button or checkbox */ .radio-group-wrapper input[type="radio"], .checkbox-group-wrapper input[type="checkbox"] { opacity: 0; position: absolute; } /* Create a custom radio button */ .radio-group-wrapper input[type="radio"] + label::before { /* ... styles for custom radio button ... */ } .radio-group-wrapper input[type="radio"]:checked + label::before { /* ... styles for checked radio button ... */ } /* Create a custom checkbox */ /* Similar styling as above, with adjustments for a square shape */ .checkbox-group-wrapper input[type="checkbox"] + label::before { /* ... styles for custom checkbox ... */ } .checkbox-group-wrapper input[type="checkbox"]:checked + label::before { /* ... styles for checked checkbox ... */ }
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 “Login Information” and the class field-label. The data-visible=“false” 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=“true”: 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=“contacts[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’s 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’s HTML structure follows conventions of the Adaptive Forms Block, this implies there’s a container marked with the class “field-otp” holds the field with the name “otp”.
See also
- Get started with Edge Delivery Services for AEM Forms
- Create a form using Google Sheets or Microsoft Excel
- Set up your Google Sheets or Microsoft Excel files to start accepting data
- Publish your form and start collecting data
- Customize the look of your forms
- Add repeatable sections to a form
- Show a custom thank you message after form submission
- Adaptive Form Block components and their properties