在 Bootstrap 4 行中对齐 SVG 元素

Posted

技术标签:

【中文标题】在 Bootstrap 4 行中对齐 SVG 元素【英文标题】:Align SVG element in Bootstrap 4 row 【发布时间】:2018-05-22 12:30:42 【问题描述】:

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

|      ICON TEXT      |

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

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

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

【问题讨论】:

【参考方案1】:

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

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

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

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

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

堆栈sn-p

.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  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 元素并将内置类 justify-content-center 添加到 row 元素 (&lt;div class="row justify-content-center"&gt;)

堆栈sn-p

.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  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>

除了它们的标记之外,两者之间的主要区别在于图标/标题之间的小间隙/边距位于其父中心的第一个样本中,或者它不是,图标/标题中心作为一个群体。

【讨论】:

以上是关于在 Bootstrap 4 行中对齐 SVG 元素的主要内容,如果未能解决你的问题,请参考以下文章

垂直对齐复选框与行中的 Bootstrap 3 选项卡

行对齐中的 Bootstrap 4 按钮

Bootstrap 4在col div内对齐元素

Bootstrap:将“卡片”水平对齐为单行

(Bootstrap 4+) - 具有多个元素的列:在同一行上对齐元素

在 Bootstrap 4 中对齐导航栏