What could be causing the bootstrap 4 col-md-3 block to shrink when using position fixed?

When working with Bootstrap 4, I encountered an issue where changing the block position from relative to fixed using a script caused the block to decrease in size.

The script I used includes a .sticky class:

.sticky {
  position: fixed !important;
  top: 2%;
}

This script is intended to change the position of elements (empty_row) from relative to fixed. However, the issue only occurs in wide screens when there are two blocks with class names col-md-3 and col-md-8.

You can view the example of the issue on my codepen.

let avatarSimple = document.querySelector(".avatar_simple");
let avatarQuantumBreak = document.querySelector(".avatar_quantum_break");

avatarQuantumBreak.style.opacity = "0";

let hover = () => {
avatarQuantumBreak.style.opacity = "1";
}
let normal = () => {
avatarQuantumBreak.style.opacity = "0";
}

// scroll function position fixed
window.onscroll = function() {
  var scrolled = window.pageYOffset || document.documentElement.scrollTop;
  scrolled >= 20 ? document.querySelector(".empty_row").classList.add("sticky") : document.querySelector(".empty_row").classList.remove("sticky");
}
.header_home {
text-align: right;
}
.home_link , .main_text {
color: #fff;
font-size: 1.5em;
}
.left_block {
  padding: 30px 20px 20px;
  box-shadow: -4px 7px 15px 1px rgba(0,0,0,.2); 
}
.avatar {
  position: relative;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  height: 195px;
}
.avatar_simple,
.avatar_quantum_break {
  position: absolute;
  display: block;
  text-align:center;
  transition: opacity 1s ease-out;
}
.avatar .avatar_simple img,
.avatar .avatar_quantum_break img {
  border-radius: 50%;
  display: inline-block;
}
.info {
  margin-top: 33px;
}
.text_uppercase {
  text-transform: uppercase;
  color: #fff;
  font-size: 1.4em;
  text-align: center;
  margin-bottom: 15px;
}
.text_muted {
  text-align: center;
  opacity: 0.65;
}
.download_resume {
  position: absolute;
  width: 100%;
  left: 0%;
  padding: 30px;
  margin: 0;
  font-size: .875em;
  background-color: #313C42;
  box-shadow: -4px 7px 15px 1px rgba(0,0,0,.2);
}
.text_widget {
  vertical-align: middle;
}
.text_widget a {
  background-color: #DEC746 !important;
  border-color: #DEC746 !important;
  color: #000 !important;
  font-size: 15px !important;
  padding: 12px 30px !important;
  border-radius: 35px !important;
}
center {
  display: block;
  text-align: -webkit-center;
}
.btn_link {
  font-weight: 700 !important;
}
.main_wrap, .personal_info {
  background-color: #313C42;
  border:1px solid #2C363B;
}
.basic_info {
  padding: 40px 0 40px 0;
}
.btn-bordered {
  border-color: #1FA184 !important;
}
.personal_info {
  margin-top: 15px;
}
/*skills*/
.left_block_align > .col-md-3 {
  padding-left: 0px !important;
  padding-right:0px !important;
}
.brd_btm {
  border-bottom: 1px solid #2C363B;
}
.section {
  margin-bottom: 30px;
}
.sticky {
  position: fixed !important;
  top: 2%;
}
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>
 <div class="container">
  <div class="row justify-content-between left_block_align">
    <div class="col-md-3">
      <div class="empty_row left_block" style="background-color: #1FA184;">
        <div class="avatar" onmouseover="hover();" onmouseout="normal();">
          <span class="avatar_simple">
            <img  src="https://certy.px-lab.com/developer/wp-content/uploads/sites/6/2017/08/certy-programmer-1-195x195.png">
          </span>
          <span class="avatar_quantum_break">
            <img src="https://certy.px-lab.com/developer/wp-content/uploads/sites/6/2017/08/certy-programmer-2-195x195.png">
          </span>
        </div>
        <div class="info">
          <h2 class="text_uppercase">Sergio Ramos</h2>
          <p class="text_muted">Front End Developer</p>
        </div>
        <div class="download_resume">
          <div class="text_widget">
            <center>
              <a href="#" class="btn btn-primary btn-lg btn_link">Download CV</a>
            </center>
          </div>
        </div>
      </div>
    </div>

    <div class="col-md-8">
      <div class="main_wrap">
        <div class="basic_info">
          <section id="about" class="section_about padd_box">
            <div class="row">
              <div class="col">
                <h2 class="title text_uppercase">About Me</h2>
                <p><b>Hello, I’m Robert Smith</b></p>
                <p>Hello! I’m Robert Smith. Senior Web Developer specializing in front end development. Experienced with all stages of the development cycle for dynamic web projects. Well-versed in numerous programming languages including JavaScript, SQL, and C. Stng background in project management and customer relations.</p>
                <div class="share-box">
                  <button class="share-btn btn btn-primary btn-bordered text_uppercase">
                    <span class="fa fa-share-alt"></span>
                    Share
                  </button>
                </div>
              </div>
            </div>
          </section>
        </div>
      </div>
      <div class="personal_info">
        <div class="basic_info padd_box">
          <section id="skills" class="brd_btm section">
           Lorem ipsum, dolor sit amet consectetur adipisicing elit. Numquam voluptates a sed similique harum reiciendis quos minus blanditiis aliquam. Amet adipisci veritatis quam quisquam animi cum dolorum natus quae eligendi.
          </section>
          <section class="brd_btm section">
           Lorem ipsum dolor sit, amet consectetur adipisicing elit. Totam qui, amet officiis veniam maiores odio sed fuga quo ut quis esse! Consectetur tempore accusantium blanditiis cupiditate eligendi ratione quisquam nostrum.
          </section>
        </div>
      </div>
    </div>
  </div>
