我正试图将一些单选按钮水平和垂直地居中:http://jsfiddle.net/yxxd0cht/
我正在考虑使用柔性盒之类的东西,但我没能让它起作用。
CSS:
.costs
{
font-family: Arial, sans-serif;
background-color:#FFBC02;
color:white;
padding: 5px 12px;
margin-left:5px;
font-size: 1.05em;
}
input[type=radio]
{
display:none;
}HTML:
<div id="green">
<fieldset id="costs">
<legend>Costs: </legend>
<input id="free" name="costs" type="radio" value="free">
<label class="costs" for="free">Free</label>
<input id="chargeable" name="costs" type="radio" value="chargeable">
<label class="costs" for="chargeable">Chargeable</label>
<input id="mixed" name="costs" type="radio" value="mixed" checked>
<label class="costs" for="mixed">Mixed</label>
</fieldset>
</div>发布于 2014-11-05 15:50:22
如果您开放使用flexbox,并且使用HTML5语法,那么我假设您的浏览器需求也允许您使用另一种策略。这是我最喜欢的方法,在未知维度的容器中精确地对齐一个未知维度的元素。
注意,我也清理了标记--因为您没有使用任何需要您通过类或ID精确标识项的JavaScript,您真正关心的唯一ID就是#green div。使用元素级选择器可以轻松地解决其余的元素,这有助于您避免过度指定样式,并使长期维护更容易。
#green {
background-color: green;
height: 200px;
/* Make this the positioning parent for fieldset */
position: relative;
}
#green label {
font-family: Arial, sans-serif;
background-color: #FFBC02;
color: white;
padding: 5px 12px;
margin-left: 5px;
font-size: 1.05em;
}
#green input[type=radio] {
display: none;
}
#green input[type=radio]:checked + label {
background-color: lightgreen;
}
#green fieldset {
/* Border added for visualization */
border: 1px solid red;
/* Position absolute will position relative to first
non-static ancestor (in this case, #green) */
position: absolute;
/* Positions fieldset's top/left corner exactly in the
center of #green */
top: 50%;
left: 50%;
/* Translate's percentages are based on dimensions of
the element, not the width of the container, like
CSS % units. */
transform: translate(-50%, -50%);
}<!-- Removed unnecessary classes & IDs throughout.
The elements are easily addressible in CSS styles using
#green as the parent. -->
<div id="green">
<fieldset>
<legend>Costs:</legend>
<input id="free" name="costs" type="radio" value="free">
<label for="free">Free</label>
<input id="chargeable" name="costs" type="radio" value="chargeable">
<label for="chargeable">Chargeable</label>
<input id="mixed" name="costs" type="radio" value="mixed" checked>
<label for="mixed">Mixed</label>
</fieldset>
</div>
https://stackoverflow.com/questions/26757931
复制相似问题