Adjusting table dimensions according to webpage dimensions

When looking at the image below, you will notice a div that contains a table with some content. However, when resizing the web browser's page, the table goes beyond the boundaries of the div.

https://i.sstatic.net/SRlzM.png

My goal is to reduce the size of the table as it scales down, while still displaying all of its contents without the need for a scrollbar or similar solution.

EDIT

#profileTable{
width:auto;
text-align:left;
color:#000080;
padding:30px 60px;
border:1px solid #ccc;
border-radius:4px;
background-color:white;
margin: 0 50px 50px;
}


#content{
margin: 0px 60px 0px;
background-color:#E9E9E9;
border:solid 1px grey;
flex: 1;
clear:both;
position:relative;
}
<html>
<head>
</head>

<body>
    <div id="content" >
        <h2><center>Your profile</center></h2>
        <table id="profileTable">
          <tr>
            <th>Username:</th>
            <td>CONTENT</td>
          </tr>
          <tr>
            <th>First name:</th>
            <td>CONTENT</td>
          </tr>        
        </table>
    </div>
</body>
</html>

Answer №1

Utilizing flex for the tr element can allow wrapping without relying on media queries. It's worth noting that this entire structure, denoted as a table, could also function effectively as a list using ul or dl.

To achieve visually collapsing horizontal margins, consider converting them to width using `calc()` and `min-width` properties.

For example:

#profileTable {
  text-align: left;
  color: #000080;
  padding: 30px 60px;
  border: 1px solid #ccc;
  border-radius: 4px;
  background-color: white;
  width: calc(100% - 100px);
  min-width: 270px;/* adjust based on your requirements */
  margin: 0 auto 2em;
}

#content {
  width: calc(100% - 120px);
  min-width: 360px;/* adjust based on your requirements */
  margin: auto;
  background-color: #E9E9E9;
  border: solid 1px grey;
  flex: 1;
  clear: both;
  position: relative;
}

tr {
  display: flex;
  flex-wrap: wrap;
}

th {
  white-space: nowrap;
  min-width: 5em;/* adjust based on your requirements */
}

td {
  flex: 1;
}

h2 {
  text-align: center;
}
<div id="content">
  <h2>Your profile</h2>
  <table id="profileTable">
    <tr>
      <th>Username:</th>
      <td>CONTENT</td>
    </tr>
    <tr>
      <th>First name:</th>
      <td>CONTENT</td>
    </tr>
  </table>
</div>

Answer №2

Check out the functionality of the table-responsive feature in Bootstrap.

Visit this link for more information on Bootstrap tables.

Include the following code:

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<table class="table table-responsive">

After that, you can

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 JavaScript and PHP to dynamically update a field based on two input values within a row of a table

Currently in the process of developing a dynamic profit margin calculator for a personal project. Overview Data is retrieved from a database table using SQL and PHP After necessary validations, the data is dynamically displayed as rows in an HTML table ...

The background image's fadeInOut transitions create a strange phenomenon where all the text appears in white

So, here's a strange issue I'm facing. On my website, the background image changes with a smooth fadeIn/Out transition. Video: Website in action: Here is the HTML markup: <div class="fondo" onclick="descargar(2,500);descargar(1,500);"> ...

The property 'innerHTML' cannot be assigned to null, as stated in Vue

I am dealing with two components that allow for editing JSON objects. Additionally, I have a button that toggles between these two components. Below is the HTML code snippet: <v-btn @click="switchConfigClicked">Switch config</v-btn> <h4 cla ...

Deploying an Azure Blob Trigger in TypeScript does not initiate the trigger

