What is the method for adjusting the body width based on the screen resolution using JavaScript?

Is there a way to adjust the body width based on screen resolution using JavaScript?

I attempted to set it with

document.body.style.width = screen.width;
, but I can only specify a fixed width like 1920px.

What I'm aiming for is to have a fixed screen width similar to websites like Stack Overflow, regardless of the device used.

Should JQuery be utilized for this purpose? Here is the HTML snippet:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <link href="https://fonts.googleapis.com/css?family=Droid+Sans|Fjalla+One|Gentium+Basic|Nunito|Hammersmith+One" rel="stylesheet">
  <script src="https://use.fontawesome.com/ca51704d87.js"></script>
  <script src="bg.js"></script>
  <meta content='width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;' name='viewport' />
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Test1</title>
  <link rel="stylesheet" type="text/css" href="style.css" />
</head>

<body onload="bload()">
  <div id="header">
    <img id="logo_head" src="src/logo_head.png" />
    <p id="head">Kkv Martial Arts Academy</p>
    <ul id="navhead">
      <li id="subnav"><a id="link" href="index.html">HOME</a>
      </li>
      <li id="subnav">BRANCHES</li>
      <li id="subnav"><a id="link" href="about_us.html">ABOUT US</a>
      </li>
    </ul>
    <ul id="s2">
      <li id="social"><a id="link" href="http://www.facebook.com"><i class="fa fa-facebook-square" aria-hidden="true"></i></a>
      </li>
      <li id="social"><a id="link" href="http://www.plus.google.com"><i class="fa fa-google-plus-official" aria-hidden="true"></i></a>
      </li>
      <li id="social"><a id="link" href="http://www.instagram.com"><i class="fa fa-instagram" aria-hidden="true"></i></a>
      </li>
    </ul>
  </div>

  <div id="picb">
    <p id="greeting">KKV Martial Arts Academy.</p>
    <p id="greeting2">One Place. Many Arts.</p>
    <ul id=b>
      <a id="link" href="karate.htm
    l">
        <button class="but1">KARATE</button>
      </a>
      <a id="link" href="slmb.html">
        <button class="but2">SILAMBATAM</button>
      </a>
      <a id="link" href="kkv.html">
        <button class="but3">KAALADI KUTHUVARISAI VARMA</button>
      </a>
    </ul>
  

...
...

Your input and suggestions are greatly appreciated.

Answer №1

It is important to remember that screen.availWidth may not always accurately reflect your current screen resolution, as CSS pixels do not necessarily align with device pixels. To obtain a value that matches the screen resolution more closely, you can use this formula:

screen.availWidth * window.devicePixelRatio

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

Encountering the following issue: Unable to load XMLHttpRequest for the function javascript:newDoc()

