Provide one column with the necessary items, and supply the second column with all remaining resources

I am facing a challenge in designing a webpage layout with a left sidebar and the remaining content aligned to the right of it. Most examples I have come across use <div> tags with the display:table and display:table-cell properties where widths are specified in pixels for both the sidebar and the content area, or at least for the sidebar.

In my scenario, the contents within the sidebar are not predetermined. All I know is that they will not require much width (i.e., short lines and line-breaks ensure it does not expand extensively). Therefore, I do not wish to define a fixed width for the sidebar.

Is there a way to automatically adjust the sidebar <div> to fit the widest elements it contains, while allowing the content <div> to act as a column spanning from the right side of the sidebar to the edge of the page?

Answer №1

To ensure both areas have the same height and fill up the width, you can utilize display: table-cell in your CSS.

One interesting aspect of using tables for layout is that cells resize to fit content. By setting a very small width for the sidebar cell, it will shrink as much as possible while still adjusting to accommodate its contents. This behavior is consistent with CSS tables as well:

HTML:

<div class="container">
  <div class="column side">Sidebar content</div>
  <div class="column content">Main content</div>
</div>

CSS:

.container {
    display: table;
    width: 100%;
}
.column {
    display: table-cell;
    background-color: yellow;
    border: 1px solid blue;
}
.side {
    width: 1%;
}

Check out this JSFiddle example

Answer №2

If you prefer not to use table elements, utilizing the CSS float property is a good alternative.

HTML

<div class="wrapper">
  <div class="box sidebar">
    Sidebar Content<br/>
  </div>
  <div class="box main-content">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  </div>
</div>

CSS

.wrapper {
    width: 100%;
    position:absolute;
}
.box {
    background-color: green;
}
.sidebar {
    float: left;
    top:0;
    position:absolute;
    height:100%;
    border-right: 1px solid black;
}

Check out the updated fiddle http://jsfiddle.net/8s4e6c21/3/ While display: table-cell works fine, using floats for columns can lead to cleaner markup and better readability in my opinion.

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

Margin discrepancies when using em units

Currently, I am utilizing an em-based margin for items within a menu that are homogeneous in terms of markup, class, and id. It appears that the margins for each of these items should be rendered the same. However, some are displaying as 1px while others a ...

If one sibling requires flexing, apply flex to all siblings

Illustrative Issue: large: https://i.sstatic.net/oq0jA.png small: https://i.sstatic.net/WXqmV.png When space is limited, one row will start to flex while others remain static if they have enough space. Is there a way to make all rows flex when at least ...

What is the method to activate sequential selection through JSON response?

I just started exploring angularjs and I'm interested in selecting values step by step. But first... Here's the JSON response I received: [ { "countryname": "India", "states": [ { "statename": "Karnataka", ...

After attempting to read in this JSON text string, it is stubbornly refusing to display properly with a line break

I have a snippet of text stored in a JSON file that appears like this "I want a new line\nhere". This is the code I am using to display the text after replacing the \n with a <br />: <p className="subtitle is-5 has-text-w ...

Tips for ensuring that divs resize to match the container while preserving their original proportions:

#container { height: 500px; width: 500px; background-color: blue; } #container>div { background-color: rgb(36, 209, 13); height: 100px; } #one { width: 1000px; } #two { width: 800px; } <div id="container"> <div id="one">&l ...

No display of Tailwind updates on local server in Safari browser using NextJS and a Mac M1

For my personal project with NextJS, I am using Tailwind CSS, but I am experiencing frequent issues with updating styles. It seems like there is a 50/50 chance that Tailwind will apply the styles to the component properly, and sometimes I have to go throug ...

Tips for changing form field values using jQuery

Is there a way to use jQuery to retrieve the values of multiple checkboxes and insert them into a hidden form field? Here is how my checkboxes are structured: <form> <input type="checkbox" name="chicken" id="chicken" />Chicken <in ...

Forward default.aspx

My current ASP.NET project is set to start on the Default.aspx page, but I'd like to change it to begin on a different page. While I know it's possible to specify the start page in VS2008, it doesn't seem to have any effect on the published ...

How can I eliminate the scrollbar from appearing on all browsers when visiting my website?

I've encountered an issue while building my portfolio with React.js. I'm having trouble removing the scrollbar. I've attempted using overflow: hidden for the parent element and overflow: scroll for the child div, but it's not producing ...

Utilizing jQuery to implement a CSS style with a fading effect, such as FadeIn()

I have a jQuery script that applies a CSS style to an HTML table row when the user clicks on a row link: $(this).closest('tr').css("background-color", "silver"); Is there a way to soften this color change effect, such as by gradually fading in ...

HTML: The art of aligning elements within a header

I've been working on creating my personal HTML record, but I'm facing one last challenge that I can't seem to overcome. I want to design my header like this: https://i.stack.imgur.com/FU8EJ.png With the following elements: TEXT SUMMARY wi ...

Ways to position the second section below the button

Is there a way to make a section of my website where, upon pressing a button, the button moves to the left and reveals collapsed text that appears below it with some padding on top? I've been trying to achieve this as a beginner by looking through doc ...

Encountering a problem with the persistent JavaScript script

I have implemented a plugin/code from on my website: Upon visiting my website and scrolling down, you will notice that the right hand sidebar also scrolls seamlessly. However, when at the top of the screen, clicking on any links becomes impossible unless ...

HTML dynamic voting system

I'm new to coding in HTML and I have a specific challenge that I need help with. I want to display a value on my webpage that increases every time a user clicks a button. Below is the code I have written so far: <script type="text/javascript& ...

Information flows from the Bootstrap 4 template

When viewed on a mobile device, the content extends beyond the page and a horizontal scrollbar appears. You can view my page by clicking this link. <div class="custom_content col-md-12"> <div class="container"> <div class="row"> ...

Tips on hovering over information retrieved from JSON data

Within my code, I am extracting information from a JSON file and placing it inside a div: document.getElementById('display_area').innerHTML += "<p>" + jsonData[obj]["name"] + "</p>"; I would like the abi ...

Wait for the Selenium test to continue only when a specific element is visible on

<img style="width: 640; height: 640;" id="img108686545" alt="Loading Word War..." src="www.something.com/anu.jpg"> Is there a way to detect when this particular element is visible on the page? The value of the "id" attributes keeps changing with eac ...

The footer at the bottom of the page is currently malfunctioning

I was able to create an always on the bottom footer using code from Codepen. Here is the HTML: <div class="content"> <header> <p>blabla</p> </header> <main> <p>blaBlabla blub</p ...

Calculate the frequency of tag_name occurrences on a webpage using Python and Selenium

Greetings! I am currently working with the following code snippet: for each in driver.find_elements_by_xpath(".//*[@id='ip_market" + market + "']/table/tbody") cell = each.find_elements_by_tag_name('td')[0] cell2 = each.find_el ...

Utilize MaterialUI's stepper component to jazz up your design with

Is there a way to customize the color of a material ui Stepper? By default, the material UI stepper's icons use the primary color for both "active" and "completed" steps. class HorizontalLinearStepper extends React.Component { state = { activeS ...