Answer the question
In order to leave comments, you need to log in
What is the best way to connect svg icons to the site for later use through use?
Good afternoon! I have a pack with svg icons. What is the best way to include icons on a page?
Constantly accessing the icons.svg file
<svg width="32" height="32">
<use xlink:href="img/ico/icons.svg#icon1" overflow="visible" />
</svg>
<!DOCTYPE html>
<html lang="ru">
<head>
</head>
<body>
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 128 32" enable-background="new 0 0 128 32" xml:space="preserve">
<symbol id="icon1"></symbol>
<symbol id="icon2"></symbol>
<symbol id="icon3"></symbol>
<symbol id="icon4"></symbol>
<symbol id="icon5"></symbol>
</svg>
<svg width="32" height="32">
<use xlink:href="#icon1" overflow="visible" />
</svg>
</body>
</html>
Answer the question
In order to leave comments, you need to log in
It is better to embed directly into HTML, so as not to produce unnecessary requests to an external SVG file. However, if you insert inline way, then it will not be cached.
At one Yandex conference, they advised inserting through JS so that they are cached.
Now I will describe how I do it.
1) Create a JS file with inline SVG. For example, such
// Тут хранятся иконки в SVG
var icon = '
<svg style="display:none;">\
<defs>\
<symbol id="header-phone" viewBox="0 0 850.394 850.394">...</symbol>\
</defs>\
</svg>';
// Вставляем иконки в HTML
document.getElementById('svg-icon-placeholder').innerHTML = icon;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question