Hidden body text occurs when the div element is set to be sticky at the bottom

I have been trying to resolve an issue with a sticky element at the bottom of a page using javascript/jquery. However, the sticky element is hiding the body text. I want to display all body text without it being hidden by the sticky element, but I can't seem to pinpoint the exact problem in my code. Can someone please provide suggestions on what may be causing this issue?

Check out the CodePen demo for reference

Javascript:

var link = $('#navbar');
var offset = link.offset();
var top = offset.top;
var left = offset.left;
var bottom = $(window).height() - top - link.height();
bottom = offset.top - bottom;
var right = $(window).width() - link.width();
right = offset.left - right;

CSS:

#navbar {
  position: fixed;
  bottom: 0;
  width: 100%;
  color: #fff;
  min-height: 50px;
  max-height: 150px;

  overflow: hidden;
  background-color: #333;
}

Answer №1

To achieve the desired layout, assign the value of the outerHeight() jQuery function for the #navbar to the .content element as the margin-bottom property using the css() jQuery method.

var link = $('#navbar');
var content = $('.content');
var linkHeight = link.outerHeight();
content.css({
  "margin-bottom": linkHeight
});
body {
  margin: 0;
  font-size: 28px;
}

.header {
  background-color: #f1f1f1;
  padding: 30px;
  text-align: center;
}

#navbar {
  position: fixed;
  bottom: 0;
  width: 100%;
  color: #fff;
  min-height: 50px;
  max-height: 150px;
  overflow: hidden;
  background-color: #333;
}

#navbar a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

#navbar a:hover {
  background-color: #ddd;
  color: black;
}

#navbar a.active {
  background-color: #4CAF50;
  color: white;
}

.content {
  padding: 16px;
}

.sticky {
  position: relative;
  bottom: 0;
  width: 100%;
}

.sticky+.content {
  padding-top: 60px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="header">
  <h2>Scroll Down</h2>
  <p>Scroll down to see the sticky effect.</p>
</div>
<div class="content">
  <h3>Sticky Navigation Example</h3>
  <p>The navbar will stick to the top when you reach its scroll position.</p>
  <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
  <p>Some text...


Alternatively, you can utilize the position:sticky CSS property if browser support is not an issue.

body {
  margin: 0;
  font-size: 28px;
}

.header {
  background-color: #f1f1f1;
  padding: 30px;
  text-align: center;
}

#navbar {
  position: sticky;
  bottom: 0;
  width: 100%;
  color: #fff;
  min-height: 50px;
  max-height: 150px;
  overflow: hidden;
  background-color: #333;
}

#navbar a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

#navbar a:hover {
  background-color: #ddd;
  color: black;
}

#navbar a.active {
  background-color: #4CAF50;
  color: white;
}

.content {
  padding: 16px;
}

.sticky {
  position: relative;
  bottom: 0;
  width: 100%;
}

.sticky+.content {
  padding-top: 60px;
}
<div class="header">
  <h2>Scroll Down</h2>
  <p>Scroll down to see...

Answer №2

It's simple, you can achieve this by applying the margin-bottom property in your CSS code

<div class="container" style="margin-bottom: 100px">

Answer №3

If you want to add some space at the bottom of the content div, consider using a padding-bottom value.

.content {
    padding-bottom: 88px;   
}

However, be mindful that adding padding may cause the text to wrap and change the height of the div. In such cases, use a media query to adjust the layout accordingly.

$(".content").css("padding-bottom",$("#navbar").height());

Answer №4

To achieve your desired layout, apply a margin-bottom using the following method:

$(".main-section").css("margin-bottom",$("#fixed-nav").height());

This piece of code dynamically adjusts the content's margin-bottom based on the fixed navigation bar's height. Give it a try and see if it meets your requirements.

Let me know if you need further assistance.

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

Is it possible to create a fading effect on an image using a border-radius of 50%?

Struggling with fading out an image that's circular in shape due to the border-radius: 50%. <section id="main"> <h1>Non-Important-Text</h1> <h4> it's all fun and games until you can't find a damn sol ...

Place a label within a container and surround it with a single border. Inside the container, create an unordered list

I'm attempting to design a filter dropdown feature where only the label of the dropdown is visible at first, and then upon clicking the label, a list of options will drop down over the top of the remaining content using absolute positioning. The chall ...

Troubleshooting Cufon Compatibility with Internet Explorer - Investigating a CSS Problem

Currently facing an unusual issue. I am creating a Wordpress theme with Cufon and it works flawlessly in Chrome and Firefox, but refuses to render in IE. I decided to put this problem on hold and focus on it later as I am still in the development phase. Y ...

