在 Bootstrap 4 行中对齐 SVG 元素

Align SVG element in Bootstrap 4 row

我想将图标 (<svg>) 和文本都对齐到容器的中心。我想要图标和文本之间的小间距:

|      ICON TEXT      |

https://jsbin.com/tatomas/edit?html,css,output

我有 Bootstrap 4 (Flexbox) 可用。但我不知道该怎么做。特别是 <svg> 很难对齐 - 我无法将它移动到任何地方。我尝试将它们放在 .row 中,每个 .col,但 SVG 保持在列的中心...

我怎样才能做到这一点(有或没有 BS 网格)??

使用现有的标记,需要做两件主要的事情:

  • col有预定义的填充,需要重新设置

  • 例如使用 text-align: right 将 SVG 向右对齐

此外,SVG 元素 w/o 宽度设置可能会在某些浏览器上造成问题,所以我在这里给了它一个(在其标记中添加 width="80"

有了这个,您现在可以将您选择的边距或填充添加到 col 元素(在下面的替代方案中,添加到 section-* 元素),并且在这些示例中我使用保证金。

堆栈片段

.container {
  max-width: 300px;
}
.row .col {
  padding: 0;                   /*  added  */
}

.section-icon {
  max-height: 30px;
  text-align: right;            /*  added  */
  margin-right: 5px;
}

.section-title {
  text-align: left;
  margin-left: 5px;
}

/*  for styling of this demo  */
div.section-icon, .section-title {
  border: 1px dotted;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" rel="stylesheet" type="text/css" />

  <div class="container p-4 border">
    <div class="row">
      <div class="col">
        <div class="section-icon">
          <svg width="80" class="section-icon active" viewBox="0 0 512 234.34"><path d="M110.93,287.83A42.67,42.67,0,1,0,153.6,330.5,42.71,42.71,0,0,0,110.93,287.83Zm0,68.27a25.6,25.6,0,1,1,25.6-25.6A25.63,25.63,0,0,1,110.93,356.1Z" transform="translate(0 -138.83)"></path><path d="M503.47,287.83v-51.2a8.53,8.53,0,0,0-5.84-8.09l-48.14-16c-18.14-30.72-43.06-59.94-98.15-69.59l-.3-.05c-101.73-14.07-178.1,8.88-227.14,68.22-36.49.53-73.75,8.16-95.66,22.19-11.35,7.27-18.08,19.92-18.72,33.39l-1,21.19A8.53,8.53,0,0,0,0,296.37V330.5A8.53,8.53,0,0,0,8.53,339H42.67a8.59,8.59,0,0,0,8.53-8.62c0-.05,0-.09,0-.14a59.73,59.73,0,0,1,119.46.23c-.62,5.18,3.47,8.53,8.53,8.53H332.8a8.58,8.58,0,0,0,8.53-8.62c0-.05,0-.09,0-.14a59.73,59.73,0,0,1,119.46.23c-.62,5.18,3.47,8.53,8.53,8.53h34.13A8.54,8.54,0,0,0,512,330.5V296.37A8.54,8.54,0,0,0,503.47,287.83Zm-230.4-81.06A4.27,4.27,0,0,1,268.8,211H157.52a4.3,4.3,0,0,1-2.83-7.51c29.61-27,67.27-42.42,113.67-46.53a4.36,4.36,0,0,1,4.71,4.3Zm102.52,0a4.27,4.27,0,0,1-4.27,4.22H294.4a4.27,4.27,0,0,1-4.27-4.26V160.18a4.28,4.28,0,0,1,4.24-4.31,393,393,0,0,1,54.17,3.86,144.79,144.79,0,0,1,24.62,6.51,4.29,4.29,0,0,1,2.77,4.08ZM420.12,211h-23.2a4.27,4.27,0,0,1-4.27-4.31l.23-23.12a4.29,4.29,0,0,1,6.89-3.35,112.87,112.87,0,0,1,23.73,23.88A4.29,4.29,0,0,1,420.12,211Z" transform="translate(0 -138.83)"></path><path d="M401.07,287.83a42.67,42.67,0,1,0,42.66,42.67A42.72,42.72,0,0,0,401.07,287.83Zm0,68.27a25.6,25.6,0,1,1,25.6-25.6A25.62,25.62,0,0,1,401.07,356.1Z" transform="translate(0 -138.83)"></path></svg>
        </div>
      </div>
      <div class="col">
        <div class="section-title">
          Car
        </div>        
      </div>
    </div>
  </div>


作为替代方案,您可以删除 col 元素并将内置的 class justify-content-center 添加到 row 元素 (<div class="row justify-content-center">)

堆栈片段

.container {
  max-width: 300px;
}

.section-icon {
  max-height: 30px;
  margin-right: 5px;
}

.section-title {
  margin-left: 5px;
}

/*  for styling of this demo  */
div.section-icon, .section-title {
  border: 1px dotted;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" rel="stylesheet" type="text/css" />

  <div class="container p-4 border">
    <div class="row justify-content-center">
        <div class="section-icon">
          <svg width="80" class="section-icon active" viewBox="0 0 512 234.34"><path d="M110.93,287.83A42.67,42.67,0,1,0,153.6,330.5,42.71,42.71,0,0,0,110.93,287.83Zm0,68.27a25.6,25.6,0,1,1,25.6-25.6A25.63,25.63,0,0,1,110.93,356.1Z" transform="translate(0 -138.83)"></path><path d="M503.47,287.83v-51.2a8.53,8.53,0,0,0-5.84-8.09l-48.14-16c-18.14-30.72-43.06-59.94-98.15-69.59l-.3-.05c-101.73-14.07-178.1,8.88-227.14,68.22-36.49.53-73.75,8.16-95.66,22.19-11.35,7.27-18.08,19.92-18.72,33.39l-1,21.19A8.53,8.53,0,0,0,0,296.37V330.5A8.53,8.53,0,0,0,8.53,339H42.67a8.59,8.59,0,0,0,8.53-8.62c0-.05,0-.09,0-.14a59.73,59.73,0,0,1,119.46.23c-.62,5.18,3.47,8.53,8.53,8.53H332.8a8.58,8.58,0,0,0,8.53-8.62c0-.05,0-.09,0-.14a59.73,59.73,0,0,1,119.46.23c-.62,5.18,3.47,8.53,8.53,8.53h34.13A8.54,8.54,0,0,0,512,330.5V296.37A8.54,8.54,0,0,0,503.47,287.83Zm-230.4-81.06A4.27,4.27,0,0,1,268.8,211H157.52a4.3,4.3,0,0,1-2.83-7.51c29.61-27,67.27-42.42,113.67-46.53a4.36,4.36,0,0,1,4.71,4.3Zm102.52,0a4.27,4.27,0,0,1-4.27,4.22H294.4a4.27,4.27,0,0,1-4.27-4.26V160.18a4.28,4.28,0,0,1,4.24-4.31,393,393,0,0,1,54.17,3.86,144.79,144.79,0,0,1,24.62,6.51,4.29,4.29,0,0,1,2.77,4.08ZM420.12,211h-23.2a4.27,4.27,0,0,1-4.27-4.31l.23-23.12a4.29,4.29,0,0,1,6.89-3.35,112.87,112.87,0,0,1,23.73,23.88A4.29,4.29,0,0,1,420.12,211Z" transform="translate(0 -138.83)"></path><path d="M401.07,287.83a42.67,42.67,0,1,0,42.66,42.67A42.72,42.72,0,0,0,401.07,287.83Zm0,68.27a25.6,25.6,0,1,1,25.6-25.6A25.62,25.62,0,0,1,401.07,356.1Z" transform="translate(0 -138.83)"></path></svg>
        </div>

        <div class="section-title">
          Car
        </div>        
      </div>
  </div>


两者之间的主要区别,除了它们的标记之外,icon/title 之间的小 gap/margin 在其父中心的第一个样本中,而在替代方案中则不是, icon/title 中心作为一个组。