
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create an Expanding Grid with CSS and JavaScript
An expanding grid is a grid that expands when a box is clicked. It hides by default but gets expanded to 100% when the box is clicked once. We will create three equal columns that floats right next to each other. On clicking any of the grid column, the grid will expand. Also, the expanded area will have a closeable button to close and return to the initial stage.
Create a container for the three columns
Begin with a parent div with three columns. On clicking any of the boxes, the onclick attribute will open the individual tabs −
<div class="container"> <div class="left" onclick="openTab('tab1');" ;> <h1>Some text on the left</h1> </div> <div class="center" onclick="openTab('tab2');"> <h1>Some text in center</h1> </div> <div class="right" onclick="openTab('tab3');"> <h1>Some text on the right</h1> </div> </div>
Position the columns
The three columns are positioned left using the float property. All these columns float next to each other. The width is set to 33% so that each column is equal −
.left, .right, .center { float: left; width: 33%; color: white; padding: 10px; height: 200px; text-align: center; }
Script for opening the tab
When any of the boxes will be clicked, the tab will open using the below script −
<script> function openTab(tabName) { var i, x; x = document.querySelectorAll(".containerTab"); Array.from(x).forEach(item => { item.style.display = "none"; }); document.getElementById(tabName).style.display = "block"; } </script>
The left tab
The following is the code for the left column. On clicking the x symbol, it will close because the onclick attribute is set to display none. This section is hidden by default −
<div id="tab1" class="containerTab" style="display:none;background:tomato"> <span onclick="this.parentElement.style.display='none'" class="closebtn">Ã</span> <h2>Tab 1</h2> <p> Lorem ipsum dolor sit amet, te quo doctus abhorreant, et pri deleniti intellegat, te sanctus inermis ullamcorper nam. Ius error diceret deseruisse ad </p> </div>
The center tab
The following is the code for the center column. On clicking the x symbol, it will close because the onclick attribute is set to display none. This section is hidden by default −
<div id="tab2" class="containerTab" style="display:none;background:rgb(166, 71, 255)"> <span onclick="this.parentElement.style.display='none'" class="closebtn">Ã</span> <h2>Tab 2</h2> <p> Lorem ipsum dolor sit amet, te quo doctus abhorreant, et pri deleniti intellegat, te sanctus inermis ullamcorper nam. Ius error diceret deseruisse ad </p> </div>
The right tab
The following is the code for the right column. On clicking the x symbol, it will close because the onclick attribute is set to display none. This section is hidden by default −
<div id="tab3" class="containerTab" style="display:none;background:teal"> <span onclick="this.parentElement.style.display='none'" class="closebtn">Ã</span> <h2>Tab 3</h2> <p> Lorem ipsum dolor sit amet, te quo doctus abhorreant, et pri deleniti intellegat, te sanctus inermis ullamcorper nam. Ius error diceret deseruisse ad </p> </div>
Example
To create an expanding grid with CSS and JavaScript, the code is as follows −
<!DOCTYPE html> <html> <head> <style> body { padding: 1%; font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } * { box-sizing: border-box; } .left, .right, .center { float: left; width: 33%; color: white; padding: 10px; height: 200px; text-align: center; } .left { background-color: tomato; } .right { background-color: teal; } .center { background-color: rgb(166, 71, 255); } .container:after { clear: both; } .closebtn { float: right; color: white; font-size: 35px; cursor: pointer; } .closebtn:hover { transform: scale(1.5); } .containerTab { padding: 20px; width: 99%; color: white; } </style> </head> <body> <h1 style="text-align: center;">Three Column grid example</h1> <div class="container"> <div class="left" onclick="openTab('tab1');" ;> <h1>Some text on the left</h1> </div> <div class="center" onclick="openTab('tab2');"> <h1>Some text in center</h1> </div> <div class="right" onclick="openTab('tab3');"> <h1>Some text on the right</h1> </div> </div> <div id="tab1" class="containerTab" style="display:none;background:tomato"> <span onclick="this.parentElement.style.display='none'" class="closebtn">Ã</span> <h2>Tab 1</h2> <p> Lorem ipsum dolor sit amet, te quo doctus abhorreant, et pri deleniti intellegat, te sanctus inermis ullamcorper nam. Ius error diceret deseruisse ad </p> </div> <div id="tab2" class="containerTab" style="display:none;background:rgb(166, 71, 255)"> <span onclick="this.parentElement.style.display='none'" class="closebtn">Ã</span> <h2>Tab 2</h2> <p> Lorem ipsum dolor sit amet, te quo doctus abhorreant, et pri deleniti intellegat, te sanctus inermis ullamcorper nam. Ius error diceret deseruisse ad </p> </div> <div id="tab3" class="containerTab" style="display:none;background:teal"> <span onclick="this.parentElement.style.display='none'" class="closebtn">Ã</span> <h2>Tab 3</h2> <p> Lorem ipsum dolor sit amet, te quo doctus abhorreant, et pri deleniti intellegat, te sanctus inermis ullamcorper nam. Ius error diceret deseruisse ad </p> </div> <script> function openTab(tabName) { var i, x; x = document.querySelectorAll(".containerTab"); Array.from(x).forEach(item => { item.style.display = "none"; }); document.getElementById(tabName).style.display = "block"; } </script> </body> </html>