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

"Utilizing jQuery and Bootstrap 4 in TypeScript, attempting to close modal window using jQuery is not functioning

Trying to make use of jquery to close a bootstrap modal within an angular project using typescript code. The following is the code: function call in html: (click)="populaterfpfromsaved(i, createSaved, createProp)" createSaved and createProp are local ...

What is the best way to extract information from a .txt file and transform it into an integer format?

I am currently working on a project involving a distance sensor that retrieves data from a Raspberry Pi and displays it on a website. The goal is to have the data stored in a .txt file and then transferred to the website for display. I am seeking assistanc ...

Posting Form Data with Ajax in CodeIgniter

Incorporating the CodeIgniter framework along with the jQuery Form plugin available at http://malsup.com/jquery/form/ Encountering challenges in ensuring proper functionality of a form. View <div class="row"> <div class="well c ...

What is the best method to fill a mat-select dropdown with an existing value?

I am encountering an issue with a mat-form-field that contains a dropdown of data fetched from an API. I can successfully select an option and save it to the form. However, upon returning to the dropdown or reloading the page, the saved value does not appe ...

preventing the ball from landing inside the container (JavaScript)

I developed a straightforward website with the following functionality: Upon entering any text into the prompt and clicking okay, a ball will drop into the 1st box, namely the Past Thoughts box. Below is the code snippet: HTML <h1> Welcome t ...

`Only firing event listener once`

I have created a JS module where I am adding a click event to all links that match a specific selector. Here is an example of how it's done: var Lightbox = (function () { var showLightbox = function () { // this does stuff }; var init = fu ...

I encountered an error in my Rails 4 project where I got a NoMethodError stating "undefined method `id' for nil:NilClass" in my create.js

When I click a button, I'm attempting to display a partial. It seems like I am making an obvious mistake... and I suspect it's not related to the following code snippet: $('#modrequest').empty(); $('#modrequest').html("<%= ...

Typescript versus ES5: A comparison of Node.js server-side applications written in different languages

Note: When I mention regular JavaScript, I am referring to the ES5 version of JS. As I lay down the groundwork for a new project, my chosen tech stack consists of Node.js for the back-end with Angular2 for the front-end/client-side, and Gulp as the build ...

Create a JavaScript array containing all the elements from a MySQL table

I am attempting to store my mysql table data in a JavaScript array. My goal is to push each element of the table into the array so that it looks like this: array[0] = [question: information01, answer: information02...] array[1] = [question: information11, ...

Issue in Firefox: Footer is wrapping around the main content

Hey, I've been dealing with a pesky bug that just won't go away. We're currently working on a redesign for www.petpoint.com - The footer looks perfectly fine in every browser except for Firefox. It gets all jumbled up for some reason. I&a ...

X-ray Picker failing to retrieve a value

I am currently using the most recent version of the x-ray npm module in the code snippet below. Despite my expectations, the Meta and Metatags elements remain empty when I print out obj. Can anyone help me identify what I might be missing? var Xray = req ...

Having trouble transferring a PHP string to jQuery

I recently set up a script on my computer that displays a list of active interfaces: $ interfaces eth0 lo wlan0 Now, let me share my PHP code snippet with you: <?php $output=shell_exec('./interfaces'); $string = trim(preg_replace(&ap ...

Despite declaring a default export, the code does not include one

Software decays over time. After making a small modification to a GitHub project that was three years old, the rebuild failed due to automatic security patches. I managed to fix everything except for an issue with a default import. The specific error mess ...

Is there a way to transition an element from a fixed layout position to an absolute layout position through animation?

I am looking to create a dynamic animation effect for a button within a form. The goal is for the button to move to the center of the form, while simultaneously undergoing a horizontal flip using a scale transform. During the midpoint of this animation, I ...

Is it possible to extract information from a string with regular expressions?

As I sift through a myriad of arbitrary "Header" data in node. Here's an example of what it looks like: _Aa:GA1.1.78037747.867108, 44907=5xyz; Webstorm-a36041d5=9fbb-48e9-b19e-e3f0a3282151; srce=coolernode; nsid=1234; cookie_data=T%3D1; _gat_PP= ...

Next.js is constantly fetching data with React Query

Within my Next.js project, I incorporated a query client into a page component, utilizing getServerSideProps for server-side rendering. The structure of the page is as follows: const Index = ({ configData }) => { const { t } = useTranslation(); cons ...

Elevate the element from the choice API to the organization API using this.$parent

I recently developed a Vue 3 component called "Tab" using the option API. Here is the code: export default { name: "Tab", props: { name: {required: true}, iconClass: {required: true}, selected: {default: false} }, da ...

What could be causing this jQuery selector to not select any elements?

Here's a simple question with some code that needs troubleshooting: $insides = $('<thead><tr><th><!--Blank space --></th></tr></thead><tbody><tr class="green-row"><td>Opportunities to D ...

Having difficulty viewing the images clearly

My JavaScript codes for scrolling images are not displaying all the images when viewed on Google Chrome. Is there a solution available? Below is the included JS code and CSS. data=[ [" images/img1.jpg"," Image1","pic01.jpg"], [" images/img2.jpg","Image2 " ...

Please enter only numerical values using jQuery

Currently, I am facing a slight issue. My goal is to only run the code when the input characters are numbers. This is the snippet of code I have been working with: $(".jq-sales, .jq-variablecosts, .jq-fixedcosts, .jq-additional-sales, .jq-sales-units, .j ...