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

The script file (.js) isn't showing up on the PHP or HTML webpage

Experiencing a peculiar issue and seeking advice on alternative solutions due to my limited experience in this matter. The Issue: I currently have the following script running smoothly: <script type="text/javascript" id="myscript" src="http://piclau ...

"Bringing the power of JavaScript to your WordPress

I have set up a wordpress page and integrated some JavaScript into it. When working in wordpress, there are two tabs on a page where you can input text - 'Visual' and 'Text'. The 'Text' tab is used for adding HTML and scripts ...

A custom class that uses toggleClass() does not trigger an alert upon a click event

I am encountering an issue with the toggleClass() function in jQuery that I haven't seen addressed before. Despite successfully toggling the class of one button when clicked, I am unable to trigger a click event on the newly toggled class. Here is th ...

Modify the content of a div by clicking on a word or link using Javascript

I'm attempting to create a clickable word (page 2) that acts as a link to display different div content. When the user selects option 2 and clicks the next button, they should be able to click the "Page 2" text to show the content with id="test1" (Cho ...

CSS Causing Scroll Bars to Appear When Browser is Maximized

I've noticed that the scroll bars are still visible even when I maximize the browser window. I don't see any reason for them to be there. There doesn't seem to be a height issue, so the vertical scrollbars shouldn't be appearing, right ...

Tips for enabling the wrap property with autogeneratecolumn set to true

In my grid view, I want to ensure that any text in a column that exceeds the width of the column will either wrap within the column or display on a new line. Below is the code for the grid view: <asp:GridView ID="GridView1" AllowSorting="True" runa ...

The CSS overflow property is a great tool to demonstrate how a box can be neatly cut off at its

According to the specifications outlined in this resource, In situations where overflow occurs, the 'overflow' property determines if a box is clipped to its padding edge. Additionally, it specifies whether or not a scrolling mechanism will b ...

Ways to extend div to fill the rest of the page's vertical space

Apologies, my search has yielded many results, but none that directly address my specific issue. It appears that there is a proliferation of div-height problems on this platform. :-( Here is the layout I am working with for testing purposes: <!DOCTYPE ...

form field with the ability to select multiple files

From my understanding, traditional html or javascript do not have the capability to upload multiple files (images) in a singular field. Please correct me if I am mistaken. Is there a way for this to be achieved with html5 or any other method aside from us ...

Juggling PHP scripts within javascript

Can you help me with a question I have? I am working on multiple JS scripts, such as this one: <script> $('#mapveto1').click(function() { $('#mapveto1').addClass('banned'); $('#map1').text('cobble ...

The optimal method to showcase lists of names: HTML or CSS?

I have a roster of 40 individuals, complete with their titles and additional details akin to a yearbook layout without the actual photos. Previously, I relied on HTML tables to format this information, but I am contemplating whether there might be a more e ...

When the JavaFX ComboBox drop-down opens in an upwards direction, the border appears incorrectly

While working with Java 8, I encountered an issue with the "javafx.scene.control.ComboBox" where if it doesn't have enough room below, it pops up instead. Strangely, the border styling of the elements still behaves as if it should pop down. Is there ...

Adjust the width of a div based on its height dimension

I have a div called #slideshow that contains images with a 2:1 aspect ratio. To set the height of the image using jQuery, I use the following function: Keep in mind that the Slideshow Div is always 100% wide in relation to the browser window. If the use ...

The form features a "Select all" button alongside a series of checkboxes for multiple-choice options

I am facing difficulties trying to get the "Select all" and "Remove all" buttons to function properly within my form. Whenever I remove the "[]" (array) from the name attribute of the checkboxes, the JavaScript code works perfectly fine. However, since I n ...

The swap feature in drag-and-drop does not have a defined function

I am currently developing a to-do app that utilizes drag and drop functionality to reorder items in the list. However, I have encountered an issue where swapping elements works perfectly for the first five elements but throws errors when dealing with the s ...

What is the limit of CSS includes that IE can process?

While working on theming Drupal, I encountered an unusual issue. After enabling a few modules that added 5 to 10 link tags to the page, I noticed a strange behavior in different browsers. Firefox successfully added the new stylesheets to the cascade, but i ...

Does MySQL Workbench automatically convert camel case attributes?

I'm curious to understand something. I defined an attribute called userId, but in MySQL Workbench it shows up as user_id. Is this usual? @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name= "userId") private ...

Personalize the color scheme for your timeline paper

I am interested in customizing this specific example of a personalized timeline: import React from 'react'; import { makeStyles } from '@material-ui/core/styles'; import Timeline from '@material-ui/lab/Timeline'; import Timeli ...

Is there a method or API available for interacting with an <object> that is currently displaying a video?

Yay, I figured it out! I've been using video.js to play videos on a website that needs to work offline too. Unfortunately, using flash is not an option due to compatibility issues. So, I decided to write my own fallback solution. For Internet Explor ...

Align navigation bar using Angular Material

Trying to center my navbar has been a struggle for me. I attempted using 'layout-align='space-between center' but it's not taking up 100% width and is acting more like an inline element. I've been following the documentation close ...