What is the best way to create a scrollable table that extends to 100% of the available width within its container, without taking up the

In the application, there is a collapsed sidebar on the left labeled "sidebar-left" and a container on the right with various types of content labeled "content." The issue arises with the width of this container, as it needs to take up 100% of the remaining width. While different types of content work well with this setup, when a scrollable table is placed in the "content" container, it constantly tries to stretch the table to 100% of the screen size instead of the container size. Below is a snippet of the code:

    .wrapper {
        display: flex;
        align-items: stretch;
      width: 100%;
    }

    .sidebar-left {
      width: 300px;
      background: red;
    }

    .content {
      /*width: calc(100% - 300px);  it works, but .sidebar-left should have flexible size*/
      width: 100%;
    }

    .sidebar-right {
      width: 20px;
      background: blue;
    }

    .scrollable-table {
        overflow: scroll;
        width: 100%;
        white-space:nowrap;
    }
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <title>Collapsible sidebar using Bootstrap 4</title>

    <!-- Bootstrap CSS CDN -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">

</head>

<body>
    <div class="wrapper">
        <div class="sidebar-left">
            Sidebar left
        </div>
        <div class="content">
            <div data-v-e474bf0e="" class="scrollable-table">
                <table class="table table-striped table-hover table-sm">
                    <!-- Table content goes here -->
                </table>
            </div>
        </div>
</div>
</body>

</html>

Attempts were made with width: calc(100% - 300px);, however, the sidebar dimensions need to be variable from 80 to 300px and remain collapsed. While common solutions have been sought, none have successfully resolved the issue of the content div stretching to 100% of the screen width instead of the remaining width.

Answer №1

You have the option to utilize relative units for setting widths. For instance, vw represents the width of the viewport (essentially the screen width).

The unit vh corresponds to the height of the viewport.

In this example, I utilized 10vw and 90vw as width measurements, which you can adjust as needed.

.wrapper {
        display: flex;
        align-items: stretch;
      width: 100%;
    }

    .sidebar-left {
      width: 10vw;
      background: red;
    }

    .content {
      width: 90vw;
      width: 100%;
    }

    .sidebar-right {
      width: 20px;
      background: blue;
    }

    .scrollable-table {
        overflow: scroll;
        width: 100%;
        white-space:nowrap;
    }
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <title>Collapsible sidebar using Bootstrap 4</title>

    <!-- Bootstrap CSS CDN -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">

</head>

<body>
    <div class="wrapper">
        <div class="sidebar-left">
            Sidebar left
        </div>
        <div class="content">
            <div data-v-e474bf0e="" class="scrollable-table">
                <table class="table table-striped table-hover table-sm"> [Table Content Hidden for brevity]
                    </tbody>
                </table>
            </div>
        </div>
</div>
</body>

</html>

Answer №2

To achieve the desired outcome, utilizing CSS custom properties (variables) is the way to go.

Give this a shot.

:root {
--sidebar-left: 300px; /* Define sidebar width */
}
.wrapper {
        display: flex;
        align-items: stretch;
      width: 100%;
    }

    .sidebar-left {
      width: 300px;
      background: red;
    }

    .content {
      width: calc(100% - var(--sidebar-left));
    }

    .sidebar-right {
      width: 20px;
      background: blue;
    }

    .scrollable-table {
        overflow: scroll;
        width: 100%;
        white-space:nowrap;
    }

@media only screen and (max-width: 768px) {
:root {
--sidebar-left: 80px; /* Sidebar width for screens below 768px */
}
}

Answer №3

I'm grateful that I finally discovered the solution! To prevent the content container from expanding in width, it needs to have the style overflow: hidden. Now, when I place my scrollable table in this container, it fills all the remaining width without enlarging the main container.

Below is the updated code:

.wrapper {
    display: flex;
}

.sidebar-left {
    width: 300px;
    background: red;
}

.content {
    white-space: nowrap;
    overflow: hidden;
    width: 100%;
}

.scrollable-table {
    overflow: scroll;
    white-space: nowrap;
    width: 100%;
}
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <title>Collapsible sidebar using Bootstrap 4</title>

    <!-- Bootstrap CSS CDN -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">

</head>

<body>
    <div class="wrapper">
        <div class="sidebar-left">
            Sidebar left
        </div>
        <div class="content">
            <div data-v-e474bf0e="" class="scrollable-table">
                <table class="table table-striped table-hover table-sm">
                    <thead><tr><th class="sticky-first-column text-center">Document No.</th> <th class="sticky-second-column text-center">Date</th> <th class="text-center">Departure Station</th> <th class="text-center">Destination Station</th> <th class="text-center">Sender</th> <th class="text-center">Receiver</th> <th class="text-center">Cargo</th> <th class="text-center">Wagon Quantity</th> <th class="text-center">Cargo Weight</th></tr></thead> 
                    <tbody><tr><td class="sticky-first-column text-center">33168097</td> <td class="sticky-second-column text-center">10.09.2020</td> <td class="text-capitalize">(341408) Slavut I</td> ... [remainder of the HTML code]

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

Problems arise when using AngularJS' .run function after navigating to a different page

