Maintaining the relative position of an SVG while scaling within a div element

I am in the process of incorporating my custom SVG knob icons into an electron application using flexbox. The challenge lies in keeping them centered within the rectangle icon regardless of browser size, as shown here. Ideally, the icons should adjust their positioning and proportions relative to the rectangle as the browser dimensions change.

Currently, when the browser window is resized, the icons tend to shift beyond or within the box, creating a misalignment issue as illustrated in this example.

As a solution, I have set a fixed aspect ratio for the app's window.

Below is the relevant HTML code snippet:

<body>
  <div id="container" class="app-graphics">
    <img src="/X/Electron/smoothiebro1/img/Rectangle 2.svg" id="blenderOutline">
    <div id = "barDiv"> 
      <img 
        src="/X/Electron/smoothiebro1/img/bottomBar.svg" 
        id="bottomBar" 
        class="container" 
        width="91%"
      > 
    </div>
    <div class="flexContainerKnobs">
      <ul class="flexContainerKnobs">
        <img src="img/Knob1.svg" id="knob1" class="knob" width="13.32%">
        <img src="img/Knob1.svg" id="knob2" class="knob" width="13.32%">
        <img src="img/Knob1.svg" id="knob3" class="knob" width="13.32%">
        <img src="img/Knob1.svg" id="knob4" class="knob" width="13.32%">
      </ul>
    </div>
  </div>
</body>

Here is the CSS involved:

html, body {
  margin: 0;
  padding: 0;
  overflow: hidden
}

#blenderOutline {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  border: none;
  padding: 0;
}

#bottomBar {
  position: absolute;
  border: none;
  margin: 0 auto;
  bottom: 3%;
  left: 5%;
}

#barDiv {
  text-align: center;
}

.flexContainerKnobs{
  width: 100%;
  padding: 0;
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  margin-top: 108%;
}

.knob{
margin: 0 auto;
}

ul {
  position: absolute;
  top: 8px;
  left: 16px;
}

You can view my SVG file here, since the code was too lengthy to include. One important detail to note is the SVG's viewbox which is defined as "0 0 72 66.5". In the code snippets provided above, the image widths are adjusted to around 13% based on the ratio between the SVG width and the background container.

Answer №1

To enhance your markup, I suggest making the following changes (omit the <ul/>):

<div class="flexContainerKnobs">
  <img src="img/Knob1.svg" id="knob1" class="knob" width="13.32%">
  <img src="img/Knob1.svg" id="knob2" class="knob" width="13.32%">
  <img src="img/Knob1.svg" id="knob3" class="knob" width="13.32%">
  <img src="img/Knob1.svg" id="knob4" class="knob" width="13.32%">
</div>

Additionally, update the CSS for that specific div as follows:

.flexContainerKnobs{
  width: 100%;
  margin-top: 108%;
  display: flex;
  align-items: center;
  justify-content: center;
}

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

Functionality Issue: Submit Button Not Working on Designed Form Select

Through dedication and hard work, I managed to create a customized form with images that display correctly in Firefox, Chrome, and Internet Explorer's compatibility mode. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...

Having a challenge with aligning a form in a view, as neither bootstrap nor plain CSS seem to be helping with centering it correctly

What techniques can I use to center this form properly in both bootstrap and plain CSS? So far, I have been able to center some parts of it, but when I try to center others, they shrink and create a mess. When I use bootstrap to center the elements, they b ...

IE11 is unable to display the background image

