【问题标题】:Horizontally align stacked radio buttons in center of Bootstrap grid在 Bootstrap 网格中心水平对齐堆叠的单选按钮
【发布时间】:2019-01-13 16:06:38
【问题描述】:
    <!-- 
    Bootstrap docs: https://getbootstrap.com/docs
    -->
<div class="container">
    <div class="row">
        <div class="col-md-6 text-center">
            <input id="show_imagery_buttons" type="checkbox">
            <label for="show_imagery_buttons">
            <span data-toggle="tooltip"
                title="Check to request an additional image type"><b>Additional Imagery</b>
            </span>
            </label>
            <ul id="imagery_buttons">
                <li class="radio">
                    <label><input type="radio" id="addImgPng" name="imagery"
                        value="png">PNG</label>
                </li>
                <li class="radio">
                    <label><input type="radio" id="addImgJpg"
                        name="imagery" value="jpg">JPG</label>
                </li>
                <li class="radio"><label><input type="radio" id="addImgPdf"
                    name="imagery"
                    value="pdf">PDF</label>
                </li>
            </ul>
        </div>
        <div class="col-md-6 text-center">
            <input id="show_dataset_buttons" type="checkbox">
            <label for="show_dataset_buttons">
            <span data-toggle="tooltip"
                title="Check to request another report format"><b>Additional Report Format</b>
            </span>
            </label>
            <ul id="dataset_buttons">
                <li class="radio"><label><input type="radio" id="addDataPDF"
                    name="report"
                    value="pdf">PDF</label>
                </li>
                <li class="radio"><label><input type="radio" id="addDataNLatex"
                    name="report"
                    value="latex">LaTeX</label></li>
            </ul>
        </div>
    </div>
</div>

我有两摞单选按钮,我想将它们并排显示。我想将它们隔开,使它们位于行的中间,在这些无线电块的外侧具有相等的空白。我已将它们放在两个引导列上,并设法使用 text-center 类将它们保持在列的中间 (see this fiddle)

虽然随着窗口大小的移动,这确实使按钮保持在引导列的中间,但我无法让单选按钮彼此垂直对齐。我认为这与使用 text-center 类有关,但我不确定用什么替换它。有人有什么想法吗?

【问题讨论】:

  • 您希望单选按钮水平堆叠还是垂直堆叠?您在帖子中提到:“我无法让单选按钮彼此垂直对齐。”当它垂直对齐时。
  • 我希望它们彼此垂直且“正方形”。理想情况下,单选按钮会垂直排列在一起。

标签: css twitter-bootstrap-3


【解决方案1】:

您是对的,您的 text-center 类影响了单选按钮及其标签的对齐方式。您还应用了 hide 类,它使您的第二组单选按钮不可见。删除这些将为您提供两列左对齐的单选按钮。请记住,引导列会随着您的屏幕尺寸调整而调整,因此您在较大的屏幕上可能只能在两列中看到它们。

#imagery_buttons, #dataset_buttons {
  margin-left: auto;
  margin-right: auto;
  width: 100px;
}
<head>
  <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>

  <!-- Latest compiled and minified CSS -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

  <!-- Optional theme -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

  <!-- Latest compiled and minified JavaScript -->
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>

<div class="container">
  <div class="row">
    <div class="col-md-6">
      <input id="show_imagery_buttons" type="checkbox">
      <label for="show_imagery_buttons">
        <span data-toggle="tooltip" title="Check to request an additional image type">
          <b>Additional Imagery</b>
        </span>
      </label>
      <ul id="imagery_buttons">
        <li class="radio">
          <label><input type="radio" id="addImgPng" name="imagery"value="png">PNG</label>
        </li>
        <li class="radio">
          <label><input type="radio" id="addImgJpg" name="imagery" value="jpg">JPG</label>
        </li>
        <li class="radio">
          <label><input type="radio" id="addImgPdf" name="imagery" value="pdf">PDF</label>
        </li>
      </ul>
    </div>
    <div class="col-md-6">
      <input id="show_dataset_buttons" type="checkbox">
      <label for="show_dataset_buttons">
        <span data-toggle="tooltip" title="Check to request another report format">
          <b>Additional Report Format</b>
        </span>
      </label>
      <ul id="dataset_buttons">
        <li class="radio">
          <label><input type="radio" id="addDataPDF" name="report" value="pdf">PDF</label>
        </li>
        <li class="radio">
          <label><input type="radio" id="addDataNLatex" name="report" value="latex">LaTeX</label>
        </li>        
      </ul>
    </div>
  </div>
</div>

【讨论】:

  • 这使单选按钮彼此对齐,但现在按钮不在列的中心。也许我应该在原始帖子中澄清一下。理想情况下,单选按钮彼此对齐,但按钮的“块”位于列的中心,所以我在按钮外部有相等的空白。
  • 啊,为此您可以应用一些自定义 css。您可以使用margin-left: automargin-right: auto 来居中,并声明一个包含您想要居中的区域的宽度。查看编辑后的答案以了解它如何与您的代码一起使用。
  • 如果你对浏览器兼容性没有限制,也可以尝试使用flexbox居中:css-tricks.com/snippets/css/a-guide-to-flexbox
猜你喜欢
  • 2012-08-31
  • 2014-07-27
  • 1970-01-01
  • 2014-03-17
  • 2011-08-21
  • 1970-01-01
  • 2017-03-09
  • 2021-06-06
  • 1970-01-01
相关资源
最近更新 更多