Ensure alignment of gradients between two divs, even if their widths vary

Among my collection of 10 inline divs, each one boasts a unique width and color gradient. While the 45-degree lines are uniform across all the divs, is there a way to ensure that these gradients match seamlessly?

I've shared my CSS code snippet below for reference, with only the color values varying between the different div elements.

   #div1 {
     background: repeating-linear-gradient(
        45deg,
        rgba(155,155,155,0.8),
        rgba(155,155,155,0.8) 3px,
        rgba(250,250,250,0.4) 3px,
        rgba(250,250,250,0.4) 6px);
    }

    #div2 {
     background: repeating-linear-gradient(
        45deg,
        rgba(235,102,107,0.6),
        rgba(235,102,107,0.6) 3px,
        rgba(250,250,250,0.4) 3px,
        rgba(250,250,250,0.4) 6px);
    }
div {
  height:100px;
  display:inline-block;
}
<div id="div1" style="width: 30px"></div><div id="div2" style="width: 40px"></div>

The current outcome showcases misaligned lines within the div elements:

My desired goal is to achieve a harmonious look among all the divs:

Answer №1

You have the option to combine two gradients on a single element by utilizing the background-clip trick to conceal a portion of the initial gradient that extends beyond the padding, allowing the second gradient to show:

.box {
  height:100px;
  width:80px;
  padding-right:50px;
  margin:5px;
  display:inline-block;
 background: 
 repeating-linear-gradient(
    45deg,
    rgba(235,102,107,0.6),
    rgba(235,102,107,0.6) 3px,
    rgba(250,250,250,0.4) 3px,
    rgba(250,250,250,0.4) 6px) content-box,
 linear-gradient(#fff,#fff) content-box, /*avoid the overlap of both gradient*/
 repeating-linear-gradient(
    45deg,
    rgba(155,155,155,0.8),
    rgba(155,155,155,0.8) 3px,
    rgba(250,250,250,0.4) 3px,
    rgba(250,250,250,0.4) 6px) padding-box;
}
<div class="box"></div>
<div class="box" style="width:100px;"></div>
<div class="box" style="padding-right:100px;"></div>

If you want to incorporate more than two gradients, using background-size may be beneficial. It involves having a white background layer beneath each gradient to mask the previous one:

.box {
  height:100px;
  width:300px;
  margin:5px;
  display:inline-block;
 background: 
 /*First gradient*/
 repeating-linear-gradient(
    45deg,
    rgba(235,102,107,0.6),
    rgba(235,102,107,0.6) 3px,
    rgba(250,250,250,0.4) 3px,
    rgba(250,250,250,0.4) 6px) left/30% 100%,
 linear-gradient(#fff,#fff) left/30% 100%,
 /*Second one*/
 repeating-linear-gradient(
    45deg,
    rgba(155,155,155,0.8),
    rgba(155,155,155,0.8) 3px,
    rgba(250,250,250,0.4) 3px,
    rgba(250,250,250,0.4) 6px) left/60% 100%,
 linear-gradient(#fff,#fff) left/60% 100%,
 /**/
 repeating-linear-gradient(
    45deg,
    rgba(15,15,15,0.8),
    rgba(15,15,15,0.8) 3px,
    rgba(250,250,250,0.4) 3px,
    rgba(250,250,250,0.4) 6px) left/80% 100%,
 linear-gradient(#fff,#fff) left/80% 100%,
 /**/
 repeating-linear-gradient(
    45deg,
    rgba(12,155,155,0.8),
    rgba(12,155,155,0.8) 3px,
    rgba(250,250,250,0.4) 3px,
    rgba(250,250,250,0.4) 6px) left/100% 100%,
 linear-gradient(#fff,#fff) left/100% 100%;
  background-repeat:no-repeat;
}
<div class="box"></div>

Another technique involving the use of mix-blend-mode can achieve the same outcome with less code:

.box {
  height:100px;
  width:300px;
  position:relative;
  display:inline-block;
  background: 
  repeating-linear-gradient(
    45deg,
    rgba(0,0,0,0.6),
    rgba(0,0,0,0.6) 3px,
    rgba(250,250,250,0.4) 3px,
    rgba(250,250,250,0.4) 6px),
    #fff;
}
.box::before {
  content:"";
  position:absolute;
  top:0;
  left:0;
  right:0;
  bottom:0;
  background:linear-gradient(to right,blue 20%,red 20%, red 40%,orange 40%);
  mix-blend-mode: lighten;
}
<div class="box"></div>

For a different approach, you can utilize background-attachment:fixed to maintain transparency:

.box {
  height:100px;
  width:30px;
  margin:5px 0;
  display:inline-block;
 background-attachment:fixed;
}
#f1 {
 background-image:repeating-linear-gradient(
    45deg,
    rgba(155,155,155,0.8),
    rgba(155,155,155,0.8) 3px,
    rgba(250,250,250,0.4) 3px,
    rgba(250,250,250,0.4) 6px);
}
#f2 {
 background-image:repeating-linear-gradient(
    45deg,
    rgba(15,15,15,0.8),
    rgba(15,15,15,0.8) 3px,
    rgba(250,250,250,0.4) 3px,
    rgba(250,250,250,0.4) 6px);
}
#f3 {
 background-image:repeating-linear-gradient(
    45deg,
    rgba(12,155,155,0.8),
    rgba(12,155,155,0.8) 3px,
    rgba(250,250,250,0.4) 3px,
    rgba(250,250,250,0.4) 6px);
 }
<div class="box" id="f1"></div><div class="box" id="f2" style="width:100px"></div><div class="box" id="f3" style="width:150px"></div>

An alternate method using multiple backgrounds:

.box {
  height:100px;
  width:300px;
  position:relative;
  display:inline-block;
  background: 
  repeating-linear-gradient(
    45deg,
    transparent,
    transparent 3px,
    rgba(250,250,250,1) 3px,
    rgba(250,250,250,1) 6px),
   linear-gradient(to right,
    rgba(235,102,107,0.6) 20%,
    rgba(155,155,155,0.8) 20%, rgba(155,155,155,0.8) 40%,
    rgba(15,15,15,0.8) 40%);
}
<div class="box"></div>

Answer №2

Here is the solution I came up with:

<div id="div1" style="width: 100px; height: 50px;"></div>

#div1 {
 background: repeating-linear-gradient(
    45deg,
    rgba(155,155,155,0.8),
    rgba(155,155,155,0.8) 3px,
    rgba(250,250,250,0.4) 3px,
    rgba(250,250,250,0.4) 6px);
}

#div2 {
 background: repeating-linear-gradient(
    45deg,
    rgba(235,102,107,0.6),
    rgba(235,102,107,0.6) 3px,
    rgba(250,250,250,0.4) 3px,
    rgba(250,250,250,0.4) 6px);
        background-position: -1px;
}
<div id="div1" style="width: 100px; height: 50px;"></div>
<div id="div2" style="width: 100px; height: 50px;"></div>

