Text not shifting within wrapper container

I am attempting to design a webpage layout with a fixed horizontal navigation bar at the top, where all other content on the page starts underneath. Additionally, I want a fixed sidebar on the left side where all content should align to the right of it.

You can view my attempt at this layout on jsfiddle here: https://jsfiddle.net/o5p8yuvx/5/.

Unfortunately, the current setup is not working as intended. While the content correctly begins under the topbar, it does not align to the right of the sidebar. I've noted in the code that sidebar sits below topbar; uncommenting it will reveal that the content does not position to the right of sidebar. My goal is to have any additional text located outside both wrappers fill the space beneath topbar and to the right of sidebar, instead of just appearing below topbar.

This issue persists with other content-containing divs as well. How can I ensure that all subsequent content appears outside of the bars, similar to how sidebar is positioned beneath topbar?

Answer №1

To optimize the layout, it is recommended to establish a separate <div> element dedicated solely to the content:

<div class="content">
  arbitrary text
</div>

Subsequently, you can adjust the positioning of this new <div> to occupy the remaining space:

.content {
  margin: 0 0 0 300px;
}

body {
  margin: 0;
}

.topwrapper {
  height: 60px;
}

.sidewrapper {
  width: 300px;
}

.topbar {
  background-color: grey;
  position: fixed;
  width: 100%;
  height: 60px;
  margin: 0;
}

.topbar ul {
  list-style-type: none;
}

.topbar li {
  color: blue;
}

.topbar a:hover {
  background-color: black;
}

.sidebar {
  background-color: black;
  position: fixed;
  width: 300px;
  height: 100%;
  margin: 0;
}

.sidebar ul {
  list-style-type: none;
}

.sidebar li {
  color: blue;
}

.sidebar a:hover {
  background-color: white;
}

.content {
  margin: 0 0 0 300px;
}
<!DOCTYPE HTML>
<html>

<head>
  <title>Webpage</title>
</head>

<body>
  <div class="topwrapper">
    <div class="topbar">
      <ul>
        <li><a style="float: left">Link 1</a></li>
        <li><a style="float: left">Link 2</a></li>
        <li>
          <p style="float: right">Welcome</p>
        </li>
      </ul>
    </div>
  </div>
  <div class="sidewrapper">
    <div class="sidebar">
      <ul>
        <li><a>Link 3</a></li>
        <li><a>Link 4</a></li>
        <li>
          <p>Welcome</p>
        </li>
      </ul>
    </div>
  </div>
  <div class="content">
    arbitrary text
  </div>
</body>

</html>

A working example using a jsfiddle link can be found here: jsfiddle.


If aiming for semantic correctness, consider utilizing the <main> tag in place of <div class="content">, with corresponding CSS modifications as follows...

main {
  margin: 0 0 0 300px;
}

body {
  margin: 0;
}

.topwrapper {
  height: 60px;
}

.sidewrapper {
  width: 300px;
}

.topbar {
  background-color: grey;
  position: fixed;
  width: 100%;
  height: 60px;
  margin: 0;
}

.topbar ul {
  list-style-type: none;
}

.topbar li {
  color: blue;
}

.topbar a:hover {
  background-color: black;
}

.sidebar {
  background-color: black;
  position: fixed;
  width: 300px;
  height: 100%;
  margin: 0;
}

.sidebar ul {
  list-style-type: none;
}

.sidebar li {
  color: blue;
}

.sidebar a:hover {
  background-color: white;
}

main {
  margin: 0 0 0 300px;
}
<!DOCTYPE HTML>
<html>

<head>
  <title>Webpage</title>
</head>

<body>
  <div class="topwrapper">
    <div class="topbar">
      <ul>
        <li><a style="float: left">Link 1</a></li>
        <li><a style="float: left">Link 2</a></li>
        <li>
          <p style="float: right">Welcome</p>
        </li>
      </ul>
    </div>
  </div>
  <div class="sidewrapper">
    <div class="sidebar">
      <ul>
        <li><a>Link 3</a></li>
        <li><a>Link 4</a></li>
        <li>
          <p>Welcome</p>
        </li>
      </ul>
    </div>
  </div>
  <main>
    arbitrary text
  </main>
</body>

</html>

Answer №2

When incorporating 2 fixed properties, it is necessary to specify the distance from the top for positioning the sidebar. You can achieve this by adding a single line in your css to indicate how far from the top of the parent container you want the div to appear:

.sidebar {
  ...
  top: 60px;
}

The value 60px was chosen based on the height of your topbar. In case the top bar undergoes any changes, you can adjust the position of the sidebar accordingly. Check out the revised fiddle for reference.

By default, the browser sets top: auto, allowing the browser to determine the placement of these elements even if they are nested within other elements as in your case.

For elements with position: absolute; or position: fixed;, the top property positions the top edge of the element at a specified distance above/below the top edge of its nearest positioned ancestor.

As a result, the css code starts tracing back to find the closest "positioned ancestor" and uses it as a point of reference for determining the top position.

Here are more details on using the Top property

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

Having trouble with conditional statements in tpl file on Prestashop (PHP, Smarty)?

