Triangle-shaped border image with a gradient pattern

How can I create an odd-shaped triangle using CSS? Initially, I used transparent borders with transform: rotate to achieve the left triangle shape successfully. However, I am now attempting to use a gradient border image pattern as the background for the same triangle, and I am facing difficulties in making it work. I have tried adjusting parameters such as border-width, utilizing wrappers, and adding overflow:hidden, among other things, but nothing seems to produce the desired result. In the snippet provided, you can see my attempt on the right side where the pattern does not conform to the triangle shape. Any suggestions or solutions?

#top-left {
  position:absolute;
  left:78px;
  width: 0;
  height: 0;
  border-top: 100px solid transparent;  
  border-right: 80px solid black;
  border-bottom: 50px solid transparent;
  -webkit-transform: rotate(-20deg); 
}

#top-right {
  position:absolute;
  left:300px;
  width: 0;
  height: 0;
  border-image: repeating-linear-gradient( 0deg, pink, pink 1%, purple 1%, purple 8%) 10;
  border-image-width: 100px 80px 50px 0px;
  border-width: 100px 80px 50px 0px;
  border-style: solid;
  -webkit-transform: rotate(-20deg); 
}
<div id="wrapper">
 <div id="top-left"></div>  
 <div id="top-right"></div>
</div>

Update: While Andrey Fedorov's answer is helpful, there is a particular issue that arises when the background is not a solid color. Take, for example, the code snippet below:

body{  
background-color: #6d695c;
background-image:
repeating-linear-gradient(120deg, rgba(255,255,255,.1), rgba(255,255,255,.1) 1px, transparent 1px, transparent 60px),
repeating-linear-gradient(60deg, rgba(255,255,255,.1), rgba(255,255,255,.1) 1px, transparent 1px, transparent 60px),
linear-gradient(60deg, rgba(0,0,0,.1) 25%, transparent 25%, transparent 75%, rgba(0,0,0,.1) 75%, rgba(0,0,0,.1)),
linear-gradient(120deg, rgba(0,0,0,.1) 25%, transparent 25%, transparent 75%, rgba(0,0,0,.1) 75%, rgba(0,0,0,.1));
background-size: 70px 120px;
}
#wrapper {
  position: relative;
}

#top-left {
  position:absolute;
  left:0px;
width: 0;
height: 0;
border-top: 100px solid #fff;  
border-right: 80px solid transparent;
border-bottom: 50px solid #fff;
  -webkit-transform: rotate(-20deg); 
}

#top-right {
  position:absolute;
  z-index: -10;
  left:0;
  width: 0;
  height: 0;
  border-image: repeating-linear-gradient( 0deg, pink, pink 1%, purple 1%, purple 8%) 10;
  border-image-width: 100px 80px 50px 0px;
  border-width: 100px 80px 50px 0px;
  border-style: solid;
  -webkit-transform: rotate(-20deg); 
}
<div id="wrapper">
 <div id="top-left"></div>  
 <div id="top-right"></div>
</div>

Answer №1

To create various design elements, you can utilize the linear-gradient property along with no-repeat and background-size. Here is an example of how you can achieve this:

Follow these steps to create different pieces using a single tag:

