Adjusting the width of a <div> element based on window size changes

So I have this Vuejs application that has three divs using CSS grid: a sidebar, a config panel, and a preview. I want to dynamically set the width of the preview div in pixels based on the current screen size and when the screen is resized.

To give a better idea, I created a gif of a website that demonstrates exactly what I am trying to achieve: https://gyazo.com/d667756474e7f4fa18e2d5d64a0dee5a

As shown in the gif, every time the screen is resized, a new class is assigned to the specific <div>, and the width of the div is updated with a new pixel value automatically.

Initially, I attempted to achieve this in the created() hook:

window.addEventListener("resize", () => {
  let preview = window.document.querySelector(".preview");
  preview.style.width = `${preview.offsetWidth}px`;
});

However, this approach did not work.

I have created a simple sample project in a CodeSandbox here.

Ideally, I am looking for a way to dynamically set the width in pixels similar to the website in the gif.

Additionally, I am curious about why and how that particular website generates a new class each time the screen is resized (I have noticed this happening a few times and I am intrigued by it).

Answer №1

If I understand your question correctly, you can achieve the desired outcome by implementing the following code snippet into your CSS file and assigning it to any class you'd like:

@media screen and (max-width: 750px) {
  .selector {
    // Insert your code here...
  }
}

When the device screen width is 750 pixels or smaller, the styles specified in the code snippet will be applied to the selected elements.

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

Monitor when users enter commas into input fields in AngularJS

My current challenge involves monitoring user input in a text field and validating the input when a comma is typed, instead of using ng-click="action()". I am looking to implement something like Comma-Typed="action()", but my attempts with ng-change and sc ...

JavaScript code for the submit button to proceed or halt the form submission

Within my JSP file, I have the following code snippet: <input type="submit" value="Transfer ULD" onclick="doSomething();" name="_eventId_transferULDTransition"/> The doSomething() function mentioned above is a JavaScript method. function doSomethi ...

When attempting to install an npm package from a local directory, I encountered a 404 Not Found error, despite the package existing in the node_modules directory

After installing an npm package from a local directory, I noticed that the package was successfully installed and is located in the node_modules directory. However, upon trying to access the package, I encountered the following error: 404 not found I a ...

Is there a way to cancel hiding on a q-dialog?

I'm currently working on integrating a confirmation modal within a dialog box that includes form input fields. The purpose is to alert the user when they try to exit without saving their changes. The modal should appear every time the user modifies th ...

Require checkboxes in AngularJS forms

Currently, I have a form that requires users to provide answers by selecting checkboxes. There are multiple checkboxes available, and AngularJS is being utilized for validation purposes. One essential validation rule is to ensure that all required fields a ...

Suggested SSR techniques for enhancing Vue 3 SPAs

I'm currently in the process of designing a single page application using Vue 3 which I plan to deploy as a Progressive Web App. Originally, I had hoped that Nuxt would offer Server Side Rendering support, but it seems that the Nuxt Composition API ma ...

Is there a way to combine multiple array objects by comparing just one distinct element?

Is there a way to combine these two arrays into one? array1 = [ { image: 'image1', title: 'title1' }, { image: 'image2', title: 'title2' }, { image: 'image3', title: 'title3' }, ]; array2 = ...

Ways to manage numerous AJAX actions within a single HTTP request?

Currently, I am utilizing jQuery to create a multipart web page containing a list of links that are updated through periodic AJAX HTTP requests. Each link on the page is triggered by a timer in JavaScript, causing it to make an HTTP request to its designat ...

The Discord.js error message popped up, stating that it was unable to access the property 'then' since it was undefined

I'm currently working on implementing a mute command for my discord bot, but I'm encountering an error that says; TypeError: Cannot read property 'then' of undefined I am unsure of what is causing this issue and would greatly apprecia ...

Utilize react-router-dom for conditional rendering based on button clicks

When the user types in "user" in the text box, they will be directed to the user page. If they type "admin", they will be redirected to the admin page. This code belongs to me. constructor(props) { super(props); this.state = { userType : 0 ...

applying a timeout for the .on(load) event

My goal is to dynamically load images using .on('load') in the script below: .on('load', function() { if (!this.complete || typeof this.naturalWidth == "undefined" || this.naturalWidth == 0) { alert('broken i ...

AngularJS: Advanced Routing for Dynamic Web Applications

Hello, I am currently exploring the possibility of implementing something similar to this code snippet using AngularJS: $routeProvider .when('/root/:controllerName/blah/:blahId/blah/:blah', { templateUrl: '/tmpl/:controllerName ...

Rendering with Node.js Express and Mongoose after saving data

I recently started diving into Node.js Express and Mongoose while constructing a basic blog platform. My focus is on establishing routes for handling simple database tasks, but I'm feeling uncertain about managing asynchronous functions and ensuring ...

What is the CSS code to modify the color of an SVG ::marker?

I am looking to change the color of an SVG li::marker based on the font color. I attempted using fill="currentColor" but it did not work as expected. Any suggestions on how to resolve this? li::marker { content: url("data:image/svg+xml,% ...

Leveraging jQuery template for JSON visualization

I've been struggling for days to understand how to render JSON data using jQuery templates with no luck. I was hoping someone could help me figure out where I'm making a mistake. The JSON data I am working with looks something like this: [{"pk" ...

Tips on integrating the createjs library into a ReactJS project

Hey there! I'm currently working on developing a canvas-based app using ReactJS, and I need to integrate the CreateJS library. As a newcomer to ReactJS, I've been struggling to figure out the best approach. I've tried two different methods - ...

Unable to transmit props through components with Vue router

Hey there, I'm currently facing an issue with passing props from my vue router. It seems like nothing is being printed and when I checked in the mounted hook, it's returning undefined. However, strangely enough, when I use console.log(this.$route ...

The CSS hover animation functions smoothly in CodeSandbox using React, but encounters issues when integrated into a Next.js project

Challenge Here, in my codesandbox project, I created a hover animation for a div element. When I implemented the code on a li element in my next.js project, the hover effect is triggered not on hover, but on click. Additionally, the element remains in the ...

Creating a unique CSS style sheet tailored specifically for Internet Explorer and Microsoft Edge – here's how!

Struggling with internet explorer acting up and causing glitches on my website? Check out my problem here. The Issue: An interactive image on my website is not functioning properly in IE & Edge. The image should expand when hovered over, but glitches ...

Submitting Multi-part forms using JQuery/Ajax and Spring Rest API

Recently, I started exploring JQuery and decided to experiment with asynchronous multipart form uploading. The form includes various data fields along with a file type. On the server side (using Spring), I have set up the code as follows: @RequestMapping ...