How to style 1 out of every 3 div elements using absolute positioning in CSS

Imagine a setup where there is a banner at the top, with 3 divs placed side by side underneath it. The interesting part is that when you scroll down, only the center and right div should move along.

#banner {
  background:blue;
  color:white;
  position: fixed;
  height:300px;
  width:500px;
  border-style: dotted;
}
#left {
  background:blue;
  color:white;
  position: fixed;
  top:300px;
  height:300px;
  width:150px;
  border-style: dotted;
  float:left;
}
#center {
  background:red;
  color:white;
  top:300px;
  left:150px;
  height:700px;
  width:150px;
  border-style: dotted;
  float:left;
  z-index:-1;
}
#right {
  background:red;
  color:white;
  top:300px;
  left:300px;
  height:300px;
  width:150px;
  border-style: dotted;
  float:left;
  z-index:-1;
}
<div id="banner">
  banner
</div>
<div id="left">
  left
</div>
<div id="center">
  center
</div>
<div id="right">
  right
</div>

It seems like using position: absolute or fixed on the left and banner div might be necessary here. However, setting all the properties such as left, top, height, width, and z-index could make it complex to make them fit together seamlessly across different browsers and resolutions. Is there a simpler solution to achieve this layout? Check out my failed attempt here: https://jsfiddle.net/y71fqthf/1/. You can notice that the boxes go under the banner, which is not desired.

Is this method of displaying a website considered bad practice in general?

Answer №1

make adjustments to some css attributes

 #banner  {
      background:red;
      color:yellow;
      position: absolute;
      height:250px;
      width:450px;
      border-style: solid;
    }
    #left   {
      background:green;
      color:white;
      position: fixed;
      top:250px;
      height:250px;
      width:100px;
      border-style: double;
      float:left;
    }
    #center {
    background: blue none repeat scroll 0 0;
    border-style: dashed;
    color: white;
    float: left;
    height: 600px;
    margin-left: 156px;
    position: relative;
    top: 250px;
    width: 100px;
    z-index: 8;
    }
    #right {
    background: blue none repeat scroll 0 0;
    border-style: dotted;
    color: white;
    float: left;
    height: 250px;
    margin-left: 0;
    position: relative;
    top: 250px;
    width: 100px;
    z-index: 5;
}

https://jsfiddle.net/y71fqthf/1/

Answer №2

Here is a sample code using Bootstrap. Please note that I have not had the opportunity to thoroughly test it due to time constraints at work. If you encounter any issues, kindly let me know so I can address them promptly.

.header {
  width: 100%;
  z-index: 10;
}
.header-inner {
    background-color: #fababa;
    position: relative;
}

.content .sidebar-right {
    margin-top: 150px;
    background-color: #a9a8a8;
    min-height: 5500px;
}
.container {
    background-color: #f2f2f2;
}
.sidebar-outer {
    margin-top: 150px;
    position: relative;
}
.fixed {
    position: fixed;
}
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="header fixed">
  <div class="container">
    <div class="row">
      <div class="col-md-12 col-sm-12 col-xs-12 header-inner" >
        <div class=" col-md-12 col-sm-12 col-xs-12" style="height: 150px;">
          Here goes the Fixed header
        </div>
      </div>
    </div>
  </div>
</div>

<div class="container content">
    <div class="row">
        <div class="col-md-3 col-sm-3 col-xs-3 sidebar-outer" >
            <div class="fixed col-md-3 col-sm-3 col-xs-3">
                <img class="img-responsive"
                     src="http://placekitten.com/300/200"/>
                Here goes the Fixed column
            </div>
        </div>
        <div class="col-md-9 col-sm-9 col-xs-9 sidebar-right">Content goes here (you can use sub-columns)...</div> 
    </div>
