W3.CSS Advanced Tutorial
By: Ng Wai Foong
Tags: CSS, Lightweight, Programming, HTML, Web Development

4. Modal

W3.CSS framework has its own w3-modal class to create a popup modal without any JavaScript code. Let’s create a button that will open up a modal when clicked. You can create it right below the accordion component. We will assign the onclick event with a simple JavaScript to show the content.

<button onclick="document.getElementById('id01').style.display='block'" class="w3-button w3-green w3-large">Login</button>

We need to create a div element with id01 as the id and w3-modal as the class. Inside the div, we will need another div element with w3-modal-content as class. You can nest any other elements inside according to your own needs.

<div id="id01" class="w3-modal">

<div class="w3-modal-content w3-card-4 w3-animate-zoom" style="max-width:600px"> <div class="w3-center"><br>

<span onclick="document.getElementById('id01').style.display='none'" class="w3-button w3-xlarge w3-hover-red w3-display-topright" title="Close Modal">×</span> </div> <p>Content here!</p> </div>

</div>

I’ve created one modal for the user to login. Your final html file should look something like this:

<!DOCTYPE html>

<html>

<title>Modal</title>

<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">

<style>

.mySlides {display:none}

.w3-left, .w3-right, .w3-badge {cursor:pointer}

.w3-badge {height:13px;width:13px;padding:0}

</style>

<body>

<div class="w3-container">



<button onclick="toggle_accordion('Demo1')" class="w3-button w3-block w3-black w3-left-align">Filter</button>

<div id="Demo1" class="w3-hide w3-container">

<p>Search for a name in the input field.</p>



<input class="w3-input w3-border w3-padding" type="text" placeholder="Search for names.." id="myInput" onkeyup="filter_list()">

<ul class="w3-ul w3-margin-top" id="myUL">

<li><a href="https://medium.com/better-programming/getting-started-with-w3-css-379ff93463d1" target="_blank">Getting Started with W3.CSS<a/></li>

<li><a href="https://towardsdatascience.com/data-science-made-easy-interactive-data-visualization-using-orange-de8d5f6b7f2b" target="_blank">Data Science Made Easy: Interactive Data Visualization using Orange<a/></li>

<li><a href="https://medium.com/better-programming/a-beginners-guide-to-botui-framework-for-chatbot-ui-a4e6d9a4a74" target="_blank">A Beginner’s Guide to Botui Framework for Chatbot UI<a/></li>

<li><a href="https://medium.com/better-programming/emoji-are-the-future-of-effective-web-design-a9bf98b18616" target="_blank">Emoji Are the Future of Effective Web Design 😃<a/></li>

<li><a href="https://towardsdatascience.com/a-beginners-guide-to-rasa-nlu-for-intent-classification-and-named-entity-recognition-a4f0f76b2a96" target="_blank">A Beginner’s Guide to Rasa NLU for Intent Classification and Named-entity Recognition<a/></li>

<li><a href="https://towardsdatascience.com/beginners-guide-to-sentiment-analysis-for-simplified-chinese-using-snownlp-ce88a8407efb" target="_blank">Beginner’s Guide to Sentiment Analysis for Simplified Chinese using SnowNLP<a/></li>

<li><a href="https://towardsdatascience.com/beginners-guide-to-bert-for-multi-classification-task-92f5445c2d7c" target="_blank">Beginner’s Guide to BERT for Multi-classification Task<a/></li>

</ul>

</div>



<button onclick="toggle_accordion('Demo2')" class="w3-button w3-block w3-black w3-left-align">Slideshow</button>

<div id="Demo2" class="w3-hide w3-container">

<div class="w3-content w3-display-container" style="max-width:800px">

<img class="mySlides" src="img_nature_wide.jpg" style="width:100%">

<img class="mySlides" src="img_snow_wide.jpg" style="width:100%">

<img class="mySlides" src="img_mountains_wide.jpg" style="width:100%">

<div class="w3-center w3-container w3-section w3-large w3-text-white w3-display-bottommiddle" style="width:100%">

<div class="w3-left w3-hover-text-khaki" onclick="plusDivs(-1)">❮</div>

<div class="w3-right w3-hover-text-khaki" onclick="plusDivs(1)">❯</div>

<span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="currentDiv(1)"></span>

<span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="currentDiv(2)"></span>

<span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="currentDiv(3)"></span>

</div>

</div>

</div>





<button onclick="document.getElementById('id01').style.display='block'" class="w3-button w3-green w3-large">Login</button>



<div id="id01" class="w3-modal">

<div class="w3-modal-content w3-card-4 w3-animate-zoom" style="max-width:600px">



<div class="w3-center"><br>

<span onclick="document.getElementById('id01').style.display='none'" class="w3-button w3-xlarge w3-hover-red w3-display-topright" title="Close Modal">×</span>

</div>



<form class="w3-container" action="/action_page.php">

<div class="w3-section">

<label><b>Username</b></label>

<input class="w3-input w3-border w3-margin-bottom" type="text" placeholder="Enter Username" name="usrname" required>

<label><b>Password</b></label>

<input class="w3-input w3-border" type="password" placeholder="Enter Password" name="psw" required>

<button class="w3-button w3-block w3-green w3-section w3-padding" type="submit">Login</button>

</div>

</form>



</div>

</div>

</div>

<script>

function toggle_accordion(id) {

let x = document.getElementById(id);

let highlight_color = "w3-red";

let base_color = "w3-black";

if (x.className.indexOf("w3-show") == -1) {

x.className += " w3-show";

x.previousElementSibling.className =

x.previousElementSibling.className.replace(base_color, highlight_color);

} else {

x.className = x.className.replace(" w3-show", "");

x.previousElementSibling.className =

x.previousElementSibling.className.replace(highlight_color, base_color);

}

}



function filter_list() {

var input, filter, ul, li, a, i;

input = document.getElementById("myInput");

filter = input.value.toUpperCase();

ul = document.getElementById("myUL");

li = ul.getElementsByTagName("li");

for (i = 0; i < li.length; i++) {

txtValue = li[i].textContent || li[i].innerText;

if (txtValue.toUpperCase().indexOf(filter) > -1) {

li[i].style.display = "";

} else {

li[i].style.display = "none";

}

}

}



var slideIndex = 1;

showDivs(slideIndex);



function plusDivs(n) {

showDivs(slideIndex += n);

}



function currentDiv(n) {

showDivs(slideIndex = n);

}



function showDivs(n) {

var i;

var x = document.getElementsByClassName("mySlides");

var dots = document.getElementsByClassName("demo");

if (n > x.length) {slideIndex = 1}

if (n < 1) {slideIndex = x.length}

for (i = 0; i < x.length; i++) {

x[i].style.display = "none";

}

for (i = 0; i < dots.length; i++) {

dots[i].className = dots[i].className.replace(" w3-white", "");

}

x[slideIndex-1].style.display = "block";

dots[slideIndex-1].className += " w3-white";

}

</script>



</body>

</html>

Click the Login button to test it out. You can see the popup modal — you can cancel it by clicking the X button at the top right.