Server Sent Events not being received by client from Express.js server

My Next.js (React) client is set up to receive Server-Sent Events from my Node.js/Express.js server, but it seems like it's not receiving any messages for some unknown reason. While the open and error events of EventSource are functioning correctly, ...

Struggling with Incorporating Variables from Jquery?

I am facing an issue with my local website built using PHP. Within my project, I have a file named functions.php containing the following code: public function saveAll($idmt_salesarea, $thn_bln, $no_pesanan, $tgl_pesan, $idms_langganan, $idms_kodebarang, ...

Automatically numbering table columns with custom language localization in JavaScript using Jquery

Is there a method to automatically number table data in a local or custom language? As a Bengali individual, I have figured out how to automatically number the first data of each row using css and js. However, I am uncertain about how to implement custom n ...

Creating an Angular table row that can expand and collapse using ng-bootstrap components is a convenient and

I need assistance with an application I am developing, where I want to expand a table row to display details when it is clicked. The issue I am facing is that currently, all rows expand and show the data of the clicked row as seen in the image result below ...

Guide on incorporating a Bootstrap date time picker in an MVC view

Is there a live example available that demonstrates how to implement the latest Bootstrap date time picker in an MVC view? In my project, I have already included: - jQuery. - Bootstrap JS. - Bootstrap CSS. ...

Commitments when using a function as an argument

While I have a good understanding of how promises function, I often struggle when it comes to passing a function as a parameter: var promise = new Promise(function(resolve, reject) { // Perform asynchronous task ec2.describeInstances(function(err, ...

Experiencing a lack of content in an Express response

Currently utilizing express to manage a POST request, but encountering an issue when sending the body using node-fetch. After sending the body and logging it in express (server-side code), I am seeing an empty object. The reason behind this behavior remain ...

Having trouble adding a new user in Knockoutjs

I'm currently utilizing knockoutjs to display multiple records in a table using a foreach loop. In the table foot, there is an Adduser button that allows for adding a new user. In the previous version of our software, when a user clicked on Adduser, i ...

The Vue component fails to refresh when the state in the store undergoes changes

Trying to create a simple todo list in Vue, but aiming to abstract everything out and utilize a dummy REST API for practice with production-level Vue projects has left my head spinning. While GET, PUT, and POST requests appear to be functioning properly, I ...

Use vtip jQuery for showcasing titles

Currently, I am utilizing the vTip plugin for displaying titles upon hover. Here is a snippet of my code: http://jsfiddle.net/bnjSs/ In my HTML, there's a Div with the ID #showTitles. Whenever this Div is clicked, I aim to toggle the display of all ...

Display and conceal interactive jQuery UI Dialogs

Despite its seemingly simple nature, I'm encountering some challenges with my current approach. My goal is to dynamically generate jQuery UI dialogs for the "help" element. The desired functionality is to toggle the visibility of the dialog when clo ...

Comparison between Bootstrap dropdowns and adjusting document height dynamically

I am in the process of constructing a webpage utilizing Bootstrap 4 Shards. I have several dropdowns, some of which are initially hidden using .d-none. The issue arises when these dropdowns start adding height to the document as they unhide, causing the po ...

Attempting to create a multi-page slider using a combination of CSS and JavaScript

Looking for help with creating a slider effect that is triggered when navigating to page 2. Ideally, the div should expand with a width of 200% or similar. Any assistance would be greatly appreciated! http://jsfiddle.net/juxzg6fn/ <html> <head& ...

JavaScript has hindered cross-origin response due to Cross-Origin Read Blocking (CORB)

I am having trouble retrieving JSON responses from an API. The error message "Cross-Origin Read Blocking (CORB) blocked cross-origin response" keeps popping up. I've tried searching online for a solution to this problem, but so far, I haven't bee ...

The functionality of the Bootstrap dropdown or radio input type is not functioning correctly

I'm currently utilizing electron([email protected]) along with bootstrap([email protected]). Whenever I attempt to incorporate a dropdown or other components from Bootstrap, they simply do not function. I am unsure of what mistake I might ha ...

If a user is already logged in, I would like to automatically redirect them to the dashboard when attempting to access the login route in Express.js

How can I automatically redirect to the dashboard in Express js if a user is already logged in and tries to access the login route? Middleware const checkAuthentication = async (req, res, next) => { const token = req.cookies.jwt; if (token) { jwt ...

Creating a consolidated HTML table by extracting and comparing data from various JSON files

Being new to JS and JSON, I am struggling to find a suitable solution that works for me. I have two distinct json files. The first one: players.json contains the following data: { "players": [ { "id": 109191123, "surnam ...