An assessment consisting of three parts, with the initial section required to match the size of the browser window using jQuery and Javascript

UPDATE: The issue was resolved by adding the Doctype at the top of the page - "<!DOCTYPE html>" (no spaces between tags < , > and other characters)

I am experimenting with a webpage that contains four sections. The first section should take up the height of the browser window. To achieve this, I tried using both jQuery and JavaScript code. Interestingly, the JavaScript code works while the jQuery code does not. Here are the codes:

jQuery:

$("section:first").css("min-height", $(window).height());
$(window).resize(function() {
    $("section:first").css("min-height", $(window).height());
});

JavaScript:

var first = document.getElementsByTagName("section")[0];
first.style.minHeight = window.innerHeight + "px";
window.onresize = function() {
    first.style.minHeight = window.innerHeight + "px";
}

I use an iMac with a resolution of 1920x1080 and my Chrome browser window height is 896px. Strangely, when I use the jQuery code (which does not produce any console errors!), the min-height value increases as I resize the window (always going up) based on the number of other sections. Does anyone know why this happens?

Below is the HTML and CSS code snippet:

body {
    margin: 0;
    background-image: url("images/bg.jpg");
    background-repeat: repeat;
    background-position: center;
    background-attachment: fixed;
    background-size: 713px 518px;
}

.content {
    background-color: #FFF;
}

.internal {
    position: relative;
    padding: 1% 20px;
    margin: 0 auto;
    border-bottom: 1px solid #21D561;
}



<section> Some content here </section>

<section class="content">
   <div class="internal">
     <p>Here a big Lorem Ipsum in eight paragraphs</p>
   </div>
</section>

<section class="content">
   <div class="internal">
     <p>Here a big Lorem Ipsum in six paragraphs</p>
   </div>
</section>

<section class="content">
   <div class="internal">
     <p>Here a big Lorem Ipsum in six paragraphs</p>
   </div>
</section>

You can view the unexpected outcome here:

Answer №1

When working with pure JavaScript, your code would look like this:

var main = document.getElementsByTagName("article")[0];
main.style.maxWidth = window.innerWidth + "px";
window.onresize = function() {
    main.style.maxWidth = window.innerWidth + "px";
}

But in jQuery, you might forget to include + "px":

$("article:first").css("max-width", $(window).width());
$(window).resize(function() {
    $("article:first").css("max-width", $(window).width());
});

For a proper implementation, consider updating your jQuery code to:

$("article:first").css("max-width", $(window).width() + "px");
$(window).resize(function() {
    $("article:first").css("max-width", $(window).width() + "px");
});

Remember, .width() only returns an integer, so make sure to concatenate + "px" to match the behavior in pure JavaScript.

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

Troubleshooting NodeJS and Express: Issue accessing a function located outside a folder

I'm having trouble accessing the function I exported in app.js Here is the code snippet from app.js: function getConnection() { return mysql.createPool({ host: 'localhost', user: 'root', password: &apo ...

Troubles with modal functionality in Ionic application involving ion-slide-box

Utilizing ion-slider to display images has been a seamless experience, except for one hiccup. If I navigate directly from the first full image back to the home screen, the slider ceases to function properly. To address this challenge, I have employed spec ...

Move your cursor over an image to modify the z-index of another image

I am facing an issue with my webpage that consists of 6 small images and one large image in the center, made up of six layers each containing one image, similar to this example: http://jsbin.com/onujiq/1/. I have set the z-index property of all center imag ...

Unable to access placeholder information from the controller

I am new to implementing the mean stack. I attempted to view data from the controller, but encountered an error message in the web browser's console. Error: [$controller:ctrlreg] http://errors.angularjs.org/1.6.3/$controller/ctrlreg?p0=AppCtrl Stack ...

Concealing the URL Once Links are Accessed

