Positioning a fixed div within a responsive div using AngularJS and Bootstrap

I'm currently working on a page layout that consists of a fixed sidebar on the left side and a main container taking up the rest of the page on the right. Inside this right-side container, which is a div, there are 2 elements.

<div class="col-sm-12 col-md-5 col-lg-3">
    <custom directive>
</div>

<div class="col-sm-12 col-md-7 col-lg-9">
    <another custom directive>
</div>

Because the content in the second div is long, scrolling is necessary. I attempted to make the first div sticky by applying position:fixed to it in css. However, this removes it from the context of the right side container, causing the responsive width classes to no longer work properly. Additionally, the two divs overlap.

In an effort to find a cleaner solution, I came up with using a dummy div like so:

<div class="col-sm-12 col-md-5 col-lg-3 dummy-div">

</div>

<div class="col-sm-12 col-md-5 col-lg-3 sticky-div">
    <custom directive>
</div>

<div class="col-sm-12 col-md-7 col-lg-9">
    <another custom directive>
</div>

My idea was to create an element directive that utilizes jQuery to set the width of the sticky-div to match the width of the dummy-div. However, I still feel this isn't the most elegant solution and am wondering if there is a better way to handle this scenario?

Answer №1

Start by avoiding duplication of classes; instead, consolidate all your classes within one class="" attribute.

Instead of using a dummy div to fix the layout, consider adding margins or padding to the non-fixed <div> to compensate for the absence of the fixed <div>.

Another approach could involve utilizing jQuery to grab the container's width and inject it as needed.

Alternatively, you may opt for dynamic widths that adjust based on the container size.

For example, setting a width of 50%, combined with the use of calc, can help align the width of the supposed parent element of the fixed <div>.

Answer №2

In the end, I decided to maintain the dummy div and used media queries to calculate the width of the fixed div.

@media only screen and (max-width : 1200px) {
    position: fixed;
    width: 30%;
    margin-left: 1.3em;
}
@media only screen and (max-width : 992px) {
    position: relative;
    width: 95% ;
    margin-right: 1.3em;
}

Additionally, I made sure the div was not fixed for smaller screens to accommodate a vertical layout.

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

What is the best way to address caching fonts and files within CSS?

Utilizing the font icon for displaying icons may result in difficulty when adding new icons to the font due to caching issues. Clearing the cache is often necessary to refresh the display. How can I address this concern? ...

How can I align the content within two div elements?

I'm facing a bit of a challenge that I can't seem to figure out. Within a div, I have one photo and inside another div, there is also a photo. I was able to resize both images from the parent and child divs successfully. <div style="width:100 ...

Is it possible for me to streamline the ng-class declaration for my button?

Currently, I am utilizing the following code: <button data-ng-repeat="question in test.testquestions" data-ng-class="{current: question.number == test.currentQuestion, correct: question.result == 't', incorrect: qh.result == & ...

Experiencing a problem with inline JavaScript onclick

I recently came across a fantastic pure javascript code for a smooth scrolling function, but I'm struggling with integrating it into an inline onclick function. My understanding of javascript is quite limited and I could really use some help... <d ...

There was an issue with the Google Maps embed API that resulted in an error with interpolation

My goal is to utilize the Google Maps API in order to showcase a map using data from a JSON file. However, whenever I attempt to incorporate the JSON data, an error message 'Error: [$interpolate:noconcat] Error while interpolating' pops up. < ...

Adjust the background color of Bootstrap 4 checkboxes

I'm trying to figure out how I can modify the background color of a Bootstrap 4 checkbox in this specific scenario. .custom-control-label::before, .custom-control-label::after { top: .8rem; width: 1.25rem; height: 1.25rem; } <link rel="style ...

Troubleshooting image loading issues when updating the base URL in an Angular JS project

I am trying to update the base URL for my application. Currently, when I load the application, the URL shows up as http://localhost:4200/#/, but I want it to be http://localhost:4200/carrom/ instead. To accomplish this, I modified the base URL and now th ...

JavaScript threw an error with message: 'Unexpected identifier' that was not caught

Upon launching Web Developer in Firefox: SyntaxError: missing } after property list note: { was opened at line 7, column 7 ...

"Triggering an event when a checkbox in a list is clicked

How can I add an onclick event to a checkbox within an <li> element, as shown in the picture? Here is my JavaScript code so far: It's not working. $(document).ready(function() { allFiles() /*----------------------------------------------- ...

Tips for avoiding css reanimation when clicking on a calendar

Recently, I have been experimenting with animating an ASP calendar using CSS and JQuery. My approach involves hiding the calendar initially with CSS and then fading it in when certain events occur. The calendar is contained within an AJAX update panel. How ...

"Firebase manages the process of sending password reset emails for users who have not yet

Upon registering with the web application, a verification email is automatically sent to new users. To ensure security, these users are unable to log in until they have verified their email. In the event that the verification link expires and a user forge ...

Ways to identify if the text entered in a text area is right-to-left justified

Within a textarea, users can input text in English (or any other left-to-right language) or in a right-to-left language. If the user types in a right-to-left language, they must press Right-shift + ctrl to align the text to the right. However, on modern O ...

Issues with jQuery compatibility when used alongside HTML and CSS

My coding tool is flagging errors in my JavaScript file, including: -'$' usage before definition. -Incorrect spacing between 'function' and '(' This is the content of my JS file: $(document).ready(function() { $(window).s ...

Utilize jQuery to encase the final word of a paragraph within span tags

Greetings! Consider the following HTML snippet: <p>Phasellus sit amet auctor velit, ac egestas augue.</p> I am interested in enclosing the last word within span tags to achieve the following result: <p>Phasellus sit amet auctor velit, ...

How come my $scope variables are not resetting when attempting to refresh the data?

I just started working with Angular and encountered an issue while trying to implement a reload button. My webpage displays a table with data pulled from a Parse database. The table can be modified using various buttons, and after making changes, I want to ...

Is it possible to utilize Angular JS without utilizing a terminal interface?

Every time I try to work with the terminal, git, and other tools, my learning curve goes through the roof as I attempt to build an app quickly using CodeIgniter. I understand the importance of a JavaScript framework like AngularJS - is there a way to avo ...

Is it possible to run both the frontend and backend on the same port when using vanilla JavaScript for the frontend and Node.js for the backend?

Is it possible to run frontend and backend on the same port in Rest APIs if using vanilla JS for the frontend and Node.js for the backend? I've come across information on how to do this with React, but nothing specific to vanilla JS. Any insights on t ...

Utilizing CSS, Javascript, and Jquery to Toggle a DIV's Visibility Based on Clicking Either of Two Images

Need help with showing and hiding divs based on image clicks. Have 2 images (Image_A and Image_B) and 2 hidden Divs (Div_A and Div_B). If Image_A is clicked, show Div_A. If Image_B is clicked, hide Div_A and show Div_B. This should work both ways, ensurin ...

The function is defined, but it cannot be set to null

Having trouble understanding this error message "Cannot set properties of null." I'm attempting to update the innerHTML with the output text from four functions that my button triggers. However, it seems to be stopping at the first function now even t ...

Position the div in the center of a container that is 100% width and has floating elements with dynamic widths

I am looking to align 3 divs inside a container div, with the arrangement of [LEFT] [CENTER] [RIGHT]. The container div should be 100% wide without a fixed width, and I want the center div to stay centered even when resizing the container. The left and ...