Fixed width in the left column with a fluid and clear:both in the right column in a 2-column layout

Struggling to figure this out after spending a lot of time on it. Here's what I'm trying to accomplish:

I have a 2 column layout, with the left column having a fixed width of 220px and the right column having a fluid width.

The code looks like this:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
     <title>Fluid</title>
    <style type="text/css" media="screen">
        html, body { background: #ccc; }
        .wrap      { margin: 20px; padding: 20px; background: #fff; }
        .main      { margin-left: 220px; width: auto }
        .sidebar   { width: 200px; float: left; }
        .main,
        .sidebar   { background: #eee; min-height: 100px; }
    </style>
</head>
<body>
    <div class="wrap">
        <div class="sidebar">This is the static sidebar</div>
        <div class="main">This is the main, and fluid div</div>
    </div>
</body>
</html>

Currently, I am using clear: both in the right column, which causes all content after it to move under the left column. While this behavior is correct, I need clear: both to only affect the right column and not move underneath it.

Is there a way to achieve this while still maintaining the basic float concept of the page design?

UPDATE: For a better understanding of my issue, please refer to this link: http://jsfiddle.net/k4L5K/1/

Answer №1

Check out the updated CSS code below:

html, body {
    background: #ccc;
}

.wrap {
    margin: 20px;
    padding: 20px;
    padding-right:240px;
    background: #fff;
    overflow:hidden;
}

.main {
    margin: 0 -220px 0 auto;
    width: 100%;
    float:right;
}

.sidebar {
    width: 200px;
    float: left;
    height: 200px;
}

.main, .sidebar {
    background: #eee; min-height: 100px;
}

.clear { clear:both; }
span { background: yellow }

The layout has been modified to float the .main div to the right. This required adding a padding of 240px to the .wrap div and setting a negative right margin of -220px on the .main div.

  1. A padding of 240px was added to the .wrap div.
  2. A right margin of -220px was set on the .main div for proper alignment.

By floating the .main div to the right, the clear: both; now only affects content within the .main div as intended.

You can view a live demonstration here: http://jsfiddle.net/6d2qF/1/

Answer №2

Although this question has been around for a while, I have recently come across another solution.

To resolve the issue, we simply need to take two steps:

  1. Include overflow: auto; in the .main div
  2. Ensure that the wrapper maintains document flow by adding overflow: hidden; to the .wrap div or by inserting a .clear div as the last child of the .wrap element

You can view the updated fiddle here: http://jsfiddle.net/k4L5K/89/

Answer №3

Give this a shot:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
     <title>Fluid Layout</title>
    <style type="text/css" media="screen">
        html, body { background: #ccc; }
        .wrap      { margin: 20px; padding: 20px; background: #fff; }
        .main      { margin-left: 220px; width: auto }
        .sidebar   { width: 200px; }
        .main,
        .sidebar   { background: #eee; min-height: 100px; float: left; }
        .clear {clear:both;}
    </style>
</head>
<body>
    <div class="wrap">
        <div class="sidebar">This is the static sidebar</div>
        <div class="main">This is the main content, which adjusts fluidly</div>
        <div class="clear"></div>
    </div>
</body>
</html>

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

The CSS styling does not seem to be affecting the text button's appearance

I am currently creating a website using Bootstrap 5, and I'm facing an issue with making the header image responsive on mobile devices. While it looks great on desktops, laptops, and tablets, on mobile screens, the text and button alignment is off. I& ...

The function .css() in jQuery is malfunctioning

This task should be a piece of cake... All I want is to hide the buttons when a user is logged in and display the log out button instead. jQuery().ready(function($) { if ($("body").hasClass("logged-in")) { $(".logged-out-button").css("display", " ...

What is the best way to display an icon and text side by side in the header of a Phonegap app

____________________________________________________________ | logo_image page_Title | ------------------------------------------------------------ I am looking to create a header for my PhoneGap app similar to the one sh ...

Having difficulty retrieving certain hyperlinks from a webpage with BeautifulSoup

I am attempting to extract images from this webpage. The page contains numerous images that lead to different pages when clicked, and I aim to navigate through each and collect the images from the linked pages. To achieve this, my first step is to compile ...

Guide to creating a distortion-free layout with Bootstrap 4

I am in need of creating a design layout using Bootstrap 4 with the following specifications: https://i.sstatic.net/JeUHh.png Here is my code : <div class="row"> <div class="col-md-7"> A </div> <div class="col-md-5"> <div ...

Expanding elements with Flexbox and Bootstrap 4: Strengthening the height to fill the remaining space

I have a row with three columns, each containing an image and a title. Currently, on a large screen, the titles are displayed on a single line and everything looks fine. However, when resizing the screen, the first title wraps onto two lines: My goal is ...

Display text on top of an image using HTML

I'm having trouble creating a submit button for my form that includes both an image and text. I've tried multiple methods, but encountered some issues. Here's the HTML code: <form action="www.page.html" method="post> some text <spa ...

Add a <span> element to the header element in the CSS using jQuery

Struggling to add this unique element after a specific css-class on my website powered by Wordpress: <span class="blink">_</span> I'm unable to alter the file structure, but I have the liberty to utilize CSS and jQuery. The chunk of code ...

HTML - Centered layout with a constant header and footer

Imagine a layout where there is always a header at the top, a footer at the bottom, and the space between them should be filled by the content 100% (image 2). Additionally, everything in the header, content, and footer should be horizontally centered, usin ...

Communicating between iframes and parent elements via events

I'm trying to trigger an event in my iframe using the following code: $('body').trigger('my_event'); However, I want to bind this event on my main page that contains the iframe like this: $('iframe').find('body&ap ...

What is the best way to add an image to a SVG placeholder?

This is all unfamiliar territory for me. I am experimenting with the Bootstrap template named "Carousel" as my starting point, utilizing Bootstrap 5.3.2. Here is the link to the Carousel example Everything seems fine so far. However, I am struggling to l ...

"Troubleshooting: Click counter in HTML/Javascript unable to function

My latest HTML project is inspired by the "cookie clicker" game, and I'm working on it for a friend. So far, I've managed to get the click counter to function to some extent. Essentially, when you click on the image, the number at the bottom sho ...

Guidelines for toggling text visibility based on a randomly generated number with if statements in vanilla JavaScript

I am currently attempting to showcase some intricate text alongside an image once a button is clicked. At the moment, I have successfully implemented the functionality to display a random image when the button is clicked. However, I am encountering diffic ...

Challenges with nesting radio button groups in jQuery validation

Trying to implement a custom validation method for a group of radio buttons in v1.9 of the jQuery validate plugin. Despite this article, it is indeed feasible to have radio buttons with the same name attribute, as long as different class rules are applied ...

Tips for populating a form in CasperJS by referencing the form name

Struggling to populate the fields in a form: <form name="step_one_login_form" action="/" method="post"> <input type="hidden" name="form_name" value="step_one_login_form"> <input type="hidden" name="return_url" value="index.php? <di ...

I need clarification on the following: content:''

I have a question regarding this particular code snippet .store { display: block; position: relative; } .store:before, .store:after { display: block; position:absolute; content:''; } .store:before { background: url(store-before.png); ...

Examining a specific element node to see if it contains a certain string

When using PHP Unit with Selenium Server, my goal is to verify if a specific element in an xpath contains a certain string value, like this: <table> <thead></thead> <tbody> <tr> <th>1</th> < ...

Adjusting the outline color of a Material UI Select component in outlined mode to a different color when the dropdown is activated (currently shown as blue)

Is there a way to change the outline color of Material-UI select component outlined variant correctly? I need help changing the default blue color to red, any assistance would be greatly appreciated Click here for an image reference Reference code snippe ...

The style from '<URL>' was not applied as it has a MIME type of 'text/html', which is not a compatible stylesheet MIME type

After attempting to run the angular application with the home component, an error appeared stating that styles were refused and images could not be found. It's puzzling because the CSS and image folder are located in the /src/assets folder. I have tr ...

What could be the reason for the hover attribute not functioning properly in NextJS

Why isn't my CSS recognizing the hover attribute? Here is my code: I would like the class "mega" to appear when hovering over the li tag. <nav className="mobile-hide"> <ul className="flex-item second-links&quo ...