Design a gradient backdrop resembling a duo of bars for a visually appealing effect

Looking to create a unique background effect that resembles two progress bars as shown in this image: https://i.sstatic.net/LpENZ.png

I know how to create one progress bar using linear gradients, but I'm wondering if there's a way to split the background into two halves and have both stop at a specific percentage?

<!DOCTYPE html>
<html>
  <head>
  <style> 
      #example1 {
         background-color: transparent;
         background-image: linear-gradient(90deg, #eceddc 25%, transparent 25%),
         linear-gradient(180deg, #eceddc 50%, transparent 25%),
         linear-gradient(90deg, #eceddc 50%, transparent 25%); 
      }
  </style>
</head>
<body>
  <div id="example1">
     <h1>Lorem Ipsum Dolor</h1>
     <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
  </div>
</body>
</html>

Answer №1

To achieve a result similar to the image shown, you can implement the code snippet below:

<!DOCTYPE html>
<html>
<head>
<style> 
#example1 {
background-color: transparent;
background-image: linear-gradient(to right, #eceddc 75%, white 25%),
linear-gradient(to right, #eceddc 25%, white 25%);
background-position: right top, right bottom;
background-repeat: no-repeat, no-repeat;
background-size: 100% 50%, 100% 100%;
}
</style>
</head>
<body>

<div id="example1">
  <h1>Lorem Ipsum Dolor</h1>
  <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
</div>

</body>
</html>

Answer №2

To achieve the desired effect, follow these steps:

#example1 {
  background: 
      linear-gradient(#eceddc 0 0) top    left,
      linear-gradient(#eceddc 0 0) bottom left;
  background-size:
    80% 30%, /* width height of the first bar  */
    40% 70%; /* width height of the second bar */
  background-repeat:no-repeat;
}
<div id="example1">
  <h1>Lorem Ipsum Dolor</h1>
  <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
</div>

Answer №3

To achieve the desired background image effect, you can create two DIVs and customize them with gradients. After styling, add these DIVs as children to a parent div that contains the text elements. Then, set the text elements to position: relative with a z-index of 10.

In case the element moves on the page, utilize Javascript to determine the position of the parent element using a handler function. Create two eventListeners for load and scroll events. Invoke the handler function with the listeners to reposition the background elements in sync with the text elements during load and scroll actions.

UPDATE: I incorporated JavaScript to obtain the height and width of the text elements' div using (offsetHeight & offsetWidth). The height and width of each BG div are now dynamically adjusted based on the height of the text div, enhancing flexibility.

window.addEventListener('load', handler, false);
window.addEventListener('scroll', handler, false);
let exOne = document.getElementById('example1');
let exTwo = document.getElementById('example2');
let exThree = document.getElementById('example3');

function handler(event) {

  function getOffset(el) {
    const rect = el.getBoundingClientRect();
    return {
      left: rect.left + window.scrollX,
      top: rect.top + window.scrollY
    };
  }
  exTwo.style.top = getOffset(exOne).top + "px";
  exTwo.style.left = getOffset(exOne).left + "px";
  exTwo.style.height = exOne.offsetHeight + "px";
  exTwo.style.width = exOne.offsetWidth + "px";
  exTwo.style.display = "block";
  exThree.style.top = getOffset(exOne).top + "px";
  exThree.style.left = getOffset(exOne).left + "px";
  exThree.style.height = exOne.offsetHeight / 1.75 + "px";
  exThree.style.width = exOne.offsetWidth + "px";
  exThree.style.display = "block";
}
#example1 {
  background-color: transparent;
  padding: 10px;
}

#example1 p,
#example1 h1 {
  z-index: 10;
  position: relative;
}

#example2 {
  background-color: transparent;
  background: linear-gradient(to top, #0FF 51%, transparent 25%), linear-gradient(to left, #0FF 51%, transparent 25%);
  position: absolute;
  z-index: 1;
  /* display none to remove any glitches shown before load 
  add back to block in JS after load*/
  display: none;
}

#example3 {
  background-color: transparent;
  background: linear-gradient(to bottom, #eceddc 51%, transparent 25%), linear-gradient(to right, #eceddc 51%, transparent 25%);
  position: absolute;
  z-index: 1;
  /* display none to remove any glitches shown before load 
  add back to block in JS after load*/
  display: none;
}
Scroll down
<br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<div id="example1">
  <div id="example2"></div>
  <div id="example3"></div>
  <h1>Lorem Ipsum Dolor</h1>
  <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
  <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
  <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
  <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
</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

In IE8, the section cannot extend to full width

Having some trouble with a section in the footer of a website I'm developing - it won't go full width in IE8. I've tried all sorts of solutions but nothing seems to work. Can anyone else take a look and see if there's something I'v ...

Looking for a smart way to extract all the selected elements from a form?

I am attempting to retrieve all the checked items from this form using JavaScript. I have looked at previous solutions, but none of them fit my requirements. <form id="checkform" class="container" style="margin-top:20px;"> <input type="checkb ...

Tips for adjusting the progress bar to 100% and back to 0%

My goal is to increase the progress bar percentage with each click. Currently, it only goes up to 75%. When saving, it should show 100%, but it's not displaying that properly. On clicking the back button, it should reset to 0% on the third click. HTM ...

Allowing scroll events to flow through from a child <div> to <canvas> in Excalidraw

In my current project, I am utilizing Excalidraw to draw a layer of <div>s on top of the <canvas>. While everything is functioning well, I have encountered an issue with scrolling through the infinite canvas. When the mouse hovers over one of t ...

Could it be that this is a mysterious foreign alphabet or a problem with the encoding?

I'm facing a strange bug with characters on my website and I can't seem to figure out what's causing it. A foreign writer submitted an article to me, and when I received it, the font was displaying oddly. After some investigation, I've ...

Displaying CSS image slideshow images side by side

Currently I am a student studying the fundamentals of HTML and CSS. My latest project involved creating a website showcasing my work from digital arts class, complete with a simple CSS slideshow. The original intention was to have each image displayed on i ...

Prevent elements from overlapping within a flexbox container

I've encountered an issue with resizing the window causing the logos to overlap the portrait image. Is there a way to fix the logos relative to the portrait? Below is the code: /* Defaults for Main Part of Pages */ body, div, h1, p { margin: 0; ...

revealing the excessive vertical space in the div upon clicking the link

Here is the structure: +-----------------------------------------------------------------------+ | | | heading {position: fixed;} | |________ ...

How can I align the menu to the right in the navbar using Bootstrap 4?

I'm currently customizing the Bootstrap 4 default navbar and I want to add a collapsing menu that is positioned on the right, instead of the left, when it's not collapsed. So far, I have attempted to use text-right and float-right, but unfortuna ...

How can I import HTML files using PHP?

Currently, I have a file structured like this: <?php if(some condition) { //Access Denied } else { echo "<html>My HTML Code</html>"; } ?> However, I am looking to optimize my PHP file by simplifying it with the following structu ...

Can you guide me on creating a horizontal scrolling feature using HTML, CSS, and React?

Struggling to create a horizontal scrolling component similar to Instagram stories? Here's what I've attempted so far: .scroller{ height: 125px; width: 300px; background-color: blue; overflow-x: scroll; overflow-y: hidden; } .itemC ...

What could be causing my chessboard to not wrap around properly with flexbox?

I have been working on creating an 8x8 chessboard by following this example: https://codepen.io/sammyslamma/pen/gJeOwY .flex-container { display: flex; flex-flow: row; flex-wrap: wrap; ...

positioning a text input in the center instead of at the top of the page

Hello everyone, I am just starting out with HTML and I have a question. How can I make the Username: <input type="text" name="Username" style="- margin-top: 200px;"> appear in the center of the page instead of at the top? I've tried changing the ...

Interacting with JSP through session management

Hello, I am working on a JSP program that involves displaying a number along with a button. The objective is to have the displayed number increment whenever the user clicks the button. Sessions are also intended to be included in this program. Here is wha ...

What is the reason behind the error message in Bokeh?

While following a Udemy tutorial, I encountered the following error message associated with Bokeh: Bokeh Error attempted to retrieve property value for property without value specification when running the code provided in the HTML. Can anyone shed some l ...

Utilizing ng-href in Angular.js Template: A Guide

I am attempting to develop a simple Single Page Application (SPA) with just one index.html file that includes templates. However, I encountered an issue with the ng-href directive: <a ng-href="#/myPage">myPage</a> This works fine in index.h ...

What is the best way to enlarge a Material UI card?

Currently, I am utilizing Material UI version 4 on my website and integrating cards (for more details, click here). https://i.stack.imgur.com/dm2M2.png I am interested in enlarging the overall size of the card so that the image appears larger. As I am si ...

Utilizing Bootstrap for Proper Alignment of "Link" and "Label"

Even though I have used "href" and a "label", the alignment is still off. Both elements are in the same line, but "My Text" appears slightly higher than "myImage". <div class="col-md-6 bottom"> <a href="~mydata"><img src="~/Images/myI ...

Is it possible to have the website show up at a default size of 90% on the display

My preference is for my website to appear at 90% instead of 100%. This seems to be a common occurrence for me. Is there a way to have it default to this appearance? ...

Add a symbol at the end of the input that signifies its value

I have a form field that expects a percentage as input, but I want to visually indicate to the user that they should enter a percent value. The challenge I face is that I want the percentage symbol to appear before the number in the input box, like this: ...