使用CSS定型下拉列表

时间:2016-11-07 13:42:35

标签: html css

我正在尝试设置类似于图片here的下拉列表。我很接近,但有些事我无法弄明白。 - 如何在列表中的每个项目之间添加行 - 如何使列表从" SELECT"开始。

This是我到目前为止所拥有的。

有没有更好的方法可以在不使用CSS的情况下执行此操作?我对CSS很新。

我不确定我的问题是如何相似的。如果有人可以解释。这篇文章与我想要达到的目标没有相似之处。



.dropbtn {
    color: white;
    width: 180px;
    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;
}

.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.sphere {
    height: 45px;
    width: 45px;
    border-radius: 50%;
    vertical-align: top;
    /* position: relative; */
    background: black;
    display: inline-block;
    margin-top: 5px;
    margin-left: -55px;
}
.dropdown-content a {
    table-layout: fixed;
    color: black;
    margin-left: 6px;
    padding: 12px 16px 13px 30px;
    text-decoration: none;
    display: block;
}

.dropdown a {
	background:url('sidearrow.png') no-repeat left;
}

.dropdown-content a:hover {
	background:url('sidewhite.png') no-repeat left;
	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;
}

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="dropdown.css">
</head>
<body>

<h2>Dropdown Menu</h2>
<p>Move the mouse over the button to open the dropdown menu.</p>

<div class="dropdown">
  <button class="dropbtn">SELECT</button>
  <div class="sphere">
  	<div id="arrow">
  		<img src="arrow.png" width="29" height="27" alt=""/>
	  </div>
  </div>
  <div class="dropdown-content">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
</div>
</body>
</html>
&#13;
&#13;
&#13;

5 个答案:

答案 0 :(得分:1)

&#13;
&#13;
.dropbtn {
    color: white;
    width: 180px;
    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;
}

.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.sphere {
    height: 45px;
    width: 45px;
    border-radius: 50%;
    vertical-align: top;
    /* position: relative; */
    background: black;
    display: inline-block;
    margin-top: 5px;
    margin-left: -55px;
}
.dropdown-content a {
    table-layout: fixed;
    color: black;
    margin-left: 6px;
    padding: 12px 16px 13px 30px;
    text-decoration: none;
    display: block;
}

.dropdown a {
	background:url('sidearrow.png') no-repeat left;
}

.dropdown-content a:hover {
	background:url('sidewhite.png') no-repeat left;
	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;
}
&#13;
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="dropdown.css">
</head>
<body>

<h2>Dropdown Menu</h2>
<p>Move the mouse over the button to open the dropdown menu.</p>

<div class="dropdown">
  <button class="dropbtn">SELECT</button>
  <div class="sphere">
  	<div id="arrow">
  		<img src="arrow.png" width="29" height="27" alt=""/>
	  </div>
  </div>
  <div class="dropdown-content">
    <a href="#">Link 1</a><hr/>
    <a href="#">Link 2</a><hr/>
    <a href="#">Link 3</a><hr/>
  </div>
</div>
</body>
</html>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您只需添加border-bottom: 1px solid black就可以了。

Working jsFiddle

.dropbtn {
  color: white;
  width: 180px;
  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;
}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}

.sphere {
  height: 45px;
  width: 45px;
  border-radius: 50%;
  vertical-align: top;
  /* position: relative; */
  background: black;
  display: inline-block;
  margin-top: 5px;
  margin-left: -55px;
}

.dropdown-content a {
  table-layout: fixed;
  color: black;
  margin-left: 6px;
  padding: 12px 16px 13px 30px;
  text-decoration: none;
  display: block;
  border-bottom: 1px solid black;
}

.dropdown a {
  background: url('sidearrow.png') no-repeat left;
}

.dropdown-content a:hover {
  background: url('sidewhite.png') no-repeat left;
  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;
}
<h2>Dropdown Menu</h2>
<p>Move the mouse over the button to open the dropdown menu.</p>

<div class="dropdown">
  <button class="dropbtn">SELECT</button>
  <div class="sphere">
    <div id="arrow">
      <img src="arrow.png" width="29" height="27" alt="" />
    </div>
  </div>
  <div class="dropdown-content">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
</div>

答案 2 :(得分:1)

检查以下提供的jsfiddle,在.dropdown-content中进行更改,即添加leftmargin值并将其z-index值设置为-1现在要添加边框,您可以在.dropdown-content a中使用border-bottom。

jsFiddle

.dropdown-content{
    left:8px;
    margin-top:-5px;
    z-index:-1;
    ..........
    ..........
    ..........
}

.dropdown-content a {
 border-bottom:1px solid #111;
    ..........
    ..........
    ..........
}

答案 3 :(得分:1)

你仍然需要调整一些尺寸,但它可以工作:

&#13;
&#13;
select_tag
&#13;
:selected
&#13;
&#13;
&#13;

答案 4 :(得分:1)

.dropbtn {
    color: white;
    width: 180px;
    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;
     z-index:2;
	position:relative;

}

.dropdown {
    position: relative;
    display: inline-block;
  

}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 177px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
	  margin-top:-20px;
    left:2px;
    z-index:0;
}
.sphere {
    height: 45px;
    width: 45px;
    border-radius: 50%;
    vertical-align: top;
    /* position: relative; */
    background: black;
    display: inline-block;
    margin-top: 5px;
    margin-left: -55px;
}
.dropdown-content a:first-child{
		padding-top: 30px;
 }
.dropdown-content a {
    table-layout: fixed;
    color: black;
    padding: 12px 16px 13px 30px;
    text-decoration: none;
    display: block;
}
	.dropdown-content a{
    border:1px solid #000;
    border-bottom:0;
	}
 .dropdown-content a:last-child{
    border-bottom:1px solid #000;
  }
.dropdown a {
	background:url('sidearrow.png') no-repeat left;
 }

.dropdown-content a:hover {
	background:url('sidewhite.png') no-repeat left;
	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;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="dropdown.css">
</head>
<body>

<h2>Dropdown Menu</h2>
<p>Move the mouse over the button to open the dropdown menu.</p>

<div class="dropdown">
  <button class="dropbtn">SELECT</button>
  <div class="sphere">
  	<div id="arrow">
  		<img src="arrow.png" width="29" height="27" alt=""/>
	  </div>
  </div>
  <div class="dropdown-content">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
</div>
</body>
</html>