如何自动调整下拉内容的宽度以匹配<p>元素的宽度?

时间:2018-12-12 21:55:43

标签: css html5

基本上,我有两个<p>元素。将鼠标悬停在一个<p>元素上时,将显示下拉菜单内容。问题是,我希望下拉菜单的 content (下拉菜单本身)的宽度 match <p>元素的宽度匹配。 These images should probably describe what I'm talking about。我在第一张图片上绘制的红线是我要“剪切”或调整大小的文字,因此它恰好适合文本。

代码如下:

#dropdown {
  display: inline-block;
  position: relative;
  text-align: center;
}

#dropdown p {
  color: #262261;
  font-family: 'Work Sans', sans-serif;
  font-size: 35px;
  letter-spacing: 5px;
  margin: auto auto auto auto;
}

#dropdown-content {
  position: absolute;
  margin: 10px auto auto auto;
  height: auto;
  min-width: 300px;
  opacity: 0;
  transition: all .2s ease-in;
  background-color: #262261;
  border-radius: 8px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

.nav-dropdown-container {
  width: 400px;
}

#dropdown-content p {
  font-size: 20px;
  font-family: 'Work Sans', sans-serif;
  font-size: 30px;
  letter-spacing: 2px;
  transition: 0.5s;
}

#dropdown:hover #dropdown-content {
  opacity: 1;
  line-height: 2;
  padding-top: 1em;
  padding-bottom: 1em;
  -webkit-transition: all 0.5s ease-in-out;
  -moz-transition: all 0.5s ease-in-out;
  -o-transition: all 0.5s ease-in-out;
  transition: all 0.5s ease-in-out;
}

#dropdown-link:link {
  color: #FFFFFF;
  display: block;
  text-decoration: none;
}
<div id="applications">
  <div class="nav-dropdown-container">
    <div id="dropdown">
      <p>APPLICATIONS</p>
      <div id="dropdown-content">
        <center>
          <p><a id="dropdown-link" href="pages/nami.html">abc</a></p>
          <p><a id="dropdown-link" href="pages/jammr.html">def</a></p>
        </center>
      </div>
    </div>
  </div>

我真的不想手动执行此操作,而是希望自动执行此操作。请让我知道这是否可行。同样,我很难解释自己想做什么,所以我会尽力而为。谢谢!

1 个答案:

答案 0 :(得分:1)

只需将left:0;right:0;positon:absolute元素一起使用,并删除所有宽度定义。 inline-block将使宽度适合内容:

And get rid of the center tag.

#dropdown {
  display: inline-block;
  position: relative;
  text-align: center;
}

#dropdown p {
  color: #262261;
  font-family: 'Work Sans', sans-serif;
  font-size: 35px;
  letter-spacing: 5px;
  margin: auto auto auto auto;
}

#dropdown-content {
  position: absolute;
  left: 0;
  right: 0;
  margin: 10px auto auto;
  text-align: center;
  opacity: 0;
  transition: all .2s ease-in;
  background-color: #262261;
  border-radius: 8px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

#dropdown-content p {
  font-size: 20px;
  font-family: 'Work Sans', sans-serif;
  font-size: 30px;
  letter-spacing: 2px;
  transition: 0.5s;
}

#dropdown:hover #dropdown-content {
  opacity: 1;
  line-height: 2;
  padding-top: 1em;
  padding-bottom: 1em;
  transition: all 0.5s ease-in-out;
}

#dropdown-link:link {
  color: #FFFFFF;
  display: block;
  text-decoration: none;
}
<div id="dropdown">
  <p>APPLICATIONS</p>
  <div id="dropdown-content">
    <p><a id="dropdown-link" href="pages/nami.html">abc</a></p>
    <p><a id="dropdown-link" href="pages/jammr.html">def</a></p>
  </div>
</div>
<div id="dropdown">
  <p>extention</p>
  <div id="dropdown-content">
    <p><a id="dropdown-link" href="pages/nami.html">abc</a></p>
    <p><a id="dropdown-link" href="pages/jammr.html">def</a></p>
  </div>
</div>