After successfully testing my Azure function locally, I deployed it only to find that it fails to trigger when a file is uploaded to the video-temp container. { "bindings": [ { "name": "myBlob", "type&qu ...

The function of jQuery .click() not triggering on elements within msDropDown

I'm having difficulty implementing jQuery on an Adobe Business Catalyst site. The HTML snippet below shows the structure: <div class="banner-main"> <div class="banner-top"> <section class="banner"> <div class="catProd ...

Identify the element with the active class name as the primary focus

Can anyone assist with this issue? Below is an example of the code I am working with: import React, { useRef, useEffect } from "react"; function App() { const inputRef = useRef(null); useEffect(() => { const testElement = document.qu ...

Unable to transmit information to a different page

My goal is to transmit both hidden data and user input data to another page. Initially, I attempted to have just one user input field, which allowed the system to transmit the data successfully. <h1><strong>Please scan barcode on JIG</stron ...

What is the best way to horizontally align my divs and ensure they stack properly using media queries?

My layout is not working as expected - I want these two elements to be side by side in the center before a media query, and then stack on top of each other when it hits. But for some reason, it's not behaving the way I intended. I've tried to ce ...

Are the files selected by the user not displaying properly in the URL?

I'm currently working on a project using PhoneGap where I am facing an issue with uploading files and adding all file names to the parameters. The desired format is: http://www.example.com/uplaod.html?file=file1,file2,file3 To achieve this, I have a ...

Display specific data within the <td> tag

I am attempting to display a specific value that is located within a <td> element. The values are retrieved from a webpage and they are structured as follows: <b>General Information</b> <table width="400"> <tr> ...

Retrieve the child element that is being clicked

Alright, I'm facing a little issue here (it seems simple, but I just can't seem to crack it)... Let me paint the picture with a snippet of HTML code below: <!-- New Website #1 --> <!DOCTYPE html> <html style='min-height:0px; ...

how can I modify the css style of an html element only when a specific sibling is present?

After searching through the MDN CSS documentation, I was unable to find any Combinators that can target the specific element I want to style. /* second-span red when first-span is present */ [first-span] ~ [second-span] { color: red; } /* firs ...

The custom directive containing ng-switch isn't being reevaluated when the value is updated

I have developed a unique directive that acts as a reusable form. The form includes an error message display section which utilizes ng-switch: <div ng-switch="status"> <span ng-switch-when="Error" class="text-error">An Error occurred</spa ...

Unlock the power of automatic code reloading in Rails with these simple steps

Looking for a way to implement 'hot code reloading' in a development Rails application? Let's say you're working on a Rails project and make changes to a stylesheet. Currently, you have to refresh the page using cmd-r or by clicking th ...

Fixed width blocks automatically adjusting to small containers

When I try to add text to my absolute block inner div, it doesn't expand as expected. I am aware that expanding within a parent container with a specified width, such as <div class="main_wrap"></div>, can be problematic. Unfortunately, I ...

Guide to building a robust tab system using JQuery and Ajax

I am currently working on a mockup page where the accordion feature is functioning well. However, I am looking to implement a tab-like system that will allow users to navigate between different pages. Specifically, I want users to be able to click on page ...

Is there a way for me to incorporate a JavaScript file from another JavaScript file in HTML, even if it's just one line below?

It seems that the issue of using a function or a variable from file1 in file2 is not working as expected: <head> <script src="file1.js"></script> <script src="file2.js"></script> </head> Since file2 ...

Issue with transition effect not functioning properly when hovering in HTML and CSS

Is there a way to achieve a slow smooth transition when hovering over the .bg-gradient element and have the same effect when removing the cursor? Currently, the transition happens quickly on hover. .asc { height: 500px; background-position: 50%; ...

jQuery mobile enhancing user experience with dynamic background images

I am having trouble setting different background images for each page in jQuery Mobile. <div data-role="page" id="pageone">... </div> <div data-role="page" id="pagetwo">... </div> Since each page has a unique ID, I tried ...

"Learn how to clear an input field using jQuery with this simple guide

I've gone through a few discussions, such as this one and that one, but I'm still struggling to clear the input field after submission. What is the simplest way to achieve this? This is my current approach: $(document).ready(function(){ $(& ...