</div>

Answer №1

It seems like you are looking to convert a relative positioned element to a fixed positioned element.

When dealing with CSS, a relative element inherits its width from its parent (for example, when using <div> with display:block, it takes up 100% of .col-md-3). However, when you switch to position: fixed, you need to specify an absolute width.

One approach to this could be:

.sticky {
  position: fixed !important;
  top: 2%;

  width: 285px; //285px represents the absolute width of .col-md-3 on a large screen.
  // Incorporate breakpoints for responsiveness
}

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

Error: The property 'ss' cannot be accessed because it is undefined

Our main source page will be index.html, while Employees.html is where our results end up. An error occurred: TypeError - Cannot read property 'ss' of undefined Error in the code: let rating = req.body.ss; Seeking assistance please >< C ...

Ways to make a jsonp request without including the 'callback' in the URL

I've been working on retrieving information from an Icecast Radio station using their API, which offers the status-json.xsl endpoint to access this data. Despite the format being in xsl, I suspect it returns a JSON file. However, I've encountere ...

Navigating through the maze of ES6 imports and dealing with the complexities

Currently, I am delving into React and creating my own components. However, the issue of project organization has arisen. Here is the structure of my project: Project_Folder - Components - Form - index.js - form.less - package.js ...

In JavaScript, you can update the class named "active" to become the active attribute for a link

My current menu uses superfish, but it lacks an active class to highlight the current page tab. To rectify this, I implemented the following JavaScript code. <script type="text/javascript"> var path = window.location.pathname.split('/'); p ...

Content Security Policy directive violation: Chrome extension policy error occured due to refusal to execute inline event handler

I've been working on a chrome extension to perform a simple task, but I've hit a roadblock with one line of HTML code that's causing issues with setting the correct permissions. Despite my efforts, I'm stuck on what exactly needs to be ...

Converting a JavaScript string containing an `import` statement into a browser-compatible function

Can my vue application transform the given string into a callable function? const test = 'import { pi } from "MathPie"; function test() { console.log(pi); } export default test;' The desired output format is: import { pi } from "M ...

Developing and integrating views within a node-webkit desktop application

For my file copier desktop application built with node webkit, I aim to create a seamless flow where the initial check for existing profile data determines the first page displayed. The header with static links/buttons to various views remains consistent ...

Special character Unicode regex for names

After spending the entire day reading about regex, I am still struggling to fully grasp it. My goal is to validate a name, but the regex functions I have found online only include [a-zA-Z], omitting characters that I need to allow. Essentially, I require ...

html The "download" attribute causing a new tab to open rather than initiating a

I have a website built with Angular 4, where users can download images from an Amazon S3 bucket. However, I encountered an issue wherein instead of downloading the image, it opens in a new tab. I've tested this problem on different browsers such as Fi ...

The document scales down automatically as I decrease the size of the window, but does not adjust when I switch to a mobile viewport

I've been working on developing a web app with a focus on mobile-first design. When I scale down my Chrome window, everything looks responsive and functions well, adjusting objects to fit the smaller screen size. However, I noticed that when I switch ...

AngularJS Compile directive allows you to specify functions that you want to run in

Can someone assist me in understanding how to call an external function from a built-in compile directive? Here is a code example: http://plnkr.co/edit/bPDaxn3xleR8SmnEIrEf?p=preview This is the HTML: <!DOCTYPE html> <html ng-app="app"> ...

Issue encountered when attempting to serve JSON response using Node.js, express, and MongoDB after the initial response

I have been experimenting with creating simple RESTful APIs using Node.js, Express, and MongoDB. For this purpose, I am utilizing the Node.js-MongoDB driver in conjunction with the Express framework. const MongoClient = require("mongodb").MongoClient cons ...

Modify the color of the header on Material-UI Date Picker

I recently integrated a Material-UI Date Picker into my React Single Page Application and now I'm facing an issue with changing the header color. I attempted to modify it using the muiTheme palette property, but unfortunately, the header color remains ...

Get the hash value after a specific character using Javascript

I'm currently working on a website with ajax functionality where pages load when clicking on navigation buttons. However, I encounter issues when reloading the page as it defaults back to loading main.html, regardless of the URL. The hash being used i ...

Exploring React hook functionalities can lead to discovering unexpected issues such as cyclic dependencies on location.hash when

My implementation of a useEffect involves reading the location.hash and adjusting the hash based on certain dependencies. Here is a snippet of how it works: useEffect(() => { const hashAlreadyPresent = () => { const hashArr = history.locati ...

Issues with npm installation not capturing all required dependencies

After running npm install to install all dependencies, I encountered an issue where the dependencies were not being installed in my current project. The following message was displayed: https://i.sstatic.net/RafcR.png I then attempted to audit the projec ...

What steps can I take to ensure that this code is validated prior to proceeding to the subsequent page?

I am experiencing an issue with this HTML fieldset form code. When I click the submit button, it redirects to the next page. However, if I remove "next" from the button class, the form validates but does not proceed to the next page. Can you please help me ...

Remove any javascript code from the ajax modal when it is closed or hidden

I am in the process of creating a music website that showcases songs along with their lyrics. One of the features I have added is a lyrics button that, when clicked while a song is playing, opens up a modal displaying the live lyrics. Everything works per ...

Embedding a CSS class within the primary class selector

Can someone help me with this HTML challenge? <div class="span3"> <div class="abc">code for first div</div> <div class="abc">code for second div</div> </div> I'm trying to hide the first "abc" division within th ...

Combining two request.get functions into a single one

Is there a way to combine these two functions into one? I have two APIs: /page1 and /page2. My goal is to merge the two arrays into one because the GitHub API only displays 100 objects per page. request.get({ url: 'https://api.github.com/users/an ...