How to Align a Spinner to the Right in lightning-datatable Cells?

Aligning a spinner to the right edge in lightning-datatable cells can be tricky due to its default center alignment. By using custom CSS and a container for the spinner, you can align the spinner to the right edge in lightning-datatable cells. This ensures a clean and responsive layout within the table.
Question:
How to Align a Spinner to the Right in lightning-datatable Cells?
When using a custom datatype in lightning-datatable with standardCellLayout: false, how can you align a spinner to the right edge of a table cell instead of its default position at the center to achieve the goal of aligning spinner to right edge in lightning-datatable cells?
Answer:
To align a spinner to the right edge of a table cell in lightning-datatable, you need to customize its positioning, as the default layout centers the spinner. Since the spinner is encapsulated in the Shadow DOM, traditional SLDS classes or tokens won’t work. The best approach is to use a custom container for the spinner and apply specific CSS to position it at the right edge of the cell. Here’s a brief summary:
Enroll in our Salesforce training in Hyderabad to gain practical experience through real-world projects. Register for a free demo today and take the first step toward advancing your career..!!
- Custom container: Use a wrapper around the spinner to control its placement and align spinner to right edge in lightning-datatable cells.
- CSS customization: Apply absolute positioning to the spinner, aligning it to the right side and vertically centering it.
- Scoped styles: Use the
:hostselector to scope the styles to the component, ensuring they don’t interfere with other parts of the page. - Avoid floats: Instead of using
slds-float_right, which can cause layout issues, use more maintainable CSS properties likeposition: absolutefor precise alignment.
This approach ensures the spinner aligns correctly and behaves responsively within the lightning-datatable, providing a clean user interface without layout issues.
<template>
<!-- Spinner container aligned to the right -->
<div lwc:if={showSpinner} class="slds-spinner_container">
<div role="status" class="slds-spinner slds-spinner_brand slds-spinner_x-small">
<span class="slds-assistive-text">Loading</span>
<div class="slds-spinner__dot-a"></div>
<div class="slds-spinner__dot-b"></div>
</div>
</div>
<!-- Display the numeric value when the data is loaded -->
<div lwc:elseif={showValue} class="slds-float_right">
{prefix}
<lightning-formatted-number
format-style={formatStyle}
value={displayValue}
currency-code={currencyCode}
minimum-fraction-digits={minimumFractionDigits}
maximum-fraction-digits={maximumFractionDigits}
></lightning-formatted-number>
{suffix}
</div>
</template>Custom CSS for Spinner Alignment:
:host {
position: relative;
}
.slds-spinner_container .slds-spinner {
position: absolute;
right: 0; /* Aligns the spinner to the right */
top: 50%; /* Centers the spinner vertically */
transform: translateY(-50%); /* Adjusts for vertical centering */
}Code Explanation:
- In the HTML, the spinner is wrapped in a
divwith the classslds-spinner_container. This container provides flexibility for applying custom alignment styles where align spinner to the right edge in lightning-datatable cells becomes effective. - The
:hostpseudo-class in the CSS ensures that the styles are scoped specifically to the component, avoiding unintentional global style changes. - The
position: absolutestyle in.slds-spinnerplaces the spinner relative to the cell, whileright: 0aligns it to the right edge of the cell. - The
top: 50%combined withtransform: translateY(-50%)ensures the spinner is vertically centered within the table cell.
This approach ensures the spinner aligns precisely at the right edge of the cell, creating a clean and professional layout in lightning-datatable. It follows best practices and avoids the pitfalls of using floats for alignment.
Kickstart Your Salesforce Learning Journey
Our comprehensive Salesforce course offers an immersive learning experience that equips you with the critical skills needed to succeed in the CRM industry. Covering key areas such as Salesforce Admin, Developer, and AI, the program combines solid theoretical knowledge with practical, hands-on experience. You’ll have the opportunity to work on live projects and assignments that will prepare you to solve complex business challenges with Salesforce solutions. With expert guidance, you’ll develop both technical expertise and valuable industry insights.
Beyond technical training, our Salesforce Training in Hyderabad also provides personalized mentorship, exam preparation assistance, and interview coaching to help you stand out in the competitive job market. You’ll gain access to extensive study materials, real-world project experience, and ongoing support throughout your learning journey. By the end of the course, you’ll be ready for certification exams and equipped with the practical skills that employers value.
Begin your Salesforce career with us and unlock a world of exciting job opportunities..!
Related links:
See also:Â LWC Interview Questions for 5 years expert
See also:Â Accenture LWC Interview Questions
See also: Infosys LWC Developer Interview Questions
See also: 50 LWC Lightning Web Component Interview Questions Part -2
See also: 61 LWC Lightning Web Component Interview Questions.

