Adjust the size of an HTML element when the window is resized

I stumbled upon a question that sparked my interest recently, and I've been attempting to apply some of the suggestions provided. Unfortunately, I have been facing difficulties in implementing them successfully. The issue lies with an ASPx page where I have two tables stacked vertically. While I have managed to format the top table to my satisfaction, I am struggling with the bottom one. My goal is to ensure that it fits within the window or alternatively displays a vertical scrollbar.

To address this, I have enclosed the bottom table within a div tagged with overflow-style: auto; in the CSS file. Additionally, I have included the following script on the page to handle resizing:

    $(function () {
        $('.tblContent table').css({ 'height': (($(window).height()) - 50) + 'px' });

        $(window).resize(function () {
            $('.tblContent table').css({ 'height': (($(window).height()) - 50) + 'px' });
        });
    });

The structure of my div element appears as follows:

<div class="tblContent">

Within the CSS file, I have defined the styling for tblContent as follows:

.tblContent 
{
    overflow-style: auto;
}

Answer №1

To create a bottom div with a table that takes up 50% of the screen, you can adjust the width accordingly:

overflow:auto;
height:50%;
width:100%;

Ensure the table itself spans the full width:

width:100%;

Answer №2

I may not be a CSS expert, but it's important to give your table a specific height in order to trigger overflow.

.tblContent 
{
    overflow: auto;
    height : 400px;
    display : block;
}

Why use display: block;? Well, my CSS skills are not great and I tend to rely on that for most things. I also opt for overflow: auto. I'm not entirely sure about overflow-style, but it might be the right choice.

Since we both struggle with CSS, here is a helpful reference link:

http://www.w3schools.com/cssref/pr_pos_overflow.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

Avoid loading external scripts from third-party sources such as JavaScript libraries, Bing Maps API, and Lighthouse performance tool

Issue: The Bing maps API I incorporated into my assignment is loading unnecessary CSS and scripts, causing a noticeable decrease in my lighthouse scores. Is there a way to prevent third-party code (such as Bing Ads) from loading when fetching the maps for ...

A web dialog box designed to gather the complete file path or UNC information

I am currently working on developing a browser application using web2py, which is a Python-based CMS. One specific requirement for this application is to allow users to browse to a folder within the local network or drive. The user will select a folder, an ...

Using JQuery to enable the movement of divs on a screen through clicking and dragging, without the use of

Recently, I've been experimenting with a small project involving draggable divs. However, the code I've written doesn't seem to be functioning properly, causing JQuery to become unresponsive. Is there an alternative method that is simple an ...

What is the significance of incorporating a colon (:) in the route parameter named "/:userQuery"?

What is the significance of using a colon (:) in the route parameter "/:userQuery"? const express = require("express"); const app = express(); app.set("view engine", "ejs"); app.listen(3000); app.get("/:userQuery", (req, res) => { res.render("about ...

Saving JavaScript variables to a JSON file

In my possession is a JSON file named example_json.json, which contains the following data: { "timeline": { "headline":"WELCOME", "type":"default", "text":"People say stuff", "startDate":"10/4/2011 15:02:00", ...

Having difficulty in animating the upward movement of textbox2

I've got a form that contains 2 buttons and 2 textareas. When the form loads, I want to display only section1 (button, textarea) and the section2 button. Upon clicking the section2 button, my aim is to hide the section1 textarea, reveal the section2 t ...

What is the best way to toggle a specific div element within an ng-repeat loop?

I have a list of products. When I click on a product, it should be added to the database. If I click on the same product again, it should be removed from the database. I am toggling the wishlistFlag using ng-click. This works correctly for one div element, ...

Receiving an 'ER_PARSE_ERROR' code in Node/MySQL while attempting to insert approximately 800 records

I have a small project in progress where I am collecting errors from various pages and storing them in a database. Then, I plan to use a graph API to visually display this error information. There are 8 sites with 100 entries on each site, totaling 800 tr ...

I am experiencing issues with my for loop not functioning correctly within my On-Click Event in Javascript

Hello, I'm facing a bit of an issue with a for loop inside an On-Click Event. It seems like the loop is only showing me the last value from the array. Can someone please lend a hand here? Below is the code snippet where I have an array with 10 values ...

What is the process for recording information using a static method in TypeScript within a class?

For my school project, I'm struggling to retrieve the names from a class using a method. One class creates monsters and another extends it. abstract class genMonster { constructor( public id: string, public name: string, public weaknesse ...

An easy guide to comparing date and time using Moment.js

Hey there, I'm looking for some help in validating two instances of date time moments. I am using this helpful Package which allows us to select hour, minute, and second. I want to validate whether the selected date time is before or after, as shown i ...

Difficulty establishing audio calls with Internet Explorer using PeerJS

I successfully implemented a user-to-user audio call system by following the steps outlined in this guide: The system is up and running flawlessly on my website while using Google Chrome. However, I encountered an issue when trying to connect to a user o ...

Concealing rows with empty cells in column E

As someone who is fairly new to incorporating scripts in Google Sheets, I stumbled upon this straightforward and beneficial code snippet online. My objective is to conceal rows within a specified range (rows 10 - 34) where there are blank cells in column E ...

Leveraging the filter functionality within an AngularJS table

I'm currently developing a web application using AngularJS with MVC. I am trying to implement data filtering within my table, but encountering an error during debugging. Here is the specific error message: link to error description Error: $rootSco ...

I encounter a black screen issue while attempting to rotate a three.js cube

How can I rotate a cube around all three axes (x, y, z) using the code snippet provided below? ` <html> <head> <title>CM20219 – Coursework 2 – WebGL</title> <meta charset="utf-8"> < ...

What steps can be taken to resolve the issue with Angular routing?

import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; import {HomeComponent} from "./home/home.component"; import {SettingsComponent} from "./settings/settings.component"; ...

Certain browsers are experiencing issues with the functionality of the input type "date"

I am facing an issue with my ASP.NET web forms page where I am setting the value of an input (type="date") using code-behind. Surprisingly, it displays correctly in Internet Explorer 11 and Firefox 51, but in Chrome 56 and Opera 43, it only shows the mm/dd ...

Problem encountered when trying to create a fixed position Sticky Div

I've attempted to create a sticky DIV using the code below, but it's not behaving as anticipated. The DIV sticks when I scroll down, but it overlaps with the website Header and Footer. How can I fix this issue using CSS/JS? Any assistance would b ...

Ways to showcase HTML content in angular when receiving entities

Is there a way to properly display HTML characters using HTML entities? Take this scenario for example: Let's say we have the following string: $scope.myString = "&lt;analysis mode=&quot;baseball&quot; ftype=&quot;&quot; version ...

Link a model to a User Model in an Express application using Mongoose

I am currently working on developing an application using the MEAN stack, and I'm still in the learning phase. One issue that I am facing is related to referencing my Surf Model to a User Model in express. My goal is to create a new Surf Object with a ...