Kumar
Kumar

Reputation: 417

I need to include a triangle as background on a div using css.

enter image description here

Need to add the triangle to the top right corner of a div and make text flow over it. Can we achieve this with plain css without using an image?

Upvotes: 0

Views: 187

Answers (3)

Akash Shrivastava
Akash Shrivastava

Reputation: 1365

you can try adding pseudo element to the box, and then using borders create triangle, check below snippet:

.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>

Upvotes: 1

Mohammed Wahed Khan
Mohammed Wahed Khan

Reputation: 898

I Hope this is what you are looking for.

* {
  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>

Upvotes: 1

Anson W Han
Anson W Han

Reputation: 409

You could apply a pseudo element or nest additional divs in your container to get the triangle in the top right position behind the text.

See https://css-tricks.com/examples/ShapesOfCSS/ for examples of css you would need.

Upvotes: 0

Related Questions