I have a Movie / TV Shows Streaming website and recently I've noticed visitors from other similar sites are coming to my site and copying my links. Is there a way to hide the links in the address bar to make it more difficult for them to access my con ...

The frequency of database updates exceeds expectations - involving vue.js this.$router.push and express operations

Having some trouble updating a MongoDB with this code. It seems to be updating three times instead of just once due to having three dates in the posts.date field. Utilizing Vue, Mongo, and Express for this project, I have the following data structure: { ...

Angular: When $scope variable is modified, a blank page issue arises

In my Angular application, I have a template called viewAll.html that is displayed after clicking on a link. This template fetches data via AJAX using scope variables. However, I encountered an issue where updating these scope variables through AJAX cause ...

Is the <style> tag effective when placed within other elements?

When I look at it, I notice something along these lines: <div> <style type="text/css"> ... </style> </div> It seems weird, but it's functional. Does this go against any standards? ...

Utilize jQuery for parsing JSON data

Asking for help here because I am struggling with a seemingly simple task. Here is the JSON data that's causing me trouble: {"name":"cust_num","comparison":"starts_with","value":"01"}, {"name":"cust_name","comparison":"starts_with","value":"ad"}, {"n ...

User agreement required for HTML5 geolocation custom notifications

Currently implementing HTML5 geolocation on my mobile website. I am exploring the possibility of displaying a custom notification instead of the default web browser notification to request user consent for sharing their location. This custom notification ...

Images cascading like a downpour on a canvas (Javascript)

I have been experimenting with canvas, attempting to create a simulation of random falling objects. I've successfully drawn the background image, but I'm having trouble with the second image that is supposed to simulate a rain drop. I've ma ...

Sending images from jQuery to Node.js server

Seeking assistance with uploading a file from an Android application to node.js using Express and jQuery. Here is the client-side code snippet: function uploadData(win) { var padI = imagedata.length-1 while( '=' == imagedata[pad ...

Comparison: Chrome extension - utilizing default pop-up vs injecting a div directly into the page

I find myself perplexed by the common practices used in popular Chrome extensions. I am currently working on creating my own Chrome extension and after completing a basic tutorial, I have set up a default popup page that appears when clicking the extensi ...

Encountering a registration error persistently: [TypeError: Network request failed]

Currently, I am in the process of developing an application using React for the frontend and node.js for the backend. However, I have encountered a persistent network error whenever I try to sign up or log in. What puzzles me is that when I test the API en ...

Where should data processing be conducted: in the service or controller layer?

Here's a question regarding the best practices for organizing code. I'm fetching data from an API using $resource and I need to manipulate it before displaying it on the view. My dilemma is at which stage should I process the data? My current b ...

The art of efficiently handling and outputting an array of files using node

In a folder filled with markdown files like so: myDir |- fileA.md |- fileB.md |- fileC.md |- fileD.md I want to extract just the filenames without the file extension and store them in an array. This is my attempt: var mdFiles = fs.readdir('myDir&a ...

I am interested in designing an arrow shape with the help of CSS. Kindly refer to the

Can someone assist me in creating a CSS arrow shape that resembles the one shown in this screenshot? Also, I am looking to create next and previous post buttons similar to this. Any guidance would be appreciated. ...

Error: The term "Particles" has not been defined

I'm attempting to integrate code from a website into my project, but encountered an error when the particles failed to run after adding it. I downloaded and installed particle.js from "https://github.com/marcbruederlin/particles.js/issues" for this pu ...

reversed json using javascript

Is it possible to efficiently reverse the order of the following JSON object: { "10": "..." "11": "...", "12": "...", "13": "...", "14": "...", } so that it becomes: { "14": "...", "13": "...", "12": "...", "11": "... ...

Top solution for maintaining smooth navigation across web pages

As I dive into the world of web development, I find myself intrigued by the idea of reusing navigation and banners across multiple web pages. However, despite my research efforts, I have yet to come across a definitive answer. My objective is simple: The ...