The navigation bar initially fills the width of the screen, but does not adjust to match the width of the table when scrolling

I've spent the last 2 hours playing around with CSS, using width:100% and width:100vw, but nothing seems to be working.

Currently, the navigation bar fits perfectly across the screen on desktop browsers, so there doesn't seem to be an issue there. However, the page contains a dynamic table that expands in width as new columns are added.

Unfortunately, the navigation bar does not stretch to match the full width of the table. As you scroll horizontally across the page, the table grows wider while the navigation bar stays narrow, creating a mismatch between the two elements.

The table-container class is currently commented out, but even when uncommented, it doesn't resolve the issue.

Here is my existing code. I would greatly appreciate any help you can provide.

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8>
  <title>Flask Web Form</title>
  <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}">
  <style>
    body {
      font-family: Arial, Helvetica, sans-serif;
      font-size: 16px;
      zoom: 175%;
    }
    
    .topnav {
      overflow: hidden;
      background-color: #333;
      display: flex;
      width: 100vw;
      position: sticky;
      top: 0;
    }
    
    .topnav a {
      float: left;
      color: #f2f2f2;
      text-align: center;
      padding: 14px 16px;
      text-decoration: none;
      font-size: 17px;
    }
    
    .topnav a:hover {
      background-color: #ddd;
      color: black;
    }
    
    .topnav a.active {
      background-color: #AAC939;
      color: white;
    }
    
    .form-container {
      display: flex;
      flex-direction: column;
      align-items: center;
    }
    
    label {
      margin-right: 5px;
    }
    /* .table-container {
          width: 100%;
        } */
  </style>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>;
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a6c4c9c9d2d5d2d4c7d6e69288958897">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"></head>
  
  <!--                                              BODY                 -->

  <body style="background-color: white;">
  <div class="topnav">
    <a href="{{ url_for('index') }}">Section 1</a>
    <a class="active" href="{{ url_for('show_table') }}">Section 2</a>
    <a href="{{ url_for('admin') }}">Section 3</a>
  </div>

  <header>
    <title>Form Data</title>
  </header>

  <div class="table-container">
    <table border="1" class="table table-striped" style="max-width:100%; white-space:nowrap">
      <thead>
        <tr>
          {% for column_name in column_names %}
          <th>{{ column_name|replace('_', ' ') }}</th>
          {% endfor %}
        </tr>
      </thead>
      <tbody>
        {% for row in rows %}
        <tr>
          {% for item in row %}
          <td>{{ item|replace('_', ' ') }}</td>
          {% endfor %}
        <tr>
        {% endfor %}
   </tbody>
</table>
  </div>

</body>

</html>

Answer №1

To demonstrate, I set the .table-container with a minimum width of 4000px to simulate the table expanding in width.

Initially, my idea was to fix the position of .topnav so it remains in view when scrolling on the horizontal axis.

However, if your table contains an unknown number of sections, a simple JavaScript snippet using getComputedStyle can be implemented to match the width of .topnav with that of .table-container.

window.onload = function setWidth() {
  var tableWidth = document.getElementById("tableWidth");
  var navWidth = document.getElementById("navWidth");
  style = window.getComputedStyle(tableWidth);
  wdt = style.getPropertyValue('width');
  navWidth.style.width = wdt;
}
body {
  font-family: Arial, Helvetica, sans-serif;
  zoom: 175%;
}

.topnav {
  overflow: hidden;
  background-color: #333;
  display: flex;
  width: 100vw;
  position: sticky;
  top: 0;
}

