2015-11-14 81 views
0

请看看这个codepen:CodePen。我有以下的html:CSS:设置div的背景颜色填充

<fieldset class="rangeset"> 
    <div class="range-container"> 
     <label for="question_three_radio_01"> 
      <input id="question_three_radio_01" name="question_three" type="radio" value="0" /><span></span> 
     </label> 
     <label for="question_three_radio_02"> 
      <input id="question_three_radio_02" name="question_three" type="radio" value="1" /><span></span> 
     </label> 
     <label for="question_three_radio_03"> 
      <input id="question_three_radio_03" name="question_three" type="radio" value="2" /><span></span> 
     </label> 
     <label for="question_three_radio_04"> 
      <input id="question_three_radio_04" name="question_three" type="radio" value="3" /><span></span> 
     </label> 
     <label for="question_three_radio_05"> 
      <input id="question_three_radio_05" name="question_three" type="radio" value="4" /><span></span> 
     </label> 
     <label for="question_three_radio_06"> 
      <input id="question_three_radio_06" name="question_three" type="radio" value="5" /><span></span> 
     </label> 
    </div> 
</fieldset> 

而这个CSS:

[type="radio"] { 
    border: 0; 
    clip: rect(0 0 0 0); 
    height: 1px; margin: -1px; 
    overflow: hidden; 
    padding: 0; 
    position: absolute; 
    width: 1px; 
} 

.radio-label { 
    display: block; 
    cursor: pointer; 
    line-height: 0; 
} 

[type="radio"] + span:before { 
    content: ''; 
    display: inline-block; 
    width: 20px; 
    height: 20px; 
    vertical-align: -5px; 
    border: 2px solid #fff; 
    box-shadow: 0 0 0 2px #000; 
    margin-right: 20px; 
    transition: 0.5s ease all; 
} 

[type="radio"]:checked + span:before { 
    background: #A4B162; 
    box-shadow: 0 0 0 4px #48530D; 
} 

fieldset { 
    border: 2px solid #48530D; 
    padding-top: 27px; 
    padding-right: 20px; 
    padding-bottom: 0px; 
    padding-left: 20px; 
} 

.rangeset { 
    padding-top: 27px; 
    padding-right: 0px; 
    padding-bottom: 20px; 
} 

.range-container { 
    display: flex; 
    justify-content: space-between; 
} 

我现在想要做的是创造一个背景颜色是一样厚和宽为所有单选按钮一起。我试图设置div的背景颜色,但结果是这样的:CodePen 2

这里设置的背景颜色已经和单选按钮一样厚(如此完美),但是如果你看一下非常合适的单选按钮,看到背景颜色比所有的单选按钮都宽。我想要在最后一个单选按钮结束时停止背景颜色。

你有什么想法吗?

+0

无法看到任何背景的条纹在第二演示。你能提供更多关于你需要填充的细节吗? – Harry

+0

真的吗?您在我发布的第二个codepen中的单选按钮的背景中看不到绿色条纹?如果我打开链接,我可以看到它。条纹太厚。我需要它更薄,只需要和单选按钮一样宽。你可以看到右边的单选按钮,条纹继续进一步。测试了我在Chrome,Firefox和Opera上发布的CodePen 2。我可以在任何地方看到它 – Mulgard

+0

我看不到任何条纹。我看到的只是一个黑色边框,有一些填充(白色)区域和一个绿色固体内部填充。 – Harry

回答

1

你可能是困惑设置您的利润。

.rangeset重置padding-right to 0;

跨度:之前给出一个margin-right了。

看到http://codepen.io/anon/pen/ojJOWM?editors=110

[type="radio"] { 
 
    border: 0; 
 
    clip: rect(0 0 0 0); 
 
    height: 1px; 
 
    margin: -1px; 
 
    overflow: hidden; 
 
    padding: 0; 
 
    position: absolute; 
 
    width: 1px; 
 
} 
 

 
.radio-label { 
 
    display: block; 
 
    cursor: pointer; 
 
    line-height: 0; 
 
} 
 

 
[type="radio"] + span:before { 
 
    content: ''; 
 
    display: inline-block; 
 
    width: 20px; 
 
    height: 20px; 
 
    vertical-align: -5px; 
 
    border: 2px solid #fff; 
 
    box-shadow: 0 0 0 2px #000; 
 
    margin-right: 20px;/* remove this from last label span */ 
 
    transition: 0.5s ease all; 
 
} 
 

 
[type="radio"]:checked + span:before { 
 
    background: #A4B162; 
 
    box-shadow: 0 0 0 4px #48530D; 
 
} 
 

 
fieldset { 
 
    border: 2px solid #48530D; 
 
    padding: 27px 20px; 
 
    display: block; 
 
} 
 

 
.rangeset { 
 
    padding-top: 27px; 
 
    padding-right: 0px;/* it doesn-t remove the margin-right of last label span:before */ 
 
    padding-bottom: 20px; 
 
} 
 

 
.range-container { 
 
    display: flex; 
 
    justify-content: space-between; 
 
    background: #A4B162; 
 
} 
 

 
label:last-child span:before { 
 
    margin-right: 0; 
 
}
<fieldset class="rangeset"> 
 
    <div class="range-container"> 
 
    <label for="question_three_radio_01"> 
 
     <input id="question_three_radio_01" name="question_three" type="radio" value="0" /><span></span> 
 
    </label> 
 
    <label for="question_three_radio_02"> 
 
     <input id="question_three_radio_02" name="question_three" type="radio" value="1" /><span></span> 
 
    </label> 
 
    <label for="question_three_radio_03"> 
 
     <input id="question_three_radio_03" name="question_three" type="radio" value="2" /><span></span> 
 
    </label> 
 
    <label for="question_three_radio_04"> 
 
     <input id="question_three_radio_04" name="question_three" type="radio" value="3" /><span></span> 
 
    </label> 
 
    <label for="question_three_radio_05"> 
 
     <input id="question_three_radio_05" name="question_three" type="radio" value="4" /><span></span> 
 
    </label> 
 
    <label for="question_three_radio_06"> 
 
     <input id="question_three_radio_06" name="question_three" type="radio" value="5" /><span></span> 
 
    </label> 
 
    </div> 
 
</fieldset>

1

这里有一种方法:

1 - 我改变.range-container以下几点:

.range-container { 
    display: flex; 
    justify-content: space-between; 
    position:relative; 
} 

2 - 然后补充说:

.range-container:before { 
    display:block; 
    width:calc(100% + 20px); 
    height:calc(100% + 27px + 20px); 
    position:absolute; 
    top:-27px; 
    left:-20px; 
    content: ''; 
    z-index:-1; 
    background: #A4B162; 
} 

这里的codepen:
http://codepen.io/Meligy/pen/EVGJmb