Is there a way to avoid a child div overflowing its parent div when adjusting the CSS zoom property?

Is there a way to avoid overflow when changing CSS property using the zoom function?

I've tried using overflow: scroll but it's not preventing overflow.

let zoomInElem = document.getElementById('zoomIn')
let zoomOutElem = document.getElementById('zoomOut')
let contentElement = document.getElementById('content')
zoomInElem.addEventListener('click', function () {
    console.log('zoomIn')
    contentElement.style.zoom = '200%'
})
zoomOutElem.addEventListener('click', function () {
    console.log('zoomOut')
    contentElement.style.zoom = '100%'
})
#main {
    width: 640px;
    height: 360px;
    border: solid;
}

#content {
    border: .1rem solid red;
    overflow: scroll;
}

button {
    margin: 1rem;
}
<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap demo</title>
    <style>

    </style>
</head>

<body>
    <div>
        <button id="zoomIn">ZoomIn</button>
        <button id="zoomOut">ZoomOut</button>
    </div>
    <div id="main">
        <div id="content">
            <h1>Hi</h1>
            <h2>Hi</h2>
            <h3>Hi</h3>
            <h4>Hi</h4>
        </div>
    </div>

    <script>

    </script>
</body>

</html>

How can I prevent overflow on changing CSS zoom setting by clicking the ZoomIn button?

Answer №1

One tip is to add overflow: scroll; to the outer div

#main { position: relative;overflow: scroll;}

Answer №2

You have the option to specify the width and height of #content, or adjust the overflow of #main to scroll.

<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap demo</title>
    <style>
        #main {
            width: 640px;
            height: 360px;
            border: solid;
            overflow: scroll;
        }

        #content {
            /*width: 100%;
            height: 100%; */
            border: .1rem solid red;
            /* overflow: scroll; */
        }

        button {
            margin: 1rem;
        }
    </stlye>
</head>

<body>
    <div>
        <button id="zoomIn">Zoom In</button>
        <button id="zoomOut">Zoom Out</button>
    </div>
    <div id="main">
        <div id="content">
            <h1>Hi</h1>
            <h2>Hi</h2>
            <h3>Hi</h3>
            <h4>Hi</h4>
        </div>
    </div>

    <script>
        let zoomInElem = document.getElementById('zoomIn')
        let zoomOutElem = document.getElementById('zoomOut')
        let contentElement = document.getElementById('content')
        zoomInElem.addEventListener('click', function () {
            contentElement.style.zoom = '200%'
        })
        zoomOutElem.addEventListener('click', function () {
            contentElement.style.zoom = '100%'
        })
    </script>
</body>

</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

The "this" keyword is not defined in React Native

