Make a CSS triangle with transparent background on

2019-01-18 07:09发布

Ok so, I'm trying to replicate the effect you see here at the bottom of the page, with the back to top button: http://www.ppp-templates.de/tilability/ - After the content area for We stay connected.

basically he's using a background image for that and I'd like to replicate it with CSS and keep the same effect.

I know how to create triangles with CSS with borders, but in my case I'd like to use the transparent bg image and not a color so I can't use borders

I removed the background image and used #FFF on the whole div, so it's all white now... I created a new div in which I added the back to top button and added background: transparent to it so it's transparent, but how do I create the triangle via CSS?

Any help is greatly appreciated.

5条回答
Luminary・发光体
2楼-- · 2019-01-18 07:34

Here's one way to make a triangle with fairly minimal markup and css:

HTML:

<div class="triangle"></div>

CSS:

.triangle {
    width: 0; 
    height: 0; 
    border-left: 35px solid transparent;
    border-right: 35px solid transparent;
    border-bottom: 35px solid gray;
}

http://jsbin.com/iribib/21

查看更多
Ridiculous、
3楼-- · 2019-01-18 07:36

Here you go, http://jsfiddle.net/pkUx7/1/

HTML

<body>
    <div id = "footer"></div>
    <div id = "bottom-line-left"></div>
    <div id = "triangle"></div>
    <div id = "bottom-line-right"></div>
</body>

CSS

body {
    background-color: purple;
}   

div {
    margin:0;
    padding:0;
    background-color: violet;
}

#footer {
    height: 100px;
}

#bottom-line-left, #bottom-line-right {
    display: inline-block;
    height: 20px;
}

#bottom-line-left {
    width: 61%;
}

#bottom-line-right {
    float: right;
    width: 37%;
}

#triangle {
    margin-left:-6px;
    margin-right: -4px;
    padding:0;
    display: inline-block;
    width: 0;
    height: 0;
    border-left: 10px solid transparent;
    border-right: 10px solid transparent;
    border-bottom: 20px solid purple;
}
查看更多
劳资没心,怎么记你
4楼-- · 2019-01-18 07:40

The Fiddle:

http://jsfiddle.net/JaMH9/2/

The HTML:

<div class="bar">
    <span class="home">^<br>Home, sweet home!</span>
</div>​

The CSS:

.bar {
    position: relative;
    width: 90%;
    height: 30px;
    margin: 0 auto;
}

.home {
    position: absolute;
    top: 0px;
    left: 60%;
    width: 20%;
    text-align: center;
}

.bar:before, .bar:after {
    content:'';
    position: absolute;
    top: 0;
    height: 0;
    border-top: 30px solid white;
  -webkit-box-sizing: border-box;
     -moz-box-sizing: border-box;
      -ms-box-sizing: border-box;
          box-sizing: border-box;
}

.bar:before {
    left: 0;
    width: 70%;
    border-right: 30px solid transparent;
}

.bar:after {
    right:0;
    width: 30%;
    border-left: 30px solid transparent;
}
​
查看更多
疯言疯语
5楼-- · 2019-01-18 07:50

I just threw this together, there's probably a better way to achieve this effect.

HTML

<div class="div1">Lorem Ipsum</div>
<div class="div2"></div>
<div class="div3"></div>
<div class="div4"></div>

CSS

body {
    background-color: gray;
    border: 20px solid gray;
}
.div1 {
    background-color: white;
    border: 20px solid white;
}
.div2 {
    float: right;
    border-top: 20px solid white;
    border-right: 20px solid white;
    border-left: 20px solid transparent;
}
.div3 {
    float: right;
    margin: 10px -20px;
    border-bottom: 20px solid white;
    border-right: 20px solid transparent;
    border-left: 20px solid transparent;
}
.div4 {
    border-top: 20px solid white;
    border-right: 20px solid transparent;
    margin-right: 40px;
}

See it here.

查看更多
爷的心禁止访问
6楼-- · 2019-01-18 07:57

You can use vector path. For instance, transparent triangle with green border:

<svg height="151" width="150">
    <path d="m0 150 h150 l -75 -150 z" fill="transparent" stroke="green" />
</svg>

See it here.

查看更多
登录 后发表回答