Text positioned in a fixed location

How can I ensure that the main content stays on the right side of the sidebar without adding margin-right?

Currently, the sidebar is being covered by the body content due to its fixed position. Is there a solution to prevent this issue?

.App {
  display: flex;
}

.s {
  position: fixed;
}
<div class="App">
  <div class='s'>sidebar</div>
  <div class='m'>
    <p>data</p>
    <p>data</p>
    <p>data</p>
    ...
    <p>data</p>
  </div>
</div>

Answer №1

Instead of using the outdated method, try implementing position:sticky.

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

::before,
::after {
  box-sizing: inherit;
}

.App {
  display: flex;
}

.s {
  position: sticky;
  top: 0;
  border: 1px solid red;
  align-self: flex-start;
  min-height: 100vh;
}
<div class="App">
  <div class='s'>sidebar</div>
  <div class='m'>
    <p>data</p>
    <p>data</p>
    <p>data</p>
    <p>data</p>
    <p>data</p>
    <p>data</p>
    <p>data</p>
   ... (remaining content)
  </div>
</div>

Answer №2

To create a fixed-width sidebar, you can enclose it in an element with a specific width.

.App {
  display: flex;
}

.s1 {
  width: 120px; /* Adjust the width as needed */
}

.s {
  position: fixed;
}
<div class="App">
  <div class="s1">
    <div class='s'>sidebar</div>
  </div>
  <div class='m'>
    <p>data</p>
    <p>data</p>
    <p>data</p>
    ...
    <p>data</p>
  </div>
</div>

Answer №3

To make the layout work, consider setting a width for app and utilizing margin-left: auto; for your content.

.App {
  display: flex;
  width: 100px;
}

.s {
  position: fixed;
}

.m {
  margin-left: auto;
}
<div class="App">
  <div class='s'>sidebar</div>
  <div class='m'>
    <p>data</p>
    <p>data</p>
    <p>data</p>
    <p>data</p>
    <p>data</p>
    <p>data</p>
    <p>data</p>
    <p>data</p>
    <p>data</p>
    <p>data</p>
    <p>data</p>
    <p>data</p>
    <p>data</p>
    <p>data</p>
    <p>data</p>
    <p>data</p>
    <p>data</p>
    <p>data</p>
    <p>data</p>
    <p>data</p>
    <p>data</p>
    <p>data</p>
    <p>data</p>
    <p>data</p>
    <p>data</p>
  </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

Troubleshooting: CSS border-radius issue with embedded iframe

I am struggling to embed an iframe from my Blogger site into a specific section on my website and round its corners using CSS. I followed a tutorial that suggested using overflow: hidden;, but for some reason, it's not working in my case - see the ima ...

Send folder to the homepage of the website

I want to implement a redirect using .htaccess to prevent people from snooping into my directories and index files. For instance, if someone tries to access a specific image file like site.com/pics/1.jpg by trying to see the entire "pics" folder at site.co ...

Maintaining aspect ratio in CSS grid layouts

I am in the process of designing a CSS grid layout where I want to maintain the same height for all columns. Additionally, I would like the first item to have a 16:9 aspect ratio. However, I am encountering an issue where the images are not fitting perfect ...

To align a div or text to the right using inline-flex, simply

I am facing an issue with aligning my animated links to the right. Currently, I am using display: inline-flex, which works well but doesn't align the links properly. I have tried using float: right for alignment, but I feel there must be a better way ...

Change the CSS element if the current URL is not example.com/page1 or example.com/page2

When I apply the following JS code snippets, my output is incorrect or does not display at all. Below are the codes in question: if (location.href != "website.com/page1" || "website.com/page2") { element.style.backgroundColor='none'; ...

What is the best way to automatically adjust the size of this HTML Menu to match the width of its

I am in the process of converting a Drupal 6 theme to Drupal 7 and I'm encountering some difficulties with this particular section. Below is the HTML code snippet: <ul id="nav" class=" scaling-active scaling-ready"> <li><a href="/demos ...

Troubleshooting problem with CSS layout when adjusting the height of a section

I recently delved into creating a simple website using HTML5 and have successfully set up the site structure. Everything has been smooth sailing so far. However, I've encountered a section of the site that seems to have a mind of its own. When I ass ...

CSS: "Cutting-edge" design. In what way?

I am seeking to replicate a design similar to that of Fantastical's website (https://flexibits.com/fantastical), where the edge of a screenshot extends beyond the page boundary. As the user resizes the window, more of the screenshot becomes visible. A ...

Encoding a JSON representation of an HTML list where all children are displayed at each parent item

Here is the structured list that I am currently working with: function convert( name_ref ) { name_ref = name_ref + " > ol > li"; var mylist = []; $(name_ref).each(function () { if ($(this).find('> ol > li').length){ myl ...

JavaScript will continue to process the submit to the server even after validation has been completed

My current challenge involves implementing form validation using JavaScript. The goal is to prevent the form from being sent to the server if any errors are found. I have made sure that my JavaScript function returns false in case of an error, like so: ...

Printing style in CSS

Everything looks good on my webpage. For printing, my CSS adjusts the layout and section sizes. Unfortunately, there is an issue with the print layout - there is a one-inch margin on the left side causing two elements to go off the page on the right. I ...

TypeScript: displaying parameters

variable_field = [["S",".","."],[".","#","."],[".",".","T"]]; <ng-template ngFor let-array [ngForOf]="field_array" let-x="index"> <ng-t ...

Navbar active class not updating on jQuery page scroll

My one-page website has a fixed navbar that I want to change its active status when scrolling down to specific div positions. Even though I tried using jQuery, the code doesn't seem to work as intended. Here is the snippet: // SMOOTH SCROLLING PAGES ...

Leveraging the power of PHP variables within jQuery code

Wondering how to incorporate a PHP value into jQuery? My approach involves retrieving the VAT rate from the database using PHP and storing it in $vatrate: $sqlv = <<<SQL SELECT * FROM `vatrate` WHERE id='1' SQL; if(!$resultv = $db-&g ...

Can a collapse that was saved in a separate file be displayed on the requested page?

Imagine having a navigation bar on your website's index page with four buttons, each leading to a different page. What if you wanted to bring all those separate pages together into one by using collapsible elements? That's the challenge this user ...

Creating a pull-up toolbar with just HTML and CSS for an Android browser

I need to create a draggable toolbar on my mobile webapp that can reveal content beneath it. I want to achieve this using just HTML/CSS, without relying on touch/scroll events or libraries. To do this, I'm overlaying a scrolling element on top of the ...

Ensuring Bootstrap 3 Table Header Widths Remain Fixed

How can I ensure the header columns in my Bootstrap 3 table maintain a fixed width, regardless of the content in the regular rows? I want to avoid the messy look of content splitting onto multiple lines. Here's an example: I'd prefer not to adju ...

Tips on creating a blurred background effect when an HTML popup modal is activated

Whenever I click on a button, a popup window appears. What I want is for the background to be blurred when this popup modal opens. <div class="modal" id="myModal"> <div class="modal-dialog modal-lg"> <div class="modal-content"> ...

Despite being in perfect condition, my If-Else statement is refusing to function properly

I had previously posted a similar question, but it was removed. However, I am still very curious about why something seemingly trivial is not functioning properly. This is the entirety of my HTML and JavaScript code... <!doctype html> <html lang=& ...

The mouse pointer adjusts according to the event

I am working on an ajax request and I have implemented a feature where the cursor changes appearance. document.body.style.cursor = "wait"; This immediately shows the cursor as a spinning circle when the request starts. At the end of the request, I ch ...