Creating responsive HTML page text and table with the use of JavaScript

Trying to create a responsive webpage has been a bit challenging. I experimented with height,style.height, style.OffsetHeight, etc. but still struggling to dynamically make a square table.

Although the code below changes the height, the table's width and height do not align.

var width = table.offsetWidth;
table.setAttribute("style","height:"+ width + "px;");
  • Should the logic be applied to cells or the table itself?
  • How can text (font-size) and other elements be made responsive?
  • Is there a way to calculate 60% of the screen height so the minimum of width and height can be set as the table parameters?

Design Pattern : Design

Code : jsfiddle

Thank you in advance.

Answer №1

1. When applying logic to a table, the following code can be used for dynamic resizing:

window.addEventListener('resize', handlersize);

function handlersize(){
  width = table.offsetWidth;
  table.setAttribute("style", "height:" + width + "px;")
}
  1. To adjust the font size, utilize the CSS style font-size: 3vw; or another appropriate value.

3. To obtain 60% of the screen height:

var element = document.getElementsByTagName("BODY")[0];
height = element.offsetHeight;
height = height * 0.6;

Answer №2

Take a look at this JSFiddle example

To make sure your tables resize properly when the window size changes, you'll need to include an resize event listener on the window element. When the window is resized, the function handlersize will be triggered, updating the height of your tables accordingly.

window.addEventListener('resize', handlersize);

function handlersize() {
  width = table.offsetWidth;
  table.setAttribute("style", "height:" + width + "px;");
}

Answer №3

Consider using a separate div-based HTML structure for mobile views rather than tables, as divs are more responsive and can be styled using media queries.

Check out this example: https://codepen.io/amwill04/pen/QNPpqx

Here's an example of the code:

<div class="table" id="results">
  <!-- Table content here -->
</div>

CSS :

.table {
    display: table;
    text-align: center;
    width: 60%;
    margin: 10% auto 0;
    border-collapse: separate;
    font-family: 'Roboto', sans-serif;
    font-weight: 400;
  }

  /* More CSS styles here */

  @media screen and (max-width: 900px) {
    /* Media query CSS styles */
  }

  @media screen and (max-width: 650px) {
    /* Additional media query CSS styles for smaller screens */
  }

By implementing a div-based structure with appropriate CSS styling, you can ensure better responsiveness on various devices. Feel free to explore the provided example for a clearer understanding.

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

Slider with Dual Images: A Visual Comparison

I'm currently working on a webpage that features before and after images using a slider based on mouse movement to display both pictures. I am trying to incorporate multiple sliders on the same page but have been facing difficulties in getting them to ...

Tips for implementing FontAwesome in Nuxt3

I'm facing some issues trying to implement FontAwesome in my NuxtJS project, and for some unknown reasons, it's not working as expected. Let's take a look at my package.json: { "private": true, "scripts": { " ...

Challenges encountered with the "load" event handler when creating a Firefox Extension

I am currently troubleshooting a user interaction issue with my Firefox extension. The tasks that my extension needs to complete include: Checking certain structures on the currently viewed browser tab Making backend server calls Opening dialogs Redirect ...

Managing the ERR_NAME_NOT_RESOLVED issue

Currently, I am facing a task related to the health check endpoint where I need to receive a response from the backend or encounter a net::ERR_NAME_NOT_RESOLVED error if we are outside of a specific network. When attempting to send a request to my endpoin ...

Using HapiJs in Node.js to communicate data back and forth with the client

I have client.js and server.js files that are used to send data to my server using ajax. I am able to successfully send the searched username, but on the server side, the domain is being received as undefined. I'm unsure if the issue lies on the clien ...

navigating through an array with a loop (for)

I created a basic phone book program where users can input a name and it will search an array of objects for the corresponding phone number. If the name is found, it will display the name and phone number. However, I am facing an issue where even though ...

Is it possible for Vue Router to remember the scroll position on a route and return to the same position when navigating back?

My Vue Router is not saving the scroll position and always loads at the top of the page (0, 0). Any suggestions on what could be causing this issue? Here is my current router code setup: const scrollBehavior = (to, from, savedPosition) => { if (saved ...

Determining the current element's index as I scroll - using Jquery

If I have a series of divs with the class name 'class' that are fixed height and enable auto-scrolling, how can I determine the index of the current 'class' div I am scrolling in? Here is an example: var currentIndex = -1; $(window).sc ...

The functionality of the `next-auth` signOut button click does not seem to accept a variable as input, although

The Nav.jsx component code provided is working as intended. In this implementation, clicking the 'Sign Out' button will redirect the application to http://localhost:3000. 'use client' import { signOut } from 'next-auth/react&apos ...

Shorten a string once a particular word is found in either Smarty or JavaScript/jQuery

I'm currently working on a website and encountering a minor bug related to the addition of content at the end of some strings. For instance: style="background-image:url({$sub.image});" displays style="background-image:url(http://blablalba.fr/phoenix ...

Ways to ensure the footer sticks to the bottom of the page when there is minimal content

Using Bootstrap 3.3.7 I am trying to ensure that my footer always stays at the bottom of the browser window. I prefer not to use a "sticky footer" as I don't want it to cover any part of my content. Currently, when there is enough content on the pa ...

Develop a seamless user experience by creating dynamic forms using Jquery, PHP, and

I have been attempting to create a simple contact form using jQuery, AJAX, and PHP without refreshing the page. Everything seems to be working smoothly except for event.preventDefault(); Here are my files: Contact_engine.php <?php $name = $_POST ...

How can I access the fourth div element within the initial row of a multirow div container?

My current HTML structure is causing me some confusion. I am trying to create a CSS selector for the element with the text "DESIRED ELEMENT": <div class="TopDiv"> <div class="container"> <div class="row"> ...

Koajs functions yield their return values

When working with expressjs, I typically utilize asynchronous functions as shown below: function foo(callback) { var bar = {a: 1, b: 2}; callback(null, bar); } foo(function(err, result) { // result is {a: 1, b: 2} }); In Koajs, I use the yield wit ...

Adjusting the display property using the CSS plus symbol operator

I am currently in the process of designing a dropdown menu using CSS. The focus is on utilizing the following code snippet: #submenu_div { display: none; } #li_id:hover + #submenu_div { display: block; } UPDATE: Here is the corrected HTML structure ...

Using both PHP and jQuery to add a class upon changing the URL

I am struggling to implement sorting functionality on my webpage, as the active class is not being added to the selected sorting option... Check out my code snippet below: <ul class="nav nav-tabs"> <li class="menusel active" name="hlavni" on ...

Modifying the Ext.js TreePanel checkbox component

I've implemented a basic tree panel checkbox example using Ext.js as shown below: Ext.onReady(function(){ var tree = new Ext.tree.TreePanel({ renderTo:'tree-div', title: 'My Task List', height: 300, ...

Adding individual flag icons for each country in your Datatables can be a significant enhancement to

Below is the code snippet in JavaScript with flags: <script type="text/javascript"> $(document).ready(function() { var dataTable = $("#example").DataTable( { processing: true, bSort: false, ...

Tips on displaying a successful message and automatically refreshing the page simultaneously

I am working on a JSP Servlet code that utilizes AJAX to handle user data. In the success function of AJAX, I aim to display a success message to the user and clear all fields in the form by reloading the page. Unfortunately, when the page reloads, the a ...

What is the best way to create a deep clone of an XMLDocument Object using Javascript?

I am currently working on a project that involves parsing an XML file into an XMLDocument object using the browser's implementation of an XML parser, like this: new DOMParser().parseFromString(text,"text/xml"); However, I have encountered a situatio ...