I have encountered an issue with ngRoute while navigating between pages in my web application. The main login page is called index.html, and the routing is controlled by the main js file. However, I face a problem when trying to use a .run block on a speci ...

Need help creating perfect round buttons with bootstrap 4?

After incorporating this fiddle into my code, the goal was to design a circular button. View Fiddle Here The code provided is for bootstrap 3. However, when using it with bootstrap 4, the button ends up being square instead of circular. See example here: ...

Is there a advantage to using Angular's component style encapsulation for improved performance?

Recently, I came across the styling methods of Angular 2 which allows you to directly include your styles within the component. If you want to explore more on this topic, check out loading styles. There are two options for style encapsulation in Angular 2 ...

Tips for showing a value in a disabled text input box

When users access the page, the table should appear as follows: Marks Per Answer Total Marks Marks Remaining (blank text input) 4 4 (blank text inp ...

Revise directive following the dynamic addition of elements

My Objective: I aim to utilize directives for creating custom elements and dynamically inserting them into the view at various points in time. The Challenge: Upon dynamically adding custom elements to the view, they appear as raw HTML without the directi ...

Is it possible for Angular to retrieve information from one JSON file but not another?

After updating the names in the code to reflect the current ones, I would greatly appreciate any help or suggestions! The json file has been provided, so there's not much additional setup required. Just a heads up: I haven't created a service fi ...

Submitting forms with Ajax in IE(8)

Sample Google form Related spreadsheet I modified the original code to create two custom forms: First created form Second created form Both forms are functional on most browsers except for IE(8). Any idea why? First form: <!DOCTYPE html> <h ...

Having trouble navigating menus and submenus?

Customizing CSS Styles On my website, I have two different classes: menu and leftside. Both classes include the ul and li elements. However, when I try to use ul and li in the left side as well, they end up matching with the styles of the menu ul and li. ...

What is the best approach to dynamically bind a style property in Vue.js 3?

I am currently working with vue3 and I am trying to apply dynamic styles. This is how my vue3 template looks: <ul> <li v-for="(question, q_index) in questions" :key="q_index" v-show="question.visible" :style="{ ...

Automatic closing of multile- vel dropdowns in Bootstrap is not enabled by default

I have successfully implemented bootstrap multilevel dropdowns. However, I am facing an issue where only one child is displayed at a time. <div class="container"> <div class="dropdown"> <button class="btn btn-default dropdown-to ...

Keep bootstrap content aligned to the left

I am working on creating a webpage using bulma/bootstrap and my goal is to have some text positioned on the left side of the screen. I want this text to be displayed in the red area and to stay fixed in place when I scroll down, similar to a side panel. ...

Overlap between sidebar and navbar

I am currently working on developing an admin panel for one of my projects. I designed a sidebar and implemented a standard bootstrap 4 navbar. However, I encountered a minor issue where the bootstrap 4 navbar is extending to the full width of the page ins ...

Tips for synchronizing the height of a scrollable list-group column with a neighboring column

Within my Bootstrap 4 layout, I have a column on the left that includes a dynamically generated .list-group, which could potentially contain numerous .list-group-items. On the right side, there is another column showcasing multiple cards inside a .row. Th ...

Creating transparent overlay boxes with CSS grid

I'm working on a design where I need to display text information over an image. The challenge is aligning the text properly, especially when the screen size changes due to using a grid system (1140). Currently, I have a div with transparent background ...

What is the correct way to create a card outline in Bootstrap?

I am currently developing a Django website to enhance my skills in coding, CSS, Html, and Bootstrap. However, I am encountering an issue with the main page of my site. The visuals on my cards do not align properly with the colored boxes at the top of the p ...

Python KeyError: Implementing a Strategy for Displaying Two Divs Side by Side Efficiently with Dynamic Data

media.py: http://pastebin.com/r1nxPzTQ This file contains a Python class with methods like __init__ and show_trailer(self). fresh_tomatoes.py: http://pastebin.com/bcdUYiiu It's a Python script that generates an HTML file and populates it using data ...

Creating a navigation bar - using the LI tag with spaces between words causes text to wrap to the next line

Currently, I am in the process of creating a dynamic navigation bar for my website. In order to ensure that the elements within the navigation bar adjust automatically based on content, I have implemented the code below: .mytaboptions { position: rela ...

Locate the XPath for text that is within a div tag and adjacent to a span tag

I've searched, but couldn't find an exact match, so my question is about a low HTML code: <div class="column1"> <div> <div class="name"> Dynamic Name <span class="id" title="ID">Dynamic ID</span> </div> </d ...

What is wrong with my notecards that they won't flip properly?

I am currently developing a text-to-flashcard program using HTML, CSS, and JS. One feature I'm working on is the ability to flip all flashcards at once with a single button click. Currently, the program only allows flipping from the back face to the f ...

The valueChanges event of a Reactive Form is activated whenever options from a datalist are selected

Whenever a user types into the input field, I am making an API call to retrieve and display data in a datalist for autocompletion (typeahead). control.get('city').valueChanges.pipe( map((searchText) => searchText.trim().toLowerCase()), fi ...