【问题标题】:How to fix css3 pseudo class nth-child如何修复 css3 伪类 nth-child
【发布时间】:2015-11-09 20:41:31
【问题描述】:

如何使左边框变圆?我有这个代码:

.container {
    margin-top: 25px;
}
.btn-group-radio input[type=radio] {
    display: none;
}
.btn-group-radio input[type=radio]:checked + .btn {
    color: #333333;
    background-color: #e6e6e6;
    *background-color: #d9d9d9;
    background-color: #cccccc \9;
    background-color: #e6e6e6;
    background-color: #d9d9d9 \9;
    background-image: none;
    outline: 0;
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.15), 0 1px 2px rgba(0, 0, 0, 0.05);
}
.btn-group-radio .btn:last-child {
    border-top-right-radius: 6px;
    border-bottom-right-radius: 6px;
}
.btn-group-radio .btn:first-child {
    border-top-left-radius: 6px;
    border-bottom-left-radius: 6px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />

    <div class="container">
        <div class="btn-group btn-group-justified btn-group-radio" role="group" id="id_offer">

            <input checked="checked" id="id_offer_0" name="offer" value="1" type="radio">
            <label class="btn btn-lg btn-default" for="id_offer_0">Offer 1</label>

            <input id="id_offer_1" name="offer" value="2" type="radio">
            <label class="btn btn-lg btn-default" for="id_offer_1">Offer 2</label>

            <input id="id_offer_2" name="offer" value="3" type="radio">
            <label class="btn btn-lg btn-default" for="id_offer_2">Offer 3</label>

        </div>
    </div>

对于最后一个标签一切正常,对于第一个标签不起作用 none:不像在 css 中,不是 .btn-group-radio .btn:nth-child(1).btn-g... .btn:nth-of-type(1),不是 ... label:first-child + .btn 和相同。

我如何理解first-child 在我的代码中为inputid="id_offer_0" 工作,但我不明白为什么nth-child 不工作以及为什么nth-of-type 不工作。

【问题讨论】:

  • 你到底想达到什么目的?
  • 我希望将左按钮上的左圆角作为右按钮上的右角。

标签: css css-selectors pseudo-class


【解决方案1】:

使用这些样式:

#id_offer .btn:first-of-type {
  border-top-left-radius: 6px;
  border-bottom-left-radius: 6px;
}

第一个按钮不是其父级的第一个子级,但它其类型的第一个。

您还应该根据 ID 进行选择,以克服 Bootstrap 的默认边框。

.container {
    margin-top: 25px;
}
.btn-group-radio input[type=radio] {
    display: none;
}
.btn-group-radio input[type=radio]:checked + .btn {
    color: #333333;
    background-color: #e6e6e6;
    *background-color: #d9d9d9;
    background-color: #cccccc \9;
    background-color: #e6e6e6;
    background-color: #d9d9d9 \9;
    background-image: none;
    outline: 0;
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.15), 0 1px 2px rgba(0, 0, 0, 0.05);
}
.btn-group-radio .btn:last-child {
    border-top-right-radius: 6px;
    border-bottom-right-radius: 6px;
}
#id_offer .btn:first-of-type {
    border-top-left-radius: 6px;
    border-bottom-left-radius: 6px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />

    <div class="container">
        <div class="btn-group btn-group-justified btn-group-radio" role="group" id="id_offer">

            <input checked="checked" id="id_offer_0" name="offer" value="1" type="radio">
            <label class="btn btn-lg btn-default" for="id_offer_0">Offer 1</label>

            <input id="id_offer_1" name="offer" value="2" type="radio">
            <label class="btn btn-lg btn-default" for="id_offer_1">Offer 2</label>

            <input id="id_offer_2" name="offer" value="3" type="radio">
            <label class="btn btn-lg btn-default" for="id_offer_2">Offer 3</label>

        </div>
    </div>

【讨论】:

  • 谢谢,它对我有用。你能描述一下为什么不工作吗:.btn-group-radio .btn:first-of-type 它在 div 类中排在最后。
猜你喜欢
  • 1970-01-01
  • 2014-08-01
  • 2014-10-14
  • 2012-05-10
  • 2014-03-07
  • 2020-12-24
  • 2018-11-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多