【问题标题】:Aligning an SVG inline with text将 SVG 内联与文本对齐
【发布时间】:2020-08-21 01:09:50
【问题描述】:

我有以下JSFiddle,其中 SVG 位于按钮内某些文本的左侧。我希望 SVG 与文本内联,以便它垂直定位在按钮中。

我想过使用以下方法:

  • 虽然使用 line-height 似乎不起作用。
  • 设置margin-top:但是按钮中的整个内容会向下移动。

如何使 SVG 与文本对齐?谢谢。

【问题讨论】:

  • 请尝试授予 jsfiddle 可编辑的权限,以便他们可以进行更改并给出您想要的结果

标签: html css svg


【解决方案1】:

如果vertical-align 没有为您提供足够细微的定位,那么您可以尝试使用position: relative; 调整图标的相对定位。

.icon {
    ...
    position: relative;
    top: 0.15em;
}

如果您使用em 值作为位置,那么这应该允许该类适用于任何字体大小。

    button {
        font-family:'Texta-Regular', sans-serif;
        font-size: 2.7em;
        line-height: 1.2em;
        display: block;
        margin: 2em auto;
        padding: .3em 2em;
        text-align: center;
        border: none;
        border-radius: 5px;
        -webkitborder-radius: 5px;
        outline: none;
        background-color: #dd4b39;
        color: #fff;
    }
    .icon {
        width: 1em;
        display: inline;
        margin-right: 40px;
        position: relative;
        top: 0.15em;
    }
<button>
    <svg version="1.1" fill="#fff" class="icon" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 96.828 96.827" xml:space="preserve">
        <path d="M62.617,0H39.525c-10.29,0-17.413,2.256-23.824,7.552c-5.042,4.35-8.051,10.672-8.051,16.912c0,9.614,7.33,19.831,20.913,19.831c1.306,0,2.752-0.134,4.028-0.253l-0.188,0.457c-0.546,1.308-1.063,2.542-1.063,4.468c0,3.75,1.809,6.063,3.558,8.298l0.22,0.283l-0.391,0.027c-5.609,0.384-16.049,1.1-23.675,5.787c-9.007,5.355-9.707,13.145-9.707,15.404c0,8.988,8.376,18.06,27.09,18.06c21.76,0,33.146-12.005,33.146-23.863c0.002-8.771-5.141-13.101-10.6-17.698l-4.605-3.582c-1.423-1.179-3.195-2.646-3.195-5.364c0-2.672,1.772-4.436,3.336-5.992l0.163-0.165c4.973-3.917,10.609-8.358,10.609-17.964c0-9.658-6.035-14.649-8.937-17.048h7.663c0.094,0,0.188-0.026,0.266-0.077l6.601-4.15c0.188-0.119,0.276-0.348,0.214-0.562C63.037,0.147,62.839,0,62.617,0z M34.614,91.535c-13.264,0-22.176-6.195-22.176-15.416c0-6.021,3.645-10.396,10.824-12.997c5.749-1.935,13.17-2.031,13.244-2.031c1.257,0,1.889,0,2.893,0.126c9.281,6.605,13.743,10.073,13.743,16.678C53.141,86.309,46.041,91.535,34.614,91.535z M34.489,40.756c-11.132,0-15.752-14.633-15.752-22.468c0-3.984,0.906-7.042,2.77-9.351c2.023-2.531,5.487-4.166,8.825-4.166c10.221,0,15.873,13.738,15.873,23.233c0,1.498,0,6.055-3.148,9.22C40.94,39.337,37.497,40.756,34.489,40.756z" />
        <path d="M94.982,45.223H82.814V33.098c0-0.276-0.225-0.5-0.5-0.5H77.08c-0.276,0-0.5,0.224-0.5,0.5v12.125H64.473c-0.276,0-0.5,0.224-0.5,0.5v5.304c0,0.275,0.224,0.5,0.5,0.5H76.58V63.73c0,0.275,0.224,0.5,0.5,0.5h5.234c0.275,0,0.5-0.225,0.5-0.5V51.525h12.168c0.276,0,0.5-0.223,0.5-0.5v-5.302C95.482,45.446,95.259,45.223,94.982,45.223z" />
    </svg>Sign In with Google</button>

【讨论】:

  • 感谢您的拯救。这对我完全有用。
【解决方案2】:

vertical-align: bottom; 应该这样做。

