The Sidebar has a flexible option that allows it to be initialized and targeted to any HTML element alongside the main content of a web page.
By default, the Sidebar targets the body element. Using the target property, you can set the target element to initialize the Sidebar inside any HTML element apart from the body element.
If required, zIndex can be set when the Sidebar acts as an overlay type.
In the following sample, the Sidebar is rendered in context to a div container element that has the CSS class e-main-content.
import{Sidebar}from'@syncfusion/ej2-navigations';import{Button}from'@syncfusion/ej2-buttons';import{enableRipple}from'@syncfusion/ej2-base';enableRipple(true);letdefaultSidebar: Sidebar=newSidebar({width:"280px",type:"Push",target:'.maincontent'});defaultSidebar.appendTo('#default-sidebar');//end of Sidebar initialization//toggle button initializationlettogglebtn: Button=newButton({iconCss:'e-icons e-menu',isToggle: true,content:'Open'},'#toggle');lettoggleEle=document.getElementById('toggle');// Close the SidebarconstcloseBtn=document.querySelector('#close');if(closeBtn){closeBtn.addEventListener('click',()=>{defaultSidebar.hide();if(toggleEle){toggleEle.classList.remove('e-active');}togglebtn.content='Open';});}// Click EventconsttoggleBtn=document.querySelector('#toggle');if(toggleBtn){toggleBtn.addEventListener('click',()=>{if(toggleEle&&toggleEle.classList.contains('e-active')){togglebtn.content='Close';defaultSidebar.show();}else{togglebtn.content='Open';defaultSidebar.hide();}});}
<!DOCTYPE html><htmllang="en"><head><title>Essential JS 2 Sidebar</title><metacharset="utf-8"/><metaname="viewport"content="width=device-width, initial-scale=1.0"/><metaname="description"content="Typescript UI Controls"/><metaname="author"content="Syncfusion"/><linkhref="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css"rel="stylesheet"/><linkhref="https://cdn.syncfusion.com/ej2/34.1.29/ej2-base/styles/fluent2.css"rel="stylesheet"/><linkhref="https://cdn.syncfusion.com/ej2/34.1.29/ej2-buttons/styles/fluent2.css"rel="stylesheet"/><linkhref="https://cdn.syncfusion.com/ej2/34.1.29/ej2-navigations/styles/fluent2.css"rel="stylesheet"/><linkhref="styles.css"rel="stylesheet"/><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script><scriptsrc="systemjs.config.js"></script></head><body><divid='loader'>LOADING....</div><divid='container'><!-- Sidebar element declaration --><asideid="default-sidebar"><divclass="title"> Sidebar content</div><divid="list"></div><divclass="sub-title">
Click the button to close the Sidebar.
</div><divclass="center-align"><buttonid="close"class="e-btn close-btn">Close Sidebar</button></div></aside><!-- end of Sidebar element --><!-- main content declaration --><divid="head"><buttonid="toggle"class="e-btn e-info"></button></div><divclass="maincontent"style="height:325px;border:1px solid gray"><div><divclass="title"> Main content</div><divclass="sub-title"> content goes here.</div></div></div><!--end of main content --></div><style>.header{width:100%;height:40px;font-size:20px;line-height:40px;font-weight:500;background:#eee;display:inline-block;}.center-align{text-align:center;padding:20px;}.e-menu:before{font-size:16px;}.title{text-align:center;font-size:20px;padding:15px;}#head{border:1pxsolid#424242;border-bottom-color:transparent;background:#00897B;}#toggle,#toggle:hover,#toggle:focus{/* csslint allow: adjoining-classes*/background:#00695C;box-shadow:none;border-radius:0;height:39px;width:100px;}#close,#close:hover,#close:active,#close:focus{/* csslint allow: adjoining-classes*/background:#fafafa;color:black}.sub-title{text-align:center;font-size:16px;padding:10px;display:flex;justify-content:center;}.radiobutton{display:inline-block;padding:10px;}.center{text-align:center;display:none;font-size:13px;font-weight:400;margin-top:20px;}.sub-title{text-align:center;font-size:16px;padding:10px;}#default-sidebar{background-color:#26A69A;color:#ffffff;}.close-btn:hover{color:#fafafa;}</style></body></html>