Setting the height of a webpage element to match the height of the window through CSS

How can I make the .Bck div stretch to the full height of the browser window? I attempted using jQuery, but the height doesn't adjust correctly when I resize the window. What is wrong with my JavaScript and can this be achieved with CSS alone?

<!-- language-all: lang-html -->
    <!DOCTYPE html>
    <head>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <script src="http://www.iconight.it/js/jquery1_11_0.js"></script>
    <script>
    `window.jQuery`(document).ready(function(){
        `window.jQuery`('.Bck').css('height', `window.jQuery`(document).height());
    });
    `window.jQuery`(window).bind("resize", function(){
        `window.jQuery`('.Bck').css('height', `window.jQuery`(document).height());
    });
    </script>
    <style>
    body {
        margin: 0px;
        font-family: arial;
        font-size: 13px;
        background: #000     url("http://fc01.deviantart.net/fs15/i/2007/059/3/7/Drawing_Surface_Paper_Texture_by_Enchantedgal_Stock.jpg");
        color: #FFF;
    }
    .Bck {
        background-image: radial-gradient(ellipse at center, transparent -100%, #000 100%);
        height: 100%;
    }
    #Section{
        text-align: center;
        width: 100%;
        margin: 0px auto;
    }
    #Footer {
        text-align: center;
        padding: 50px 0px;
        width: 100%;
        overflow: hidden;
        margin: 0px auto;
        position: absolute;
        bottom: 0px;
    }
    </style>
    <head>
    <body>
        <div class='Bck'>
            <div id='Section'>Hello world</div>
            <div id='Footer'>aaa</div>
        </div>
    </body>
    </html>

Answer №1

Erase the JavaScript code. Following that, apply a 100% height to both the 'html' and 'body' elements in your CSS style:

html, body {
    height: 100%;
}

Answer №2

Consider utilizing the vh units for better responsiveness:

.background {
    height: 100vh;
}

View it in action: http://jsfiddle.net/bu0qps4s/1/

Some pointers regarding your code:

  1. The convention is to use lower case for id and class names (e.g., "background" instead of "Background")
  2. @01dskoo1's advice about resetting your html and body tags is valid. Refer to this link for more information on eliminating browser inconsistencies with CSS resets. However, this may not be causing issues currently.
  3. Your JavaScript code has a couple of issues:
    1. As mentioned, "window.jQuery results in a string in ECMAScript 6 or a syntax error in earlier versions. Therefore, you should simply use $ instead (e.g., $(document).ready())
    2. Your bind statement should be within $(document).ready so that it executes once the elements to be bound are present in the document.
    3. In jQuery, bind is deprecated; consider using on instead.

Update: If you're facing challenges with excessive content in your .Background div, adjust the overflow settings so that .Background manages it rather than the <body> element (I've updated the fiddle as well):

body {
    overflow: hidden;
}
.background {
    overflow: auto;
}

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

steps for setting up an endless scroll masonry grid

I have incorporated masonry js to display content on my website. Now, I am interested in adding infinite scrolling functionality using this script. I came across a demo that showcases this feature. However, I am struggling to figure out how to implement ...

A step-by-step guide on extracting JSON data from PHP and presenting it in a formatted list

