CSS-only method for creating a fixed position sidebar with adjustable width

https://i.stack.imgur.com/e4Gsv.png

I have a vision of creating something similar to the image above solely using CSS. The design must incorporate the following elements:

  • The image-container will hold a 3x4 aspect ratio image, filling the available height of the viewport. As a result, the width of image-container will vary based on the viewport height.
  • The image-container should remain fixed in place as the user scrolls through the window.
  • The content-container needs to be scrollable.
  • The content-container should occupy all the space between the right edge of image-container and the right edge of the viewport.

In previous instances, I might have achieved this effect using jQuery:

// style.css
.image-container {
   position:fixed;
   top: 0px;
   left:0px;
   bottom: 0px;
}

.image-container img {
   height: 100%;
}

// script.js
$(window).load(function() {
   var width = $('.image-container').width();
   $('.content-container').css({'margin-left': width});
});

Is there a way to achieve this design using only CSS? Perhaps leveraging flexbox?

Answer №1

styling solution

implement flexbox

html,
body {
  height: 100%;
  overflow: hidden;
}
html {
  background-color: #000;
}
.body {
  margin: 0;
  width: 100%;
  height: 100%;
  background-color: #333;
  color: #999;
}
.container-wrapper {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: flex-start;
  height: 100%;
}
.image-container {
  background-size: contain;
  text-align: center;
  height: 100%;
}
.image-container img {
  height: 100%;
  width: auto;
}
.content-container {
    height: 100%;
    display: flex;
    flex-direction: column;
}
.content-inner-container {
    flex-grow: 1;
    overflow-y: auto;
    padding: 20px 20px 40px 20px;
}
<div class="body">
  <div class="container-wrapper">
  <div class="image-container">
      <img src="http://placehold.it/750x1334">
    </div>
    <div class="content-container">
      <div class="content-inner-container">
        <p>
    Unique content will be displayed here after applying the CSS styling rules.
    </p>
     <p>
    Additional unique content can also be included in this section to enhance the design and layout.
    </p>
    <p>
    The use of Lorem Ipsum text is common as a placeholder until real content is provided for webpage development.
    </p>
      </div>
    </div>
  </div>
</div>

scripting solution

