Hey guys, today in this post we are going to learn about How to use inline class for the animation CSS style to show/hide and Toggle container based on button click in Salesforce LWC (Lightning Web Componenet).
Style Components with Lightning Design System
Salesforce Lightning Design System (SLDS) is a CSS framework that provides a look and feel that’s consistent with Lightning Experience. Use SLDS styles to give your custom Lightning web components a UI that is consistent with Salesforce, without having to reverse-engineer our styles. And best of all, it just works with Lightning components running in Lightning Experience and in the Salesforce mobile application. To know more details about Salesforce Lightning Design System (SLDS), Click Here..
Files we used to add inline class in Salesforce LWC →
lwcInlineStyleCss.html | LWc HTML File | Template HTML file to create dynamic css inline class add/remove based on button click in Salesforce Lightning Web Component (LWC) |
lwcInlineStyleCss.js | LWC JavaScript File | It’s javascript actions functions to control the animation CSS style show/hide & Toggle container in Salesforce LWC |
lwcInlineStyleCss.js-meta.xml | XML Meta File | It is used to where this lightning web component file you want to display as lightning__AppPage, lightning__RecordPage, lightning__HomePage. |
lwcInlineStyleCss.css | Style CSS | It is used to creaet animation CSS style and alignment of the container. |
lwcInlineStyleCssApp.app | Lightning Application | It is used for call the component and preview on browser. |
Final Output →
You can download file directly from github by Click Here.
Other related post that would you like to learn in Salesforce
- Find the below steps ▾
Create Lightning Web Component HTML →
Step 1:- Create Lightning Web Component : lwcInlineStyleCss.html
SFDX:Lightning Web Component >> New >> lwcInlineStyleCss.html
lwcInlineStyleCss.html [Lightning Web Component HTML]
<template>
<lightning-card>
<div style="padding: 20px; overflow:hidden;">
<h3 class="slds-text-heading_medium"><lightning-icon icon-name="custom:custom101" size="small"></lightning-icon> <strong style="color:#270086; font-size:13px; margin-right:5px;"> How to move container Left, Right, Left and Bottom in Lightning Web Component (LWC) </strong></h3>
<br/><br/>
<div class="menuLeftSide">
<div class="spanInlineOver">
<span onclick={fadeTopAction}>Top</span>
<span onclick={fadeBottomAction}>Bottom</span>
<span onclick={fadeLeftAction}>Left</span>
<span onclick={fadeInAction}>Right</span>
<span onclick={fadeRightAction}>Fade Slide Right</span>
<span onclick={fadeLeftSlideAction}>Fade Slide Left</span>
<span onclick={fadeZoomAction}>Zoom-Up</span>
</div>
</div>
<div class="menuRightSide">
<div class={customCss}>
<div class="slideBox"><a href="https://www.w3web.net/" title="w3web.net" target="_blank" rel="noopener noreferrer">
<img class="logoImg" alt="w3web.net" src="https://www.w3web.net/wp-content/uploads/2020/08/cropped-w3webnetLogo.png" title="w3web.net"/>
</a> <strong><a href="https://www.w3web.net/" target="_blank" rel="noopener noreferrer">w3web.net</a> </strong> is the place where you can learn step-by-step about Blog, Salesforce Lightning Component, Lightning Web Component (LWC), Visualforce, Technical of Computer Application, Salesforce Plugin, JavaScript, Jquery, CSS, Computer & Accessories, Software Configuration, Customization, Development and much <strong><a href="https://www.w3web.net/" target="_blank" rel="noopener noreferrer">more…</a></strong> </div>
</div>
</div>
</div>
<br/><br/>
<!--Start RelatedTopics Section-->
<div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
<p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/05/thumbsUpLike.png" width="25" height="25" style="vertical-align:top; margin-right:10px;" data-aura-rendered-by="436:0"><strong data-aura-rendered-by="437:0"><span style="font-size:16px; font-style:italic; display:inline-block; margin-right:5px;">Don't forget to check out:-</span><a href="https://www.w3web.net/" target="_blank" rel="noopener noreferrer" style="text-decoration:none;" data-aura-rendered-by="440:0">An easy way to learn step-by-step online free Salesforce tutorial, To know more Click <span style="color:#ff8000; font-size:18px;" data-aura-rendered-by="442:0">Here..</span></a></strong></p>
<br/><br/>
<p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/07/tickMarkIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;" data-aura-rendered-by="436:0"><strong data-aura-rendered-by="437:0"><span style="font-size:17px; font-style:italic; display:inline-block; margin-right:5px; color:rgb(255 128 0);">You May Also Like →</span> </strong></p>
<div style="display:block; overflow:hidden;">
<div style="width: 50%; float:left; display:inline-block">
<ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;">
<li><a href="https://www.w3web.net/lwc-get-set-lightning-checkbox-value/" target="_blank" rel="noopener noreferrer">How to get selected checkbox value in lwc</a></li>
<li><a href="https://www.w3web.net/display-account-related-contacts-in-lwc/" target="_blank" rel="noopener noreferrer">how to display account related contacts based on AccountId in lwc</a></li>
<li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to create lightning datatable row actions in lwc</a></li>
<li><a href="https://www.w3web.net/if-and-else-condition-in-lwc/" target="_blank" rel="noopener noreferrer">how to use if and else condition in lwc</a></li>
<li><a href="https://www.w3web.net/get-selected-radio-button-value-and-checked-default-in-lwc/" target="_blank" rel="noopener noreferrer">how to display selected radio button value in lwc</a></li>
</ul>
</div>
<div style="width: 50%; float:left; display:inline-block">
<ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;">
<li><a href="https://www.w3web.net/display-account-related-contacts-lwc/" target="_blank" rel="noopener noreferrer">display account related contacts based on account name in lwc</a></li>
<li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to insert a record of account Using apex class in LWC</a></li>
<li><a href="https://www.w3web.net/fetch-picklist-values-dynamic-in-lwc/" target="_blank" rel="noopener noreferrer">how to get picklist values dynamically in lwc</a></li>
<li><a href="https://www.w3web.net/edit-save-and-remove-rows-dynamically-in-lightning-component/" target="_blank" rel="noopener noreferrer">how to edit/save row dynamically in lightning component</a></li>
<li><a href="https://www.w3web.net/tree-grid-dynamic-expand-collapse-in-lwc/" target="_blank" rel="noopener noreferrer">how to create tree grid with expanded/collapsed section for the entire row in LWC</a></li>
</ul>
</div>
<div style="clear:both;"></div>
<br/>
<div class="youtubeIcon">
<a href="https://www.youtube.com/channel/UCW62gTen2zniILj9xE6LmOg" target="_blank" rel="noopener noreferrer"><img src="https://www.w3web.net/wp-content/uploads/2021/11/youtubeIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;"/> <strong>TechW3web:-</strong> To know more, Use this <span style="color: #ff8000; font-weight: bold;">Link</span> </a>
</div>
</div>
</div>
<!--End RelatedTopics Section-->
</lightning-card>
</template>
Create Lightning Web Component JavaScript →
Step 2:- Create Lightning Web Component : lwcInlineStyleCss.js
SFDX:Lightning Web Component >> New >> lwcInlineStyleCss.js
lwcInlineStyleCss.js [LWC JavaScript File]
import { LightningElement } from 'lwc';
export default class LwcInlineStyleCss extends LightningElement {
customCss = 'regularContentBox';
fadeTopAction(){
this.template.querySelector('.regularContentBox').classList.remove('fadeBottom');
this.template.querySelector('.regularContentBox').classList.remove('fadeleft');
this.template.querySelector('.regularContentBox').classList.remove('fadeIn');
this.template.querySelector('.regularContentBox').classList.remove('fadeRight');
this.template.querySelector('.regularContentBox').classList.remove('fadeLeftSlide');
this.template.querySelector('.regularContentBox').classList.remove('zoomFadeIn');
this.template.querySelector('.regularContentBox').classList.add('fadeTop');
}
fadeBottomAction(){
this.template.querySelector('.regularContentBox').classList.remove('fadeTop');
this.template.querySelector('.regularContentBox').classList.remove('fadeleft');
this.template.querySelector('.regularContentBox').classList.remove('fadeIn');
this.template.querySelector('.regularContentBox').classList.remove('fadeRight');
this.template.querySelector('.regularContentBox').classList.remove('fadeLeftSlide');
this.template.querySelector('.regularContentBox').classList.remove('zoomFadeIn');
this.template.querySelector('.regularContentBox').classList.add('fadeBottom');
}
fadeLeftAction(){
this.template.querySelector('.regularContentBox').classList.remove('fadeTop');
this.template.querySelector('.regularContentBox').classList.remove('fadeBottom');
this.template.querySelector('.regularContentBox').classList.remove('fadeIn');
this.template.querySelector('.regularContentBox').classList.remove('fadeRight');
this.template.querySelector('.regularContentBox').classList.remove('fadeLeftSlide');
this.template.querySelector('.regularContentBox').classList.remove('zoomFadeIn');
this.template.querySelector('.regularContentBox').classList.add('fadeleft');
}
fadeInAction(){
this.template.querySelector('.regularContentBox').classList.remove('fadeTop');
this.template.querySelector('.regularContentBox').classList.remove('fadeBottom');
this.template.querySelector('.regularContentBox').classList.remove('fadeleft');
this.template.querySelector('.regularContentBox').classList.remove('fadeRight');
this.template.querySelector('.regularContentBox').classList.remove('fadeLeftSlide');
this.template.querySelector('.regularContentBox').classList.remove('zoomFadeIn');
this.template.querySelector('.regularContentBox').classList.add('fadeIn');
}
fadeRightAction(){
this.template.querySelector('.regularContentBox').classList.remove('fadeTop');
this.template.querySelector('.regularContentBox').classList.remove('fadeBottom');
this.template.querySelector('.regularContentBox').classList.remove('fadeleft');
this.template.querySelector('.regularContentBox').classList.remove('fadeIn');
this.template.querySelector('.regularContentBox').classList.remove('fadeLeftSlide');
this.template.querySelector('.regularContentBox').classList.remove('zoomFadeIn');
this.template.querySelector('.regularContentBox').classList.add('fadeRight');
}
fadeLeftSlideAction(){
this.template.querySelector('.regularContentBox').classList.remove('fadeTop');
this.template.querySelector('.regularContentBox').classList.remove('fadeBottom');
this.template.querySelector('.regularContentBox').classList.remove('fadeleft');
this.template.querySelector('.regularContentBox').classList.remove('fadeIn');
this.template.querySelector('.regularContentBox').classList.remove('fadeRight');
this.template.querySelector('.regularContentBox').classList.remove('zoomFadeIn');
this.template.querySelector('.regularContentBox').classList.add('fadeLeftSlide');
}
fadeZoomAction(){
this.template.querySelector('.regularContentBox').classList.remove('fadeTop');
this.template.querySelector('.regularContentBox').classList.remove('fadeBottom');
this.template.querySelector('.regularContentBox').classList.remove('fadeleft');
this.template.querySelector('.regularContentBox').classList.remove('fadeIn');
this.template.querySelector('.regularContentBox').classList.remove('fadeRight');
this.template.querySelector('.regularContentBox').classList.remove('fadeLeftSlide');
this.template.querySelector('.regularContentBox').classList.toggle('zoomFadeIn');
}
}
Create Lightning Web Component Meta XML →
Step 3:- Create Lightning Web Component : lwcInlineStyleCss.js-meta.xml
SFDX:Lightning Web Component >> New >> lwcInlineStyleCss.js-meta.xml
lwcInlineStyleCss.js-meta.xml [LWC Meta Data XML]
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>45.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
Create Component Style CSS →
Step 4:- Create Lightning Web Component : lwcInlineStyleCss.css
SFDX:Lightning Web Component >> New >> lwcInlineStyleCss.css
lwcInlineStyleCss.css [Style CSS]
.menuLeftSide{width: 20%; float: left; margin-right: 2%px;}
.menuRightSide{width: 70%; float: left;}
.spanInlineOver{font-size: 17px; color: #333; margin-bottom: 15px; width: 150px; }
.spanInlineOver span{width: 150px; color: #fff; background-image: linear-gradient(180deg, #00759f, #00c5cf); border: 1px #ccc solid; padding: 5px 10px; min-width:100px; text-align:center; display:block; margin: 0 0 15px 0;}
.spanInlineOver span:hover{text-decoration: none; color: #ffff00; cursor: pointer;}
.spanInlineOver span.active{text-decoration: none; background-image: linear-gradient(#00726e, #fff); color: #fff;}
.regularContentBox{ position:relative; overflow: hidden;}
.regularContentBox .slideBox{ background-color:#ddd; border: 1px #ccc solid; width: 450px; height: 250px; font-size: 16px; padding: 15px;}
.regularContentBox.fadeTop .slideBox{ position: absolute; -webkit-transform: translateY(-500px); -webkit-transition:all 0.2s linear; opacity:1; position: relative; }
.regularContentBox.fadeBottom .slideBox{position: absolute; -webkit-transform: translateY(0px); -webkit-transition:all 0.2s linear; opacity:1; position: relative;}
.regularContentBox.fadeleft .slideBox{position: absolute; -webkit-transform: translateX(-500px); -webkit-transition:all 0.2s linear; opacity:1; position: relative;}
.regularContentBox.fadeIn .slideBox{position: absolute; -webkit-transform: translateX(0px); -webkit-transition:all 0.2s linear; opacity:1; position: relative;}
.regularContentBox.fadeRight .slideBox{position: absolute; -webkit-transform: translateX(1500px); -webkit-transition:all 0.2s linear; opacity:1; position: relative;}
.regularContentBox.fadeLeftSlide .slideBox{position: absolute; -webkit-transform: translateX(0px); -webkit-transition:all 0.2s linear; opacity:1; position: relative;}
.regularContentBox.zoomFadeIn .slideBox{font-size:22px; width: 700px; height: 500px; position: absolute; -webkit-transform: translateY(0px); -webkit-transition:all 0.2s linear; opacity:1; position: relative;}
Create Lightning Application →
Step 5:- Create Lightning Application : lwcInlineStyleCssApp.app
From Developer Console >> File >> New >> Lightning Application
lwcInlineStyleCssApp.app [Component Application File]
<aura:application extends="force:slds">
<c:lwcInlineStyleCss/>
</aura:application>
Further post that would you like to learn in Salesforce
Can we add class to template in LWC?
LWC framework doesn't allow computed expressions in html markup to change CSS Class in Lightning Web Component dynamically but there are ways to do so. ... You can create a style sheet in the CSS file, and it's automatically applied to the corresponding HTML file.
How do you add a class in lightning component?
We use $A. util. toggleClass to add or remove a CSS style on a component or element during runtime. To retrieve the class name on a component, use component.
What does this template querySelector () return?
The querySelector() method returns the first element that matches a specified CSS selector(s) in the document. Template: To access elements rendered by a component, use the template property.
Related Topics | You May Also Like
Our Free Courses →
👉 Get Free Course →
📌 Salesforce Administrators 📌 Salesforce Lightning Flow Builder 📌 Salesforce Record Trigger Flow Builder |
👉 Get Free Course →
📌 Aura Lightning Framework 📌 Lightning Web Component (LWC) 📌 Rest APIs Integration |