Stable sidebar positioned beside a centrally located blog entry

When creating a page with extensive text, it is important for readers to easily navigate between sections. To achieve this, I am looking to have a fixed Index displayed on the left-hand side of the main text at all times. The main text should be centered and occupy about 75% of the screen.

Despite spending several hours working on it, I am struggling to get the layout just right. I keep running into issues where the columns overlap or have awkward white space between them, making it difficult to center them nicely.

Here is the HTML code:

<div class="wrapper">
<div class="row">
    <!-- Sidebar -->
    <nav id="sidebar">
        <ul> Content
            <li><a href="#Guidelines">Guidelines</a> </li>
            <li><a href="#Geographic Focus">Geographic Focus</a> </li>
            <li><a href="#Fields of Interest">Fields of Interest</a> </li>
        </ul>
    </nav>
   <div id="content">
                <div id="blog">
                </div>
   </div>
 </div>
 </div>

And here is the CSS code:

.wrapper .row {
    display: flex;
    border-style:solid;
    width:100%;
    justify-content:center;
    align-content: center;
    align-self:center;
}          

#sidebar {
  position: fixed;
  display:flex;
  border-style:solid;
  min-height:100vh;
  color:black;
  flex-direction:column;
  padding-top:70px;
  width: auto;
}

#blog {
  float: left;
  width:75%;
  padding:0;
  border-style:solid;
  margin: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
}

Answer №1

Revised with new input

To enhance the layout, incorporate flexbox along with grid for repositioning the navigation closer to the content

Initial response

An alternative approach is utilizing CSS Grid for setting up this design. Here’s an example:

.wrapper {
  display: grid;
  grid-template-columns: 25% 75%;
}

#sidebar {
  display: flex;
  justify-content: flex-end;
}

#sidebar ul {
  position: fixed;
  display: grid;
  align-content: center;
  height: 100%;
  max-width: 25%;
  padding: 0;
}
<div class="wrapper">
  <nav id="sidebar">
    <ul> Content
      <li><a href="#Guidelines">Guidelines</a> </li>
      <li><a href="#Geographic Focus">Geographic    Focus</a> </li>
      <li><a href="#Fields of Interest">Fields of    Interest</a> </li>
    </ul>
  </nav>
  <div id="content">
    <div id="blog">
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tempore praesentium sit, magnam voluptate pariatur harum veniam nostrum explicabo quibusdam mollitia vel blanditiis qui, voluptates totam, alias rerum sequi...
      
      <p>Additional paragraphs with similar lorem ipsum text...</p>
      
      <p>Final batch of lorem ipsum texts.</p>
    </div>
  </div>
</div>

Answer №2

Here is a snippet that provides an initial solution with minimal changes to the original code.

While this approach may work, there could be alternative ideas. This suggestion involves placing a fixed block, as specified, with a width of 25% and ensuring that the main content respects this by having a left-margin of 25%.

<html>
<head>
<style type="text/css">
#sidebar {
  background: red; /* added for visual testing */
  position: fixed;
  min-height:100vh;
  padding-top:70px;
  width: 25%;
}

#blog {
  background: blue; /* added for visual testing */
  width:75%;
  margin-left: 25%;
  text-align: center;
}
</style>
</head>
<body>
    <!-- Sidebar -->
    <div id="sidebar">
        <ul> Content
            <li><a href="#Guidelines">Guidelines</a> </li>
            <li><a href="#Geographic Focus">Geographic Focus</a> </li>
            <li><a href="#Fields of Interest">Fields of Interest</a> </li>
        </ul>
    </div>
   <div id="content">
        <div id="blog">
            Test text.
        </div>
   </div>
</body>
</html>

Answer №3

Change the positioning by removing fixed and incorporating this code into your #sidebar

#sidebar {
  position: -webkit-sticky;
  position: sticky;
  top: 21px;
}

Remember to adjust the sticky position for smaller screens as well :)

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

One-click process succeeds where two clicks fail

The code below is intended to go through a two-click process as follows: First click on .imagenes decreases opacity and makes .logo visible. Second click on .imagenes returns the opacity to 1 and hides .logo again. However, during this cycle of two ...

Guide to aligning a container to the left in Bootstrap 5

I have a grid container in Bootstrap 5 and I'd like to align it to the left on my webpage. <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3f5d50504b4c4b4b54e42f56b61a0acf40"&g ...

Could someone please assist me in identifying the issue with my CSS file? I am having trouble getting the primary color to display correctly in my HTML

I have incorporated a CSS style sheet found here: https://codepen.io/lorenzoyeung/pen/zYqxNQg However, when I attempt to implement the following code snippet, background-color: primary; border-color: secondary; the changes do not take effect. Can anyon ...

