我在按钮中使用 Bootstrap 4 的 SVG 图标。我将使用 SVG 的代码硬编码到页面的 HTML 中。现在我想将 SVG 重构为 CSS 类。我已按照 Bootstrap 4 说明创建 CSS 类,但我无法弄清楚如何在按钮上显示图标。我尝试了很多变体但没有成功。
CSS:
.bi::before {
display: inline-block;
content: "";
background-image: url('data:image/svg+xml,<svg width="2em" height="2em" viewBox="0 0 16 16" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M.5 9.9a.5.5 0 0 1 .5.5v2.5a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-2.5a.5.5 0 0 1 1 0v2.5a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2v-2.5a.5.5 0 0 1 .5-.5z"><path fill-rule="evenodd" d="M7.646 11.854a.5.5 0 0 0 .708 0l3-3a.5.5 0 0 0-.708-.708L8.5 10.293V1.5a.5.5 0 0 0-1 0v8.793L5.354 8.146a.5.5 0 1 0-.708.708l3 3z"></svg>');
background-repeat: no-repeat;
background-size: 1rem 1rem;
}
HTML:
<a href="#" class="btn bi" role="button" download></a>
非常感谢您的帮助。
好吧,为了让你的代码正常工作,你应该考虑一些注意事项。
<path>
元素没有任何结束标记,因此它会在数据属性中产生错误,并且会阻止图标显示。width
和 height
并使用 preserveAspectRatio="none"
代替。data:image/svg
中,以便为交叉浏览目的提供有效的字符串格式。您可以使用this之类的工具来解决这个问题。根据上面的注释,你的最终代码应该是这样的:
.bi::before {
content: "";
display: inline-block;
background-image: url('data:image/svg+xml,%3Csvg%20preserveAspectRatio%3D%22none%22%20viewBox%3D%220%200%2016%2016%22%20fill%3D%22currentColor%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%3Cpath%20fill-rule%3D%22evenodd%22%20d%3D%22M.5%209.9a.5.5%200%200%201%20.5.5v2.5a1%201%200%200%200%201%201h12a1%201%200%200%200%201-1v-2.5a.5.5%200%200%201%201%200v2.5a2%202%200%200%201-2%202H2a2%202%200%200%201-2-2v-2.5a.5.5%200%200%201%20.5-.5z%22%20%2F%3E%3Cpath%20fill-rule%3D%22evenodd%22%20d%3D%22M7.646%2011.854a.5.5%200%200%200%20.708%200l3-3a.5.5%200%200%200-.708-.708L8.5%2010.293V1.5a.5.5%200%200%200-1%200v8.793L5.354%208.146a.5.5%200%201%200-.708.708l3%203z%22%20%2F%3E%3C%2Fsvg%3E');
background-repeat: no-repeat;
width: 2rem;
height: 2rem;
}
<a href="#" class="btn bi" role="button" download></a>
注意: 您还可以在项目中创建 SVG 文件,然后像常规图像一样直接在背景图像属性中引用它,以方便使用。
您可以在 或 标签内添加标签并将类应用于它。您可以尝试在没有伪类的情况下执行此操作。您还可以调整按钮中的图标,为按钮添加填充。
这里有同样的问题。就我而言,我在演示中使用了这种方法(其他方法之一),并使用了世界上最快的 php 框架(我使用类似的引导按钮来更改页面中的语言)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="Custa">
<title>Document</title>
<style>
.flcn{ content:url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA1MTIgNTEyIj48Y2lyY2xlIGN4PSIyNTYiIGN5PSIyNTYiIHI9IjI1NiIgZmlsbD0iI2Q4MDAyNyIvPjxwYXRoIGZpbGw9IiNmZmRhNDQiIGQ9Ik0xNDAuMSAxNTUuOGwyMi4xIDY4aDcxLjVsLTU3LjggNDIuMSAyMi4xIDY4LTU3LjktNDItNTcuOSA0MiAyMi4yLTY4LTU3LjktNDIuMUgxMTh6bTE2My40IDI0MC43bC0xNi45LTIwLjgtMjUgOS43IDE0LjUtMjIuNS0xNi45LTIwLjkgMjUuOSA2LjkgMTQuNi0yMi41IDEuNCAyNi44IDI2IDYuOS0yNS4xIDkuNnptMzMuNi02MWw4LTI1LjYtMjEuOS0xNS41IDI2LjgtLjQgNy45LTI1LjYgOC43IDI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oMjYuOGw4LjMtMjUuNSA4LjMgMjUuNWgyNi44TDIxMiAyMDVsOC4zIDI1LjV6bS04LjMtMTAwbDguMyAyNS40LTIxLjctMTUuNy0yMS43IDE1LjcgOC4zLTI1LjUtMjEuNy0xNS43aDI2LjhsOC4zLTI1LjYgOC4zIDI1LjZoMjYuOEwyMTIgMTMwLjN6bTAtNzQuN2w4LjMgMjUuNi0yMS43LTE1LjhMMTc3IDgxLjNsOC4zLTI1LjZMMTYzLjUgNDBoMjYuOGw4LjMtMjUuNUwyMDcgNDBoMjYuOEwyMTIgNTUuN3oiLz48L3N2Zz4=); }
/*
generated by me with php $flagencoded= base64_encode($svgimageloadedfrompath);
then with echo,fwrite,content output in some other way I generate
echo '.classname1{ content:url(data:image/svg+xml;base64,'.$flagencoded.');}';
so I got what you reading up
in my case all svg files are from github: flags public domain(free to anyone to use)
*/
p{
width: 30px;
height:30px;
}
</style>
<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
</head>
<body>
<div class="pb-3">
<div class="container">
<div class="row">
<div class="col">
<button class="btn btn bg-info" type="submit">
<p class="fles"></p>
</button>
</div>
<div class="col">
<button class="btn btn bg-info" type="submit">
<p class="flfr"></p>
</button>
</div>
<div class="col">
<button class="btn btn bg-info" type="submit">
<p class="flus"></p>
</button>
</div>
<div class="col">
<button class="btn btn bg-info" type="submit">
<p class="flit"></p>
</button>
</div>
<div class="col">
<button class="btn btn bg-info" type="submit">
<p class="flde"></p>
</button>
</div>
<div class="col">
<button class="btn btn bg-info" type="submit">
<p class="flcn"></p>
</button>
</div>
<div class="col">
<button class="btn btn bg-info" type="submit">
<p class="flro"></p>
</button>
</div>
<div class="col">
<button class="btn btn bg-info" type="submit">
<p class="flru"></p>
</button>
</div>
</div>
</div>
</div>
</body>
</html>
你可以尝试一下:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.btn .chevron{
vertical-align:middle;
background:#fff;
display: inline-table;
width:30px;
height:32px;
-webkit-mask-image:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M8 4a.5.5 0 0 1 .5.5v3h3a.5.5 0 0 1 0 1h-3v3a.5.5 0 0 1-1 0v-3h-3a.5.5 0 0 1 0-1h3v-3A.5.5 0 0 1 8 4'/%3E%3C/svg%3E");
mask-image:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M8 4a.5.5 0 0 1 .5.5v3h3a.5.5 0 0 1 0 1h-3v3a.5.5 0 0 1-1 0v-3h-3a.5.5 0 0 1 0-1h3v-3A.5.5 0 0 1 8 4'/%3E%3C/svg%3E")
}
</style>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>
<body>
<button class="btn addto btn-danger m-4"><span class="chevron"></span> Alert Danger</button>
</body>
</html>