【问题标题】:How to Center Radio Button如何居中单选按钮
【发布时间】:2015-10-23 12:25:12
【问题描述】:

对于我的网页,我使用单选按钮在网页顶部制作包含内容的标签。我希望标签在屏幕中居中。我尝试使用 display: flex 和 text-align: justify 但它不起作用。有没有一种简单的方法可以使单选按钮居中?任何帮助将不胜感激。

这里是 HTML

<body>
<div class="allTabs">
  <div class="tab">
    <input type="radio" id="tab-1" name="tab-group-1" checked>
    <label for="tab-1">Tab One</label>
      <div class="content">
         stuff 1
      </div> 
  </div>
  <div class="tab">
    <input type="radio" id="tab-2" name="tab-group-1">
    <label for="tab-2">Tab Two</label>  
    <div class="content">
       stuff 2
    </div> 
  </div> 
  <div class="tab">
    <input type="radio" id="tab-3" name="tab-group-1">
    <label for="tab-3">Tab Three</label>
    <div class="content">
       stuff 3
    </div> 
  </div> 

这里是 CSS

.tabs {   
  min-height: 200px; /* This part sucks */
  clear: both;
}
.tab {
  float: left;
}
.tab label {
  background: white; 
  padding: 10px; 
  border: 1px solid #ccc; 
  margin-left: -1px; 
  position: relative;
  left: 1px;
}
.tab [type=radio] {
  display: none;   
}
.content {
  position: absolute;
  top: 28px;
  left: 0;
  background: white;
  right: 0;
  bottom: 0;
  padding: 20px;
  border-top: 4px solid #DADEDE;
  margin-top: 4px;
}
[type=radio]:checked ~ label {
  background: white;
  padding-bottom: 0px;
  border-bottom: 4px solid blue;
  margin-bottom: 5px;
  z-index: 2;
}
[type=radio]:checked ~ label ~ .content {
  z-index: 1;
}

这里是 JSFiddle https://jsfiddle.net/mk8r6kL0/

【问题讨论】:

  • 你试过margin-left and right:auto;吗?
  • 是的,但遗憾的是它什么也没做
  • jsfiddle.net/mk8r6kL0/3 我不确定你想要对齐的位置
  • 首先请注意,在您的 CSS 中,您将样式添加到似乎不存在的 .tabs 类。你是说.allTabs 类吗?
  • 是的,它对齐得很好。谢谢!

标签: html css radio-button flexbox centering


【解决方案1】:

看到这个fiddle

.tab 的 CSS 中删除 float:left; 并添加 display:inline-block;

另外,添加

.allTabs {
    text-align: center;
}

到你的 CSS

所以,完整的 CSS 如下所示

.allTabs {
    text-align: center;
}
.tabs {
    min-height: 200px;
    clear: both;
}
.tab {
    display: inline-block;
}
.tab label {
    background: white;
    padding: 10px;
    border: 1px solid #ccc;
    margin-left: -1px;
    position: relative;
    left: 1px;
}
.tab[type=radio] {
    display: none;
}
.content {
    position: absolute;
    top: 28px;
    left: 0;
    background: white;
    right: 0;
    bottom: 0;
    padding: 20px;
    border-top: 4px solid #DADEDE;
    margin-top: 4px;
}
[type=radio]:checked ~ label {
    background: white;
    padding-bottom: 0px;
    border-bottom: 4px solid blue;
    margin-bottom: 5px;
    z-index: 2;
}
[type=radio]:checked ~ label ~ .content {
    z-index: 1;
}

【讨论】:

  • 谢谢@Noobie123 ..:)
  • 哦。现在我想我修好了
【解决方案2】:

移除浮动并尝试...

.allTabs {
    text-align: center;
}

.tab {
    display: inline-block;
}

JSfiddle Demo

【讨论】:

    猜你喜欢
    • 2012-08-29
    • 2015-01-01
    • 1970-01-01
    • 2011-10-19
    • 2012-10-14
    • 2013-02-09
    • 2015-09-28
    • 2021-09-08
    • 1970-01-01
    相关资源
    最近更新 更多