How can I create a custom-sized striped background using CSS?

Looking to replicate the design shown in the image below using only CSS, where a striping pattern extends downwards at an angle from a block of content with a similar color. The challenge is to maintain the base background color outside of this striped area.

I was able to create the striped background effect by combining linear gradients with percentage stops, utilizing the background-size property, and incorporating rgba colors. However, the issue lies in the fact that the pattern fills the entire section when I want it to be limited to a parallelogram shape extending from the right-hand div without disrupting the content flow.

Avoiding the use of an image as the background solution is essential for maintaining a mobile-first responsive design approach. Therefore, finding a pure CSS solution with smooth scaling is necessary.

Experimenting with the transform:skewX property showed promise, particularly when paired with a wrapper to unskew the contents of the block. While I have achieved the angled boundaries I desired, aligning them with the right-hand div above without affecting the intended flow remains a challenge.

https://i.sstatic.net/yTZ18.png

Here is a JSFiddle link demonstrating the initial attempt at implementation. However, the current solution lacks responsiveness. The existing striped background utilizes a :before pseudo-element with the following styling:

#striped::before {
  content: '';
  position: absolute;
  top: -150px;
  right: 130px;
  width: 200%;
  height: 200px;
  background: linear-gradient( rgba(93, 165, 182, 0.3) 25%, transparent 25%, transparent 50%, rgba(93, 165, 182, 0.3) 50%, rgba(93, 165, 182, 0.3) 75%, transparent 75%, transparent);
  );
  background-size: 100% 4px;
  transform: rotate(-45deg);
  transform-origin: top right;
  margin:0;
}

Answer №1

Since a specific code is not provided, I will demonstrate a proof of concept on how it can be achieved

div {
  position: relative;
  height: 500px;
  background: lightblue;
  overflow: hidden;
}
div::before {
  content: '';
  position: absolute;
  top: -150px;
  right: 130px;
  width: 200%;
  height: 200px;
  background: linear-gradient(
    to bottom,
    #8cf,
    #8cf 50%,
    transparent 50%,
    transparent
  );
  background-size: 100% 6px;
  transform: rotate(-45deg);
  transform-origin: top right;
}
<div></div>


Revamped version as per comment and question edit

The key element was to relocate the striped pseudo within the p tag for responsive sizing, here is an updated version based on your initial code with new/modified rules (and added a div container in markup).

.container {
  overflow: hidden;
}
#preceding p {
  position: relative;
}
#preceding p::before {
  content: '';
  position: absolute;
  top: 100%;
  left: 0px;
  width: 70%;
  height: 500vh;
  transform: rotate(45deg) translate(0%,-50vw);
  transform-origin: left top;
  background: linear-gradient(90deg, rgba(93, 165, 182, 0.3) 25%, transparent 25%, transparent 50%, rgba(93, 165, 182, 0.3) 50%, rgba(93, 165, 182, 0.3) 75%, transparent 75%, transparent);
  );
  background-size: 14px 100%;
  margin:0;
  z-index: -1;
}

* {
  box-sizing: border-box;
}

body {
  background-color: #e0dabf;
  margin: 0;
}

p {
  font-family: "Libre Baskerville";
}

p a {
  font-size: 1rem;
}

#preceding {
  display: flex;
  margin: 0;
}

#preceding h2 {
  flex: 3;
  font-family: "Libre Franklin";
  font-size: 2.25rem;
  font-weight: 600;
  margin: 0;
  padding: 1rem 1rem 1rem 1.5rem;
  color: #5da5b6;
}

#preceding p {
  display: block;
  flex: 7;
  background-color: #5da5b6;
  color: #d5caa2;
  padding: 0.75rem;
  margin: 0;
  text-align: right;
}

#preceding p a {
  color: #f2da83;
}

#striped {
  text-align: center;
  margin: 0;
  position: relative;
  overflow: hidden;
  display: flex;
  flex-flow:row wrap;
  padding:0;
}

#striped h2 {
  flex:1 100%;
}

#striped div {
  flex:1 50%;
  margin:0;
}

.container {
  overflow: hidden;
}
#preceding p {
  position: relative;
}
#preceding p::before {
  content: '';
  position: absolute;
  top: 100%;
  left: 0px;
  width: 70%;
  height: 500vh;
  transform: rotate(45deg) translate(0%,-50vw);
  transform-origin: left top;
  background: linear-gradient(90deg, rgba(93, 165, 182, 0.3) 25%, transparent 25%, transparent 50%, rgba(93, 165, 182, 0.3) 50%, rgba(93, 165, 182, 0.3) 75%, transparent 75%, transparent);
  );
  background-size: 14px 100%;
  margin:0;
  z-index: -1;
}

#further {
  margin:0;
  background-color:#5da5b6;
}

#further h1 {
  margin:0;
}
<div class="container">
<section id="preceding">
  <h2>Content goes here</h2>
  <p>Additional information provided below. The aim is for the striped component below to adjust its size responsively without altering angle or thickness.</p>
</section>
<section id="striped">
  <h2>Header Section</h2>
  <div>
    <h3>
    Block of Content
    </h3>
  </div>
  <div>
    <h3>
    Block of Content
    </h3>
  </div>
  <div>
    <h3>
    Block of Content
    </h3>
  </div>
  <div>
    <h3>
    Block of Content
    </h3>
  </div>
