Tips for arranging HTML elements in a horizontal line, even if they belong to different parent elements

My HTML document features a sticky header and footer, with a div below the header that sticks to it to eventually hold tabs above a form. I've been struggling to align the form vertically below this div. The issue arises because the tab div lacks a scrollbar, while the form has one. This results in the widths of the form and tabs being different, causing alignment problems. I've attempted to set them to 70% of the width and center them, but the scrollbar throws off the alignment. I even tried using JavaScript to calculate the scrollbar's width and adjust the form's margin, but it hasn't solved the problem. The form is visibly narrower than the tab div, and I've spent countless hours trying to fix this issue. Additionally, adding margin-bottom to the form hasn't worked as expected, as no margin appears below the border.

$(document).ready(function () {
  setFormsWidth();
});

function setFormsWidth() {
   
  let scrollbox = document.createElement('div');

  // Make box scrollable
  scrollbox.style.overflow = 'scroll';

  // Append box to document
  document.body.appendChild(scrollbox);

  // Measure inner width of box
  scrollBarWidth = scrollbox.offsetWidth - scrollbox.clientWidth;

  // Remove box
  document.body.removeChild(scrollbox);

  // Get current width of right margin, which should be 30% of the
  // width of the form-panel parent (the content class). 
  var formPanel = document.getElementById("main-form");

  // Get the current right margin and remove the px at end of number
  var style = window.getComputedStyle(formPanel);
  var marginRightString = style.getPropertyValue('margin-right');
  var marginRight = marginRightString.slice(0,-2);

  // now addthe scrollBarWidth to the right margin
  var newMargin = marginRight + scrollBarWidth;

  formPanel.style.marginRight = newMargin + "px";

}
html, body {
  height: 100%;
  margin: 0;
  overflow:hidden;
}

.wrapper {
  height: 100%;
  display: flex;
  flex-direction: column;
}
.header, .footer {
  background: silver;
}
.page {
  flex: 1;
   overflow: auto; 
   background: pink;
}

.content {
  background-color: green;;
}

.tabs {
  width: 70%;
  height: 50px;
  background-color: aqua;
  margin: 0 auto;
  border-bottom: solid #FE6D73 7px; 
}

.form-panel {
  width: 70%; 
  height: 1000px;
  background-color: #FFF;
  margin: 0 auto;
  overflow-y: auto;
  border-bottom: solid #FE6D73 7px; 

}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8>
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="style.css"/>
</head>
<body>
  
  <div class="wrapper">
    <div class="header">Header</div>
    <div class="tabs">
      THIS IS TAB
    </div>
    <div class="page" id="calculator">
      <div style="height:1000px;">
        <div class="content">
       
          <form class="form-panel" id="main-form">THIS IS FORM</form>
            
      </div>
      </div>
    </div>
    <div class="footer">Footer</div>
  </div>

  <script type="text/javascript" src="script.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>


</body>
</html>

Answer №1

To optimize the layout and function of the webpage, it is recommended to display the scrollbar next to the .form-panel element rather than the .page div. This adjustment will ensure that the centering of the .form-panel element is not impacted.

Below are the CSS modifications:

  • Assign a height of 100% to all elements between the .page and .form-panel elements, as well as the .form-panel itself, to prevent the scrollbar from appearing on the .page
  • Apply box-sizing: border-box; to the .form-panel to keep the border within the .form-panel
  • Move the .footer outside the .page element

If a specific height needs to be set for the .form-panel, you can create a div inside it and adjust its height as illustrated below:

html, body {
  height: 100%;
  margin: 0;
  overflow:hidden;
}

.wrapper {
  height: 100%;
  display: flex;
  flex-direction: column;
}
.header, .footer {
  background: silver;
}
.page {
  flex: 1;
   overflow: auto; 
   background: pink;
}

.content {
  background-color: green;
  height: 100%;
}

.tabs {
  width: 70%;
  height: 50px;
  background-color: aqua;
  margin: 0 auto;
  border-bottom: solid #FE6D73 7px; 
}