.topnav a {
  float: left;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.topnav a:hover {
  background-color: #ddd;
  color: black;
}

.topnav a.active {
  background-color: #AAC939;
  color: white;
}

.form-container {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.table-container {
  width: 100%;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8>;
  <title>Flask Web Form</title>
  <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0b6964647f787f796a7b4b3f2538253a">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>

<body style="background-color: white;">
  <div class="topnav" id="navWidth">
    <a href="{{ url_for('index') }}">Section 1</a>
    <a class="active" href="{{ url_for('show_table') }}">Section 2</a>
    <a href="{{ url_for('admin') }}">Section 3</a>
  </div>
  <div class="table-container" id="tableWidth" style="min-width: 4000px;">
    <table border="1" class="table table-striped" style="max-width:100%; white-space:nowrap;">
      <thead>
        <tr>
          {% for column_name in column_names %}
          <th>{{ column_name|replace('_', ' ') }}</th>
          {% endfor %}
        </tr>
      </thead>
      <tbody>
        {% for row in rows %}
        <tr>
          {% for item in row %}
          <td>{{ item|replace('_', ' ') }}</td>
          {% endfor %}
        </tr>
        {% endfor %}
      </tbody>
    </table>
  </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

What is the reason for having a filled array containing data on currency exchange rates within an asynchronous function, while simultaneously having an empty array located outside of it? - React

After struggling with this programming issue for the past 5 days, I'm reaching out for help. The challenge I'm facing involves retrieving the rate of a specific currency between 1 and 50000 from an API. While I have successfully managed to obtai ...

Displaying information on a table using data from three separate JSON arrays within AngularJS

I have some JSON data: { tableList:["a","b"], dbList:["c","d"], score:[2 , 5] } I would like to display this data in a table using AngularJS like so: DB Table Score c a 2 d b 5 Can we utiliz ...

Is there a way to automate the uploading of captcha images to this solving service using Python?

Currently, I am working on a Python program with the goal of solving captchas on a website. My plan is to integrate 2captcha into the solution. Using Selenium, I have managed to write a Python script that fulfills all required tasks except for solving the ...

What is the best way to locate the position of a different element within ReactJS?

Within my parent element, I have two child elements. The second child has the capability to be dragged into the first child. Upon successful drag and drop into the first child, a callback function will be triggered. What is the best way for me to determi ...

What steps should I take to resolve this issue with the undefined index?

Can anyone help me troubleshoot this persistent error I'm encountering? I've tried various solutions, but the error persists. Your assistance in resolving this issue is greatly appreciated. [My error][1] Below is the code snippet from my update ...

The error message "Uncaught TypeError: Cannot read property 'getUniforms' of undefined" is thrown from the ThreeJS r82 Custom Shader in the three.min.js file at line

I've been working on setting up a js fiddle to showcase an issue I'm currently tackling with a custom shader. After linking it to three.min.js for r82, I encountered some runtime errors during rendering. Interestingly, this problem was absent whe ...

Is there a way to automatically transfer the store's order value to my payment gateway's checkout process during the checkout process?

I am facing an issue with integrating the Razorpay payment gateway into my online store built on ecwid. The process involves decrypting order details from the store, sending them to the payment gateway for processing, and redirecting the customer back to t ...

Adding content from a dialog box to the body of an HTML document

I'm struggling with a problem involving adding a value from a textbox in the dialog box to my HTML body. <div id="dialog-form"> <form> <label for="name">Name</label> <input type="text" name="name" id="txt2 codein ...

Retrieve the selected value of a Bootstrap dropdown using an onclick event with jQuery

In my HTML file, there is a table containing a button component from Bootstrap in one of the columns. When this button is clicked, a dropdown menu with three options - 'medium', 'low', 'servicerequest' appears. My goal is to r ...

Implement a feature that adds a circle element when an image is clicked in a React application

I am attempting to create a program that allows users to add circles to an image by clicking on it. Essentially, when the user clicks at coordinates (x,y), a circle with a radius of 10 will appear at that location. I am exploring ways to implement meta-pro ...

I'm looking for a way to display the value stored in a variable within an ejs template. Any suggestions

I am experiencing an issue with my ejs template that should greet the user by displaying their name when the page loads, but it is not showing anything. Here's a snippet of my template: <!DOCTYPE html> <html> <head> < ...

Issues with the animation of the navbar menu button are preventing it from functioning

I have been attempting to incorporate animation when the navbar-button is toggled on smaller screen sizes. Inspired by the design of .navbar-toggle.larr in this particular template, I tried to implement a similar effect. /* ANIMATED LEFT ARROW */ .navbar- ...

Ways to modify the .MuiSelect-nativeInput element within Material-UI

const styles = makeStyles((theme) => ({ root: { margin: "0px 20px" }, textStyle: { fontFamily: "Comfortaa", }, container: {}, textField: { fontFamily: "Comfortaa", }, dropDownFormSize: { width: &qu ...

Utilizing dual identifiers in a Jquery plugin

I am currently working with a jQuery plugin and I need to apply the same functionality to two different IDs. How can I achieve this? It involves a next and previous functionality where clicking on the next button automatically scrolls both divs. The issu ...

Vue alert: Issue with rendering - TypeError: Unable to access property 'NomeStr' as it is undefined

I'm having trouble displaying the 'NameSrt' item array value in my template, and I keep encountering this issue: vue.runtime.esm.js?2b0e:619 [Vue warn]: Error in render: "TypeError: Cannot read property 'NomeStr' of undefined" The ...

Modify HTML style content

Here's an excerpt from my CSS file: .pricing .pricing-value .price:before { position: absolute; content: 'different text'; top: 10px; left: -35px; } I'm wondering if it's possible to change 'different text' to s ...

How to make a GET request to a Node server using Angular

I am currently running a node server on port 8000 app.get('/historical/:days' ,(req,res,next){..}) My question is how to send a request from an Angular app (running on port 4200) in the browser to this node server. Below is my attempt: makeReq ...

Converting Markup Language to PDF or HTML

Are there any markup languages that work seamlessly with a popular .NET open source project to produce highly customized PDF or HTML documents with precise control over styling and positioning? These documents will consist of a combination of static conte ...

JavaScript fails to focus on dynamically inserted input fields

Within my HTML template, there is an input element that I am loading via an Ajax call and inserting into existing HTML using jQuery's $(selector).html(response). This input element is part of a pop-up box that loads from the template. I want to set f ...

Ways to showcase the outcome of a spin after each rotation

I am currently working on a spinning code snippet that allows users to spin and potentially win different amounts. The code includes functionality to prevent more than 3 spins, set random rotation values, and animate the spinning effect. However, I now wa ...