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

Is there a method to retrieve all Class elements without using a for-loop?

I am trying to retrieve all elements with a specific class using document.getElementsByClassName(); <body> <div class="circle" id="red"></div> <div class="circle" id="blue"></div> <div class="circle" id="yell ...

Preventing browsing beyond subdirectories in web file management without relying on a web server

I am currently in the process of developing a command line application that generates an index.html file containing links to other HTML files as well as links to subdirectories within the filesystem. An example of such a link is shown below: <a href=. ...

Having trouble making changes to FontAwesome CSS using jQuery?

I am currently working on an MVC project where I am using Ajax to update a specific section of a View that contains a FontAwesome 5 icon. The FontAwesome icons are set using CSS classes, so my initial assumption was that it would be simple to remove and ad ...

What is the source of the width for this expansive dropdown menu?

I can't seem to crack this puzzle. When you navigate to the following link: click here, and select 'Accordian' from the topbar menu, where exactly does the dropdown menu width originate from? Upon inspecting it, I only see a computed value w ...

Reading settings from web.config file in an HTML page

Currently working with ASP.NET 3.5 web forms. I've recently saved the path of my website in the web.config file under the key "CommonPath". I have a basic static HTML page that I would like to utilize this key on. Any suggestions on how to achieve ...

Refresh PHP Calculator Display Once Results are Shown

Currently working on a basic calculator project coded in HTML, CSS, JS, and PHP. Here is a glimpse of it: The user input retrieval is handled by JS, while the actual calculations and result display are taken care of by PHP. I am striving to clear out the ...

The database server is not appearing on the main.js page of the client

My client's main.js file contains code that is meant to display previous form entries on the webpage, but after submitting the form, no entries appear on the HTML page. My server is running on port 7777 and the root route works in Postman, as does the ...

Netlify deployment is failing to display the CSS styling on a website built with Hugo

Currently in the process of creating my own website using the "Call Me Sam" hugo theme. Everything looks great locally but once deployed on Netlify, the CSS styling seems to be completely disregarded. If you want to check out my repository: https://github ...

Applying CSS Filters can sometimes interfere with the functionality of 3D

While playing around with CSS transforms, I discovered that filters flatten the transforms just like setting transform-style: flat. var toggleFilter = function() { var div = document.getElementById("cube") if (div.className == "cube") { d ...

Errors in AMP Validator

I have recently started working with next/amp and I am encountering some issues with the amp validator in dev mode. The errors that I am struggling to understand are as follows: An error related to CSS syntax in the 'style amp-custom' tag - show ...

Tips on transmitting form information from client-side JavaScript to server-side JavaScript with Node.js

Having created an HTML page with a form, my goal is to capture user input and store it in a JSON file. However, I've run into some confusion... In the process, I utilized the express module to set up a server. My mind is juggling concepts such as AJA ...

Electron does not prioritize the @css prefers-color-scheme option

I recently completed an Electron project and decided to incorporate dark mode support into it. However, for some reason, the dark mode feature is not functioning properly. Below you will find the dark.css styling that I have included in every page: @medi ...

bootstrap thumbnail displayed without a border

I have decided to incorporate Bootstrap into my latest project: <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS ...

Are there alternative methods to retrieve the CSS properties of web elements more effectively than relying on selenium?

I have a situation in my code where I need to retrieve the CSS properties of a large number of web elements. Currently, I am using Selenium and the code looks like this: ... node = browser.find_element_by_xpath(xpath_to_node) return {k: node.v ...

Enhancing jQuery Component while making an Ajax request

When a user clicks a button, I am making an ajax call to send out multiple emails. My goal is to update a "Please wait..." div before and after the call with status updates, as well as report any errors that may occur. However, I have encountered an issue ...

Upon executing `$('div')`, an empty array is returned

My goal is to retrieve all the div classes on a page using jQuery $('div');. However, the page does not natively support jQuery, so I am loading it through external links until $ === jQuery evaluates to true. Even on Stack Overflow where jQuery ...

How can I prevent tinymce from stripping out my <link> tag?

I'm having an issue with tinymce. dd<script id="kot-id" src="***insert link here***"></script><div id="kotcalculator"></div><link rel="stylesheet" href="***insert link here***" type="text/css" media="screen" /> It seems ...

Tips for creating CSS styles for a selected input field

I seem to be stuck in a situation where my screen looks similar to the screenshot provided. There are four input elements that I would like to have bordered just like in the image, complete with a circled tick mark. I've managed to create these four i ...

What is the process for adding a download link to my canvas once I have updated the image?

Is it possible to create a download link for an image that rotates when clicked, and then allows the user to download the updated image? Any help would be appreciated.////////////////////////////////////////////////////////////////////// <!DOCTYPE ht ...

The HTML embed element is utilized to display multimedia content within a webpage, encompassing

Currently, I am working on a static website for my Computer Networks course. Students need to be able to download homework files in PDF format from the website. I have used embed tags to display the files online, but I'm facing an issue where the embe ...