Skip to main content

Title: How to Add Icons Inside Input and Output Fields in Salesforce LWC

In plain words: Adding icons to input and output fields in LWC gives users clear visual cues about what data a field expects or displays. While standard record forms follow default layouts, custom LWC markup lets you place Salesforce Lightning Design System (SLDS) utility icons directly inside field containers, labels, or alongside read-only output values.

Standard Salesforce record page layouts do not always fit bespoke business requirements, such as custom search bars, multi-step wizards, or interactive calculation panels. Building custom form elements in Lightning Web Components (LWC) with integrated icons enhances visual hierarchy, speeds up data entry, and maintains full alignment with the Salesforce Lightning Design System (SLDS).

1. Methods for Adding Icons to Form Controls

Salesforce developers have two primary ways to incorporate icons with inputs and outputs:

  • Base Component Attributes (lightning-input): Base components support native icon properties (such as icon-name="utility:search") for search or text inputs with zero custom CSS.
  • Custom SLDS Form Elements: Using standard SLDS markup classes (like slds-input-has-icon and slds-input__icon) to place icons inside inputs or beside custom output badges.
360 Form Icons Architecture Card:
  • Base Icon Component: <lightning-icon> with categories like utility:search, standard:user, or custom:custom14.
  • SLDS Layout Wrapper: slds-form-element > slds-form-element__control > slds-input-has-icon.
  • Icon Sizing: Prefer xx-small or x-small inside form inputs to match platform line heights.
  • Accessibility: Always provide an alternative-text property on icons unless they are purely decorative alongside text.

2. Step-by-Step Implementation

Below is a working custom form component demonstrating an input field with an embedded search icon and a formatted output field displaying a user badge icon.

Step 1: Build the Template Markup (iconFieldsDemo.html)
Combines a base lightning-input with a custom SLDS output group.
<template>
    <lightning-card title="Custom Form with Icons" icon-name="custom:custom18">
        <div class="slds-p-around_medium">

            <!-- Option A: Base Input with Embedded Search Icon -->
            <div class="slds-m-bottom_medium">
                <lightning-input
                    type="search"
                    label="Search Records"
                    placeholder="Enter keywords..."
                    value={searchQuery}
                    onchange={handleInputChange}>
                </lightning-input>
            </div>

            <!-- Option B: Custom SLDS Input with Left-Aligned Icon -->
            <div class="slds-form-element slds-m-bottom_medium">
                <label class="slds-form-element__label" for="customEmailInput">Email Address</label>
                <div class="slds-form-element__control slds-input-has-icon slds-input-has-icon_left">
                    <lightning-icon 
                        icon-name="utility:email" 
                        size="x-small" 
                        class="slds-icon slds-input__icon slds-input__icon_left slds-icon-text-default" 
                        alternative-text="Email">
                    </lightning-icon>
                    <input 
                        type="email" 
                        id="customEmailInput" 
                        placeholder="user@example.com" 
                        class="slds-input" 
                        value={emailValue} 
                        oninput={handleEmailChange} />
                </div>
            </div>

            <!-- Option C: Custom Output Display with Leading Icon -->
            <div class="slds-form-element slds-m-bottom_small">
                <span class="slds-form-element__label">Assigned Owner</span>
                <div class="slds-form-element__control slds-m-top_xx-small">
                    <div class="output-badge">
                        <lightning-icon 
                            icon-name="standard:user" 
                            size="small" 
                            alternative-text="User Record">
                        </lightning-icon>
                        <span class="slds-m-left_x-small slds-text-body_regular">
                            <strong>{assignedUser}</strong>
                        </span>
                    </div>
                </div>
            </div>

        </div>
    </lightning-card>
</template>
Step 2: Implement the JavaScript Controller (iconFieldsDemo.js)
import { LightningElement, track } from 'lwc';

export default class IconFieldsDemo extends LightningElement {
    searchQuery = '';
    emailValue = '';
    @track assignedUser = 'Alex Morgan';

    handleInputChange(event) {
        this.searchQuery = event.target.value;
    }

    handleEmailChange(event) {
        this.emailValue = event.target.value;
    }
}
Step 3: Component Stylesheet (iconFieldsDemo.css)
.output-badge {
    display: inline-flex;
    align-items: center;
    background-color: #f3f3f3;
    padding: 6px 12px;
    border-radius: 4px;
    border: 1px solid #dddbda;
}
Step 4: Configure Metadata XML (iconFieldsDemo.js-meta.xml)
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>60.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__AppPage</target>
        <target>lightning__RecordPage</target>
        <target>lightning__HomePage</target>
    </targets>
</LightningComponentBundle>

3. Common Traps & Platform Best Practices

Metadata Trap: Adding Invalid Tags to .js-meta.xml
Older tutorials often include invalid tags like <target:importStaticResources> inside the component metadata XML. Lightning Design System icons and styles are available in LWC out of the box and do not require static resource imports. Adding unsupported XML tags will fail component deployment.
Core Rule: Use base components like <lightning-input type="search"> where possible, and use standard SLDS classes (slds-input-has-icon_left) with <lightning-icon> when building custom input wrappers.
  • Keep Icon Proportions Balanced: Use size="x-small" or size="xx-small" inside standard form fields so the icon does not stretch the line height.
  • Ensure Accessible Forms: Always pair HTML <label> elements with matching for="..." and id="..." attributes so screen readers connect labels with inputs.
  • Color Consistency: Use SLDS utility classes such as slds-icon-text-default or slds-icon-text-weak to ensure icon colors adapt automatically to Salesforce themes.

Summary

Integrating icons into custom Lightning Web Component inputs and outputs makes user interfaces more intuitive and visually engaging. By taking advantage of native base components and standard SLDS icon wrapper classes, developers can build polished, accessible custom screens that integrate seamlessly into any Salesforce Lightning experience.