</section>
<section id="further">
  <h1>
  More details soon!
  </h1>
</section>
</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

Guide to centering a Material UI Table on a webpage

Is it possible to center the <Table> within a fixed width <div>, and have a scrollbar appear when the browser is resized, while maintaining the fixed width of the <Table>? Thanks in advance. This is my current setup: <div> ...

laravel active li class dynamic

This is the route I have set up: <?php /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web route ...

Struggling to find and interact with a button element, however Selenium Webdriver is throwing a NoSuchElementException

After experimenting with various selectors and attributes in an attempt to target and click a sign-up button on a website's homepage, I have been unsuccessful. The website in question can be found at: When the webdriver is initiated, all actions are ...

Looking to align a div in the middle of a parallax background image?

I'm trying to center a div with a parallax background image both horizontally and vertically on the page. I attempted using classes from Bootstrap 4 such as d-block, mx-auto, and text-center. While it centered the div horizontally, it did not center i ...

How come certain browsers are treating "n°" as two separate words, and what steps can I take to avoid it?

I need to show a string that includes "n°######" where # represents random numbers. Chrome and Safari sometimes separate the word between n and ° as needed. Firefox, on the other hand, does not do this. Does anyone have any suggestions on how to preven ...

Upon submission, the select input in Vue.js and PHP fails to persist

My situation revolves around a lack of knowledge. I have an HTML form where I am using Vue.js to populate a v-select input with PHP data: <div id="app"> <form> <v-select name="user2_id" placeholder="Select User" :options="[{!! $ ...

Executing a .sh script upon loading a webpage

Is there a way to automatically run a .sh file on webpage load to fetch a JSON file using cURL, without making it visible? I need to keep the API key confidential. ...

Creating a CSS div that can be resized while maintaining a constant aspect ratio

My goal is to create a resizable SVG with a fixed aspect ratio by placing it inside a <div> that adjusts to the size of the SVG. I'm using the Rust Seed library, so although my code isn't in JavaScript, you should still be able to understan ...

Is there a way to activate the window load event within this Jest test scenario?

I'm in the process of setting up my first Jest test for DOM manipulation and jQuery in my Rails project. Right now, I'm facing a basic challenge of ensuring that imported JavaScript functions are functioning as expected. In order to tackle this ...

The removal of classList.remove() only eliminates the class itself, not its contents

My goal is to add one class and remove another class when the start quiz button is clicked. While the 'info_box' class is successfully added, the 'start_btn' class does not get removed; it just changes position (from flex to no flex). T ...

Styling an HTML Table with CSS to Add Cell Padding

I am trying to add padding to a single cell in my table. I created a class for the td element and specified its style in my CSS file as shown below: .itemQuantity { padding-right:30px; text-align:right; background-color: #EEE; } However, the ...

What changes can I make to this javascript code?

I am currently utilizing the Google Local Search API along with the Google Maps API. Take a look at it here: After performing a search, the javascript populates both the map and the results adjacent to it. The function responsible for populating the resu ...

Combining 2 divs harmoniously

I have a set of HTML div elements that are styled using the following CSS: div{ width:250px; height:250px; display:inline-block; border-radius: 10px; margin:10px; background:#2E2922; vertical-align:top; } In my HTML, these div ...

Shopify Inventory Dropdown Tailored to Stock Levels

I'm having an issue with my Shopify store. How can I make sure that the quantity dropdown matches the actual items in stock? For instance, if there are only 3 items available, the maximum value in the quantity dropdown should be set to 3. And if there ...

Why does .attr("value") keep giving me undefined?

I'm experiencing an issue with a hidden element in my jQuery script: <input type="hidden" id="1val" value="24"> var valID = $("#1val").attr("value"); Despite setting the value attribute, when I attempt to print valID, it always shows up as und ...

How to Align an Icon to the Right in a Bootstrap 4 Modal Title

I'm attempting to position two icons next to the close button within a Bootstrap 4 modal. Unfortunately, I am struggling to remove some unwanted margin-left that persists despite my best efforts. While inspecting the element, I was successful in achie ...

What is the best way to restrict the width and height of the background image in my unique bullet list design?

I am currently experimenting with customizing a bullet list. My goal is to apply the same background image, while changing the background position to create a unique effect. Here is my CSS code: #sidebar ul{ width:190px; list-style:none; marg ...

I am facing an issue where the HTML button is not properly interacting with the JavaScript function

function idk() { let cypher = {A:"G",B:"L",C:"Z",D:"E",E:"Y",F:"R",G:"D",H:"N",I:"K",J:"W",K:"J",L:"B",M:"Q",N:"F",O:"S",P:"C",Q:"V",R:"X",S:"I",T:"O",U:"M",V:"T",W:"A",X:"U",Y:"H",Z:"P"}; var answer = prompt('What cypher are you going to use 1 - 26& ...

Revamp responsiveness in bootstrap 3 for printing with @media queries

My goal is to disable the responsive features of bootstrap when the event javascript:print() is triggered. This way, I want my webpage to maintain the column grid layout that I have defined, regardless of screen size. One solution suggested in this answer ...

What is the best way to split an array into smaller chunks?

My JavaScript program fetches this array via ajax every second, but the response time for each request is around 3 to 4 seconds. To address this delay, I attempted to split the array into chunks, however, encountered difficulties in completing the task: { ...