我有一个图片svg元素,它的宽度可以是任意的。我想要水平居中图片在svg元素或div容器内。我试过使用
margin: 0 auto;
或者text-align : center;
但是都没有起作用。
这里是jsfiddle
html
<div class="background-img" style="width: 100px; height: 110px;">
<svg class="svg-defs" width="100px" height="110px">
<defs>
<clipPath id="clip-triangle-10">
<polygon points="0,0 110,0 110,100 19,100 12,107 5,100 0,100 0,0"></polygon>
</clipPath>
</defs>
<rect class="svg-background" clip-path="url(#clip-triangle-10)" width="100px" height="110px"></rect>
<image class="svg-image" clip-path="url(#clip-triangle-10)" height="100px" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://upload.wikimedia.org/wikipedia/en/thumb/6/6c/Seafarers_title.jpg/225px-Seafarers_title.jpg"></image>
</svg>
</div>
image{
position: relative;
display: block;
margin: 0 auto;
text-align : center;
}
<div class="background-img" style="width: 100px; height: 110px;">
<svg class="svg-defs" width="100px" height="110px">
<defs>
<clipPath id="clip-triangle-10">
<polygon points="0,0 110,0 110,100 19,100 12,107 5,100 0,100 0,0"></polygon>
</clipPath>
</defs>
<rect class="svg-background" clip-path="url(#clip-triangle-10)" width="100px" height="110px"></rect>
<image class="svg-image" clip-path="url(#clip-triangle-10)" height="100px" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://upload.wikimedia.org/wikipedia/en/thumb/6/6c/Seafarers_title.jpg/225px-Seafarers_title.jpg"></image>
</svg>
</div>