What is the best way to adjust the height of a container to equal the viewport height minus a 300px height slider?

Forgive me for the rookie question, I know what I want to achieve should be simple but I seem to be having trouble articulating it in my search for solutions.

Essentially, I am trying to set a section in HTML to the height of the viewport minus the height of the navigation bar and a small slider at the bottom.

Do I need JavaScript to accomplish this? Any assistance would be greatly appreciated.

I've tried using the following CSS:

.container {
    height: 100%;
    margin-top: -300px;
    min-width: 100%;
}

.slider-wrapper {
    height: 300px;
    min-width: 100%;
}

However, this doesn't have the desired effect as it cuts off the top of the container. Is there some jQuery math that can be used to subtract the height of .slider from the height of .container?

As requested in the comments, here is additional markup, including only the container sections:

<section class="top-bar">
</section>

<section id="container">
</section>

<section class="slider-wrapper">
</section>

Those are the three containers that currently make up the layout of the screen. Here is the CSS for the #container:

#container {
    background: url(../img/cafe-background.jpg) no-repeat center center fixed;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
    width: 100%;
    height: 100%;
    position: relative;
}

So, I just need to subtract the height of the .slider-wrapper from this container.

Answer №1

It appears that you are inquiring about utilizing calc() with viewport height units:

.container {
        height: calc(100vh - 300px);
}

Answer №2

Using jQuery:

You can see a live demonstration here

<head>
 <script src="https://code.jquery.com/jquery-2.1.0.min.js"></script>
 <script>
     $(document).ready(function () {
        var indexd = parseInt($(document).height()) - parseInt($('.slider-wrapper').css('height')) + 'px';
        $('#container').css('height', indexd);
     });
 </script>
</head>

Answer №3

Instead of relying on JavaScript, you can achieve the desired effects using the css3 calc function:

.container {
    height: calc(100% - 300px)
    min-width: 100%;
}

.slider-wrapper {
    height: 300px;
    min-width: 100%;
}; 

To automatically adjust the size of .slider-wrapper, consider nesting it within a section in your HTML markup like this:

<section id="container">
   <section class="slider-wrapper">
   </section>
</section>

Answer №4

If you are looking to achieve a layout similar to this example, here is how it can be done: The #top-bar and #slide-wrapper are fixed in position with preset heights. The #container fits between them by specifying top and bottom values, also with a fixed position.

However, a potential issue arises if the content within #container exceeds its height, resulting in a strange scrollbar behavior.

To address this problem, one solution is to change the position of #container to relative and add margin to the top and bottom accordingly.

It is worth noting that achieving this layout does not necessarily require CSS3 or JavaScript. Understanding the position property in CSS is key, and there are plenty of resources available online to help grasp its concept.

For reference:

http://jsfiddle.net/e0tpnojf/

HTML Code:

<section id="top-bar">
</section>

<section id="container">
</section>

<section id="slider-wrapper">
</section>

CSS Code:

#top-bar{
    width: 100%;
    background: blue;
    height: 100px;
    position: fixed;
    top: 0px;
}
#container{
    width: 100%;
    background: yellow;
    top: 105px;
    bottom: 105px;
    position: fixed;
}
#slider-wrapper{
    width: 100%;
    background: red;
    height: 100px;
    position: fixed;
    bottom: 0px;
}

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

Step-by-step guide on repairing a countdown clock using JavaScript, HTML, and

I'm having issues with my JS on my website. I am new to this and currently in the process of setting up a website. I want to add a timer to show when the site will be active. It seems like there is an error somewhere in the JS code that I can't ...

Tips for accessing an Angular service from different Angular controllers