button {
  font-family: 'Texta-Regular', sans-serif;
  font-size: 2.7em;
  line-height: 1.2em;
  display: block;
  margin: 2em auto;
  padding: .3em 2em;
  text-align: center;
  border: none;
  border-radius: 5px;
  -webkitborder-radius: 5px;
  outline: none;
  background-color: #dd4b39;
  color: #fff;
}
.icon {
  width: 1em;
  vertical-align: bottom;
  margin-right: 40px;
}
<button>
  <svg version="1.1" fill="#fff" class="icon" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 96.828 96.827" xml:space="preserve">
    <path d="M62.617,0H39.525c-10.29,0-17.413,2.256-23.824,7.552c-5.042,4.35-8.051,10.672-8.051,16.912c0,9.614,7.33,19.831,20.913,19.831c1.306,0,2.752-0.134,4.028-0.253l-0.188,0.457c-0.546,1.308-1.063,2.542-1.063,4.468c0,3.75,1.809,6.063,3.558,8.298l0.22,0.283l-0.391,0.027c-5.609,0.384-16.049,1.1-23.675,5.787c-9.007,5.355-9.707,13.145-9.707,15.404c0,8.988,8.376,18.06,27.09,18.06c21.76,0,33.146-12.005,33.146-23.863c0.002-8.771-5.141-13.101-10.6-17.698l-4.605-3.582c-1.423-1.179-3.195-2.646-3.195-5.364c0-2.672,1.772-4.436,3.336-5.992l0.163-0.165c4.973-3.917,10.609-8.358,10.609-17.964c0-9.658-6.035-14.649-8.937-17.048h7.663c0.094,0,0.188-0.026,0.266-0.077l6.601-4.15c0.188-0.119,0.276-0.348,0.214-0.562C63.037,0.147,62.839,0,62.617,0z M34.614,91.535c-13.264,0-22.176-6.195-22.176-15.416c0-6.021,3.645-10.396,10.824-12.997c5.749-1.935,13.17-2.031,13.244-2.031c1.257,0,1.889,0,2.893,0.126c9.281,6.605,13.743,10.073,13.743,16.678C53.141,86.309,46.041,91.535,34.614,91.535z M34.489,40.756c-11.132,0-15.752-14.633-15.752-22.468c0-3.984,0.906-7.042,2.77-9.351c2.023-2.531,5.487-4.166,8.825-4.166c10.221,0,15.873,13.738,15.873,23.233c0,1.498,0,6.055-3.148,9.22C40.94,39.337,37.497,40.756,34.489,40.756z"
    />
    <path d="M94.982,45.223H82.814V33.098c0-0.276-0.225-0.5-0.5-0.5H77.08c-0.276,0-0.5,0.224-0.5,0.5v12.125H64.473c-0.276,0-0.5,0.224-0.5,0.5v5.304c0,0.275,0.224,0.5,0.5,0.5H76.58V63.73c0,0.275,0.224,0.5,0.5,0.5h5.234c0.275,0,0.5-0.225,0.5-0.5V51.525h12.168c0.276,0,0.5-0.223,0.5-0.5v-5.302C95.482,45.446,95.259,45.223,94.982,45.223z"
    />
  </svg>Sign In with Google</button>

【讨论】:

  • 它更接近,但预览中的图标显示低于文本。
【解决方案3】:

您好,只需将其添加到以下课程中,您就会得到您想要的..!!

.icon {
    width: 1em;
    display: inline;
    margin-right: 40px;
    vertical-align: bottom;
}

【讨论】:

    【解决方案4】:

    恕我直言,一种适当的(2020 年)方法/避免该问题不会使用负值重新调整,而是使用 &lt;text&gt;&lt;tspan&gt; 将其放入 SVG 中(您仍然可以使用 CSS 解决):

    css:

    button svg text {
      font-family: 'Texta-Regular', sans-serif;
      font-size:   20pt;
      }
    

    html:

    <button>
      <svg viewBox="0 0 200 50">
        <rect rx="5" height="100%" width="100%"/>
        <!-- path d="bla,etc"/ -->
        <!-- path d="bla,etc"/ -->
        <text dx="0" dy="0" x="50%" y="50%" text-anchor="middle"><tspan dominant-baseline="middle">Sign In with Google</tspan></text>
      </svg>
    </button>
    

    请注意 Safari 希望 y 以 &lt;tspan&gt; 标记为中心。

    所需的边框半径是在&lt;rect&gt; 上使用rx 完成的

    这也是 SVG 2.0 - 但我猜它在 1.0 中并没有什么不同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-19
      • 1970-01-01
      • 2012-02-19
      • 1970-01-01
      • 1970-01-01
      • 2019-06-22
      • 1970-01-01
      • 2021-12-13
      相关资源
      最近更新 更多