您好,
我遇到了一个非常具有挑战性的情况。我有点理解如何使用CSS制作各种不同的形状,但是如果它的内部有一个容器,那么它的背景如下:
(请在黑色背景上看到它以获得更好的视图)
正如您可能已经注意到的那样,它的列表中有一个不规则形状的单选按钮,其中半透明图像作为背景。如何只用CSS实现这种效果?我不认为伪元素会起作用,因为透明背景必须在整个容器和整个线条上均匀。如果我重叠伪元素,它看起来就像一团糟。此外,左侧边框具有完全透明的背景。有什么想法吗?
谢谢。
ul {
background: rgba(0, 0, 0, 0) url("back.png") no-repeat scroll 0 0;
border: 1px solid gray;
margin: 20px;
padding: 5px;
width: 100px;
}
li {list-style:none;}
ul::before {
border: 1px solid gray;
content: "";
display: block;
height: 124px;
margin: 0 0 0 -17px;
position: absolute;
top: 20px;
width: 5px;
}
ul::after {
border: 1px solid gray;
content: "";
height: 5px;
left: 28px;
position: absolute;
top: 150px;
width: 110px;
}

<ul>
<li><input type="radio"> Option 1</li>
<li><input type="radio"> Option 2</li>
<li><input type="radio"> Option 3</li>
<li><input type="radio"> Option 4</li>
<li><input type="radio"> Option 5</li>
<li><input type="radio"> Option 6</li>
</ul>
&#13;
答案 0 :(得分:2)
这种形状非常特别,我会创建一个png文件作为背景 现在如果由于某种原因你不能拥有png背景,你可以使用svg:
body {
margin: 0;
}
.container {
background-image: url("http://www.lorempixel.com/500/500/");
position: relative;
height: 300px;
width: 200px;
/*color: white; Looks reallly bad*/
}
.container svg {
position: relative;
height: 100%;
width: 100%;
}
.container h3{
position: absolute;
left: 27%;
top: 4%;
}
.container label {
position: absolute;
left: 32%;
top: 30%;
}
.container label:nth-of-type(2) {
top: 40%;
}
.container label:nth-of-type(3) {
top: 50%;
}
.container label:nth-of-type(4) {
top: 60%;
}
.container label:nth-of-type(5) {
top: 70%;
}
&#13;
<div class="container">
<svg viewBox="0 -5 60 110">
<path d="m10,0 50,0 0,90 -45,0 0,-60 -5,-10, 0,-20z" fill="#bbb" fill-opacity="0.9" stroke="black" />
<path d="m0,0 0,20 5,10 0,60 5,0 0,-60 -5,-10, 0,-20" fill="none" stroke="#ccc"/>
<path d="m5,93 55,0 -2.5,2.5, -2.5,0 -2.5,2.5 -43.5,0 -4,-2.5z" fill="#bbb" fill-opacity="0.9" stroke="black"/>
</svg>
<h3>Some content</h3>
<label>
<input type="radio" name="radioSelc" />Option 1</label>
<label>
<input type="radio" name="radioSelc" />Option 2</label>
<label>
<input type="radio" name="radioSelc" />Option 3</label>
<label>
<input type="radio" name="radioSelc" />Option 4</label>
<label>
<input type="radio" name="radioSelc" />Option 5</label>
</div>
&#13;
是一种反应灵敏的形状,所以它适用于不同的尺寸
纵横比为2/3,因此为0.66%
身高:300px; - &GT; 400px - &gt; 600px等
宽度:200px; - &GT; 266px - &gt; 400px等
答案 1 :(得分:1)
您还可以使用多个背景图像,使用您设计的方框(经典方式)或渐变,或最终两者的混合。
渐变的想法示例:
h3, ul,li {
margin:0;
padding:0;
display:block;
}
h3 {
text-indent:-20px;
}
div {
width:100px;
margin:1em 1em 3em;
padding:5px 10px 5px 60px;
background:
linear-gradient(to right, transparent 23px,
gray 23px, gray 29px, transparent 29px,transparent 34px, black 34px) 0 bottom no-repeat,
linear-gradient(to right, gray 1px, gray 6px,transparent 6px,transparent 10px, black 10px) 0 0 no-repeat,
linear-gradient(to right, gray 1px, transparent 1px, transparent 5px, gray 5px, gray 6px,transparent 6px,transparent 11px, black 11px, black 12px, gray 12px) 0 0 no-repeat,
linear-gradient(60deg, transparent 20px, gray 20px, gray 21px, transparent 21px, transparent 25px, gray 25px, gray 26px,transparent 26px, transparent 30px, black 30px, black 31px, gray 31px) 0 20px no-repeat,
linear-gradient(to right, transparent 23px,
gray 23px, gray 24px, transparent 24px, transparent 28px,gray 28px, gray 29px, transparent 29px,transparent 34px, black 34px, black 34px, gray 35px) 0 61px no-repeat;
background-size:100% 1px,100% 1px, 100% 20px ,100% 41px, 100% 100%;
border-right:1px solid;
position:relative;
float:left;
}
div:after {
content:'';
position:absolute;
top:100%;
right:0;
left:22px;
margin-top:5px;
height:16px;
border-top:solid 1px;
background:
linear-gradient(to right,black 1px, gray 1px) 0 0 no-repeat,
linear-gradient(-60deg,transparent 3px, black 3px, black 5px, gray 5px) top right no-repeat,
linear-gradient(-60deg,transparent 24px, black 24px, black 25px, gray 25px) right 5px no-repeat,
linear-gradient(60deg,transparent 3px, black 3px, black 4px, gray 4px) left 9px no-repeat,
linear-gradient(-60deg,transparent 3px, black 3px, black 4px, gray 4px) 5px 9px no-repeat,
linear-gradient(black,black) 0 5px no-repeat,
linear-gradient(black,black) 0 9px no-repeat,
linear-gradient(black,black) 3% 15px no-repeat;
;
background-size:80% 9px, 40% 5px, 40% 4px, 20% 6px, 30% 6px, 97% 1px, 81% 1px, 29% 1px
}
body {
background:yellow;
<div>
<h3>Some options</h3>
<ul>
<li><input type="radio"> Option 1</li>
<li><input type="radio"> Option 2</li>
<li><input type="radio"> Option 3</li>
<li><input type="radio"> Option 4</li>
<li><input type="radio"> Option 5</li>
<li><input type="radio"> Option 6</li>
</ul>
</div>
<div>
<h3>Some options</h3>
<ul>
<li><input type="radio"> Option 1</li>
<li><input type="radio"> Option 2</li>
<li><input type="radio"> Option 3</li>
</ul>
</div>
<div>
<h3>Some options</h3>
<ul>
<li><input type="radio"> Option 1</li>
<li><input type="radio"> Option 2</li>
<li><input type="radio"> Option 3</li>
<li><input type="radio"> Option 4</li>
<li><input type="radio"> Option 5</li>
<li><input type="radio"> Option 6</li>
<li><input type="radio"> Option 7</li>
<li><input type="radio"> Option 8</li>
</ul>
</div>
切片示例:
h3, ul,li {
margin:0;
padding:0;
display:block;
}
div {
width:100px;
margin:1em;
padding:5px 5px 30px 40px;
float:left;
background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHIAAAAlCAYAAACeVr0yAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAK6wAACusBgosNWgAAAB50RVh0U29mdHdhcmUAQWRvYmUgRmlyZXdvcmtzIENTNS4xqx9I6wAACDtJREFUeJztW81v3MYVf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ErkJggg==) bottom no-repeat;
background-size:114px auto ,114px calc(100% - 52px);
}
label {
display:inline-block;
background:gray;
border-radius:100%;
padding:0px 1px 2px 2px;
background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAMCAYAAABWdVznAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAK6wAACusBgosNWgAAAB50RVh0U29mdHdhcmUAQWRvYmUgRmlyZXdvcmtzIENTNS4xqx9I6wAAABZ0RVh0Q3JlYXRpb24gVGltZQAwNS8yOS8xNrtVQFkAAAHBSURBVCiRXcvPbhJRGEDx89G5c+8MDFgp01RjLDQp2gZX7Czdie9hF/Z16Kr4Erhm1bBw40KMC9G0c2PiggkJprUWqjO9LvwT41mfn0RRhNYapRSP9/ZcvV5nfX0dgDRNsdbycjAQrTXGGDwRQUR4dnDg9vf3pfEfSKyl2Wy6o15PROQXeH546J52u7Kzs+s+nFr5aD8DUF2t0G63CYNARMT1j4/Fe9Ltuk6nI9vNB7x6/UbCMKRarSIiZN+vefvuPa3dh1wtFjKZTJy31Wiw1WhwmnyiWCwRlUqsrBTQvkLfilhcLUjTlD9foRbHxHHMl/NzKuUIoxVR0VApBUTFgNrabbIsJ45janGMx++0UoSBJtA+gfHxfR8RwTkoFkPyPCfPcwqz2YzZbMZadZWb7MffWSmFUgrnbohKJebzOfP5nIJNEs6ShHt377BcLlheXyMiAGRZRp7nhGHozpIEmyR4w+FQtptNFxgjrdYjLr5ecHn5DeUrlKcwxjAej2U0GrmTkxPxnHO86PcFcIvlUuqbm2xsbAAwnU5JrGU0GrmjXk+01ki5XEZrjed5dDodd/8fkE6nWGsZDAZijEFrzU8XuqnWNgsutQAAAABJRU5ErkJggg==) center no-repeat;
background-size:90% 90%;
}
label input {
display:block;
margin:0;
}
<div>
<h3>options</h3>
<ul>
<li><label><input type="radio"/></label> Option 1</li>
<li><label><input type="radio"/></label> Option 2</li>
<li><label><input type="radio" checked/></label> Option 3</li>
<li><label><input type="radio"/></label> Option 4</li>
<li><label><input type="radio"/></label> Option 5</li>
<li><label><input type="radio"/></label> Option 6</li>
</ul>
</div>
<div>
<h3>Some options</h3>
<ul>
<li><input type="radio"> Option 1</li>
<li><input type="radio"> Option 2</li>
<li><input type="radio"> Option 3</li>
</ul>
</div>
<div>
<h3>Some options</h3>
<ul>
<li><input type="radio"> Option 1</li>
<li><input type="radio"> Option 2</li>
<li><input type="radio"> Option 3</li>
<li><input type="radio"> Option 4</li>
<li><input type="radio"> Option 5</li>
<li><input type="radio"> Option 6</li>
<li><input type="radio"> Option 7</li>
<li><input type="radio"> Option 8</li>
</ul>
</div>
答案 2 :(得分:0)
您可以使用剪辑路径执行此操作。 CSS在302行以上。 确保剪裁的元素位于绝对位置并牢记浏览器支持并不是最好的。 是沙箱游乐场,以产生路径,这很容易。
-webkit-clip-path: polygon(1% 3%, 3% 9%, 18% 21%, 54% 20%,
83% 63%, 66% 76%, 15% 83%, 25% 45%);
链接到Codepen,将鼠标悬停在饮料上: - )