I believe that setting the background-position property might be what you need in this case.

Answer №3

If you're looking to achieve a unique design effect, consider layering solid divs with striped divs on top of them. This approach can create an interesting visual appeal on your webpage. (credit to Byoung730 for the inspiration)

div {height: 100px; display: inline-block;}

#div1 {
 background: repeating-linear-gradient(
    45deg,
    rgba(155,155,155,0.8),
    rgba(155,155,155,0.8) 3px,
    rgba(250,250,250,0.4) 3px,
    rgba(250,250,250,0.4) 6px);
}

#div2 {
 background: repeating-linear-gradient(
    45deg,
    rgba(235,102,107,0.6),
    rgba(235,102,107,0.6) 3px,
    rgba(250,250,250,0.4) 3px,
    rgba(250,250,250,0.4) 6px);
}

#div3 {
 background:rgba(155,155,155,0.8)}
 
#div4 {
 background:rgba(235,102,107,0.6)}
 
#div5 {
  position: relative;
  top: -100px;
  width: 500px;
 background: repeating-linear-gradient(
    45deg,
    rgba(255,255,255,1),
    rgba(255,255,255,1) 3px,
    rgba(255,255,255,0) 3px,
    rgba(255,255,255,0) 6px);}
your example:<br>
<div id="div1" style="width: 100px"></div><div id="div2" style="width: 400px"></div>

smooth one:<br>
<div id="div3" style="width: 100px"></div><div id="div4" style="width: 400px"></div><div id="div5"></div>

Answer №4

To create a single row div, consider using the following CSS:

