What is a way to arrange elements on top of each other without any overlap, without resorting to the "position: absolute" property?

Is there a way to make this design responsive with dynamic scaling? My primary concern is managing the positioning of the meter scales without having to use absolute positioning. The main question here is: how can I stack elements on top of each other without them overlapping, while avoiding the use of position: absolute?

body {
  background-color: #151515;
}

.meter {
  height: 400px;
}

.meter-pointer {
  width: 2px;
  height: 200px;
  background: #2c3e50;
  transform: rotate(45deg);
  transform-origin: bottom;
  transition: transform 0.5s;
  position: absolute;
  margin-left: 50%;
}

.meter-dot {
  width: 10px;
  height: 10px;
  background: #2c3e50;
  border-radius: 50%;
  position: absolute;
  margin-top: calc(200px - 5px);
  margin-left: calc(50% - 5px);
}

.meter-scale {
  color: #cfcfcf;
  width: 1px;
  height: 200px;
  transform-origin: bottom;
  transition: transform 0.2s;
  box-sizing: border-box;
  border-top: 10px solid;
  position: absolute;
  margin-left: 50%;
}

.meter-scale-strong {
  width: 2px;
  border-top-width: 20px;
}
<div class="meter">
  <div class="meter-dot"></div>
  <div class="meter-scale meter-scale-strong" style="transform: rotate(-45deg);"></div>
  <div class="meter-scale" style="transform: rotate(-36deg);"></div>
  <div class="meter-scale" style="transform: rotate(-27deg);"></div>
  <div class="meter-scale" style="transform: rotate(-18deg);"></div>
  <div class="meter-scale" style="transform: rotate(-9deg);"></div>
  <div class="meter-scale meter-scale-strong" style="transform: rotate(0deg);"></div>
  <div class="meter-scale" style="transform: rotate(9deg);"></div>
  <div class="meter-scale" style="transform: rotate(18deg);"></div>
  <div class="meter-scale" style="transform: rotate(27deg);"></div>
  <div class="meter-scale" style="transform: rotate(36deg);"></div>
  <div class="meter-scale meter-scale-strong" style="transform: rotate(45deg);"></div>
  <div class="meter-pointer" style="transform: rotate(12deg);"></div>
</div>

Answer №1

A creative solution incorporating gradient, mask, and clip-path techniques to minimize the amount of HTML code required.