$(document).ready(function(){
var width = $('.image-container').width();
   $('.content-container').css({'width': 'calc(100% - '+width+')'});
});
html,
body {
  height: 100%;
  overflow: hidden;
}
html {
  background-color: #000;
}
.body {
  margin: 0;
  width: 100%;
  height: 100%;
  background-color: #333;
  color: #999;
}
.container-wrapper {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: flex-start;
  height: 100%;
}
.image-container {
  background-size: contain;
  text-align: center;
  height: 100%;
}
.image-container img {
  height: 100%;
  width: auto;
}
.content-container {
    height: 100%;
    display: flex;
    flex-direction: column;
}
.content-inner-container {
    flex-grow: 1;
    overflow-y: auto;
    padding: 20px 20px 40px 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="body">
  <div class="container-wrapper">
  <div class="image-container">
      <img src="https://dummyimage.com/480x640/6b676b/fff">
    </div>
    <div class="content-container">
      <div class="content-inner-container">
        <p>
    Implementation of JavaScript code allows for dynamic adjustments based on the image container's width.
    </p>
     <p>
    This section can feature interactive elements or modify content dynamically based on user interactions.
    </p>
    <p>
    Utilizing jQuery for scripting enhances the website's interactivity and performance.
    </p>
      </div>
    </div>
  </div>
</div>

Answer №2

I have successfully achieved what you're trying to do. Take a look at this fiddle.

.image-container{
  height:100vh;
  width:100vh - 25%;
  border:2px solid red;
  display:inline-block;
  margin:0;
  position:fixed;
}

.content-container{
 border:2px solid green;
 margin-left: 100vh - 25%;
 display:inline-block;
 width:100%;
 height:2000px;
}

<div class="image-container"></div>

<div class="content-container">
  <p>I aim to create something similar to the image above, 
using only CSS. It should include the following features. </p>
</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

Update the image source within a CSS element

I'm facing an issue with changing the source image of an Element. The situation is that I have a customized CSS script and I need to modify a logo image. Below, you can find the source code: https://i.stack.imgur.com/8HLCE.png The problem lies in at ...

api for enhancing images in Laravel app through preview, enlarge, and zoom functionalities

As I work on my website, I aim to display images in a compact space, such as within a 300x300 <div>. What I envision is the ability for users to preview or enlarge these images upon clicking, allowing for a closer and more detailed view. For exampl ...

How can I vertically align a photo or image on Facebook?

Can anyone explain how Facebook manages to vertically align its photos without using padding or margins? I've looked into their img tag and its parent, but neither seem to have any spacing properties. Although there is a vertical-align attribute prese ...

Is there a way to specifically target the MUI paper component within the select style without relying on the SX props?

I have been experimenting with styling the Select MUI component using the styled function. I am looking to create a reusable style and move away from using sx. Despite trying various methods, I am struggling to identify the correct class in order to direct ...

Retrieve a dynamic HTML object ID using jQuery within Angular

In my Angular application, I have implemented three accordions on a single page. Each accordion loads a component view containing a dynamically generated table. As a result, there are three tables displayed on the page - one for each accordion section. Abo ...

JavaScript's connection to the CSS property visibility seems to be causing some issues

The Javascript code seems to be ignoring the CSS display property, even though it's set in the style sheet. I added a debugger statement and noticed that the display value was empty. I've been staring at this for some time now, so I must be overl ...

Is there a way to control the text animation loop on iTyped, both starting and stopping it?

Currently utilizing React, MUI, along with iTyped The animation implemented involves backspacing text and typing the next word in a specified array until reaching the final word. I am looking to enable a click function on the div containing this animation ...

The PNG image that is stored in the MySQL database is not displaying completely on the web browser

Here are the PHP codes from index.php <?php include("connection.php"); $sql = "SELECT prod_cost, prod_name, prod_image FROM products"; $result = mysqli_query($con, $sql); $row = mysqli_fetch_all($result, MYSQLI_ASSOC ...

Tips for dividing the AJAX response HTML using jQuery

I have a piece of code that is functioning properly. However, I am having trouble splitting the HTML response using AJAX jQuery. I need to split it into #div1 and #div2 by using "|". Could you please provide a complete code example and explanation, as I am ...

Fixed bar on top, revealed only when the scroll attempts to conceal it

I have placed a menu on the top section of my website, but not at the very top. I would like it to stick to the top of the page when the user scrolls down and disappears from view. However, if the title is still visible, I want the menu to remain below it ...

Disappearing CSS Box Shadows

I'm attempting to replicate effect number 2 from a website I found, but I'm running into an issue. Whenever I add more HTML content to the page, the effect disappears. Here is a sample of what I have so far: http://jsfiddle.net/aHrB7/ Here is a ...

Creating distinct identifiers for table cells utilizing the map function

Is there a way to assign a unique id to each MenuItem using the map() function nested within another one? <table className={classes.table}> <thead> <tr> <td /> {sit.sit.map(sit => ( <td className={ ...

Is there a way to restrict the number of words displayed in React? Check out this code snippet: `<ProductImg imgtext={products.description}/>`

imgAlt={product.name} Note: Consider the product name as: HD Single Sided Cantilever Rack. In this case, only HD Single Sided... should be displayed Please find the code snippet below <ProductImg imgtext={products.description}/> ...

Programmatically simulate a text cursor activation as if the user has physically clicked on the input

I have been attempting to input a value into a text field using JavaScript, similar to the Gmail email input tag. However, I encountered an issue with some fancy animations that are tied to certain events which I am unsure how to trigger, illustrated in th ...

Animation of two divs stacked on top of each other

I am trying to replicate the animation seen on this website . I have two divs stacked on top of each other and I've written the following jQuery code: $('div.unternehmen-ahover').hover( function () { $('div.unternehmen-ahover' ...

Using Jquery to set values for css properties

I've recently started using jquery and I have a task related to a drop-down menu that I'm struggling with: if (scrnwidth <= 761) { if (display was block) { //Defaultly testi has display:none property. testi = m ...

Unable to retrieve HTML code from a webpage using its URL address

Currently, I am trying to retrieve the HTML code of a specific webpage located at . My initial attempts using HttpClient were unsuccessful due to exceeding request processing time. It seems like this website may have anti-bot protection in place. I attempt ...

Is there a way to escape from an iFrame but only for specific domains?

if (top.location != self.location) { top.location = self.location.href; } If my website is being displayed in an iFrame, this code will break out of it. But I want this to happen only for specific domains. How can I perform that check? ...

Tips for customizing MUI PaperProps using styled components

I am trying to customize the width of the menu using styled components in MUI. Initially, I attempted the following: const StyledMenu = styled(Menu)` && { width: 100%; } `; However, this did not have any effect. After further research, I ...

Decode my location and input the address before validating it

While I have come across numerous plugins that enable geolocation and display it on a map, I am searching for something unique. I am interested in implementing geocoding when a user visits the page with an option to "allow geolocation." If the user agrees ...