CaptainStiggz
CaptainStiggz

Reputation: 1897

Variable width sidebar with "fixed" position using pure CSS

enter image description here

I would like to accomplish something like what's pictured above, using pure CSS. It must have the following features:

In the past, I might have accomplished it with something like this (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 this possible using only CSS? Perhaps using flex?

Upvotes: 0

Views: 932

Answers (2)

Amal
Amal

Reputation: 3426

css solution

use flex

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>
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>
     <p>
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>
    <p>
    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
    </p>
      </div>
    </div>
  </div>
</div>

js 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>
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>
     <p>
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>
    <p>
    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
    </p>
      </div>
    </div>
  </div>
</div>

Upvotes: 1

Chris Owens
Chris Owens

Reputation: 58

I believe I've accomplished what you're attempting. Check out 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 would like to accomplish something like what's pictured above, 
using pure CSS. It must have the following features. </p>
</div>

Upvotes: 0

Related Questions