【问题标题】:Bootstrap: Remove border and arrows for dropdown fieldBootstrap:删除下拉字段的边框和箭头
【发布时间】:2014-10-02 09:02:19
【问题描述】:

当我使用 Bootstrap 3 时,如何在不弄乱高度的情况下删除下拉箭头并同时隐藏边框?

这是一个plunk,我尝试这样做。隐藏箭头(自定义选择类)基于this blog 复制this code

也许最好检查一下 plunk,但这里是 CSS:

.no-border {
    border: 0;
    box-shadow: none;
}

.custom-select {
    background-color: #fff;
    border: 1px solid #ccc;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    margin: 0 0 2em;
    padding: 0;
    position: relative;
    width: 100%;
    z-index: 1;
}

.custom-select:hover {
    border-color: #999;
}

.custom-select:before {
    color: #333;
    display: block;
    font-family: 'FontAwesome';
    font-size: 1em;
    height: 100%;
    line-height: 2.5em;
    padding: 0 0.625em;
    position: absolute;
    top: 0;
    right: 0;
    text-align: center;
    width: 1em;
    z-index: -1;
}

.custom-select select {
    background-color: transparent;
    border: 0 none;
    box-shadow: none;
    color: #333;
    display: block;
    font-size: 100%;
    line-height: normal;
    margin: 0;
    padding: .5em;
    width: 100%;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
}

.custom-select select::-ms-expand {
    display: none; /* to ie 10 */
}

.custom-select select:focus {
    outline: none;
}

/* little trick for custom select elements in mozilla firefox  17/06/2014 @rodrigoludgero */

/* pseudo class https://developer.mozilla.org/en-US/docs/Web/CSS/:any */

:-moz-any(.custom-select):before {
    background-color: #fff; /* this is necessary for overcome the caret default browser */
    pointer-events: none; /* https://developer.mozilla.org/en-US/docs/Web/CSS/pointer-events  */
    z-index: 1; /* this is necessary for overcome the pseudo element */
}

编辑:如果我将 !important 添加到我的无边框边框,那么它解决了与边框相关的问题:

.no-border {
    border: 0 !important;
    box-shadow: none;
}

因此,当切换自定义选择以删除/添加下拉箭头时,高度变化问题仍然存在......

【问题讨论】:

  • 试试这个 - jsfiddle.net/dfdon4gb
  • 我在您的 css 中添加了 .custom-select:before{display:none !important;},以根据您的第一个要求删除下拉箭头 “如何在不弄乱高度的情况下删除下拉箭头?”看这里 - jsfiddle.net/xj3zjLmz
  • 哦,现在看。但它解决了什么问题(与我已经拥有的 plunk 相比,有问题的链接)?移除箭头是有效的(就像你的小提琴一样)。我的问题是增加的高度(如果你在小提琴下面添加一些内容并删除/添加自定义选择类,或者只是查看我的 plunk,你会看到这个)和同时删除边框。
  • 如果我在我的无边界边界定义中改用“!重要”,那么它解决了边界问题。然后添加的高度问题仍然存在......
  • @MaryMelody 你的小提琴在里面使用了很棒的字体。 OP不是。如果是这种情况,您可以通过从选择中删除 fa-caret-down 类来轻松删除箭头。

标签: css twitter-bootstrap twitter-bootstrap-3


【解决方案1】:

使用!important 确实应该仅作为最后的手段。
在我看来,这是懒惰的出路。

在您的 .custom-select 课程中,您有两件事

.custom-select {
    background-color: #fff;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    margin: 0 0; /* "0 0 2em" reads as margin-top: 0; margin-left: 0; margin-right: 0; margin-bottom: 2em; (32px) */
    padding: 0;
    position: relative;
    width: 100%;
    z-index: 1;
}

你的边距是margin: 0 0 2em;,你自己给它一个边框。相反,我只是删除了它。或者你可以把它改成border: 0;

另外:语义...但是:

<select id="status" class="form-control" ng-class="{'no-border': border}" id="inputEmail3">
    <option>First option</option>
    <option>Another option</option>
    <option>We also have a tird</option>
</select>

你有两个 id 属性。你应该删除一个。

【讨论】:

  • +0001 for 使用!important 是懒惰的出路。
  • 我不会撒谎...我过去使用它的方式比我应该使用的要多。 ;)
  • 谢谢@Oberst!而且我意识到我根本不需要下拉字段的无边框类来满足我的需要(我只是切换自定义选择类),因此没有理由使用 !important :) 谢谢!跨度>
猜你喜欢
  • 2021-12-06
  • 1970-01-01
  • 2017-08-11
  • 2016-11-29
  • 1970-01-01
  • 2020-01-22
  • 1970-01-01
  • 2020-11-25
  • 2014-08-17
相关资源
最近更新 更多