.book {
  background-image: linear-gradient(105deg,
  rgba($color-white, .9) 0%,
  rgba($color-white, .9) 50%,
  transparent 50%),
  url(../img/nat-10.jpg);

Having equal percentages will result in an abrupt color change, bypassing a gradual transition. This configuration will shift directly from the white color to transparent. By utilizing a single div, the lines will remain straight while adjusting the percentages alters the colors. If your design had a slant, be sure to modify the initial angle accordingly.

HTML:

<section class="section-book" id="book">
  <div class="row">
    <div class="book">
      <div class="book__form">
        <form action="#" class="form">

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

Integrate XML information into a webpage's table layout

I am currently working on integrating an XML file into my HTML and displaying it in a table. A snippet of the XML is provided below: <?xml version="1.0" encoding="UTF-8"?> <product category="DSLR" sub-category="Camera Bundles"> <id>0 ...

Issues with CodeIgniter paths causing disruption to CSS background-image declaration

I've been working on a website with backend for two separate customers, each with their own unique URLs. I encountered an issue with Javascript links (ajax calls using url:) but managed to resolve it by using a global variable: var SiteURL='< ...

Ways to arrange and space out td values

Here is an example of my HTML table: <table> <tr> <td>123 - 20 - 20</td> </tr> </table> The numerical values in the table are retrieved from a database and displayed like in this image: How can I ensure that ...

Is it necessary to have authorization on a community website?

I'm currently revamping the website structure for a thriving open-source community. In order to combat potential spamming issues arising from a large number of members, I am considering offering certain options or features exclusively to authenticated ...

Adjust the hue as you scroll

I've been searching for a solution to this issue, but I haven't been able to make it work. My goal is to have the page header change from a transparent background to a red background as the user starts scrolling down the page. $(window).on("s ...

Understanding how flex-wrap property works in flexbox is essential for creating

Take a look at the code snippet below: .flex-container { padding: 20px; margin: 20px; list-style: none; border: 1px solid silver; -ms-box-orient: horizontal; display: -webkit-box; display: -moz-box; display: -ms-flexbox; display: -moz- ...

What is the best way to position an image in the bottom right corner of a fluid div container?

I am struggling to position this icon image at the bottom right hand corner of this div, so that it appears as if the message is originating from the icon's head. Despite my efforts, I have yet to find a definitive solution. https://i.stack.imgur.com ...

Discovering the positions of HTML elements rendered with WebKit (or Gecko) technology

I am seeking a way to retrieve the dimensions (coordinates) of all HTML elements on a webpage as they appear in a browser, including their rendered positions. Specifically, I need to obtain values like (top-left, top-right, bottom-left, bottom-right) Afte ...

What is the best way to display text from a file on a different html page using jQuery's json2html?

Here is the json data: var data = [ { "name": "wiredep", "version": "4.0.0", "link": "https://github.com/taptapship/wiredep", "lice ...

My goal is to create collapsible and expandable rows within this table

Discover the code snippet here, without pasting the entire content... As an illustration, let's utilize this example: <head> </head> <body> <table> <tr> <th></th> < ...

Using the ng-app directive value with HTML and CSS can sometimes cause issues and the styles may not apply

angular.js:38 Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.4.8/$injector/modulerr?p0=mainapp&p1=Error%3A…ogleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.4.8%2Fangular.min.js%3A19%3A463) <body ng-app=""> It was working fine unt ...

Implementing Content-Security-Policy with React and Material-UI v4 for secure styling requirements

Utilizing a React UI with material-ui version 4, the web server is embedded within a JVM application during Production. Following npm build for the UI, the bundle is deployed alongside the server side code. An essential requirement involves implementing C ...

Is there a way to showcase the present weather conditions using simpleweather.js in plain text format?

I have integrated the weather.js library into my HTML file by adding the necessary imports: <script src="js/current.js"></script> <script src="js/sugar.min.js"></script> <script src="js/weather.js"></script> <script ...

Using JavaScript to retrieve data from a JSON file and showcase it on a jQuery mobile webpage

I am struggling to retrieve JSON data from a PHP URL using JavaScript and display it within a JQuery mobile "li" tag as a category list. Despite spending the last 8 hours on it, I can't seem to get it working using the code provided below. Any help wo ...

Utilizing HTML5 canvas to extract pixel data from an image loaded from an external source

When it comes to security reasons, doing it directly may not be feasible. Nevertheless, there are rumors circulating about certain image-hosting platforms that permit the use of their images in a comparable way (could Google Picasa be one?). I might be mis ...

Printing a specified week number in PHP

I have a piece of code that I need help with. It is related to printing the selected day of the week from the server: $daysOfWeek = array( "Monday" => 1, "Tuesday" => 2, "Wednesday" => 3, ...

Combining SVG icons into a sprite sheet and referencing them using the elements <defs>, <g>, and <use> to optimize

I am attempting to incorporate an SVG icon within my HTML code for future use, but I'm facing issues with its display on the webpage when referenced using the <use> tag. Initially, I copied the SVG code of the icon and inserted it directly into ...

Switch on and activate a button using AngularJS

I have a set of four buttons that I want to toggle and activate upon clicking them. Currently, the buttons toggle when double-clicked. My desired solution is for the button current btn to be highlighted and display data when clicked, and for the previous ...

Tips for incorporating fading text and a CTA into your content block

I am looking to protect the full content of my blog posts and allow access only to subscribed members. I want readers to see just a glimpse of the article before it disappears, prompting them to take action with a Call to Action (CTA) like in the fading te ...

Result of utilizing Bootstrap Radio buttons

Looking at the HTML snippet below, I am trying to display two lines of radio buttons. However, when I click on a radio button in the first line and then proceed to click any radio button in the second line, the result of the radio button in the first line ...