Positioning and resizing elements based on varying screen sizes and levels of zoom

I recently designed a basic webpage using HTML with two main elements - a chat window for user interaction and a chart.js plot. Here is a snippet of the HTML code:

<!DOCTYPE html>
<html lang="en">
    
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible">
  <link rel="shortcut icon" href="#" />
  <link rel="stylesheet" href="{{ url_for('static', filename='styles/style.css') }}">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
  ...
</head>

<body>
  <section class="msger">
    <header class="msger-header">
       <div class="msger-header-title">
         <!-- Buttons here -->
       </div>
    </header>
    <main class="msger-chat">
        <div class="container" id="start-btn">
          <button class="start-btn" type="button" onClick="start()">
            Start Conversation
          </button>
        </div>
        <div class="msg left-msg">
        </div>
    </main>

    <form class="msger-inputarea" id="msger-input">
       <input type="text" class="msger-input" id="textInput" placeholder="Enter your message..." style="display:none">
       <button type="submit" class="msger-send-btn" style="display:none" id="sendInput">Send</button>
     </form>

  </section>

  <div style="height: 50vh; width: 50%;">
        <canvas id="myChart"></canvas>
  </div>
</body>

</html>

Below is a snippet of the accompanying CSS styling:

:root {
    /* Variables */
}

/* Styles go here */

Currently, I'm encountering three challenges with this setup:

  • The placement of the chart.js plot alongside the chat window appears fine on larger screens but becomes problematic on smaller devices like mobile phones. Is it feasible to adjust the positioning so that on larger screens, the plot is on the right of the chat window, and on smaller screens, it appears below?

  • The chart.js plot seems excessively wide in relation to the chat window. Is there a way to modify the widths so that the chat window occupies 60% of the screen width while the plot takes up 40%, adjusting its height accordingly?

  • When zooming out the webpage, the chat window shrinks while the chart.js plot expands. Is there a solution to maintain consistent widths for both elements when zooming in or out?

Answer №1

To tackle the three issues you mentioned:

If you want to place the chart.js plot below the chat window on smaller screens, one way is to utilize media queries in CSS. You can specify different styles based on screen sizes. Here's an example of how you can adjust your CSS:

@media screen and (max-width: 768px) {
  .msger {
    flex-direction: column;
  }

  .msger-chat {
    order: 2;
  }

  .msger-inputarea {
    order: 3;
  }

  .chart-container {
    order: 1;
    width: 100%;
    height: 50vh;
  }
}

In your HTML code, wrap the chart.js plot <div> with a container element and assign it a class like chart-container.

<div class="chart-container">
  <canvas id="myChart"></canvas>
</div>

This setup will ensure that the chart.js plot appears beneath the chat window on screens with a maximum width of 768 pixels.

To make the chat window take up 60% of the width and the chart.js plot occupy 40%, adjust the CSS styles for the .msger and .chart-container classes. Update the width property as shown below:

.msger {
  width: 60%;
}

.chart-container {
  width: 40%;
}

These changes allocate 60% of the available space to the chat window and 40% to the chart.js plot.

To maintain consistent widths for the chat window and chart.js plot when zooming, implement the min-width property to set a minimum width for the .msger and .chart-container classes. Add the following CSS rules:

.msger {
  min-width: 500px;
}

.chart-container {
  min-width: 300px;
}

With these adjustments, both the chat window and chart.js plot will have a minimum width even after zooming out.

I hope these suggestions are helpful!

UPDATE:

You could also explore an alternative method using CSS flexbox along with the flex-wrap property.

Here's a revised snippet of CSS code:

@media screen and (max-width: 768px) {
  .msger {
    flex-wrap: wrap;
  }

  .msger-chat {
    order: 2;
    width: 100%;
  }

  .msger-inputarea {
    order: 3;
    width: 100%;
  }

  .chart-container {
    order: 1;
    width: 100%;
    height: 50vh;
  }
}

By setting flex-wrap: wrap on the .msger class, the chat window and input area will wrap if there isn't enough horizontal space.

Ensure your HTML structure resembles the following:

<body>
  <section class="msger">
    <header class="msger-header">
      <!-- Header content -->
    </header>
    <div class="msger-chat">
      <!-- Chat window content -->
    </div>

    <form class="msger-inputarea" id="msger-input">
      <!-- Input area content -->
    </form>
  </section>

  <div class="chart-container">
    <!-- Chart.js plot -->
    <canvas id="myChart"></canvas>
  </div>
</body>

With this update, the plot should be positioned under the chat window on smaller screens while retaining its placement next to the chat window on larger screens such as mobile devices.

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

Loading Kendo JS on the client side proves to be rather time-consuming

Working on my project, I have encountered a problem with the kendo ui scheduler where the downloading of the kendo js and css files on the client side is causing slowness on our website. To address this issue, we are attempting to download the kendo js and ...

Show full-screen images on click using Ionic framework

