What is the easiest way to create a transparent section on an image using only HTML and CSS?

Is it possible to create a barometer using only HTML and CSS? My idea is to have a background image with just borders and a foreground image filled with color on top of it. I want to display x% of the foreground image's color to represent progress. Would making a (1-x)% part of the image transparent achieve this effect?

Here is an example of how you could implement this:

<div class="alignfullhigher">
<table> 
<tbody> 
<tr>
<td class="backtable"><img src="background_no_fill_only_outline.png" width="100%"></td> 
</tr>
<tr> 
<td class="fronttable"><img src="foreground_fill.png" width="100%" height="160px"></td> 
</tr> 
</tbody>    
</table> 
</div>

CSS:-

.alignfullhigher{position:relative; width:100%; height:160px;top:0;right:0;}
.backtable{position:absolute;z-index:-1; width:100%; height:auto;}
.fronttable{position:relative;width:100%; clip: rect(0px,100px,160px,0px); )}   

Answer №1

If you want to add graphics easily, consider using background images.
You can achieve this by layering two images on top of each other:

Simply place the "full" image within a smaller rectangle than the "empty" one.

div {
  width: 49px; height: 184px;
  background-size: 49px 184px;
  background-position: left bottom;
}

.empty {
  background-image: url(https://i.sstatic.net/B4KVi.png);
  position: relative;
}

.full {
  background-image: url(https://i.sstatic.net/93Cf3.png);
  position: absolute;
  bottom: 0;
  height: 60%; /* adjust this value to alter the barometer */
}
<div class="empty">
  <div class="full"></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

Troubleshooting Issue with Absolute Positioning and Hover Effect on Material UI Button

I have a div containing a hidden button and an inner div filled with graphs and text. Occasionally, I need to blur the inner div and make the button float on top in the center of the blurred section, similar to the layout seen on subscription prompts found ...

Ways to regain control of the iframe window once a popup has been launched

I have a question regarding managing windows in Javascript. In my application, when I click on a link within an iframe, it opens a popup window where I enter my login credentials. After entering the credentials, I am able to close the popup window using a ...

What is the best way to emphasize the chosen node in a treeview using jQuery?

.treeview ul { background-color: white; margin-top: 4px; } .treeview a:visited { background-color: Yellow; } .treeview a:active { background-color: Yellow; } .treeview a:selected { background-color: Yellow; } When I click on a node ...

Using a for loop in JavaScript to fetch and display a specified number of results from an API

Recently, I delved into the world of Javascript and API arrays to grasp the concept of retrieving and manipulating various APIs. The topic of Javascript loops and arrays has been discussed numerous times, both here on StackOverflow and on other platforms. ...

enclosing a fixed or absolute div within a relative div

I am trying to ensure that the text on my website remains below an absolute positioned menu. The menu is overlaying a div with text, and I want the layout to appear as if the menu was relatively positioned. HTML <nav id="s-nav-wrap"> <di ...

Steps to duplicate a text within a span tag

I need to extract the dynamic text from a spam generated by a slider set by the user. This text should then be copied to an input field. I attempted the following, but it did not work: <h4>Consórcio Value: <span class="slider-value quote-form-e ...

Does adding !important to the @font-face rule validate it?

Can anyone help me with a problem I'm facing? I want to ensure that users are forced to use the web font instead of the font installed on their computers because the formatting is slightly different. I've looked into using !important but I'm ...

jQuery enables the creation of fresh form fields

Currently, I am working on a form that initially consists of 2 text input fields. The typical use case involves the user entering a number in one field and their name in the other. Following this, the page updates itself without reloading. However, there a ...

Embed a Telegram account onto an HTML page using the <iframe> element

Is there a way to display the personal Telegram account in an iframe tag? I experimented with the code below, but unfortunately it did not produce the desired result: <iframe src="https://telegram.me/joinchat/BfNEij9CbDh03kwXacO5OA"></iframe> ...

Using line breaks in JSX

In my application, I save all the text in a settings.ts file. However, I am facing an issue where \n does not seem to add line breaks as expected. Are there any alternative methods to include breaklines in my text? My tech stack includes Next.js, Reac ...

Attempting to use tabs within a tab does not function correctly

Visit the CodePen link here This is the codepen example. <div class="row"> <div class="col s12"> <ul class="tabs2"> <li class="tab col s3"><a href="#test1">Test 1</a></li> <li class=" ...

Implementing a delay of X seconds after a click event in JQuery: A step-by-step guide

Is there a way to delay the triggering of a click event after one has been recently triggered? I am facing an issue on my website where users can click multiple times on the "dropdown icon" and cause it to toggle the slide effect multiple times. What I wan ...

What is the proper way to encode image URLs for use in CSS?

For the div element in my code, I want to allow users to input a URL that will be applied as a CSS background image, like this: background-image: url("/* user specified URL here*/") I'm concerned about security. How can I properly escape the URL to ...

Using jQuery and CSS to Filter and Sort Content

I need help with filtering a list of content based on their class names. The goal is to show specific items when a user clicks on one of two menus. Some items have multiple classes and users should be able to filter by both menus simultaneously. Currently ...

Increasing the height of a menu using jQuery animations

Hello everyone, I'm working on a project and need some assistance with this jsfiddle: Check it out here. Currently, the box expands to a static width upon hovering, but sometimes it extends beyond the last text item causing it to slide back up when t ...

Simple method for extracting data from JSON and saving it into an array with jQuery

I have a JSON file containing the resource "A" and literals "B", "C", and "D". My goal is to extract only the items B, C, and D, convert them into strings, and store them in an array. Below is the script I have written for this purpose: <script> // ...

What causes the form to consistently show as invalid once it has been submitted?

register.html : <form [formGroup]="signupForm" (submit)="onSubmit()" class="form-detail"> <h2>Registration Form</h2> <div class="form-row-total"> <div class="form-row"> <in ...

Comparing the efficiency of Jquery draggable with thousands of elements versus updating the elements continuously

Currently, I am developing an application that involves dragging an image using Jquery's draggable utility. The image is accompanied by several overlay divs containing various components positioned based on pixel locations, sometimes reaching into the ...

Navigating to different HTML pages by utilizing <a> elements in Angular 2

As a newcomer to Angular 2, I've decided to build my personal website using this framework. The main page of my website contains bio information, while the other page will feature blog content. Both pages will have a shared header and footer, but diff ...

A guide on how to align Hamburger CSS in the center, center text, and include a

For a while now, I've been attempting to achieve three specific things with a Hamburger menu, but so far, I've had no luck. This Hamburger menu is responsive, transitioning into an X symbol upon clicking and moving to the right of the page with a ...