/* testing gradients */
p , div#wrapper {
  width:80px;
  float:left;
  margin:1em;
  height:150px;
  /* see me then remove this  shadow */
  box-shadow:0 0 0 2px;
}
p {
  background:
    linear-gradient(130deg, transparent 49.75%, pink 50.5%) 0 42px  no-repeat ;
  background-size:
    100% 15px;
  transform: rotate(-20deg); 
}
p + p{
  background:
    linear-gradient(130deg, transparent 49.75%, pink 50.5%) 0 42px  no-repeat ,
    linear-gradient(130deg,transparent 62px, purple 63px) top no-repeat;
  background-size:
    100% 15px,
    100% 65%;
}
p + p + p {
  
  background:
    linear-gradient(130deg, transparent 49.75%, pink 50.5%) 0 42px  no-repeat ,
    linear-gradient(130deg,transparent 62px, purple 63px) top no-repeat,
    linear-gradient(33deg , trans

/* Simplify your CSS code */
body{  
background-color: #6d695c;
background-image:
repeating-linear-gradient(120deg, rgba(255,255,255,.1), rgba(255,255,255,.1) 1px, transparent 1px, transparent 60px),
repeating-linear-gradient(60deg, rgba(255,255,255,.1), rgba(255,255,255,.1) 1px, transparent 1px, transparent 60px),
linear-gradient(60deg, rgba(0,0,0,.1) 25%, transparent 25%, transparent 75%, rgba(0,0,0,.1) 75%, rgba(0,0,0,.1)),
linear-gradient(120deg, rgba(0,0,0,.1) 25%, transparent 25%, transparent 75%, rgba(0,0,0,.1) 75%, rgba(0,0,0,.1));
background-size: 70px 120px;
}
<!-- your issue -->
<div id="wrapper">
 <div id="top-left"></div>  
 <div id="top-right"></div>
</div>
<!-- p for testing purpose -->
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>

Another method is to use nested elements with gradients and transformations:

body{  
background-color: #6d695c;
background-image:
repeating-linear-gradient(120deg, rgba(255,255,255,.1), rgba(255,255,255,.1) 1px, transparent 1px, transparent 60px),
repeating-linear-gradient(60deg, rgba(255,255,255,.1), rgba(255,255,255,.1) 1px, transparent 1px, transparent 60px),
linear-gradient(60deg, rgba(0,0,0,.1) 25%, transparent 25%, transparent 75%, rgba(0,0,0,.1) 75%, rgba(0,0,0,.1)),
linear-gradient(120deg, rgba(0,0,0,.1) 25%, transparent 25%, transparent 75%, rgba(0,0,0,.1) 75%, rgba(0,0,0,.1));
background-size: 70px 120px;
}

div.nested {
  margin:1em;
  height:150px;
  width:80px;
  position:relative;
  overflow:hidden;
  transform:rotate(-20deg);
  box-shadow: 0 0 ;
}
.nested div {
  transform:rotate(31deg) scale(1.2, 0.9) skew(-5deg);
  transform-origin: 100% 102%;
  height:100%;
  background:linear-gradient(-40deg, pink 8%, purple 8%, purple 65%, pink 65%, pink 75%, purple 75%)
}
<div class=nested>
  <div>
  </div>
</div>

Answer №2

Here's a potential solution to the issue:

  • Place both shapes in the same position.
  • Utilize the z-index property to have the patterned shape appear behind the other shape.
  • Fill the border area outside the triangle with a color matching the shape's background.
  • Make the border with the triangle color transparent.

#wrapper {
  position: relative;
}

#top-left {
  position:absolute;
  left:0px;
width: 0;
height: 0;
border-top: 100px solid #fff;  
border-right: 80px solid transparent;
border-bottom: 50px solid #fff;
  -webkit-transform: rotate(-20deg); 
}

#top-right {
  position:absolute;
  z-index: -10;
  left:0;
  width: 0;
  height: 0;
  border-image: repeating-linear-gradient( 0deg, pink, pink 1%, purple 1%, purple 8%) 10;
  border-image-width: 100px 80px 50px 0px;
  border-width: 100px 80px 50px 0px;
  border-style: solid;
  -webkit-transform: rotate(-20deg); 
}
<div id="wrapper">
 <div id="top-left"></div>  
 <div id="top-right"></div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to Place Buttons on the Left, Center, and Right in Bootstrap Framework

I'm currently working on developing some pages using bootstrap. In the form at the bottom, I have three buttons - Save, Cancel, and Commit. The issue I'm facing is that while the left and right buttons are perfectly aligned, the Cancel button is ...

Looking to switch up the background color while scrolling, any tips?

