使CSS下拉内容与右侧而不是左侧对齐

时间:2018-10-15 19:04:25

标签: html css

此下拉菜单(框)的内容在图标下方左对齐。我需要它在右边的下面对齐。谢谢!

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.1/css/all.css" integrity="sha384-O8whS3fhG2OnA5Kas0Y9l3cfpmYjapjI0E4theH4iuMD+pLhbf6JI0jIMfYcK3yZ" crossorigin="anonymous" />
<style>
.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-span {
    cursor: pointer;
}

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

.dropdown:hover .dropdown-content {
    display: block;
}
</style>
</head>
<body>


<div style="text-align:center;">
<div class="dropdown">
  <span><i class="far fa-caret-square-down fa-lg"></i></span>
  <div class="dropdown-content">
    <p>Hello World!</p>
  </div>
</div>
</div>


</body>
</html>

3 个答案:

答案 0 :(得分:3)

我认为这可能会有所帮助。 通过在.dropdown-content

中添加 right:0;

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.1/css/all.css" integrity="sha384-O8whS3fhG2OnA5Kas0Y9l3cfpmYjapjI0E4theH4iuMD+pLhbf6JI0jIMfYcK3yZ" crossorigin="anonymous" />
<style>
.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-span {
    cursor: pointer;
}

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

.dropdown:hover .dropdown-content {
    display: block;
}
</style>
</head>
<body>


<div style="text-align:center;">
<div class="dropdown">
  <span><i class="far fa-caret-square-down fa-lg"></i></span>
  <div class="dropdown-content">
    <p>Hello World!</p>
  </div>
</div>
</div>


</body>
</html>

答案 1 :(得分:2)

在下拉式内容类别中添加 right:0 属性。

答案 2 :(得分:2)

更新:对不起-我可以从其他答案中得知我误解了您的问题。我将在此处保留此答复,以供后人参考。

这是一个将下拉图标的高度硬编码为18px的骇客。

您的原始代码与下面的代码之间的区别是:

  • top: 18px;已添加到.dropdown-content CSS规则
  • display: inline-block已在.dropdown:hover .dropdown-content CSS规则中更新

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.1/css/all.css" integrity="sha384-O8whS3fhG2OnA5Kas0Y9l3cfpmYjapjI0E4theH4iuMD+pLhbf6JI0jIMfYcK3yZ" crossorigin="anonymous" />
<style>
.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-span {
    cursor: pointer;
}

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

.dropdown:hover .dropdown-content {
    display: inline-block;
}
</style>
</head>
<body>


<div style="text-align:center;">
<div class="dropdown">
  <span><i class="far fa-caret-square-down fa-lg"></i></span>
  <div class="dropdown-content">
    <p>Hello World!</p>
  </div>
</div>
</div>


</body>
</html>