</div>

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 format the date in an input tag to comply with ISO standards (ex: 2017-06-17T21:35:07

Here is an example of an input tag: <input type="datetime-local" class="datepicker-date" id="start-date" name="start-date" placeholder="YYYY-MM-DD" class ="form-control" formControlName = "startTime" data-date-format=""> To achieve the desired date ...

XPath //*[text()] is not picking up the desired text

Using the XPath 1.0 expression //*[text()] does not capture the phrase "now revenue of kfc is" https://i.stack.imgur.com/GeFCY.png However, with the XPath 2.0 expression //text(), it can be selected. https://i.stack.imgur.com/uTSqW.png Unfortunately, S ...

Seeking Up/Down Arrows HTML numerical input feature specifically designed for iOS devices

I am having an issue with a number input box on iOS. I am utilizing jquery, kendo mobile, and HTML5 for this particular task. While the number input displays up and down arrows in the Icenium simulator, they are not showing on the iPad. I am seeking a sol ...

What is the best way to assign a URL to an image source using a JavaScript variable?

I am working on a script that displays different image URLs based on the time of day using an if-else statement. I want to dynamically load these images in an img src tag so that a different picture is loaded for each time of day. I have defined variables ...

When using jQuery with a large selectbox, you may encounter the error message: "Uncaught RangeError: Maximum

My select box works fine in IE and Mozilla, but throws an uncaught rangeError in Chrome when choosing the "Others" option to display a second select box with over 10k options. How can I diagnose and resolve this issue? <!DOCTYPE html> ...

How can you use jQuery to select and manipulate multiple form inputs with a common class?

I am currently working on a program that requires at least one of various form inputs to have data in order for the user to submit the form. I was able to successfully code for a scenario with only one input field, but I am facing a challenge when it comes ...

The arrows on the Slick Slider appear cropped on ios devices like the iphone 8, however, they display perfectly when tested on Chrome and Firefox

I am currently facing an issue with my slick slider setup at https://www.labtag.com/shop/product/clear-cryogenic-labels-for-frozen-vials-1-75-x-1-aha-224/. It is configured for 4 slides on desktop and 2 slides on mobile devices (768px breakpoint). The pr ...

Exploring the ins and outs of utilizing pseudo selectors with material-ui

I've been attempting to accomplish a simple task. I wanted to toggle the visibility of my <TreeMenu/> component in material UI v1 using pseudo selectors, but for some reason it's not working. Here is the code: CSS: root: { ba ...

"Utilizing the power of Twitter Bootstrap and Snap.js for

I am currently working on integrating Snap.js by jakiestfu with Twitter Bootstrap. I have managed to make it work to some extent (I can slide the content and open a drawer through a click event). However, I am facing a challenge where the drawer content re ...

Tips for replicating input boxes in a while loop

HTML : <table> <tr> <td> <input type="text" name="name[]" value=" echo $ail"> <label >Address</label> <input type="text" name="countryname[]" id='countryname_1 ...

Guide to customizing color themes using Twitter Bootstrap 3

Recently, I dove into the world of Twitter Bootstrap 3 without any prior experience with earlier versions. It's been a learning curve, but I managed to set up a WordPress theme using it as the framework. All my elements are in place, but they all see ...

Tips for loading a webpage directly to the center instead of the top position

Is there a way to make the page open at a specific div halfway down the page instead of starting from the top? Here is an example: <div id="d1"> <div id="d2"> <div id="d3"> <div id="d4"> <div id="d5"> <div id="d6"> Do ...

Retrieving all buttons from a webpage and removing those with a specific background-image using JavaScript

Hey everyone, I'm still learning about javascript and other web development languages. My current task is to locate all the buttons on a webpage and remove the ones that have a specific background image. I know about using getElementsByTagName but n ...

Retrieve jQuery CSS styles from a JSON database

I've been attempting to pass CSS attributes into a jQuery method using data stored in a JSON database. However, it doesn't seem to be functioning as expected. I suspect that directly inputting the path to the JSON variable may not be the correct ...

The CSS media query isn't functioning properly on Safari browser

Currently, I am working on a project using React JS and CSS. In order to make it responsive, I have implemented media queries. However, I have encountered an issue where the media query is not functioning properly in Safari browsers on both phones and PC ...

Ways to achieve text transparency with CSS without converting it to an image

Is there a way to have my navigation bar indicate the selected page by changing the text color and adding a black background, while making only the text inside the current page nav transparent? For example, you can see the effect here: sammarchant.co.uk/n ...

Tips for ensuring browsers maintain the aspect ratio specified by width and height HTML attributes when CSS is applied

I am seeking a method to reserve space in my layout for images before they are loaded by adding width and height properties to the img element. This approach functions as anticipated: img { display: block; border: 1px solid tomato; } <img src="" ...

Creating a Full Page Background Image That Fits Perfectly Without Resizing or Cropping

Can someone help me achieve the same effect as the website linked below, where the background image fades instead of being a slideshow? The image should be 100% in width and height without any cropping. I have managed to set this up with the codes provided ...

Having trouble getting a background image to show up using node.js?

Hi there, I have a quick and simple question. I am attempting to include a div with a background-image in my *.ejs page file. <style> .signPage{ background-image: url("//back.jpg"); width: 98%; height: 98vh; top: ...

How can I implement a hover-over image change effect similar to the one seen in Gmail accounts?

I am attempting to implement a feature that allows users to change their image, similar to what can be done in a GMail account. When the user hovers over the image, an option to "change image" should appear. This may be a new concept for me because I am ...