Mani Mani - 24 days ago 19
CSS Question

Weird dropdown behavior css

I have two dropdown lists and they work as expected(that they show the content on hover) however, If I click on the area outside (left side) the list , the dropdown list still shows.

I have tried fixing the CSS but I have not been able to pin point the problem.

Is there a better approach for styling dropdown as I am still new to CSS.

UPDATE:

Testing this in firefox and I was unable to recreate the issue. How can I make sure that works consistently in all browsers?



.dropbtn {
color: white;
width: 200px;
margin-top: 160px;
margin-left: 276px;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
text-align: left;
font-weight: 900;
background: rgba(0, 173, 239, 1);
border-radius: 0px;
-moz-border-radius: 0px;
-webkit-border-radius: 50px 50px 50px 50px;
position: relative;
z-index: 2;
}

.dropdown {
position: relative;
display: inline-block;
width: 480px;
height: 0px;
background: antiquewhite;
}

.dropdown-content {
display: none;
position: relative;
z-index: 1;
background-color: #f9f9f9;
/* min-width: 160px; */
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
right: 5px;
top: -20px;
margin-left: 283px;
}

.sphere {
height: 45px;
width: 45px;
border-radius: 50%;
vertical-align: top;
/* position: relative; */
background: black;
display: inline-block;
position: relative;
margin-top: 165px;
margin-left: -55px;
z-index: 2;
}
.dropdown-content a {
table-layout: fixed;
color: black;
margin-left: -1px;
padding: 12px 16px 13px 30px;
text-decoration: none;
display: block;
border: 1px solid #000;
border-top: none;
font-size: 21px;
}
.dropdown-content a:first-child {
padding-top: 35px;
background: url("Images/sidearrow.png") no-repeat 7px 36px;
}

.dropdown a {
background:url("Images/sidearrow.png") no-repeat 7px;
}

.dropdown-content a:first-child:hover {
padding-top: 35px;
background: url("Images/sidewhite.png") no-repeat 3px 36px;
color:black;
background-color: rgb(255,131,0);
}

.dropdown-content a:hover {
background:url("Images/sidewhite.png") no-repeat 3px;
background-color: rgb(255,131,0);
color:white;
}

.dropdown:hover .dropdown-content {
display: block;
}

.dropdown:hover .dropbtn {
background-color: #3e8e41;
}

div#arrow {
position: absolute;
margin-left: 10px;
margin-top: 11px;
}


/* SENTINAL */


.dropbtn-sentinal {
color: white;
width: 200px;
margin-top: 160px;
margin-left: 276px;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
text-align: left;
font-weight: 900;
background: rgba(0, 173, 239, 1);
border-radius: 0px;
-moz-border-radius: 0px;
-webkit-border-radius: 50px 50px 50px 50px;
position: relative;
z-index: 2;
}

.dropdown-sentinal{
position: relative;
display: inline-block;
width: 480px;
height: 0px;
background: antiquewhite;
}

.dropdown-content-sentinal {
display: none;
position: relative;
z-index: 1;
background-color: #f9f9f9;
/* min-width: 160px; */
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
right: 5px;
top: -20px;
margin-left: 283px;
}

.sphere-sentinal {
height: 45px;
width: 45px;
border-radius: 50%;
vertical-align: top;
/* position: relative; */
background: black;
display: inline-block;
position: relative;
margin-top: 165px;
margin-left: -55px;
z-index: 2;
}
.dropdown-content-sentinal a {
table-layout: fixed;
color: black;
margin-left: -1px;
padding: 12px 16px 13px 30px;
text-decoration: none;
display: block;
border: 1px solid #000;
border-top: none;
font-size: 21px;
}
.dropdown-content-sentinal a:first-child {
padding-top: 35px;
background: url("Images/sidearrow.png") no-repeat 7px 36px;
}

.dropdown-sentinal a {
background:url("Images/sidearrow.png") no-repeat 7px;
}

.dropdown-content-sentinal a:first-child:hover {
padding-top: 35px;
background: url("Images/sidewhite.png") no-repeat 3px 36px;
color:black;
background-color: rgb(255,131,0);
}

.dropdown-content-sentinal a:hover {
background:url("Images/sidewhite.png") no-repeat 3px;
background-color: rgb(255,131,0);
color:white;
}

.dropdown-sentinal:hover .dropdown-content-sentinal {
display: block;
}

.dropdown-sentinal:hover .dropbtn-sentinal {
background-color: #3e8e41;
}

div#arrow-sentinal {
position: absolute;
margin-left: 10px;
margin-top: 11px;
}

div#dropdown-one {
width: 500px;
height: 198px;
}

div#dropdown-two {
margin-left: 700px;
margin-top: -198px;
}


div#dropdown-content a {
text-decoration: none;
border: solid black 1px;
display: table-caption;
background-color: rgb(237,237,238);
background: url(Images/sidearrow.png) no-repeat 7px 36px;
}