I am attempting to create a process roadmap by using background images on list items. The background-image on the list item is not showing up in IE11 (also in all versions of IE10, 9, etc). CSS CODE body{padding: 50px 0;} .status-line { width: 80%; ...

Prevent the propagation of a particular CSS class's inheritance

I need to make modifications to an existing HTML5 app that features two unique themes. Here's an example of the current structure: <body class="theme1 theme2"> <div id="div1">I'm satisfied with both themes</div> <di ...

I have a `null` input value. Is there a way to compute this value in relation to a date

When the input is null, what can be done to calculate the value with date? https://i.stack.imgur.com/DmFsJ.png /* // ======================================================== * * * * * How To Calculate Input Value With Date When Input Is Null * * */ // ...

The issue of Ajax failing to execute an HTTP POST request in an HTML environment

I am creating a sample HTML code that involves making HTTP post requests using an Ajax function. The task at hand is to execute 2 HTTP POST requests and 1 GET request sequentially based on the correct response received. POST: URL: http://localhost:8082 ...

Vue: Implement out-in transition where the incoming element appears before the outgoing element has completely disappeared

Check out my code on Codepen: here In this scenario, I have set up two div elements: Block 1 and Block 2. The functionality I am looking for is when a button is clicked, Block 1 smoothly translates to the left until it goes out of view. Once that happens ...

Connect and interact with others through the Share Dialogues feature in the

Update - Edit I'm just starting to explore the concept of share dialogues and I want to integrate it into my website. On my site, there is a reaction timer game that shows the user's reaction time in seconds in a modal popup. I want users to be ...

Stop the external animation on the SVG

One way to stop an SVG animation is by utilizing document.getElementById("mysvg").pauseAnimations(). Nonetheless, this method appears to only be effective for inline SVGs. Is there a technique available to halt an SVG animation that has been imported usin ...

Expansive Carousel Feature with Ng Bootstrap

In my Angular 5 application, I am utilizing the Carousel component from "@ng-bootstrap/ng-bootstrap": "^1.1.2". I am trying to display pictures in full screen but when I press F11, the image appears like this. I am unsure of which CSS properties to apply ...

Is it possible to protect passwords internally via URL and AJAX?

During my time at a previous company, we had an internal website that required a password to be entered at the end of the URL in order to view it. I suspect this was done using AJAX, but I am unsure. Even if AJAX was used, I do not know how to code it myse ...

Display a preview of a React component

Currently facing a challenge where I need to create a form for users to adjust the title, background color, button background, text color, and other settings of a component. I want to provide a live preview of the component as the user makes changes. I en ...

Is it possible for HTML/CSS to automatically adjust content size to fit or fill a container?

I'm interested in finding a CSS-only technique that can ensure content within a container scales to fit, regardless of whether it's text or images. Take a look at the example below: .container { display: inline-block; width: 2in; hei ...

What is the proper way to add my image to CSS?

I'm encountering an issue while trying to insert my picture - I keep receiving a 404 error even though I believe my path is correct. Here are my files: main-menu phone.png _menu.scss Within my _menu.scss file, the following code is p ...

Selecting the correct data type for react-icons

I'm currently working on designing a navigation bar with a customized type to utilize the map() function. My goal is to display an icon for each entity, so that the icon appears in front of the text within the navbar. However, I am encountering diffic ...

The expansion animation for the Nextjs/React accordion box did not work as expected when utilizing tailwindcss

I am currently working on creating an animation for a collapsible box (accordion). My goal is to have the child component initially hidden with display:none. When I hover over the parent component, the child should be revealed and the dimensions of the pa ...

Tips for updating the class of a button upon clicking it

I have a dilemma with my two buttons - one is green and the other has no background. I want them to change appearance when clicked, from green to one with a dashed border-bottom style. Below is the HTML code for these buttons: <div class="btns"> ...

Step-by-step guide to applying a CSS class to a grid cell using Syncfusion

Is there a way to define a specific style for a cell using the Syncfusion grid grouping control? I attempted the code below, but it doesn't seem to be effective in my webform. tdescriptor.Columns[0].Appearance.AnyRecordFieldCell.CssClass = "hideColum ...

Does the padding and margin of an element alter when the position is set to relative?

By utilizing relative positioning, an element can be moved in relation to its original position in normal flow. - Citation from the book "HTML&CSS: design and build websites" by John Duckett If an element has a relative position property, it opens up ...

Adjusting the left and right margins within a Bootstrap row nested inside a card component

I am struggling to adjust the left and right margins for a Bootstrap row inside a card. Below is my CSS code along with some screenshots for reference. <div class="container" style="margin-bottom: -20px;"> <div class=" ...