I've been experiencing an issue with my script that opens custom subdomains which were previously working fine. Since I integrated ajax for the contact form, it's not functioning properly. ERROR:(jquery.js:4 XMLHttpRequest cannot load javascri ...

The selector By.PARTIAL_LINK_TEXT in Selenium is failing to meet the expected condition

I am trying to store the friend list of a Facebook ID. When I click on the Friends tab on the profile, the friend list should be opened if it's not blocked; otherwise, something else should open. To determine if the friend list is opened or blocked, ...

There may be instances where data is null in Java Spring and React JS

My goal is to store all data using react.js and Java Spring. I have sent data via REST, but one of the data classes is coming up as null in the MongoDB collections. I have checked to ensure that all data types are the same but I am unable to identify and r ...

The security protocols prevent me from placing calls to the Steam API site from my own website

I am venturing into the creation of a web application that uses steam web APIs and I find myself at a loss on how to kickstart this project. This question perfectly encapsulates my struggles, particularly in establishing endpoints and resolving the issue. ...

Trouble with the onload function in onMounted vue3

For my vue3 component, I need to load certain scripts. Here is what I am currently implementing: <script setup> import { onMounted } from 'vue' function createElement (el, url, type) { const script = document.createElement(el) if (type ...

Trouble with importing CSS in my Angular application (excluding Angular-CLI)

As I attempted to incorporate Bootstrap's CSS and Material Design's styles into my Angular application, I encountered a dilemma along with a potential compromise. When I attempt to include them using the "link href" method in my index.cshtml fi ...

Is there a way to utilize jquery to click a submit button within a concealed form?

In the current setup, there are 25 products displayed on a single page. Each product contains a form that needs to be dragged to the mini cart located on the right side. The challenge lies in identifying the form ID of the product that is being dragged. T ...

Explore more by clicking on the linked slider item

I created a simple jQuery slider using only margin-left, without the use of plugins or access to the HTML. The next and previous buttons successfully transition between sliders. Now, I am facing a challenge where I need to link each slider so that clicking ...

Issue: Unhandled rejection TypeError: Unable to access properties of an undefined variable (retrieving 'data')

Currently, I am developing applications using a combination of spring boot for the backend and react for the frontend. My goal is to create a form on the client side that can be submitted to save data in the database. After filling out the form and attemp ...

Using Jquery to update the value in a textarea when the arrow keys are pressed, mimicking the behavior of Firefox Developer Tools

One feature that I find really helpful in Firefox is when you click on a CSS property and it turns into an input field. You can then use the ↑ or ↓ keys to increase or decrease the value by 1 (hold shift for +10 or -10 increment). https://i.sstatic.ne ...

Troubleshooting NodeJS and Express: Issue accessing a function located outside a folder

I'm having trouble accessing the function I exported in app.js Here is the code snippet from app.js: function getConnection() { return mysql.createPool({ host: 'localhost', user: 'root', password: &apo ...

What is the conventional method for organizing forms in ASP.Net?

Here's my forms layout: <body> <p>Please note that this form is solely for illustrating the Required Field Validator and does not perform any actual function.</p> <form id="frmValidator" method="post" runat=" ...

Searching for elements by tag name in JavaScript

Recently, I attempted to create JavaScript code that would highlight an element when a user hovers their cursor over it. My approach involves adding an event listener to every child within the first "nav" tag in the current document: let navigation = docum ...

Rotating a loaded file using THREE.js

After experimenting with the ThreeJS library for a few weeks and drawing inspiration from others, I managed to create a canvas that displays an external .stl-file. However, my main issue is that I am struggling to apply any transformations to it. While I ...

Obtaining element attributes using JQuery selectors in Selenium IDE version 2.0.0

I have a webpage that automatically selects a custom Raphael radio button when the page loads. I am attempting to confirm that the correct id is selected using Selenium IDE 2.0.0 and FireFox 20.0.1. I have tried the following... |getEval | javascript{sel ...

Responsive design

Is it possible to change the background-color of the h1 element when the browser ratio is 4:3? I've attempted using the aspect-ratio property in my media query, but it doesn't seem to be working. Any suggestions on how to resolve this issue? Che ...

404 errors are occurring with fragments of jQuery 1.3.2

Recently, I implemented jQuery 1.3.2 on my website for DOM manipulation and some ajax functionality. However, my logs are showing consistent 404 errors originating from the folder containing the jQuery file. Surprisingly, the 'pages' triggering t ...

Utilizing Jquery's .GET method to retrieve and handle JSON data

In the jQuery snippet below, I am trying to fetch product data from . However, I am facing difficulty in iterating through the loop to access all 30 products along with their details. $.get("https://dummyjson.com/products/1") .done(function ...

How come I am unable to terminate this Angular HTTP request?

I've been trying to implement a solution for canceling HTTP requests in AngularJS by referring to this Stack Overflow post. Here's the code snippet I'm using: var canceller = $q.defer(); $timeout(function() { canceller.resolve(); alert ...

"Troubleshooting Issue: jQuery dataTables Filter Functionality Inoperative When Conjoined with

Incorporating the dataTables plugin and recycling code from another page to create a select for filtering a specific column, I encountered issues with the filtering functionality not working as expected. This was perplexing since it was code that had previ ...