I'm trying to change the background color of my navbar section on scroll, but I've had no luck so far. Below is the code I have been using: const [colorChange, setColorChange] = useState(false); const changeColor = () => { if (window.scro ...

Effective Methods for Implementing CSS Variables within nth-child Selectors

Objective: To pass a variable successfully as a parameter to nth-child. The task is to make the third line turn green in the example provided. Dilemma: Currently, the parameter is being disregarded as a variable. Inquiry: Is achieving this possible? If s ...

Conditionals in Angular 2 using Sass

Looking to apply this style with Sass or CSS: IF :host.form-control MATCHES .ng-valid[required] OR .ng-valid.required THEN :host ::ng-deep input.form-control = border-left: 5px solid #42A948; Appreciate the help! ...

Vertically animating an image using jQuery

I have been experimenting with trying to make an image appear as if it is floating by using jQuery to animate it vertically. After some research, I stumbled upon this thread: Animating a div up and down repeatedly which seems to have the solution I need. H ...

"Exploring the best locations for storing CSS, JS, and image files in Spring

Having trouble figuring out where to place my public files. I attempted the solution mentioned in this thread on Stack Overflow but it didn't work for me. I've tried putting my public folder in various locations within the WEB-INF directory, but ...

Can the Bootstrap grid be arranged vertically?

I'm finding it difficult to work on a project with Bootstrap's horizontal grid system. What I want to achieve is to have my page divided into 4 sections, with the left side blocks having equal height and the right side blocks having varying heigh ...

"Fixing index.html with the power of Bootstrap 4: A step-by

*I am facing an issue where the default bootstrap navbar style appears before my custom styles get applied every time I reload the page. Any assistance would be greatly appreciated* - Whenever I reload the page, the default bootstrap navbar style appears ...

Adjusting canvas dimensions for angular chart.js: A quick guide

I am currently creating my first sample code using angular chart.js, but I am facing an issue with changing the custom height and width of my canvas. How can I adjust the height and width accordingly? CODE: CSS #myChart{ width:500px; he ...

What are the steps for rearranging a table column?

I am trying to show a text file in a table and allocate some code to each entry. I can display the text file. I can assign the code for each record (though this part is still under development). However, I have encountered an issue. The columns of the t ...

Error 404: Django is unable to locate the static/css files

This particular issue appears to be quite widespread, but existing Q&A resources are outdated and do not address my specific problem. Currently, with Django 2.0.2, my basic webpage is failing to render the bootstrap.css file simply because it cannot locat ...

Tips for securing a navbar in material ui

https://i.stack.imgur.com/CYfmQ.png In my current React project, I am facing an issue with aligning components within the Material-UI AppBar Component. My aim is to achieve a seamless and perfectly straight alignment for three key elements: a logo image, ...

Troubleshooting: Custom checkbox using CSS ::before does not display properly on Firefox and Edge browsers

Encountering a frustrating CSS challenge while working on a project that needs to be compatible across various browsers. Specifically, I'm having issues with custom styling for checkboxes. Despite following recommendations to use a ::before pseudo-ele ...

I seem to be having an issue with the decimal point on my calculator - I only want to see one dot

Only need one dot for the calculator to function properly. The current situation with multiple dots is causing confusion. I intend to address other aspects of the code later, but I'm currently struggling with this issue. function dec(d) { let ...

jQuery accordion section refuses to collapse

In order to achieve the desired outcome, each Section should initially appear in a collapsed state. Panel 0 and 2 start off collapsed, however, panel 1 does not but can be collapsed upon clicking. Additionally, Panel 1 incorporates an iframe that displays ...

Are there any instances where CSS is overlooking certain HTML elements?

In the following HTML code snippet, the presence of the element with class ChildBar is optional: <div class="Parent"> <div class="ChildFoo"></div> <div class="ChildBar"></div> <!-- May ...

Implementing scrolling functionality within a nested container

I need help with getting a nested container (.widget2) to scroll while still keeping the borders intact. Currently, the inner container is scrolling past the bottom edge of the parent container, and the scrollbar isn't displaying correctly. If you w ...

Verify if there are DOM elements located within a DIV container, execute the functions associated with those elements sequentially

I am in the process of creating a game using HTML, CSS, and JavaScript. My focus right now is on manipulating DOM elements without relying on the canvas tag. The goal is to develop a pseudo graphical programming language, similar to the environment provide ...

Modify the appearance of the gradient progression bar

I am working on a gradient progress bar with the following code: CSS: .progressbar { height: 15px; border-radius: 1em; margin:5px; background: linear-gradient(-45deg, rgba(255, 255, 255, 0.15) 25%,transparent 25%, t ...

React component causing footer overlap

Currently, I'm facing a challenge in creating a footer that stays at the bottom of the page without overlapping any other components. In my index file, I've included the following code: .footer{ margin-top: 1rem; padding: 1rem; background- ...