在Bootstrap 4行中对齐SVG元素

时间:2017-12-08 09:32:25

标签: css css3 svg flexbox twitter-bootstrap-4

我想将图标(<svg>)和文本对齐到容器的中心。我想在图标和文字之间留一小段距离:

|      ICON TEXT      |

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

我有Bootstrap 4(Flexbox)可用。但我无法找到如何做到这一点。特别是<svg>很难对齐 - 我不能把它移到任何地方。我尝试将它们放在.row中,每个.col,但SVG保留在col的中心...

我如何实现这一点(有或没有BS网格)??

1 个答案:

答案 0 :(得分:1)

使用现有标记,需要完成两件事:

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

  • e.g。使用text-align: right将SVG与右侧对齐

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

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

Stack snippet

&#13;
&#13;
.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;
}
&#13;
<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>
&#13;
&#13;
&#13;

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

Stack snippet

&#13;
&#13;
.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;
}
&#13;
<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>
&#13;
&#13;
&#13;

除了标记之外,两者之间的主要区别在于图标/标题之间的小差距/边距位于父母中心的第一个样本中,而另一方面则不是,图标/标题中心作为一个群体。