I'm having trouble implementing the following: Showing a conditional voucher on the order confirmation page. The voucher depends on two specific conditions: the weight of the order and whether the user has an account Currently, I am working on the f ...

Ways to update the value in localstorage with the click of a button

After a user inputs a numerical value on one screen of my web app, the value is stored in local storage. On the next screen, the user can click a button to increment the stored value by a specified amount, such as adding 5. For example, if the variable X i ...

Building an expansive navigation menu with react-bootstrap

My current project involves creating a mega menu. Although I successfully made a responsive navbar, the challenge now is to implement a dropdown panel with 100% width. I've tried various approaches but haven't found one that works. Note: The oth ...

Utilizing JavaScript to display numerous variables within a text box

After creating an HTML form, I encountered an issue where only one selected item was displayed in the text field. Can anyone help me solve this problem so that multiple names can be printed in the textfield? function myFun(extras) { document.get ...

Utilizing Node modules like Sentry in HTML template files when served from Golang using the gin-gonic package

Summary: I am trying to integrate Sentry into a Golang application that renders a simple HTML page using the gin-gonic package. Including vanilla Javascript works fine, but I am facing difficulties when trying to include scripts with dependencies. I have ...

What specific style of CSS is utilized in Material UI's `sx` property feature?

I came across this code snippet in the Material UI documentation: <FormControl sx={{ "& > :not(style)": { m: 1 }, maxWidth: "100%", minWidth: "100%", }} > It appears that & is not a standard p ...

Center text within a div container

I am facing an issue with a nested div element: <div id="footer"> <div id="footer_text"> <p>My foooo text</p> </div> </div> The CSS code for the nested div is as ...

Updating the iFrame source using jQuery depending on the selection from a dropdown menu

I want to create a dynamic photosphere display within a div, where the source is determined by a selection from a drop-down menu. The select menu will provide options for different rooms that the user can view, and the div will contain an iframe to showca ...

How the top property in absolute positioning is determined by the parent element's width

After analyzing the provided HTML code <div class="child-of-body"> This is a text </div> and reviewing the accompanying CSS code .child-of-body { position: absolute; top: 10%; } I have successfully adjusted the top value for the ...

Issue with X overflow visible at the right edge of the webpage (Vue.js and Tailwind CSS)

RESOLVED There is a strange white overflow on the right side of my responsive website that appears and becomes smaller as I scroll down the page. I can't seem to figure out why this is happening. None of the elements are overflowing into that space. ...

Can multiple if statements be executed simultaneously in plain Javascript? If yes, what is the method to do so?

Currently, I'm developing a game using canvas in HTML, CSS, and vanilla JavaScript. I've successfully created two characters with movement controls assigned to player 1 and player 2. However, a challenge arises when waiting for one player to move ...

Implement a full-width dropdown menu with Bootstrap 4

Hey guys, I'm having a real head-scratcher here. I've created a dropdown menu using Bootstrap 4, but I'm having trouble making it responsive for mobile screens. I've tried using media queries to make the dropdown menu occupy 100% width ...

After encountering a character with CSS, begin a new line

I have a CSV spreadsheet with data that looks like this: <p>Features:• first feature• second feature• third feature• fourth feature• and so on (the total number of features is variable)</p> I want each feature to display on a new li ...

Delayed suggestions using Typeahead and Bloodhound technology

I have a situation where certain fields require a three-letter shortcode for locations. These shortcodes are not widely known and are mainly familiar to a select group of long-time users. For example: LND - London SWN - Swindon The dropdown menu displa ...

Moving data from one table to another and making changes or removing it

After successfully adding data from one table to another using .click, I encountered an issue. Whenever I utilize the search field in the top table, it clears the appended rows in the bottom table. I am looking for a solution to have these tables generate ...

Why isn't a single click enough to activate an anchor generated with the Sidr jQuery library in Rails?

I am utilizing a library named Sidr to generate sidebars. I have included all the necessary jQuery and CSS, and while it is operational, I find that I need to double click instead of just one click to open the sidebar. Here is the code snippet. I essentia ...

Issue with URL redirection and reloading in Safari and IE browsers

My current method for redirecting to the Item page and performing a full page reload works well in most browsers, but unfortunately not in Safari and IE. In those two browsers, the current page is reloaded instead. Does anyone have any suggestions for an ...

Is it possible to utilize various return values generated by a regex?

Working on a project where I am utilizing regex to extract links from a Google Calendar XML feed. The links appear in the following format: <a href="http://www.drsketchysdublin.com/event-registration/?ee=11">http://www.drsketchysdublin.com/event-reg ...

Monitoring changes in SASS files using node-sass through NPM

I have node-sass installed with the options set according to the guidance provided in a response on Using node-sass watch option with npm run-script, however, I am facing issues with it not working as expected. Below is the content of my package.json file ...

Inject CSS styles into panelgrid on-the-fly

How can I pass a color code property fetched from ebean to a css function? <h:panelGrid id="testpanel" columns="#{message.no_of_columns}" rows="#{message.no_of_rows}" styleClass="dynamicGrid"> <c:forEach items="#{bLDashBoar ...