Is it acceptable to use JavaScript to code positioning if the standard HTML flow is not behaving as expected?

After contemplating for a long time, I have finally mustered the courage to ask this question. The behavior of HTML elements in normal flow has always bewildered me. Controlling them can be quite challenging, especially when dealing with server-side coding and dynamic content creation. Sometimes, understanding these elements takes more time than actually coding on the server side or using JavaScript. Predicting how a change will impact another element can be a daunting task.

That's why I am considering using absolute and relative positioning for elements, calculating their placement based on the window's innerWidth and innerHeight to ensure responsiveness. This method would give me more control over the elements.

By adjusting the element's position upon window resize, I aim to make it dynamically responsive. Here is an example:

window.onload = function()
{
document.getElementById("gamectrl").style.position = "absolute";
document.getElementById("gamectrl").style.top = document.getElementById("rtctrls").offsetTop + 10 + "px";
document.getElementById("gamectrl").style.left = document.getElementById("rtctrls").offsetLeft + 10 + "px";
}

window.onresize = function()
{
    document.getElementById("gamectrl").style.top = document.getElementById("rtctrls").offsetTop + 10 + "px";
    document.getElementById("gamectrl").style.left = document.getElementById("rtctrls").offsetLeft + 10 + "px"; 
}

Do you think this approach is valid for positioning HTML elements? It feels comfortable to me, but I'm unsure if it's the right way to go about it.

If you have any experience in this area, please share your insights. I'm struggling to decide on the best approach as I lack a comprehensive resource that delves into the intricacies of the normal document flow. If you know of any good resources discussing normal document flow in depth, I'd greatly appreciate it if you could share them with me.

Answer №1

If you are looking to adjust the position of an element, I recommend sticking to CSS instead of using JavaScript as shown in the example. To address concerns about responsiveness, explore using @media queries. You can also set the element's margin using percentage values (e.g., margin-top: 10%) to control its distance from the window edge or another element.

  1. Media Queries: http://css-tricks.com/css-media-queries/
  2. Queries: http://www.w3schools.com/css/css_mediatypes.asp
  3. Positioning:
  4. Positioning: http://www.w3schools.com/css/css_positioning.asp

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

Using jQuery UI Tabs to Dynamically Select a Tab Based on a Link

Recently, I have been exploring the idea of using a script to open a specific tab: $('.tofour').click(function() { // bind click event to link $tabs.tabs('select', 3); // switch to third tab return false; }); However, my dilem ...

Identifying the relationship between child and parent components in Vue.js

I am new to Vue.js and I am practicing some simple exercises on communication between Vue components. However, I am struggling with understanding who is a child component and who is a parent component. For example, consider the following code snippet: HTM ...

A sleek fixed navbar in Bootstrap showcasing a shrunk logo positioned above the navbar

Is there a way to implement a fixed navbar with a parent logo using bootstrap? I would like the logo to shrink as you scroll, while still remaining visible within the fixed navbar. For a demo of the shrinking logo and navbar, you can visit: ...

Stop clicks from interfering with dragging in three.js

My GLTF model in Three.js has objects within it, and I want the camera to zoom in on an object when the user clicks on it. However, I am facing an issue where if the user drags after clicking on an object, a click is triggered upon releasing the mouse butt ...

Shifting of SVG coordinates

My challenge involves fitting an svg into a div. The svg consists of a single path. Even though I have correctly set up the viewBox and preserveAspectRatio, part of the path is drawn outside the expected boundaries. You can find the fiddle here, and below ...

Connecting table checkboxes to buttons in Angular 4.x: A step-by-step guide

Exploring Angular 4.x for the first time and faced with a challenge. I have created an HTML table where each row contains a checkbox and buttons. My goal is to link the checkboxes with the buttons in such a way that when a checkbox is checked, the correspo ...

Developing a personalized file upload button in React

I have been working on creating a custom <input type="file"> upload button in React. My goal is to display the name of the uploaded file on the button itself after the upload. I encountered some challenges while trying to create a codepen demo, so I ...

Tips and tricks for personalizing the leaflet Lopup component using vue js

Seeking guidance on customizing the design of the LPopup component in leafletjs. I found a helpful guide at: https://i.sstatic.net/qEZol.png After inspecting the Lpopup in the dev tools, I tried adding CSS styles to the 'leaflet-popup-content-wrappe ...

Learn how to dynamically show the chosen option from a dropdown menu in input text fields using AngularJS

html file: <select ng-model="shipping" ng-options="shipping.shipping for shipping in shipAddress"> <option value=''>--Select Address --</option> </select> <form name="shippingForm" class="form-horizontal" role="form" ...

Sending a POST request to a PHP file in React: A Step-by-Step Guide

Just starting out with reactjs and trying to figure out how to send an ajax request to a server (php file). Here's the structure of my project: check it out here src/index.js import React from 'react'; import ReactDOM from 'react-dom& ...

How to center multiple divs in a row using HTML and CSS

I've been attempting to achieve a pretty basic task, but unfortunately my efforts have fallen short thus far. I have an image and a title that need to be centered within their parent div element. However, I'm struggling to find the proper way to ...

NodeJS: Extract images based on specified coordinates

Dealing with images that contain text can be a challenge, but by using tesseract and the imagemagick node module, I was able to extract the text successfully. The only issue I encountered was related to the image size. https://i.sstatic.net/XldZC.png For ...

The preventDefault() method is failing to work when submitting a form

Here is a form that processes a password recovery script requested by the user. <form class="forget-form" id="forget-form" action="datacenter/functions/passwordRecovery.php" method="POST"> <h3 class="font-green">Forgot your password?</h ...

The challenge of navigating CSS specificity guidelines

In my CSS file, I have defined two styles for a table with the class "myTable". The first style sets the background color of header cells to gray, while the second style sets the background color of odd rows to blue: .myTable th { background-color: gr ...

A Step-by-Step Guide to Launching PDF Blob from AJAX Response in a Fresh Chrome Tab

I'm sending a POST request to a server, and in response, I receive a PDF file that is returned as a blob. To handle this blob, I am using the "handle-as='blob'" attribute of iron-ajax (a Polymer element), just to cover all bases. Now, my g ...

the ability to utilize the @click glyphicon signal

In this scenario, as soon as I type something into the input field, the glyphicon-remove icon will appear. Upon clicking on it, the intention is for it to reset the search input. Below is the code snippet: <div class="form-group has-feedback has-feedb ...

Unable to locate properties "offsetHeight" or "clientHeight" within a React/Next.js project developed in TypeScript

I have a unique customized collapsible FAQ feature that adjusts its height based on whether it's expanded or collapsed. import { useState, useRef, useEffect } from "react"; export default FAQItem({title, description}: FAQItemProps) { cons ...

I am curious as to why the "default + label" pseudo-class is not functioning properly on the "input" element. Specifically, I am having trouble with the "+ label" portion

":default" works fine on its own, but when combined with the "label" pseudo-class, it fails to apply. .pseudo-test input:default { box-shadow: 0 0 20px 20px red; } .pseudo-test input:default+label { color: coral; } <div style ...

Tips for updating an array in TypeScript with React:

Encountering an issue while updating my state on form submission in TypeScript. I am a newcomer to TS and struggling to resolve the error. enum ServiceTypeEnum { Replace = 'replace product', Fix = 'fix product', } interface IProduc ...

Avoiding future clicks with a delay in VUE: A guide

Is there a way to prevent the next click for 600ms after an initial click? I want to temporarily "disable" all a tags for 600ms after one is clicked. Any help would be appreciated. VUE <ul> <li v-for="item in navigation" :key=& ...