<!DOCTYPE html>
<html>
<head>
<title>title</title>
<link rel="stylesheet" type="text/css" href="dropdown.css">
<meta charset="utf-8">
</head>
<body>
<div class="averios-header">
<div id="averios-logo">
<img src="averioslogo.png" width="176" height="129">
</div>
<div id="setting-dropdown">
<!-- SETTING DROPDOWN WILL GO HERE -->
</div>
<div id="header-hr">
</div>
</div>

<div id="log-in">
<div id="loginHeader">
<h1>Portal</h1>
</div>
<div id="welcome-text">
<p> Welcome name </p>
<p> Your last login was time on date </p>
<br>
<p> Please select an application below to begin </p>
</div>
</div>
<div id="dropdown-one">
<div class="dropdown">
<button class="dropbtn">SELECT</button>
<div class="sphere">
<div id="arrow">
<img src="C:\Users\mseh\Desktop\Images\arrow.png" width="29" height="27" alt=""/>
</div>
</div>
<div class="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 1</a>
<a href="#">Link 1</a>
<a href="#">Link 1</a>
</div>
</div>
</div>

<div id="dropdown-two">
<div class="dropdown-sentinal">
<button class="dropbtn-sentinal">SELECT</button>
<div class="sphere-sentinal">
<div id="arrow-sentinal">
<img src="C:\Users\mseh\Desktop\Images\arrow.png" width="29" height="27" alt=""/>
</div>
</div>
<div class="dropdown-content-sentinal">
<a href="#">Link 1</a>
<a href="#">Link 1</a>
<a href="#">Link 1</a>
<a href="#">Link 1</a>
</div>
</div>
</div>
</body>
</html>





Image

Answer

Here - I shortened the example down a bit (I also don't think you need all that separate CSS for more than one button, you should be able to utilize the classes, and just change specifics by using #dropdown_two .classname for the things (like position) you might want to change.

The problem in Chrome, I think, was that the width of the container was wider than the button itself, and that made it misbehave. It might not be 100%, but it should get you started. I changed some values to dynamic (em instead of px) so there might be some issues.

#dropdown-one {
  margin-left: 20em;
  }
.dropbtn {
    color: white;
    width: 100%;
  height: 3.2em;
    font-size: 16px;
    border: none;
    cursor: pointer;
    text-align: left;
    font-weight: 900;
    background: rgba(0, 173, 239, 1);
    border-radius: 0px;
    -moz-border-radius: 0px;
    -webkit-border-radius: 50px 50px 50px 50px;
    position: relative;
    z-index: 2;
}

.dropdown {
    position: relative;
    display: block;
    width: 12.5em;
    background:antiquewhite;
}

.dropdown-content {
    display: none;
    position: relative;
    z-index: 1;
    background-color: #f9f9f9;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    top: -20px;
}

.sphere {
    height: 2.9em;
    width: 2.9em;
    border-radius: 50%;
  border: 2px solid rgba(0, 173, 239, 1);
    /* position: relative; */
    background: black;
    position: absolute;
  top: 0;
  bottom: 0;
  right: .1em;
    z-index: 2;
}
.dropdown-content a {
    table-layout: fixed;
    color: black;
    padding: .75em;
    text-decoration: none;
    display: block;
    border: 1px solid #000;
    border-top: none;
    font-size: 21px;
  box-sizing: border-box;
}
.dropdown-content a:first-child {
  padding-top: 35px;
  background: url("Images/sidearrow.png") no-repeat 7px 36px;
}

.dropdown a {
	background:url("Images/sidearrow.png") no-repeat 7px;
}

.dropdown-content a:first-child:hover {
  background: url("Images/sidewhite.png") no-repeat 3px 36px;
  color:black;
  background-color: rgb(255,131,0);
}

.dropdown-content a:hover {
	background:url("Images/sidewhite.png") no-repeat 3px;
	background-color: rgb(255,131,0);
	color:white;
}

.dropdown:hover .dropdown-content {
    display: block;
}

.dropdown:hover .dropbtn {
    background-color: #3e8e41;
}

div#arrow {
    position: absolute;
    margin-left: 10px;
    margin-top: 11px;
}
<div class="averios-header">
	<div id="setting-dropdown">
		<!-- SETTING DROPDOWN WILL GO HERE -->
	</div>
</div>

<div id="log-in">
	<div id="loginHeader">
		<h1>Portal</h1>
	</div>
	<div id="welcome-text">
		<p> Welcome name </p>
		<p> Your last login was time on date </p>
		<p> Please select an application below to begin </p>
	</div>
</div>
<div id="dropdown-one">
	<div class="dropdown">
	  <button class="dropbtn">SELECT</button>
	  <div class="sphere">
		<div id="arrow">
			<img src="C:\Users\mseh\Desktop\Images\arrow.png" width="29" height="27" alt=""/>
		  </div>
	  </div>
	  <div class="dropdown-content">
		<a href="#">Link 1</a>
		<a href="#">Link 1</a>
		<a href="#">Link 1</a>
		<a href="#">Link 1</a>		
	  </div>
	</div>
</div>

Comments