Search code examples
htmlcssbackground-imagepseudo-element

Putting a background image inside a shape with pseudo-elements


I have a slanted div and I was wondering how I could get an image within the div that would fill it entirely. For example, it is a rectangle but there is a triangle added to each end to create the "slanted" part of the shape. I would like separate pictures for each div (so to fill the entire purple space), so being able to have different. Thanks for your help in advance.

/*1st set*/
.rrcontainer {
    padding-bottom: 265px;
}
.rr h2{
    font-family: montserrat;
    color: white;
    font-size: 3vw;
    text-align: center;
     position: relative;
     text-align: center;
}
.rr > div {
  text-align: center;
}

.rr {
  position: relative;
  height: 250px;
  background: purple;
}
.rr.rr-left {
  z-index: 1;
  float: left;
  width: 55%;
}
.rr.rr-right {
  z-index: 2;
  float: right;
  width: 44%;
}

.rr:after,
.rr:before {
  content: "";
  position: absolute;
  top: 0;
  width: 0;
  height: 0;
}

.rr-left:after {
  right: 0;
  border-left: 100px solid purple;
  border-bottom: 250px solid white;
}

.rr-right:before {
  left: -100px;
  border-right: 100px solid purple;
  border-top: 250px solid transparent;
}

.rr-left > div {
  margin-right: 100px;
  margin-left: 50px;
}

.rr-right > div {
  margin-right: 50px;
  margin-left: 25px;
}

.rr:hover {
  background: pink;
}

.rr-left:hover:after {
  border-left-color: pink;
}

.rr-right:hover:before {
  border-right-color: pink;
}

/*2nd set*/
.llcontainer {
    padding-bottom: 265px;
}

.ll h2{
    font-family: montserrat;
    color: white;
    font-size: 3vw;
    text-align: center;
     position: relative;

  text-align: center;
}
}
.ll > div {
  text-align: center;
}

.ll {
  position: relative;
  height: 250px;
  background: purple;
}
.ll.ll-left {
  z-index: 1;
  float: left;
  width: 55%;
}
.ll.ll-right {
  z-index: 2;
  float: right;
  width: 44%;
}

.ll:after,
.ll:before {
  content: "";
  position: absolute;
  top: 0;
  width: 0;
  height: 0;
}

.ll-left:after {
  right: 0;
  border-left: 100px solid purple;
  border-bottom: 250px solid white;
}

.ll-right:before {
  left: -100px;
  border-right: 100px solid purple;
  border-top: 250px solid transparent;
}

.ll-left > div {
  margin-right: 100px;
  margin-left: 50px;
}

.ll-right > div {
  margin-right: 50px;
  margin-left: 25px;
}

.ll:hover {
  background: pink;
}

.ll-left:hover:after {
  border-left-color: pink;
}

.ll-right:hover:before {
  border-right-color: pink;
}
<div class="rrcontainer">
 <div class="rr rr-left">
	<div>
		<h2>TITLE HERE</h2>
	</div>
</div>
<div class="rr rr-right">
	<div>
        <h2>TITLE HERE</h2>
	</div>
</div>
</div>
<!--     ----------------------------   -->
<div class="llcontainer">
<div class="ll ll-left">
	<div>
		<h2>TITLE HERE</h2>
	</div>
</div>
<div class="ll ll-right">
	<div>
        <h2>TITLE HERE</h2>
	</div>
</div>
</div>


Solution

  • I only did left one. Notice I added a background image to the "main" div element(<div class="rr rr-left">) and for the triangle part instead of the color purple I used transparent. Hopefully it's what you needed.

    /*1st set*/
    .rrcontainer {
        padding-bottom: 265px;
    }
    .rr h2{
        font-family: montserrat;
        color: white;
        font-size: 3vw;
        text-align: center;
         position: relative;
         text-align: center;
    }
    .rr > div {
      text-align: center;
    }
    
    .rr {
      position: relative;
      height: 250px;
      background: purple;
    }
    .rr.rr-left {
      z-index: 1;
      float: left;
      width: 55%;
      background-image: url(http://lorempixel.com/400/200/);
    }
    .rr.rr-right {
      z-index: 2;
      float: right;
      width: 44%;
    }
    
    .rr:after,
    .rr:before {
      content: "";
      position: absolute;
      top: 0;
      width: 0;
      height: 0;
    }
    
    .rr-left:after {
      right: 0;
      border-left: 100px solid transparent;
      border-bottom: 250px solid white;
    }
    
    .rr-right:before {
      left: -100px;
      border-right: 100px solid purple;
      border-top: 250px solid transparent;
    }
    
    .rr-left > div {
      margin-right: 100px;
      margin-left: 50px;
    }
    
    .rr-right > div {
      margin-right: 50px;
      margin-left: 25px;
    }
    
    .rr:hover {
      background: pink;
    }
    
    .rr-left:hover:after {
      border-left-color: pink;
    }
    
    .rr-right:hover:before {
      border-right-color: pink;
    }
    
    /*2nd set*/
    .llcontainer {
        padding-bottom: 265px;
    }
    
    .ll h2{
        font-family: montserrat;
        color: white;
        font-size: 3vw;
        text-align: center;
         position: relative;
    
      text-align: center;
    }
    }
    .ll > div {
      text-align: center;
    }
    
    .ll {
      position: relative;
      height: 250px;
      background: purple;
    }
    .ll.ll-left {
      z-index: 1;
      float: left;
      width: 55%;
    }
    .ll.ll-right {
      z-index: 2;
      float: right;
      width: 44%;
    }
    
    .ll:after,
    .ll:before {
      content: "";
      position: absolute;
      top: 0;
      width: 0;
      height: 0;
    }
    
    .ll-left:after {
      right: 0;
      border-left: 100px solid purple;
      border-bottom: 250px solid white;
    }
    
    .ll-right:before {
      left: -100px;
      border-right: 100px solid purple;
      border-top: 250px solid transparent;
    }
    
    .ll-left > div {
      margin-right: 100px;
      margin-left: 50px;
    }
    
    .ll-right > div {
      margin-right: 50px;
      margin-left: 25px;
    }
    
    .ll:hover {
      background: pink;
    }
    
    .ll-left:hover:after {
      border-left-color: pink;
    }
    
    .ll-right:hover:before {
      border-right-color: pink;
    }
    <div class="rrcontainer">
     <div class="rr rr-left">
    	<div>
    		<h2>TITLE HERE</h2>
    	</div>
    </div>
    <div class="rr rr-right">
    	<div>
            <h2>TITLE HERE</h2>
    	</div>
    </div>
    </div>
    <!--     ----------------------------   -->
    <div class="llcontainer">
    <div class="ll ll-left">
    	<div>
    		<h2>TITLE HERE</h2>
    	</div>
    </div>
    <div class="ll ll-right">
    	<div>
            <h2>TITLE HERE</h2>
    	</div>
    </div>
    </div>