.form-panel {
  width: 70%; 
  height: 100%;
  background-color: #FFF;
  margin: 0 auto;
  overflow-y: auto;
  border-bottom: solid #FE6D73 7px; 
  padding-bottom: 7px;
  box-sizing: border-box;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8>;
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="style.css"/>
</head>
<body>
  
  <div class="wrapper">
    <div class="header">Header</div>
    <div class="tabs">
      THIS IS TAB
    </div>
    <div class="page" id="calculator">
      <div style="height:100%;">
        <div class="content">
       
          <form class="form-panel" id="main-form">
            <div style="height:1000px">
              THIS IS FORM
            </div>
          </form>
            
      </div>
      </div>
    </div>
    <div class="footer">Footer</div>
  </div>



</body>
</html>

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

Populating fields in a new AngularJS document from a table

I have a project where there is a table displaying different instances of our service, and each row has an info button that opens a form with the corresponding row's information autofilled. However, I am facing an issue where by the time I retrieve th ...

Strange outcomes observed with CSS Selector nth-child

When utilizing the CSS nth-child selector, I am encountering some peculiar issues. The HTML structure is as follows: <div class="block feature-callout-c" id="overview"> <div class="row"> <div class="span twelve"> ...

Utilizing AngularJS to create a dynamic autocomplete feature with data sourced

I'm currently implementing an autocomplete feature using the REST Countries web service. The goal is to provide suggestions as the user starts typing in an input field, and once a country is selected, display information about it. I am considering usi ...

Having difficulty reaching a div element using either its id or class in a CSS stylesheet

I'm having trouble adding padding to the home section div and I can't seem to access it using either the id or class. Any suggestions on how to solve this issue would be greatly appreciated. Below is the CSS code: body { margin: 0 ; font ...

The function to set the state in React is malfunctioning

I'm currently in the process of developing a website where I utilize fetch to retrieve information and display it to the user. Initially, I opted to save this data in a state variable, but for some reason, it's not functioning as expected. Upon ...

Achieving Text Centering in kableExtra Cells with CSS: A Step-by-Step Guide

Recently, I received a helpful solution that involved using the extra_css = argument in cell_spec() from the kableExtra package. This allowed me to fill the entire cell of my table instead of just the text inside it. However, even after specifying align = ...

Retrieve the text content of the paragraph element when its parent is clicked. The paragraph element belongs to a group that contains many

In a function I'm working on, I need to retrieve the specific text within a p element when its parent (.photoBackground) is clicked. The purpose of this operation is to grab the caption for a gallery, where there are multiple p elements with the same ...

Error message: "Property undefined when Angular attempts to call a function from jQuery/JavaScript."

I'm currently attempting to invoke an angular controller from my JavaScript code. This is my first encounter with Angular and I must admit, I'm feeling a bit overwhelmed! I've been following this example: Unfortunately, when testing it out ...

Page refreshing in Angular 5 consistently redirects to the home page instead of staying on the current page

I am experiencing an issue with the navigation on my application. When I navigate to routes like getEmp-by-id or page-not-found and hit refresh, the application automatically redirects me back to app-home. However, I would like it to stay on the same pag ...

Creating a dynamic canvas with a three-dimensional model using three.js: A step-by-step guide

I'm currently developing a website where I have integrated a canvas element to display a 3D object (specifically, a cube) using three.js. The canvas is housed within a div, following the guidelines found in various documentation. The issue arises wh ...

I wasn't able to use the arrow keys to focus on the select box in my table, but I had no problem focusing on all

I'm a novice in jQuery and I encountered a situation where I have select boxes and text fields within my table. I successfully implemented arrow key functionality (down for next, up for prev) for shifting focus to the field by assigning classes to eac ...

Warning: Promise rejection not handled in error

I've been working with nodejs, express, and argon2 in my project. However, I encountered an error that has left me puzzled as to why it's happening. Strangely, even though I don't have any callbacks in my code, this error keeps popping up. H ...

JavaScript counter that starts at 1 and increments with each rotation

I am working with an array of image IDs. let images = ['238239', '389943', '989238', ... ]; let max = images.length; The array begins at index 0 and its size may vary. For instance, if there are 5 images in the array, the i ...

Is there a way to transform a tabulated tree into JSON using JavaScript?

I've been searching for a solution, but I have come to the conclusion that this question is quite peculiar. How can I convert the following text file using tabs for spacing: parent child child parent child grandchild grand ...

Basic library using babel, TypeScript, and webpack - Error: (...) is not a recognized function; within Object.<anonymous>

Recently, I encountered a strange issue while trying to bundle a simple function using babel, typescript, and webpack. You can find the example that is not working at this link. To test it, simply download the code, run npm/yarn install, npm/yarn run buil ...

Transferring an array of objects from one array to another with the click of a button

I'm facing an issue with moving data between two arrays of objects using buttons in a Nextjs project. The functionality works correctly when selecting a single data item, but it gives unexpected results when selecting multiple items. Although my code ...

Arrange a collection of objects by two criteria: the end time, followed by the status in accordance with the specified array order if the end times are equal

Is this the best method to arrange data by using infinity? I gave it a try but it doesn't quite meet my requirements. data = [{ "status": "Accepted", "endTime": "" }, { "status": "New", ...

Having trouble displaying data from a MongoDB database using ng-repeat in AngularJS

Currently, I am facing an issue while trying to retrieve data from the database and display it using ng-repeat. The getAll function in the factory is fetching the data properly, returning an object with all the information. However, when I try to display t ...

npm encountered an error while trying to install selenium-webdriver using the npm install command

My operating system is Windows Server 2008 R2 EE and I have the npm package manager installed. I am attempting to install the Selenium Webdriver package using the command below. Command: npm install selenium-webdriver However, when running this comma ...

The PUT rest service does not function in AngularJS version 1.0.8

I am facing an issue with my AngularJS application that has a CRUD Rest service. While the Create, Read, and Delete methods are functioning properly, the PUT method is not working. I have searched on Stackoverflow and found similar problems with accepted s ...