I have created a custom dropdown list with checkboxes from scratch because the project I am working on needs to be compatible with older versions of IE. Overall, the result is good but there is one issue - when I click on the drop down list, every other element on the page shifts downwards.
What I want to achieve is for the dropdown list to behave like a select element where it appears on top of all other elements on the page. Is there a CSS property or pure JavaScript solution that can help me achieve this?
Here is the code snippet:
var checkList = document.getElementById('list1');
var items = document.getElementById('items');
document.getElementById('anchor').onclick = function(evt) {
if (items.className.indexOf("visible") !== -1) {
items.className = "";
items.style.display = "none";
} else {
items.className = " visible";
items.style.display = "block";
}
}
.dropdown-check-list {
display: inline-block;
}
.dropdown-check-list .anchor {
position: relative;
cursor: pointer;
display: inline-block;
padding: 5px 50px 5px 10px;
border: 1px solid #ccc;
}
.dropdown-check-list .anchor:after {
position: absolute;
content: "";
border-left: 2px solid black;
border-top: 2px solid black;
padding: 5px;
right: 10px;
top: 20%;
transform: rotate(-135deg);
}
.dropdown-check-list .anchor:active:after {
right: 8px;
top: 21%;
}
.dropdown-check-list ul {
padding: 2px;
display: none;
margin: 0;
border: 1px solid #ccc;
border-top: none;
}
.dropdown-check-list ul li {
list-style: none;
}
<body>
<div id="list1" class="dropdown-check-list" tabindex="100">
<span id="anchor" class="anchor">Type</span>
<ul id="items" class="items">
<li><input type="checkbox" />Choice 3</li>
<li><input type="checkbox" />Choice 2</li>
<li><input type="checkbox" />Choice 1</li>
</ul>
</div>
<h1>
this shou
</h1>
</body>