hon*_*k31 26 html css svg css-shapes
有没有机会将图像放在六边形内?我已经习惯了html中的六角形单元格,但是我不能用它来填充(背景?)图像.
这是我尝试过的:
.top {
height: 0;
width: 0;
display: block;
border: 20px solid red;
border-top-color: transparent;
border-right-color: transparent;
border-bottom-color: red;
border-left-color: transparent;
}
.middle {
height: 20px;
background: red;
width: 40px;
display: block;
}
.bottom {
height: 0;
width: 0;
display: block;
border: 20px solid red;
border-top-color: red;
border-right-color: transparent;
border-bottom-color: transparent;
border-left-color: transparent;
}Run Code Online (Sandbox Code Playgroud)
<div class="hexagon pic">
<span class="top" style="background: url(http://placekitten.com/400/400/)"></span>
<span class="middle" style="background: url(http://placekitten.com/400/400/)"></span>
<span class="bottom" style="background: url(http://placekitten.com/400/400/)"></span>
</div>
<div class="hexagon">
<span class="top" style="overflow: hidden;"><img src="http://placekitten.com/400/400/" /></span>
<span class="middle" style="overflow: hidden;"><img src="http://placekitten.com/400/400/" /></span>
<span class="bottom" style="overflow: hidden;"><img src="http://placekitten.com/400/400/" /></span>
</div>
<div class="hexagon">
<span class="top"><img src="http://placekitten.com/400/400/" /></span>
<span class="middle"><img src="http://placekitten.com/400/400/" /></span>
<span class="bottom"><img src="http://placekitten.com/400/400/" /></span>
</div>Run Code Online (Sandbox Code Playgroud)
这是一个小提琴:http://jsfiddle.net/jnz31/kGSCA/
kiz*_*izu 68
使用CSS3几乎一切皆有可能:http://jsfiddle.net/kizu/bhGn4/
在那里我使用了不同的旋转overflow: hidden,所以你可以得到一个跨浏览器(好的,现代的交叉更加蒙版)的面具甚至可以在蒙面区域上可以覆盖和点击.
web*_*iki 20
使用图像实现六边形的最灵活方法是使用内联SVG:
svg{
width:30%;
}Run Code Online (Sandbox Code Playgroud)
<svg viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg">
<defs>
<pattern id="img" patternUnits="userSpaceOnUse" width="100" height="100">
<image xlink:href="https://farm4.staticflickr.com/3165/5733278274_2626612c70.jpg" x="-25" width="150" height="100" />
</pattern>
</defs>
<polygon points="50 1 95 25 95 75 50 99 5 75 5 25" fill="url(#img)"/>
</svg>Run Code Online (Sandbox Code Playgroud)
使用SVG至少有两种方法可以实现六边形图像:
pattern元素填充多边形(我在前面的代码片段中使用的方法)svg{width:30%}Run Code Online (Sandbox Code Playgroud)
<svg viewbox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg">
<defs>
<clipPath id="hexClip">
<polygon points="50 1 99 25 99 75 50 99 1 75 1 25"/>
</clipPath>
</defs>
<image xlink:href="https://farm4.staticflickr.com/3165/5733278274_2626612c70.jpg" x="-25" width="150" height="100" clip-path="url(#hexClip)"/>
</svg>Run Code Online (Sandbox Code Playgroud)
SVG方法允许控制六边形形状和图像的许多方面.它们可以用CSS设计.这是一个例子:
svg{
width:30%;
margin:0 auto;
}
#hex{
stroke-width:2;
stroke: teal;
fill-opacity:0.6;
transition:fill-opacity .8s;
}
#hex:hover{
fill-opacity:1;
}
#text{
stroke-width:0.5;
stroke:teal;
fill-opacity:0.4;
fill:teal;
transition:fill-opacity .8s;
}
#hex:hover + #text{
fill-opacity:1;
}Run Code Online (Sandbox Code Playgroud)
<svg viewbox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg">
<defs>
<pattern id="img" patternUnits="userSpaceOnUse" width="100" height="100">
<image xlink:href="https://farm4.staticflickr.com/3165/5733278274_2626612c70.jpg" x="-25" width="150" height="100" />
</pattern>
</defs>
<polygon id="hex" points="50 1 95 25 95 75 50 99 5 75 5 25" fill="url(#img)"/>
<text id="text" font-size="20" x="50" y="50" text-anchor="middle">Some text</text>
</svg>Run Code Online (Sandbox Code Playgroud)
另一种制作带有图像的六边形的方法检查这个问题:六边形的响应网格
一种新的简单方法是使用css3 clip-path属性.
来自文档:
剪辑路径CSS属性通过定义要显示的剪辑区域来防止元素的一部分被显示,即,仅显示元素的特定区域.
以下css将显示六边形的矩形元素:
div.hexagon {
clip-path: polygon(50% 0, 100% 25%, 100% 75%, 50% 100%, 0 75%, 0 25%);
}
Run Code Online (Sandbox Code Playgroud)
输出图像:
body {
background: linear-gradient(orange, yellow);
min-height: 100vh;
margin: 0;
}
.hexagon {
clip-path: polygon(50% 0, 100% 25%, 100% 75%, 50% 100%, 0 75%, 0 25%);
background: url("https://i.imgur.com/waDgcnc.jpg") no-repeat;
background-size: cover;
margin: 10px auto;
height: 200px;
width: 200px;
}Run Code Online (Sandbox Code Playgroud)
<div class="hexagon">
</div>Run Code Online (Sandbox Code Playgroud)
我们可以使用此属性绘制我们想要的任何形状.以下是几个例子:
div.pentagon {
clip-path: polygon(50% 0, 100% 45%, 80% 100%, 20% 100%, 0 45%);
}
div.octagon {
clip-path: polygon(33.33% 0%, 66.66% 0%, 100% 33.33%, 100% 66.66%, 66.66% 100%, 33.33% 100%, 0 66.66%, 0 33.33%);
}
Run Code Online (Sandbox Code Playgroud)
输出图像:
body {
background: linear-gradient(orange, yellow);
min-height: 100vh;
margin: 0;
}
div {
background: url("https://i.imgur.com/waDgcnc.jpg") no-repeat;
background-size: cover;
margin: 10px 20px;
height: 170px;
width: 170px;
float: left;
}
.pentagon {
clip-path: polygon(50% 0, 100% 45%, 80% 100%, 20% 100%, 0 45%);
}
.octagon {
clip-path: polygon(33.33% 0%, 66.66% 0%, 100% 33.33%, 100% 66.66%, 66.66% 100%, 33.33% 100%, 0 66.66%, 0 33.33%);
}Run Code Online (Sandbox Code Playgroud)
<div class="pentagon">
</div>
<div class="octagon">
</div>Run Code Online (Sandbox Code Playgroud)
注意:
clip-pathIE和Edge不支持css属性.但是,预计Edge的未来版本将支持此属性.
你可以通过覆盖这样的角来做到这一点:
http://jsfiddle.net/Eric/kGSCA/3/
<div class="hexagon pic">
<span class="top"></span>
<span class="bottom"></span>
</div>
Run Code Online (Sandbox Code Playgroud)
.hexagon {
background: url(http://placekitten.com/400/400/);
width: 400px;
height: 346px;
position: relative;
}
.hexagon span {
position: absolute;
display: block;
border-left: 100px solid red;
border-right: 100px solid red;
width: 200px;
}
.top {
top: 0;
border-bottom: 173px solid transparent;
}
.bottom {
bottom: 0;
border-top: 173px solid transparent;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
75694 次 |
| 最近记录: |