When I tried to use this.props.navigation.navigate(), I encountered an error message stating that it was undefined. After researching on Stack Overflow, I discovered that I needed to declare a constructor like this: constructor(props) { super(props); ...

After refreshing the page, Google Chrome finally displays the CSS styles correctly

I'm currently working on a JavaScript script to showcase images on a webpage. These images are loaded using an AJAX request and a CSS style is directly applied using jQuery. The script functions correctly on Firefox, Opera, and IE, but Google Chrome i ...

Ensure that the link's color remains constant and remove any styling of text-decoration

Attempting to create a customized header. My goal is to change the color and remove text decoration in the navbar. Below is my code snippet: ReactJS: import React from 'react'; import './Header.css'; function Header() { return ( ...

Is it possible to make API calls within the componentWillMount lifecycle method in React?

I've been immersed in the world of React for the past year. The standard practice within my team is to make an API call in componentDidMount, fetch the data, and then setState after receiving the data. This ensures that the component has fully mounted ...

How can I dynamically modify the class name of a specific element generated during an iteration in React when another element is clicked?

My goal is to dynamically add a class to a specific element within a table row when a user clicks a remove button. The desired behavior is to disable the entire row by adding a "removed" class to the containing row div. The challenge is that there are mult ...

Is a VPS necessary for hosting an Ajax crawlable website, or can a shared server suffice?

I have a website hosted on a 1&1 shared server, and I'm looking to make my ajax-loaded content crawlable by Google bots. The site is set up for "hash-bang," but now I'm facing challenges with the escaped_fragment issue. I am considering insta ...

Retrieve the date from 7 days ago and display it in the format "2015-06-23" using JQuery/JavaScript

Looking to retrieve last week's date in the following format: "2015-06-23", as opposed to "2015-06-16". JavaScript: t = new Date(); // Tue Jun 23 2015 21:00:47 GMT-0700 (PDT) t.toISOString(); // "2015-06-24T04:00:47.955Z" The current date format i ...

Creating a hierarchical JSON structure to populate a Handlebars template (HTML) for an iterative component, allowing for the display of three levels of interconnected

Currently, I am working on developing a mega menu component that involves three levels of content. Level 1 (L1): This level is displayed horizontally in a traditional navbar. When hovered over, it expands to reveal the mega menu. Level 2 (L2): These item ...

Shut down a pop-up overlay

I've been facing a challenge in implementing greasemonkey to automatically close a modal when the "x" button is clicked. Allow me to share with you the code snippet for the webpage: <div class="modal-header"> <button type="button" class="clo ...

Guide to successfully verifying the correct value in ReactJS unit tests

I recently designed a reactjs component with an onClick event that modifies text within the component. Here is the code snippet for the component: import React, {Component} from 'react' export default class SimpleComponent extends Component{ c ...

Showing the computation outcome on the identical page

I have implemented a table within my PHP code. In the second column of this table, it typically displays "---". However, once I click the submit button, the same page should now display the calculated result. Here is an example: <table> <tr>& ...

Using CSS with absolute positioning and dynamic height

There are 4 consecutive <div> tags in absolute position, aligned using top and left. The third div tag has dynamic content, causing its height to adjust based on the text within it. However, since the top and left properties are set for all divs, th ...

Tips for extracting values from a JSON object

I have been attempting to retrieve the value from an array without success. Here is the current array: [{0: {value: 2, label: "ARKANSAS"}}] When I try to use JSON.parse(object), I encounter the error VM20617: 1 Uncaught SyntaxError: Unexpected ...

Every time I adjust the browser height on my landing page, the elements start to overlap each other

Something strange is happening - as I shorten the height of the browser, elements on the page start to overlap. I've tried using Firebug to troubleshoot, but haven't had any luck so far. Maybe someone here can help me out! EDIT: It seems that e ...

Modifying the color of drawings on a Javascript canvas

I am currently working on developing a drawing board using HTML and JavaScript (Node.js on the server side). One challenge I'm facing is implementing a color picker that allows users to change the paint color dynamically. While I could hard code the c ...

Comparing jQuery Mobile and Sencha Touch: Which is the Best

I'm in the process of developing a mobile or tablet version for my current web application. It will be compatible with devices like the iPad and Xoom (Android). I'm stuck between deciding whether to use jQuery Mobile or Sencha Touch as the JS li ...

Generating a torus graph in three.js can be a visually stunning and

I'm attempting to design a unique torus graph showcasing different colored segments with varying widths based on data size, essentially a more visually appealing version of a pie chart. Would it be feasible to achieve this using torus geometry, or wo ...

Before displaying the output, Flask first shows the HTML heading

I have developed a user input form that displays a graph after selection. The title of the graph result is 'Results,' however, it appears before the user makes a selection. Check out the screenshot below: https://i.sstatic.net/1WkZ4.png No sele ...

Creating a cohesive design by ensuring buttons match the appearance of other elements, and vice versa

During the process of building a website, I encountered a challenge in making buttons work seamlessly with other elements to ensure uniformity in appearance. This issue often arises in menus, where some buttons are functional while others are simply li ...

What's the best way to switch between colors in a Vue list?

I have a custom tree-view component that I'm working on: <template> <li class="main__li list" :style="{'margin-left': `${depth * 20}px` ,'background-color': `${col}`}" @click="toggle(e); getEl( ...