我需要使用css在div上包含一个三角形作为背景。

时间:2017-11-13 05:05:21

标签: html css

enter image description here

需要将三角形添加到div的右上角并使文本流过它。我们可以使用普通的CSS而不使用图像来实现这一目标吗?

3 个答案:

答案 0 :(得分:1)

我希望这就是你要找的东西。

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

.container {
  width: 450px;
  margin: 0 auto;
  background-color: #777777;
}

.inner {
  padding: 40px;
  position: relative;
}

.text-area {
  display: flex;
  z-index: 10;
}

.text-area i {
  font-size: 60px;
  color: #ffffff;
  margin-right: 20px;
}

.text-area p {
  z-index: 99;
}

.text-area:after {
  content: "";
  display: block;
  background-color: rgba(255, 255, 255, 0.32);
  width: 150px;
  height: 304px;
  position: absolute;
  top: -75px;
  right: -73px;
  transform: rotate(150deg);
  z-index: 0;
}
<head>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<div class="container">
  <div class="inner">
    <div class="text-area">
      <i class="fa fa-compass" aria-hidden="true"></i>
      <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
        It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
    </div>
  </div>
</div>

答案 1 :(得分:1)

您可以尝试在框中添加伪元素,然后使用边框创建三角形,请查看下面的代码段:

.box{
  width: 500px;
  height: 300px;
  background: #233058;
  padding: 20px 40px;
  position: relative;
  z-index:-1;
}

.box:before{
  content: '';
  width: 0px;
  height: 0px;
  background: transparent; 
  position: absolute;
  right: 0px;
  top: 0px;
  display: block;
  z-index: 100;
  border-left: 80px solid transparent;
  border-bottom: 150px solid transparent;
  border-right: 150px solid rgba(70, 88, 158, .5);
  border-top: 80px solid rgba(70, 88, 158, .5);
  z-index: 0;
}

h1, P{
  color: white;
  font-family: Arial;
  font-weight: lighter;
  position: relative;
  z-index: 999;
}

p{
 font: 1.5em sans-serif; 
}
<div class="box">
  <h1>Request and instance</h1>
  <p>Need to add the triangle to the top right corner of a div and make text flow over it. </p>
</div>

答案 2 :(得分:0)

您可以在容器中应用伪元素或嵌套其他div,以使三角形位于文本后面的右上角位置。

有关您需要的CSS的示例,请参阅https://css-tricks.com/examples/ShapesOfCSS/