css - 按钮内的 Svg 高度与文本不同

标签 css

如何解决?

Sandbox .

enter image description here

html {
  font-size: 18px; //user-defined
}

.s1 {
  height: 1rem;
}

.b1 {
  padding: 10px;
}

import * as React from "react";
import "./styles.css";

const MenuSvg = () => {
  return (
    <svg
      viewBox="0 0 24 24"
      strokeWidth="2"
      stroke="black"
      strokeLinecap="round"
      strokeLinejoin="round"
      className="s1"
    >
      <line x1="3" y1="12" x2="21" y2="12"></line>
      <line x1="3" y1="6" x2="21" y2="6"></line>
      <line x1="3" y1="18" x2="21" y2="18"></line>
    </svg>
  );
};

export default function App() {
  return (
    <div>
      <button className="b1">Hello</button>
      <button className="b1">
        <MenuSvg />
      </button>
    </div>
  );
}

最佳答案

display:block 添加到 SVG 以删除其下方的空间,并使用 line-height 定义第一个按钮的高度。请注意,按钮元素具有浏览器应用的默认字体大小,因此您可能需要覆盖它:

html {
  font-size: 18px;
}

.s1 {
  height: 1rem;
  display:block;
}

.b1 {
  padding: 10px;
  line-height: 1rem;
  font-size:1rem;
  vertical-align:top; /* to fix the alignment */
}
<div id="root">
  <div><button class="b1">Hello</button><button class="b1"><svg viewBox="0 0 24 24" stroke-width="2" stroke="black" stroke-linecap="round" stroke-linejoin="round" class="s1"><line x1="3" y1="12" x2="21" y2="12"></line><line x1="3" y1="6" x2="21" y2="6"></line><line x1="3" y1="18" x2="21" y2="18"></line></svg></button></div>
</div>

关于css - 按钮内的 Svg 高度与文本不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64508444/

相关文章:

css - `vp-preview-invisible` 类被添加到 Safari 中的预览缩略图,但不是在 Chrome 中的 vimeo 嵌入视频

css - 如何使用 z-index 将图像定位在框阴影后面?

html - 事件跟踪器显示 flex 的替代方案

javascript - 更改选择菜单内的文本颜色

javascript - 鼠标悬停在菜单上无法正常工作

javascript - 单击以滚动到 div JQuery 中的内容无法正常工作

html - CSS - 如何在调整窗口大小时使 100% 高度的 div 不与其内容重叠

css - 如何清除 DIV

html - 将 DIV 置于背景图片下,无论视口(viewport)如何(响应式)

html - 我应该使用 span 标签还是 p 标签?