Currently, I am working on developing a mobile app using the Ionic framework. I have created a layout that resembles the one shown in this Card Layout. My question is: How can I make the card image display in full screen when clicked by the user and retur ...

What is the best way to include the border-radius CSS property to a dropdown element in Ant Design?

Adding the border-radius CSS property to my dropdown component from ant design has been a bit challenging for me. I attempted to modify the antd-css file and include a style object directly into the component, but unfortunately, I did not achieve the des ...

Decoding Labels for Content

Recently, I've been working on a VBA script that scrapes company reporting dates from the London Stock Exchange website. However, I encountered an issue as they have made changes to their web query interface. So, I've been trying to tweak the scr ...

Performing JavaScript functions after fetching an XSLT page via AJAX

Is there a way to trigger JavaScript at a particular time without relying on setInterval() or onClick()? The issue I'm facing is that when I click to load an XSLT page using Ajax, the JavaScript within it does not execute even though it's writt ...

animation for closing the hamburger menu

Having trouble creating a hamburger menu animation. I've set up two divs - one for the basic horizontal lines and one for the X icon. Using jQuery, I can hide and show them (clicking on the lines hides them and shows the X). But now I want to animate ...

Can you target an 'input' field that is within a component conditionally rendered using 'v-if'?

Imagine this vue.js template code: <div v-if="y===0"> <input ref="textbox">{{textbox}}</input> </div> In the scenario where y is data retrieved asynchronously, Is there a way to direct focus to the 'input' element onc ...

Regular expression for matching a CSS definition that includes a specific name and value

Looking to match all CSS definitions with the name "test" and color set to gray? Here are a few examples: a.test { color: grey; } or .test { color: grey; }, as well as a.test:after, a.test { font-size: 10px; color: gray; } It's important to consider ...

What is the best way to determine the height of a DIV element set to "auto"?

When setting a fixed height on a div using jQuery, such as $('div').height(200);, the value of $('div').height() will always be 200. This remains true even if the content within the div exceeds that height and overflow is hidden. Is th ...

Ensuring the modal is stored in one central location (Bootstrap)

I'm currently working on a website project using bootstrap, and I need to incorporate the same modal across all pages. Right now, I find myself duplicating the entire code in each HTML file to make it accessible on every page. However, this method is ...

The sticky element should only be active when the visible section is overflowing vertically. There should be no scrollbar if the height is minimal

I'm currently working on a Whatsapp-style navigation bar and facing an issue with the sticky navbar functionality. https://i.stack.imgur.com/Cy3lA.gif Please refer to the details below for more information. To resolve this issue, I have included ...

Tips for creating consistent spacing between elements using Tailwind CSS version 3

I am currently utilizing Tailwind CSS in conjunction with next.js for my project. I have a total of 8 images, each varying in size, and my goal is to display 4 images per row on medium and large devices, while displaying 2 images per row on small devices. ...

Tips on implementing CSS to the subpar class in Vuejs

I am working on an HTML file that operates with button in Vue.js. The v-bind:class can be utilized for a single tag as shown below. It disappears based on the boolean value of bool data. <h3 v-bind:class="{active: bool}">{{counter.document}}&l ...

How to obtain the height of the parent screen using an iframe

Imagine a scenario where a div contains an image that is set to perfectly fit the height of the screen. This setup works flawlessly, with one exception - when placed within an iframe, the height of the div adjusts to match the content's height rather ...

Utilize jQuery function within an HTML form

I am trying to integrate my jQuery function with an HTML tag for my login form that connects to an Azure database. I want a modal pop-up to appear when the client presses the Login button, displaying a specific message based on whether the user is in the d ...

What could be the reason for the extra tags being returned by YQL?

Currently, I am executing a query in the YQL console with the following data: select * from html where url='http://www.motorni-masla.net/index.php?main_page=product_oil_info&cPath=140&products_id=294&zenid=c8281021bbfed454176247900b3b9d4a ...

Attempting to use jQuery on click to set the background image of a div to a base64 encoded data image

Check out what I'm working on here The first div contains html with data:image;base64 <div id="large_photo_null" style="width:50px; height:50px; background-image: url( data:image;base64,R0lGODlhR.. );" > </div> When using html, the ba ...

Get rid of the drop-down shadow effect

Currently working on a website project for a client and looking to remove the blue "shadow" effect from the dropdown menus. I believe the code that needs editing is as shown below: .main_nav ul.sub-menu { position: absolute; top: 65px; width: ...

Is there a common method for generating complex identifiers to be used as the HTML element's id, class, or name attributes

Is there a recommended method for "encoding" complex identifiers such as {Source:"ARCH.1", Code: "456-789.456 A+", SubNumber:##2} to be used in HTML elements' id, class, and name attributes? I could come up with something myself, but perhaps there is ...

Strange image movement occurs when utilizing jQuery slideDown and slideUp

My HTML structure is as follows: <div class ='profileName'> <hr> <span class='name'>Content</span> </div> <div class ='profile'> <div class='floatRightImg padded'> < ...