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;

}

ulli{

    list-style: none;
    cursor: pointer;

    padding: 4px10px;
}

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><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width, initial-scale=1.0"><title>Document</title><linkrel="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><buttononclick="showPop()">Pet</button></div><script>let ul = document.getElementsByTagName('ul')[0]

        onload = function(){

            ul.style.display = 'none'
        }

        functionshowPop(){


            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;
}

ulli {

  list-style: none;
  cursor: pointer;

  padding: 4px10px;
}

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><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width, initial-scale=1.0"><title>Document</title><linkrel="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><buttononclick="showPop()">Pet</button></div><script>let ul = document.getElementsByTagName('ul')[0]

      onload = function() {

        ul.style.display = 'none'
      }

      functionshowPop() {


        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'
        }

        functionshowPop(){


            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;
}

ulli{

    list-style: none;
    cursor: pointer;

    padding: 4px10px;
}

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;
	}
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><section><p>Some content</p></section><divclass='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><buttononclick="showPop()">Pet</button></div>

Post a Comment for "Css Custom Combobox Issue"