Alignment in the vertical direction of two lines

I've been struggling to align some HTML elements visually the way I want. There are two lines of text followed by a link. I'm trying to get the link to be vertically centered between the two lines, rather than just after the second line. Using a ...

Is CSS Friendly Control Adapters responsible for triggering a security issue?

I have recently integrated the CSS Friendly Control Adapters dll into my project and included the CSSFriendlyAdapters.browser file in the "App_Browsers" folder. Here is the content of the file: <browsers> <browser refID="Default"> <co ...

The height of the Material UI dropdown is displaying in an improper manner

I'm currently experimenting with the Material UI dropdown on my website, utilizing the react-select library. However, I've encountered an issue where the UI gets compromised when using an option that exceeds the width of the dropdown. If anybody ...

Aligning a flex container that has wrapping enabled

I need help with displaying a list on the user's screen that can be reordered by drag and drop. Currently, the drag and drop functionality is working fine. However, I want to display the list in multiple columns as sometimes there are too many option ...

Styling elements using css and flexbox

Looking for some help with wrapping 4 items in CSS using flex. I want to display 3 items in a row, followed by a single item. .movies { display: flex; flex-direction: row; justify-content: space-between; align-items:flex-start; flex: 1 ...

Transform this background into a transparent one, then ensure it becomes visible and remains that way

Here's what I've been working on: My HTML code: <div id="smith"> <img class="top" src="http://i.imgur.com/D6Kohra.png"/> </div> And the CSS for it: #smith { background: url("http://i.imgur.com/1ABXZ1y.png") no-repeat ...

Get Facebook to recognize and display link previews for websites using ajax technology

It is commonly known that when copying a link from an HTML website onto Facebook, the platform will attempt to fetch available images as previews for the link. But what happens when the content is dynamically generated, such as with JavaScript? In this c ...

JavaScript Challenge: Calculate the Number of Visible Characters in a Div

I have a div with text content (a string of length S) that is fixed in size but can be of any length. When the text exceeds a certain point (referred to as L), it gets truncated, and the portion beyond that limit becomes invisible. In other words, characte ...

What is the best way to create a toggle button that can show more or show less of a text snippet with animation?

Is it possible for someone to assist me with showing a long text partially and providing a "show more" button to reveal the rest, along with a "show less" option, all with some CSS animation? I was thinking of using a font awesome arrow down icon for expan ...

The navbar slide toggle is supposed to slide behind the navbar, but instead it slides in front of

Recently, I created an animated navbar that works perfectly fine. However, there is one issue that bothers me - when the list of menu items slides open, it appears in front of the navbar instead of behind it. If you'd like to see the problem in actio ...

Tips for utilizing a JQuery Selector for locating a DOM Element with one of its several classes

At the bottom of this text, you'll find my specific question. First, let me provide some context. I have a JQuery function written in TS: startEventListeners = () => { $(document).ready(() => { $('.dropdown-submenu ...

Ensure the footer remains fixed while scrolling

For the past day, I've been tackling a challenge with my online shop project. On the specific item's page, I want to include a fixed [add to cart] button as a footer initially, which then becomes sticky when scrolling to a certain point. You can ...

Instructions for creating a dropdown menu button that can also act as a link button

I'm struggling with my HTML code and can't seem to get the button to work as a link. Even after inserting an anchor tag with a link inside, clicking on the button does nothing. <!DOCTYPE html> <html lang="en"> <head> ...

Why is the value returned by getBoundingClientRect 190 when the y attribute is set to 200 in SVG?

When placing textBox1 at a y-position of 200, getBoundingClientRect is returning a value of 190. What could be causing this discrepancy? For more details and code snippets, please refer to the following link: https://codepen.io/anon/pen/REKayR?editors=101 ...

Attempting to load the parent window of a framed page from a separate domain results in a permission denial issue in Internet

Having an issue with a login page that is hosted within an iframe on a different domain. After a successful login, I am attempting to load the following page: <html> <head> </head> <body onload="parent.window.loca ...

The div element is not extending all the way to the left side of the browser window

I'm having trouble positioning a menu at the top of my webpage following a tutorial. The menu icon doesn't seem to reach all the way to the left side of the page. Despite multiple attempts to adjust the margin and padding settings, I can't ...

Add an element to the end within a separate div

I am facing a challenge where I need to rearrange ten divs with the class pcre within a single div identified by huge. The task involves moving the clicked div, which has the class pcre, to be placed at the end of the div with an id of huge using jQuery. ...