首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对中单选按钮(柔性盒?)

对中单选按钮(柔性盒?)
EN

Stack Overflow用户
提问于 2014-11-05 13:03:52
回答 1查看 900关注 0票数 1

我正试图将一些单选按钮水平和垂直地居中:http://jsfiddle.net/yxxd0cht/

我正在考虑使用柔性盒之类的东西,但我没能让它起作用。

CSS:

代码语言:javascript
复制
.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:

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-05 15:50:22

如果您开放使用flexbox,并且使用HTML5语法,那么我假设您的浏览器需求也允许您使用另一种策略。这是我最喜欢的方法,在未知维度的容器中精确地对齐一个未知维度的元素。

注意,我也清理了标记--因为您没有使用任何需要您通过类或ID精确标识项的JavaScript,您真正关心的唯一ID就是#green div。使用元素级选择器可以轻松地解决其余的元素,这有助于您避免过度指定样式,并使长期维护更容易。

代码语言:javascript
复制
#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%);
}
代码语言:javascript
复制
<!-- 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>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26757931

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档