6

How to align lightning label and lightning input in 1 row? I can't seem to putll it off. enter image description hereHere's my current form. However, my goal is to make it look like this enter image description here This is my lightning form code for the first image above:

<div class="slds-grid">
<div class="slds-col slds-size_1-of-2">
    <lightning:input aura:id="contactField" type="text" value="{!v.newContact.LastName}" Name="Last Name" label ="Last Name"/>
</div>
<div class="slds-col slds-size_1-of-2">
    <lightning:input aura:id="contactField" type="text" value="{!v.newContact.FirstName}" Name="First Name" label ="First Name"/>
</div>
<br/>
<div class="slds-col slds-size_1-of-2">
    <lightning:input aura:id="contactField" type="email" name="email" label="Email"  value="{!v.newContact.Email}" />
</div>

I'm trying to make it look like the second picture. However, for the second picture, I used the label tag which is separate from the input tag. It's never hard to make the input field and the label appear in 1 line where the input box has similar alignment with the input box below it. But using the lightning:input with the label together in it, would it be possible to align them in 1 row?

2 Answers 2

15

Yes, simply place your elements in a horizontal form, and give the elements the correct size. Here's a modified example of your code:

<div class="slds-form_horizontal slds-form slds-grid slds-wrap">
    <lightning:input aura:id="contactField" class="slds-size_1-of-2" type="text" value="{!v.newContact.LastName}" Name="Last Name" label ="Last Name"/>
    <lightning:input aura:id="contactField" class="slds-size_1-of-2" type="text" value="{!v.newContact.FirstName}" Name="First Name" label ="First Name"/>
    <lightning:input aura:id="contactField" class="slds-size_1-of-2" type="email" name="email" label="Email"  value="{!v.newContact.Email}" />
</div>
2
  <lightning:input value="{!v.searchKeyword}"
                                 required="true"
                                 aura:id="searchField"
                                 label="Search label"
                                 variant="label-inline" />
2
  • 2
    Welcome to SFSE! Please take the tour and read How to Answer. Right now, this is what we call a code dump. It's a block of code/markup with little to no explanation. Code dumps tend to be poor answers because they don't explain what the problem is, how to fix it, or why the fix works. Those things are important for people to learn how to avoid making the same mistakes over and over. Code dumps also tend to generate a lot of back-and-forth in the comments like "that didn't work" and "try this instead", which makes it hard for other people (with similar problems) to follow.
    – Derek F
    Commented Sep 5, 2019 at 3:00
  • This is the best and simplest solution.
    – Chris.Walz
    Commented Jul 7, 2023 at 7:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .