I want to position a css triangle as a background

旧城冷巷雨未停 提交于 2019-12-01 00:23:35

You can set 2 light gradients on top of the darker background.

They overlap each other and leave only the remaining triangle darker

div {
    width: 400px;
    height: 200px;
    border: solid 1px green;
    background: linear-gradient(to top left, lightgreen 50%, transparent 50%),
    linear-gradient(to top right, lightgreen 50%, transparent 50%), green;
}
<div></div>

Try this one, but still need some work on the responsive part.

.box{
  position: relative;
  width: 100%;
  max-width: 600px;
  background: #ccc;
  min-height: 300px;
}
.box:before {
  width: 0; 
  height: 0;
  content: "";
  position: absolute;
  z-index: 0;
  top: 0;
  left: 0;
  border-left: 300px solid transparent;
  border-right: 300px solid transparent;
  border-top: 180px solid #555;
}

.box .content{
  z-index: 10;
  position: relative;
  color: #fff;
  text-align: center;
  padding-top: 40px;
}

h1, h2{
  margin: 0;
  padding: 0;
}
h2{
  margin-bottom: 80px;
}

.btn{
  background: #f00;
  color: #fff;
  display: inline-block;
  padding: 5px 10px;
  text-align: center;
  text-decoration: none;
  min-width: 200px;
  font-size: 20px;
}
<div class="box">
  <div class="content">
  <h1>Headline</h1>
  <h2>Headline</h2>
    
    <a href="#" class="btn">CTA</a>
  </div><!--// end .content -->
</div><!--// end .box -->

This should get you close, and illustrates a CSS only approach:

* {
  margin: 0;
  padding: 0
}

body {
  background: #ccc;
  min-height: 500px;
}

div {
  width: 0;
  height: 0;
  margin: 0px auto;
  border: 200px solid transparent;
  border-top-color: grey;
}

a {
  display: block;
  background: blue;
  color: white;
  padding: 5px 10px;
  width: 200px;
  margin: 0px auto;
  position: relative;
  top: -200px;
  text-align: center;
  text-decoration: none;
}
<div></div>
<a href="#">link</a>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!