Creating a CSS sidebar that adjusts its margin as you zoom in and out

I am currently working on designing a side bar that will feature buttons and other elements with text on the opposite side. Initially, when you open the page, the sidebar occupies 39% of the left side, which is functioning correctly. However, when zooming in, the sidebar should decrease in width, and when zooming out, it should increase in width. The issue arises when zooming in from the original browser position as the sidebar ends up extending too much to the right. Conversely, when zooming out, the sidebar shifts excessively to the left.

In essence, I want the sidebar to maintain its initial appearance as shown here: (View how it should always look)

Regardless of whether you are zooming in or out, it should preserve this original proportion. Nevertheless, when you zoom out, it appears like this: (View when zoomed out)

And when you zoom in, it looks like this: (View when zoomed in)

To address this, I require a solution involving code that adjusts the margin size by increasing it when zooming out and decreasing it when zooming in.

I apologize if this seems trivial. Thank you for your assistance!

Answer №1

After some exploration, I have managed to come up with a solution for the issue at hand:

<html>
    <style>
* { margin:0; padding:0; box-sizing:border-box; }
#container {
    margin: 0;
    overflow: hidden;
    background: #FFFFFF;
    padding:0;
}
#left {
    float: left;
    width: 51%;
    position: relative;
    left: -295px;
    padding:0;
}
#left2 {
    height: 240px;
    margin: 0;
    background: #F4F4F4;
    padding:0;
}

</style>

<div id="container">
    <div id="left"><div id="left2"></div></div>
</div>
</html>

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

Is there a more efficient method for replacing all attributes on cloned elements?

While this code does the job, I can't help but feel like it's a bit outdated. I'm not very experienced with JS/JQuery and only use them when necessary. My approach involves cloning an element and then replacing all of its attributes with on ...

Turn off the ability to click on images, but allow users to access the right

https://i.stack.imgur.com/jriaP.png Example image sourced from reddit.com Illustration represents the desired effect using CSS and possibly JS. In essence: I aim to make an image on a website unclickable to its imageURL There should be a context menu ...

Is it possible that certain data (such as images) may not be accessible for web scraping?

As I attempt to extract the image URL from a website using Python, Selenium, and Firefox, it appears that I have encountered a situation where it is proving difficult. Despite there being no <​img> element present, the image is still visible on the ...

How to create a captivating image effect using CSS on hover

I am attempting to create an animated image on hover. The desired outcome is similar to the one showcased here: Check it out (scroll to view the image "Our Team") Essentially, I want a background where I can showcase information such as names and links ju ...

Shuffling HTML content into sets of three rows

We have been working on randomizing a series of listings, arranging them in rows of 3 items each. Our goal is to display the entire list in a random order while breaking the line after every 3 entries. Although we have successfully implemented the layout u ...

Having trouble with a jQuery selector that is supposed to target an element with both an id and

A scenario where a specific event-handling function is in place: jQuery(document).on('click', '#button .submitb', function(e){alert();}); Despite having jQuery included in the HTML document, clicking on <div id="button" class="subm ...

Implementing a breadcrumb-style back button using jQuery

The menu is currently displayed on the left side without any issues. My next task involves implementing a back button functionality. When a user clicks from Demo1 to Demo 1.1, additional options like Demo 1.1.1 and Demo 1.1.2 will appear. To facilitate nav ...

Adding the </tr> element to a table does not seem to be registering

I encountered an issue with my table that was prepopulated with JSON data while attempting to add collapsibles between specific rows. Strangely, I found myself unable to properly insert a closing tag to start a new row. The insertion kept happening in an u ...

Why is it that when I store a DOM element reference in a JavaScript Array, I cannot reuse that stored reference to add an event listener

I have a little confusion and I hope someone can help me out. I am facing an issue with dynamically created buttons, where each button has a unique id. To keep track of these buttons in a well-organized manner, I store their references using a simple two-d ...

Modify styling of elements dynamically according to URL changes repeatedly without refreshing the page (requires AJAX functionality)

In my effort to introduce conditionality into the "Search Filters Section" of a Wordpress theme, I am facing some challenges due to the unconventional selectors I have to work with. Within this section, there is a dropdown menu featuring various options ( ...

Utilize the tab functionality within AngularJS

By default, HTML disables two tabs "<tab heading='Test Case Details' active='tabs[1].active' disable='true'>"+ "<tab heading='Test Case Past Results' active='tabs[2].active' disable='true&apo ...

What is the best way to remove the background from a button that uses -moz

After adding the select component to my page, I noticed a problem with the select button in Firefox. In Chrome, the arrow down button looks normal, like this, but in Firefox it appears differently, as shown in this image. How can I remove the background ...

The change in image is not being implemented

When hovering over a list item, I want to display the corresponding image. While I have this functionality working, the issue lies in the CSS transition not functioning as expected. Should I incorporate a CSS selector or can the transition be added through ...

Tips for clearing the blue color box from inputs and buttons in bootstrap after they are clicked

How can I get rid of the blue color box that appears when clicking on form elements such as inputs, select, textarea, and buttons? I have tried removing it using CSS but it still persists and is quite annoying. Is there a way to remove this blue box using ...

Use $.ajax to display the menu by capturing an array of elements {0}

Whenever I click on one of the DIV elements, a menu pops out on the side displaying more information about the clicked element. I can determine which element I'm clicking on, but I'm struggling to pass that information from jQuery to the PHP pag ...

The backdrop of the Bootstrap modal popup refuses to disappear

Currently, I am working on building an app using Bootstrap and AngularJS. I have integrated a Bootstrap modal popup for the content while other pages are loaded into ng-view. The issue I am facing is that when the content displayed in ng-view contains the ...

Contact form in Bootstrap 5 lacks responsiveness

I am currently exploring Bootstrap 5 for my website design project, and one of the features I have implemented is a contact form. While the white arrow that separates the two sections blends in seamlessly on desktop and laptop screens, it doesn't quit ...

Hovering over an image causes the text to be displayed even when z-Index is

I am facing an issue with my code where, on hovering over the H2 element, an image is supposed to be revealed. However, even though I have set the z-index of the text higher than that of the image, the text still moves down when the image appears. I want ...

JavaScript onclick event triggers only the second audio to play

Does anyone have experience with adding sound effects to a website? I'm attempting to have a positive sound play when the correct image is clicked and a negative sound play when the wrong image is clicked. However, regardless of which image I click on ...

The setTimeout function executes immediately without any delay

In my code, an html button triggers a jQuery function (let's call it funcOne) which then calls another function (funcTwo) recursively to modify CSS in the DOM. The funcTwo function involves setTimeout() calls to create a blinking effect by delaying th ...