html - 如何向上移动下拉菜单

标签 html css

我无法将下拉菜单向上移动...以使其与文本更好地对齐。我尝试了 margin-top:-10px 但它似乎没有任何效果。

<h1 style="display: inline;"> Most popular     </h1>
<select class="selectpicker" style="margin-bottom: 20px; margin-top: -20px">
  <option id="7">last week</option>
  <option id="30">last month</option>
  <option id="180">last 6 months</option>
  <option id="365">last year</option>
</select>

最佳答案

默认选择框在不同的浏览器上可以有不同的 View (宽度、高度......),所以当你以固定的数量(例如 5px)将它向上推时,这在不同的浏览器上可能会有不同的结果。

所以最好首先设置您的选择框的样式以具有跨浏览器 View (无论如何您可以避免此步骤,我的解决方案也可以在没有此的情况下工作)例如:

select {    
    width: 200px;
    height: 34px;
    padding: 6px 12px;
    font-size: 14px;
    line-height: 1.42857143;
    color: #555;
    background-color: #fff;
    background-image: none;
    border: 1px solid #ccc;
    border-radius: 4px;
}

示例 1 提供了如何将 h1 和 select 彼此垂直对齐:

.example1 h1,
.example1 select {
  display: inline-block;
  vertical-align: middle;
  line-height: 35px; /* set any value as you need */
}

<div class="example1">
   <h1>Most popular</h1>
   <select class="selectpicker">
      <option id="7">last week</option>
      <option id="30">last month</option>
      <option id="180">last 6 months</option>
      <option id="365">last year</option>
   </select>
</div>

示例 2 提供了如何根据需要相对于 h1 元素定位选择框,只需设置顶部位置即可:

.example2 {
  display: inline-block;
  position: relative;
  padding-right: 200px; /* set value equal to select-box width */
}

.example2 select {
  top: 3px; /* set any value to position select-box */
  right: 0;  
  display: block;
  position: absolute;    
}

<div class="example2">
    <h1>Most popular</h1>
    <select class="selectpicker">
        <option id="7">last week</option>
        <option id="30">last month</option>
        <option id="180">last 6 months</option>
        <option id="365">last year</option>
     </select>
 </div>

查看工作示例 here

希望这会有所帮助:)

关于html - 如何向上移动下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35183703/

相关文章:

javascript - 选项卡在 Bootstrap v4.3.1 中不工作,但在 v4.1.0 中工作

CSS:如何根据字符高度与行高设置样式

html - 在 li 中显示数据 angularjs ng-repeat

javascript - 悬停在无形区域上的动画

javascript - jQuery 动画不适用于 iframe 内容

javascript - Bootstrap 导航栏在单击侧边栏导航列表后隐藏页面部分

javascript - 全屏 HTML5 视频要大吗?

jquery - 试图滚动到 "fix"导航位置

java - 如何在jsp或html5的数据列表中添加复选框

javascript - 将鼠标悬停在列出的元素文本上时会出现图像