I am working on a simple jQuery AJAX form submission. When the form is submitted, my PHP script echoes json_decode($result). Below is my AJAX script: <script> $("#ajaxquery").live( "submit" , function(){ // Intercept the for ...

Step-by-step guide on how to configure the URL path in an AJAX function call

How can I dynamically set the URL path in an AJAX function call when clicking on a page so that the page name is included in the URL? For example, if I click on a particular page (let's say the "ask" page on Stack Overflow), the URL should look like: ...

Tips on attaching a suffix icon to a material-ui-pickers input box

Here is a snippet of my code: <Box p={6}> <Grid container spacing={2}> <Grid item xs={6}> <TimePicker autoOk label={t('checkIn')} value={time1} onChange={handlecheckIn} clearable /> </Grid> < ...

loading xml data into a table partially using jquery

Currently, I am utilizing AJAX to load and parse XML data. I have constructed a table where I am inserting the data from the XML using a loop. The issue lies in the fact that only around 3000 rows are being inserted into the table even though the XML conta ...

`Hovering over a div triggers a continuous animation loop`

I've gone through various questions and solutions related to this issue, but unfortunately, none of them seem to work for me. It's possible that I might be overlooking something or my scenario is slightly unique. The main problem I'm facing ...

Refresh selected items after inserting data via ajax in CodeIgniter

I have a select list on my view that allows users to add new items using a plus button. However, when a new item is added, the list does not refresh. I don't want to refresh the entire page with an ajax query. Instead, I am trying to implement a metho ...

- "Queries about Javascript answered with a drop-down twist

Having some trouble with setting up a straightforward FAQ dropdown feature. Could someone lend a hand and see what might be going wrong? Appreciate your help! CSS #faqs h3 { cursor:pointer; } #faqs h3.active { color:#d74646; } #faqs div { height:0; o ...

What is the best way to create a toggle button that can show more or show less of a text snippet with animation?

Is it possible for someone to assist me with showing a long text partially and providing a "show more" button to reveal the rest, along with a "show less" option, all with some CSS animation? I was thinking of using a font awesome arrow down icon for expan ...

Items in Bootstrap row are not aligning in the center

Utilizing the Bootstrap 5 row property for responsiveness on my website, I encountered an issue where two divs placed next to each other did not align at the center of the webpage as expected. There was an unusual gap on the right that I couldn't elim ...

Is it possible to integrate an html page within a wordpress website?

I'm a WordPress newbie but I have experience with HTML, CSS, JS, and PHP. Can anyone guide me on how to incorporate existing HTML pages into my WordPress site? I recently set up WordPress successfully on my domain hosted by GoDaddy (www.mydomain.com) ...

What is the reason behind the observation of executed javascript upon receiving a JSON response from the server?

My current knowledge of HTTP involves web browsers sending mainly GET or POST requests, with the server responding accordingly. I am aware that servers can return a complete HTML document that is ready to be displayed in a web browser. Additionally, I un ...

Arranging three items within a div container

I am facing an issue with the layout provided in the code snippet below. Essentially, there is a div container with tools on the left side, main content in the center, and tools on the right side (a visual drag handle on the left and a delete button on the ...

What is the best way to incorporate both the left and right menus with the main content

My goal is to incorporate a left and right menu alongside a main feed in the center. However, I'm encountering an issue where the left and right menus are scrolling with the scroll bar. Ideally, I would like the main feed to scroll with the bar while ...

Guide to extracting the data from a specific row in an HTML table and converting it into an array

I am trying to extract a specific row content from the features-table and store it in an array. However, I am facing an issue because I have embedded the table within an iframe. The requirement is that this extraction should occur when a user clicks on the ...

Effective steps after Ajax request

Whenever a user clicks the "like" button, I perform a check in the database to see if they have already liked the photo. This check is done using ajax. If the photo has already been liked, the success message will indicate "already liked", otherwise it wi ...

How can I retrieve the POST values using xmlhttp?

This is an example of AJAX functionality. xmlhttp.send("fname=Henry&lname=Ford"); UPDATE I am looking to extract values from text/input fields after the equals sign. The current setup shows the values directly written out. How can I retrieve and dis ...

Pass the callback function `cb_func` as a parameter to the `foobar` function when calling it in the `onclick`

I have an element that I want to trigger an onclick event with a callback function. Here is the code snippet: function cb_func() { alert("hello world!"); } function foobar(i, cb) { // do something if (cb != undefined) cb(); } onclic ...

Add a new row to the table when a dropdown option is selected, and remove the row when deleted. Ensure that the row is only added

Here is my specific requirement: I need a table with a default row containing a dropdown menu in the first column. When an option is selected from the dropdown, a new table row should be added with the same content as the main row and a delete button for ...

Is it possible to install non-root dependencies in node_modules using NPM?

The repository @superflycss/component-navbox contains the following list of dependencies: "devDependencies": { "@superflycss/component-body": "^1.0.1", "@superflycss/component-display": "^1.0.2", "@superflycss/component-header" ...