Skip to content Skip to sidebar Skip to footer

CSS Custom Combobox Issue

I need a custom combo box. So, I implemented with ul. The problem is I can't get combo box list opens on top by clicking the button. While showing ul, it moves button to bottom of

Solution 1:

Try this:

ul{

    width: 100px;
    background-color: rgb(224, 224, 224);
    border-radius: 4px;
    border: 1px solid black;
    padding: 0;
    margin: 0;
    position: absolute;
    bottom: 40px;
    z-index: 9;

}

ul li{

    list-style: none;
    cursor: pointer;

    padding: 4px 10px;
}

li:hover{

    background-color: white;
}

div{

    width:  100%;
    height: 40px;

    background-color: bisque;
    
    position: relative;

}

section{

    height: 400px;
    background-color: #525252;
}

button{

    width: 100px;
    height: 100%;
    margin: 0;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <link rel="stylesheet" href="style.css">
</head>
<body>
    <section>
        <p>Some content</p>
    </section>
    <div>
        <ul>
            <li>Parrot</li>
            <li>Dog</li>
            <li>Cat</li>
            <li>Squirrel</li>
            <li>Otter</li>
            <li>Cow</li>
            <li>Goat</li>
            <li>Finch</li>
        </ul>
        <button onclick="showPop()">Pet</button>
    </div>

    <script>
        let ul = document.getElementsByTagName('ul')[0]

        onload = function(){

            ul.style.display = 'none'
        }

        function showPop(){


            if(ul.style.display == 'none'){
                ul.style.display = 'block'
            }else{
                ul.style.display = 'none'
            }
        }
    </script>

</body>
</html>

Solution 2:

Try to use position absolute for your list and set position relative for ul parent.

ul {
  width: 100px;
  background-color: rgb(224, 224, 224);
  border-radius: 4px;
  border: 1px solid black;
  padding: 0;
  margin: 0;
  bottom: 40px;
  left: 0;
  position: absolute;
}

ul li {

  list-style: none;
  cursor: pointer;

  padding: 4px 10px;
}

li:hover {

  background-color: white;
}

div {
  width: 100%;
  height: 40px;
  position: relative;
  background-color: bisque;

}

section {

  height: 400px;
  background-color: #525252;
}

button {

  width: 100px;
  height: 100%;
  margin: 0;
}
<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <link rel="stylesheet" href="style.css">
  </head>

  <body>
    <section>
      <p>Some content</p>
    </section>
    <div>
      <ul>
        <li>Parrot</li>
        <li>Dog</li>
        <li>Cat</li>
        <li>Squirrel</li>
        <li>Otter</li>
        <li>Cow</li>
        <li>Goat</li>
        <li>Finch</li>
      </ul>
      <button onclick="showPop()">Pet</button>
    </div>

    <script>
      let ul = document.getElementsByTagName('ul')[0]

      onload = function() {

        ul.style.display = 'none'
      }

      function showPop() {


        if (ul.style.display == 'none') {
          ul.style.display = 'block'
        } else {
          ul.style.display = 'none'
        }
      }

    </script>

  </body>

</html>

Solution 3:

I have modified your CSS little bit. Please check the snippet.

let ul = document.getElementsByTagName('ul')[0]

        onload = function(){

            ul.style.display = 'none'
        }

        function showPop(){


            if(ul.style.display == 'none'){
                ul.style.display = 'block'
            }else{
                ul.style.display = 'none'
            }
        }
ul{

    width: 100px;
    background-color: rgb(224, 224, 224);
    border-radius: 4px;
    border: 1px solid black;
    padding: 0;
    margin: 0;

  position: absolute;
  bottom: 40px;
}

ul li{

    list-style: none;
    cursor: pointer;

    padding: 4px 10px;
}

li:hover{

    background-color: white;
}

div{

    width:  100%;
    height: 40px;

    background-color: bisque;

}

section{

    height: 400px;
    background-color: #525252;
}

button{

    width: 100px;
    height: 100%;
    margin: 0;
	position: relative;
  display: inline-block;
}
.dropup{
position: relative;
    display: inline-block;
	}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section>
        <p>Some content</p>
    </section>
    <div class='dropup'>
        <ul>
            <li>Parrot</li>
            <li>Dog</li>
            <li>Cat</li>
            <li>Squirrel</li>
            <li>Otter</li>
            <li>Cow</li>
            <li>Goat</li>
            <li>Finch</li>
        </ul>
        <button onclick="showPop()">Pet</button>
    </div>

Post a Comment for "CSS Custom Combobox Issue"