Static size element overlapping side panel

I am trying to center a fixed width element on my page, ensuring that it stays centered if it fits within the page width but extends beyond the page if needed with scroll accessibility. I have almost achieved this, but the element overlaps with the sidebar.

1/ How can I center the large-fixed-grid (green element) if it fits inside the container/screen width, and if not, start it after the sidebar?

2/ Additionally, when I scroll horizontally to reveal the fixed width element, there is a gap between the top-header and the large-fixed-grid (red element). Is there a way to adjust the position of the top-header to eliminate this gap as I scroll horizontally?

The yellow element should remain centered on the original width without any scrolling, and it currently behaves as expected.

Despite trying various CSS configurations, I have been unsuccessful in getting this to work properly.

See the JSFiddle link for a visual representation: https://jsfiddle.net/7tg2jo69/

Image Example: https://i.sstatic.net/GU87N.png

Html:

<!DOCTYPE html>
<html>
<header class="top-header"></header>
<div class="page">

  <div class="sidebar">
    <div class="menu">Item1</div>
  </div>
  
  <main class="main">
    <article class="content">
      <div class="container">
        <div class="row justify-content-center">
          <div class="small-flexible-grid">
          
          </div>        
          <div class="large-fixed-grid">
          
          </div>
        </div>
      </div>
    </article>
  </main>
  
</div>
</html>

CSS:

.top-header {
  height: 40px;
  background:red;
}

.sidebar {
  z-index: -1;
  background:blue;
  float: left;
  width: 200px;
  height: calc(100vh - 3.5rem);
  position: sticky;
  top: 0;
  overflow-y: auto;
}

.main {
  margin-left: 200px;
}

.content {
  padding: 1.1rem;
}

.container {
  max-width: 100%;
}

.row { 
  display: flex;
  flex-wrap: wrap;
}

.justify-content-center {
  justify-content: center;
}


.small-flexible-grid {
  background: yellow;
  width: 60%;
  height: 100px;
}

.large-fixed-grid {
  background: green;
  min-width:600px;
  width: 600px;
  height: 100px;
}

Update:

To address issue 1/, I removed the justify-content-center class and added margin: auto; to both .small-flexible-grid and .large-fixed-grid.

Answer №1

What if we update the appearance to something like this?

.custom-flex-container {
  background-color: blue;
  width: 80%;
  max-width: 700px;
  height: 120px;
}

Answer №2

Resolved both of these problems by utilizing the following solutions

JSFiddle: https://jsfiddle.net/m9wdzgb1/

HTML:

<!DOCTYPE html>
<html>
<header id="top-header-onscroll" class="top-header"></header>
<div class="page">

  <div class="sidebar">
    <div class="menu">Item1</div>
  </div>
  
  <main class="main">
    <article class="content">
      <div class="container">
        <div class="row">
          <div class="small-flexible-grid">
          
          </div>        
          <div class="large-fixed-grid">
          
          </div>
        </div>
      </div>
    </article>
  </main>
  
</div>
</html>

CSS:

.top-header {
  height: 40px;
  background:red;
}

.sidebar {
  z-index: -1;
  background:blue;
  float: left;
  width: 200px;
  height: calc(100vh - 3.5rem);
  position: sticky;
  top: 0;
  overflow-y: auto;
}

.main {
  margin-left: 200px;
}

.content {
  padding: 1.1rem;
}

.container {
  max-width: 100%;
}

.row { 
  display: flex;
  flex-wrap: wrap;
}

.small-flexible-grid {
  background: yellow;
  width: 60%;
  margin: auto;
  height: 100px;
}

.large-fixed-grid {
  background: green;
  min-width:600px;
  width: 600px;
  margin: auto;
  height: 100px;
}

Javascript:

window.onscroll = function (e) {
    var top_header = document.getElementById('top-header-onscroll');
    if (top_header) {
        if (window.pageXOffset > 0) {
            top_header.style.width = (window.innerWidth + window.pageXOffset - 30) + 'px';
        }
        else {
            top_header.style.width = "";
        }
    }
}

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

"Internet Explorer naturally selects the submit button when users press the enter key to submit a

In my current application, I have implemented a form with a hidden button to address issues with the numeric keyboard on Android. Essentially, pressing enter or focusing on the invisible button will trigger form submission. Pressing enter works fine in Ch ...

Which CSS preprocessor is the best choice for WordPress: SASS or

After comparing SASS and LESS, I found that SASS is the clear winner in my opinion. Unfortunately, setting up SASS on my server requires ruby, gems, and other tools that I don't have access to. On the other hand, it seems like adding LESS to my proj ...

Styling text using CSS depending on the displayed text