.meter {
  width:300px; /* adjust width for responsiveness */
  display:grid;
  margin:auto;
}
.meter::before,
.meter::after,
.meter i{
  content:"";
  grid-area:1/1;
  padding-top:50%;
}
.meter::before,
.meter i{
  border-radius:400px 400px 0 0;
  clip-path:polygon(-10% 0,110% 0,50% 100%);
  background:repeating-conic-gradient(from -.5deg at bottom,red 0 1deg,#0000 0 9deg);
  -webkit-mask:radial-gradient(farthest-side at bottom,#0000 calc(100% - 15px),#000 0); 
}
.meter i {
  background:repeating-conic-gradient(from -.5deg at bottom,red 0 1deg,#0000 0 45deg);
  -webkit-mask:radial-gradient(farthest-side at bottom,#0000 calc(100% - 30px),#000 0); 
}
.meter::after {
  width:20px;
  z-index:1;
  margin:0 auto -10px;
  background:
    linear-gradient(blue 0 0) top/3px calc(100% - 10px),
    radial-gradient(farthest-side,blue 97%,#0000) bottom/20px 20px;
  background-repeat:no-repeat;
  transform-origin:50% calc(100% - 10px);
  transform:rotate(10deg); /* adjust this */
}
<div class="meter"><i></i></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

Expanding the Element prototype with TypeScript

Is there a corresponding TypeScript code to achieve the same functionality as the provided JavaScript snippet below? I am looking to extend the prototype of the HTML DOM element, but I'm struggling to write TypeScript code that accomplishes this with ...

The functionality of Jquery Ajax is limited to a single use

I'm having an issue with a page that is supposed to reload the data within a div using jQuery, but it only updates once. Here's a snippet of my code: <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0 ...

Dynamic text input and selection menu with AJAX (PHP and JavaScript)

As a student who is new to Javascript and PHP, I am trying to create a login page for my website that can verify user input in the database using AJAX. For example, when a user enters their username and password, the system should automatically check if t ...

Could not locate the CSS file within the HTML document (404)

I've searched high and low on YouTube but can't seem to find a solution to this problem. Every time I run the code, it gives me an error message saying GET net::ERR_ABORTED 404 (NOT FOUND) <html> <head> <title>itays websit ...

What is the reason behind the change in size and shape of the text when using "overflow: hidden"?

Check out this interactive demo using the provided HTML and CSS: You can view the demo by clicking on this link. /*CSS*/ .header { margin: 0px; padding: 0px; width: 100%; } .headertable { padding: 0px; margin: 0px; width: 100%; border-sp ...

"Styling mysteriously disappears from Vue project when deployed to production

While running my docker image and routing the requests through nginx, I encounter issues with the styling. Although I can see all the html-tags and locate the css and js files in the network tab of the browser, the styling is not being applied. Even thoug ...

Creating a Duplicate of the Parent Element and its Child Elements using jQuery

Here is a code snippet I have that adds a new paragraph when a button is clicked. Currently, the script clones the "sub" div and appends it to the "main" div. However, the issue is that it only copies the content of the "inner" div within the "sub" div ins ...

Which is the preferable option for creating a circular daily planner: canvas or SVG?

As a novice programmer delving into the world of coding (or at least hoping to), I have a question regarding graphic tools in html5 for my latest project. My goal is to create a planner app using electron, with the unique twist that the planner form sho ...

What is the best way to iterate through anchor links surrounding an image and dynamically load content into the figcaption element using AJAX?

Seeking assistance for an issue related to my understanding of the $this keyword in jQuery. I am facing a problem where I have 3 images on a page, each wrapped in an anchor link. My goal is to loop through these links, retrieve the URL of each anchor lin ...

In ASP.NET, using the <%#Eval %> tag to navigate between pages

On a 'List' page similar to this one, I am incorporating an <a> tag within a td, along with three buttons labeled 'Go up', 'Go down', and 'Go list'. Upon clicking the link, I successfully navigate to another p ...

Deleting a field from a Django model form causes the HTML layout to be disrupted

When I remove the "useremail" field from my forms.py in Django, it causes the formatting to be removed from all fields in the HTML, even though the form functions correctly. This issue occurs when I remove any of the 4 fields. All forms work fine, but the ...

Is there a way to activate sorting icons in bootstrap datatables?

My attempt to replicate the datatable from this template is not working as expected. I can't seem to get the up and down icons for sorting to show up. I've followed all the instructions but they are still missing... $(function () { $(&a ...

How can you extract information from a text document and seamlessly incorporate it into an HTML webpage?

I am currently working with an HTML file structured like this.. <html> <body> <table border="1" cellpadding="5"> <tr> <td>Some Fixed text here</td> <td id="data">---here data as per values in external text file--- ...

Looping in jQuery: Tips for Improving Performance

In my popup window, I have a JavaScript function that utilizes jQuery to retrieve checked checkboxes from the parent window. It then uses jQuery again to access associated data from hidden fields in the parent window for each checkbox. var chked = $(&apos ...

Transitioning images in JQuery by using a rollover effect

Is there a way to use Jquery for a hover effect that transitions images with a slide up animation? I've looked everywhere online, but I can't seem to find a code that works. All the examples I've found use fadeIn or fadeOut. Thank you for yo ...

Is there a way to retrieve the dates from last month using jQuery?

Currently, I am unable to select past dates with the provided code. How can I adjust it to allow for selecting dates from previous months? let firstDay = new Date(); $('#date_filter_startmonthly').datepicker({ startDate: firstDay, endDate: ...

javascript create smooth transitions when navigating between different pages

As a newcomer to JS, I am currently working on creating a website with an introduction animation. My goal is to have this animation displayed on a separate page and once it reaches the end, automatically redirect to the next webpage. <body onload="setT ...

The formgroup label is currently displayed in uppercase, but I would prefer it to be in title case

In the angular template below, I have noticed that even though the labels are in titlecase, they appear in uppercase when the page is rendered. This wasn't the desired outcome so I attempted to use the titlecase pipe and enclose the label text within ...

Dynamically implementing event listeners in JavaScript to remove specific elements based on their

My website has a list of phones displayed as ul li. When a user clicks the "Buy" button, it should create a new li within another div called "ordersDiv". Users can delete their purchase from the cart by clicking "Remove", which should remove the li with ma ...

Issues with unresponsive links (HTML5/CSS)

As a beginner, I've encountered an issue where my previously working links are no longer functioning. Can anyone help identify what could be going wrong? Below is the HTML code: <!doctype html> <html> <head> <meta charset="UTF-8 ...