I am a beginner with angular js and I am currently exploring ways to call the service provided in the code snippet below from a controller. The service is defined as follows. app.factory('myappFactory', ['$http', function($http) { v ...

transmitting error messages from a service to a controller in AngularJS

Controller.js var vm = this; vm.admin = {}; vm.add = function () { API.addAdmin(token, vm.admin) .then(function (resp) { vm.hideForm = true; vm.showButton = true; Notify.green(resp); }, function (re ...

React component that enables radio inputs to repeat upon selection

My current project involves creating a quiz app where users can answer single questions using React on Codepen. I am utilizing an API to fetch a question, along with 3 incorrect answers and 1 correct answer, then storing them in the app's state. Howev ...

Ways to arrange an array in JavaScript or jQuery when each array record contains multiple objects

Before giving a negative vote, please note that I have thoroughly searched for solutions to this problem and found none similar to what I am facing. I am looking to alphabetically sort the array by image.name using JavaScript or jQuery: var myArray = [{ ...

The jQuery ajaxError() function triggers if the user navigates away from the page before it has fully loaded

We have implemented a global ajaxError() handler using jQuery to notify users of any AJAX failures: $(document).ajaxError(function() { $("There was a network or server error. Please try again later.").dialog({ title: "Error", mod ...

Generating and Importing Graphics through Iterations in Javascript

I apologize for my lack of experience in this matter. Can someone please guide me on how to utilize a loop to load images? For instance, could you show me how to rewrite the code below using a loop to automate the process? function loadImages() { let ...

How can we use jQuery to animate scrolling down to a specific <div> when clicking on a link in one HTML page and then navigating to another HTML page?

I'm currently working on creating a website for my friend who is a massage therapist. Utilizing a bootstrap dropdown menu, I have added links to different treatments that direct to the treatment.html from the index.html page. Is there a way to creat ...

Utilize Mongoose to seamlessly integrate online shopping cart items into MongoDB

I am facing an issue while trying to insert a shopping cart of items in the form of a JSON object into a MongoDB collection using a mongoose schema. Although the customer's ID is successfully stored (extracted from the User DB), unfortunately, the ca ...

Translating coordinates into their corresponding location on the chart

I'm currently working with a dataset containing information about an area in Western Europe. I am trying to convert coordinates into values within this table, facing a challenge similar to the one described in this query. However, I lack experience in ...

Clear out all current cookies

I utilized the following JavaScript code to generate a pop-up window on the website that would only appear once. However, my client now wants to launch a new promotion and I am attempting to remove existing cookies so that the pop-up window displays again ...

What is the procedure to switch the state using useState when an event is activated?

I'm trying to learn React by implementing a basic shop. One idea I had was to have multiple product images and when a user clicks on an image, it flips around to display additional information such as comments and ratings for the product. For this pr ...

Developing a tool for switching between languages in an internationalization application

I have been exploring the implementation of Lingui(i18n) in apps. All set up, but I'm interested in adding a language switcher to enable users to change between language catalogs on my app. Here's my index.js file: import React, { useEffect } fr ...

Can using display: none impact the SEO of a website's navigation menu?

Is there a difference in SEO impact between using display:none with CSS and using display:none with jQuery for menus? Appreciate the insights! ...

Exploring the option of eliminating the email field from the PHP redirect function and transforming it into a pop-up notification

I am currently utilizing the following code to send an email notification to my address whenever a new user signs up: <?php $errors = ''; $myemail = '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b0ded1ddd ...

The received data object appears to be currently undefined

I am currently using MapBox to display multiple coordinates on a map, but I am encountering an issue where the longitude and latitude values in my return object are showing up as undefined. Could there be a missing configuration that is preventing the data ...

What is the best way to deactivate a button using AngularJS?

$scope.date = moment(currentDate); $scope.date1 = moment(currentDate).add(-1, 'days'); function checkDate(){ if ($scope.date > $scope.date1) { return true; } else{ return false; } }; checkDate(); ...

I'm struggling with my project called "Number TSP" and I can't seem to figure out why it's not working properly

Upon reaching the end of my code, I am encountering an issue where instead of seeing the expected output of "Done!", it displays undefined. This is the code in question: const container = document.querySelector(".container") const table = document.querySe ...

Creating a time-limited personal link with Django Rest Framework and React

I have a frontend application built with React Framework that I want to provide access to via a time-limited personal link, without the need for additional authentication functions. With approximately 1-2 new users per day, my proposed implementation is as ...

How can I import an excel spreadsheet using Javascript, or alternatively, convert an excel file to a text document

I have been updating my file upload page to handle both text and excel files. However, when trying to read excel files with my current code, I am getting strange output. It seems that my function, which was originally designed for text files, needs modific ...