Utilizing Awesome Tables (AT), I am extracting data from a Google Sheets document to display on a website. The HTML template in the sheets is used for formatting the data, specifically focusing on the table output with inline CSS styling. Since the templat ...

When an element is appended, its image height may sometimes be mistakenly reported as

I am dynamically adding divs and I need to retrieve the height and width of an image. Based on this information, I have to apply CSS to the MB-Container class. For example: if the image is portrait orientation, set container width to 100%. If it's ...

JavaScript allows for inserting one HTML tag into another by using the `appendChild()` method. This method

My goal is to insert a <div id="all_content"> element into the <sector id="all_field"> element using Javascript <section id="all_field"></section> <div id="all_content"> <h1>---&nbsp;&nbsp;Meeting Room Booki ...

Enhancing Image Upload with Ajax/JavaScript: Generating Multiple Previews

I've been experimenting with an Ajax image uploader that I found on this website. Currently, I have managed to create duplicate preview images: one displayed under the input field and the other elsewhere on the page labeled as "this what you chose". H ...

What are some ways to monitor the movement of elements and activate functions at precise locations?

I am working on a project that involves a #ball element which, when clicked, utilizes jQuery animate to move downwards by 210px. The code I currently have is as follows: $('#ball').click(function() { $(this).animate({ top: '+=2 ...

The Owl-Carousel's MouseWheel functionality elegantly navigates in a singular, seamless direction

Issue at Hand: Greetings, I am facing a challenge in constructing a carousel using Owl-Carousel 2.3.4. My goal is to enable the ability to scroll through my images using the mousewheel option. Code Implementation: Incorporating HTML code : <div style ...

Tap on the HTML5 video to exit the fullscreen mode

Objective I have successfully implemented a fullscreen video setup that triggers when a link is tapped on a mobile device. To maintain a clean aesthetic, I have hidden the HTML5 video controls using CSS. The desired functionality includes closing the full ...

How deep can nesting go within a CSS rule?

When working with stylesheets, the majority of CSS rules follow a structured format: selector { property_1 : value_1; . . . property_n : value_n; } Each selector typically has only one {} block attached to it (without any sub {} blocks ...

Selection menu drops down once the save is complete

I have two drop-down menus named source and campaign, displaying data from the database. In addition to these drop-downs, there are other input fields as well. My concern is that I want to save the filled data along with the selected options in the drop-do ...

Modifying the HTML attribute value (of input) does not impact the value property

After inputting a single tag, I ran the following code in my Chrome console: https://i.stack.imgur.com/ySErA.jpg The result was unexpected for me. According to what I have read in a book, when I change an HTML attribute, the corresponding property should ...

select specific region within image

I'm currently working on cropping an image and sending the cropped data to the server side. To achieve this, I am utilizing the imgareaselect plugin. While I am able to obtain the coordinates of the selection, I am facing challenges in actually croppi ...

What is the best method for linking an HTML file to CSS, javascript, and image files?

I need assistance with running an HTML file along with its source files that I downloaded from the server. The HTML file is located in NSCachesDirectory and here is the code snippet I am using. Can someone please help me figure this out? NSArray *paths ...

Adding text after a div in React-JS using Bootstrap: A quick guide

Just starting out with website development and I have a question. As I practice making this website, I am struggling to figure out how to add the text "To know more about us click here" below the 'Get started' button. I tried adding a simple < ...

Menu navigation - ensuring the background color extends to cover the entire page, not just the viewport

I'm facing an issue with the left navigation bar's design. Specifically, I'd like the grey color of the bar to extend vertically across the entire page. Currently, the gray bar ends at the viewport's edge. Is there a way to make the ba ...

Highlight the parent name in the menu when I am on the corresponding child page

Here is a snippet of a recursive function: function recursive($arrays, $out) { if (is_array($arrays)){ //$out .= "<ul>"; foreach($arrays as $parent => $data) { //if parent is empty if ($parent === '') { ...

Incorporating Social Share Buttons to Enhance User Engagement on Your WordPress E

Looking to enhance my product page by adding social icons right below the 'Add to Cart' button. The product page URL is https://flowersforeveryone.co.za/product/cheerful-orange-tulips/. I already have a 'social menu' set up. How can I ...

Tips for transforming Javascript, CSS, and HTML elements into an engaging interactive PDF or .h5p page

I have developed a unique web application that allows users to place dots on a sitemap and link them to images. The technology stack includes Javascript, CSS, and HTML. phase1 Subscribed users are able to utilize a wide range of features which enable them ...

Deleting items with a swipe gesture in Angular 10

Hey there, fellow developer! I could really use some assistance in implementing the swipe delete feature for our Angular project. Could you take a look at the screenshot provided below? https://i.sstatic.net/LqXlm.jpg The code snippet given to me for thi ...