我在 Safari 的<svg>
中使用<image>
元素。
虽然<image>
显示在Chrome中,但它不会显示在Safari中。
这是我的代码
.swatch {
width: 10rem;
}
.swatch-base {
width: 100%;
height: 100%;
}
.filter-swatch {
width: 100%;
height: 100%;
mix-blend-mode: multiply;
}
<svg class="swatch" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 305 305" preserveAspectRatio="xMidYMin slice">
<image class="swatch-base" xlink:href="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/7QA4UGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAAAA4QklNBCUAAAAAABDUHYzZjwCyBOmACZjs+EJ+/9sAQwAIBgYHBgUIBwcHCQkICgwUDQwLCwwZEhMPFB0aHx4dGhwcICQuJyAiLCMcHCg3KSwwMTQ0NB8nOT04MjwuMzQy/9sAQwEJCQkMCwwYDQ0YMiEcITIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIy/8AAEQgAZABkAwEiAAIRAQMRAf/EABkAAAMBAQEAAAAAAAAAAAAAAAACAwEEB//EADoQAAIBAgMHAgQFAgQHAAAAAAECEQASAyExEyIyQVFhYlJxI0JygQQzocHhU9EUkbHxQ2OCk7LS0//EABQBAQAAAAAAAAAAAAAAAAAAAAD/xAAUEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwD0R8ScMEuT8l4HOPy/5pHZrnEEWykTp/y/v1pwyEZAcHYQvt6q0lLiqgLukC4aLznyoG3zgSMITNpxbteVoH6TUmmSLeEWH2/pj+9UVkGFkWv1EjdCx/lMVJnSJAk2zmNF6nyoNKkqCEkncZieIegU5QlGkBiTDEdI4B370k4AVS1+kxGSrGv1frWDZDgLjK7ThHq+qgoAQuJJA3Apy1HoHfvUSGMgKwFkR29I8quVdwwtDRh5BmyVfV9RrnCACZmEn2HrPlQVS5MPEzjIBiBmR6V7jnU2MMSeGB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EEiGk9TvnlHtTkrdiy4e/iCj80zG7/FYgnDxJIAEBui55Bf3pijK2LeFWBvWxujw70Bg2XBgGxCTJs+YjmOwqknZmQxBi8DXEPj2qeENmwLALGsRl0AHfnTtIRpIUJFzD5M9FoMw2BYA4ReNbdWPbsNKm2pkZ5XRlPSPbnToHw8ziHDjWzOMsgOvU1FtDLQF1PonSPfnQa6IXJKM5nNgcie1FSdipAOI2HlwLovYUUF8YxiG2BmqZD9ffvTYsK+IVUCFVRHyn1DvlrRRQRLMAIdhBKrnw6ZjvVVBUXKzAiLc+HLMiiigbEOy2gTILhhgPLr71JsVxh4rAicNUK5aExJoooK8KY8AfCtCdpIn/Wmx0XAf8QMMQMFVZOxOtFFBN2OCWsgWlAMhldrVUCl8UFFIwmhMtM6KKBXA/DNibIRs7FWc4u1rHw1wybZGzIC/wDVrRRQT/EA4WKcNDCqIAgUUUUH/9k=
" preserveAspectRatio="xMinYMin" onerror="" x="0" y="0"></image>
<rect class="filter-swatch" fill="#ecd5cf"></rect>
</svg>
请注意,在我的代码中,我已经内联了图像,因此不使用外部资源,因此当图像不再存在于我的服务器上时,这个答案仍然有用。不过,在生产中,我引用的是图像而不是内联它。
如何按在 Chrome 中显示的方式显示图像?
对于 Safari,图像上的宽度和高度必须指定为属性,而不是通过 CSS。
在图像上使用CSS是SVG 2中的新功能,Safari尚未实现SVG 2的这一部分。
.swatch {
width: 10rem;
}
.filter-swatch {
width: 100%;
height: 100%;
mix-blend-mode: multiply;
}
<svg class="swatch" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 305 305" preserveAspectRatio="xMidYMin slice">
<image class="swatch-base" xlink:href="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/7QA4UGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAAAA4QklNBCUAAAAAABDUHYzZjwCyBOmACZjs+EJ+/9sAQwAIBgYHBgUIBwcHCQkICgwUDQwLCwwZEhMPF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6KKBXA/DNibIRs7FWc4u1rHw1wybZGzIC/wDVrRRQT/EA4WKcNDCqIAgUUUUH/9k=
" preserveAspectRatio="xMinYMin" onerror="" x="0" y="0" width="100%" height="100%"></image>
<rect class="filter-swatch" fill="#ecd5cf"></rect>
</svg>