a container holding two floating divs

I have been attempting to create two divs positioned side by side, with one containing content and the other acting as a sidebar. Both of these divs are contained within another larger container div. Despite my efforts, I have not been successful in achieving this layout. Below is the CSS code that I have used:

#container {
   width:1000px;
   position: relative;
}



#content {
    width:700px;
    background-color: white;
    border-top: 3px solid midnightblue;
    padding: 80px 10px 0px 10px;
    float:left;
}

#sidebar{
    border-top: 3px solid midnightblue;
    background-color:#E0E0E0;
    padding: 20px;
    width: 250px;
    float:left;
}

The corresponding HTML code is as follows:

 <div id="container">

<div id="content">
    <p> This is my blog website. </p>
</div>

<div id="sidebar">
   <p>This is the sidebar. </p>
</div> 

</div>

I have tried changing the "float:left" property to "float:right" on the sidebar, as well as adding "display:table;" to the container, but neither solution has worked. The sidebar consistently appears below the content region instead of beside it.

There is also a wrapper class enclosing everything. When I remove the wrapper and the container, the layout works as intended. However, I require the presence of the wrapper class. Any advice or suggestions would be greatly appreciated. Thank you!

Answer №1

Implement the box-sizing: border-box property for better styling control. Learn more about box-sizing here.

To apply this, include the following CSS code:

* { 
  -webkit-box-sizing: border-box; /* For Safari/Chrome and other WebKit browsers */
  -moz-box-sizing: border-box;    /* For Firefox and other Gecko browsers */
  box-sizing: border-box;         /* For Opera and IE 8+ */
}

View JSFIDDLE DEMO

Answer №2

The problem lies in the padding of the div element. To fix this, decrease the width of the content div to width:680px;. Once adjusted, the display should appear correctly, as demonstrated here:

http://jsfiddle.net/k8ggmctq/

Answer №3

The issue you're experiencing is due to the overall width exceeding that of the container. Please refer to this example: http://jsfiddle.net/kez5c71m/ I made an adjustment to the padding of the #sidebar, changing it from 20 to 10.

#container {
   width:1000px;
   position: relative;
}



    #content {
        width:700px;
        background-color: white;
        border-top: 3px solid midnightblue;
        padding: 80px 10px 0px 10px;
        float:left;
    }

    #sidebar{
        border-top: 3px solid midnightblue;
        background-color:#E0E0E0;
        padding: 10px;
        width: 250px;
        float:left;
    }

Answer №4

Following the guidelines in your CSS file:

 #container has a width of 1000px

 #content has a width of 720px (700px + 10px + 10px for left and right padding ) 

 #sidebar has a width of 290px (250px + 20px + 20px for left and right padding )

    The total width is 1010px (the #sidebar did not position properly within the #container due to exceeding its width)

Check out the code in action on jsfiddle!

Answer №5

To properly display your content and sidebar, set the width and height of the container div and adjust the widths of the individual elements accordingly. You can refer to the following fiddle link for a working example,

#container {
   width:450px;
    height:400px;
   position: relative;
   border: 3px solid midnightblue;
}

#content {
    width:200px;
    background-color: white;
    border: 3px solid midnightblue;
    padding: 80px 10px 0px 10px;
    float:left;
}

#sidebar{
    border: 3px solid midnightblue;
    background-color:#E0E0E0;
    padding: 20px;
    width: 100px;
    float:left;
}

http://jsfiddle.net/w5dmzoqj/

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

Achieving a sleek line effect with a gradient border

Is there a way to make the gradient border transition between two colors look like a straight line? Additionally, is it possible to have the line start in the bottom left corner instead of the middle of the right side of the button? The current CSS code I ...

Issue with KeyboardDatePicker in Material-UI/Pickers occurs when InputAdornmentProps property with position start is added, leading to an unexpected margin

I am currently working with a component from material-ui/pickers and this is the code snippet: <KeyboardDatePicker value={selectedDate} onChange={(_, newValue) => handleClick(newValue)} labelFunc={renderLabel} disableToolbar variant=&a ...

How to Use CSS to Crop a String from the Middle

My file has a long name and I am using CSS text-overflow: ellipsis to crop it. <style> #fileName { width: 100px; white-space: nowrap; text-overflow: ellipsis; overflow: hidden; } </style> <div id="fileName"> ...

What is the best way to incorporate AngularJS bindings when loading HTML content from a string?

I am facing an issue in my angular project where I need to load external HTML from a string variable into a div that currently has a controller scoped to it. The problem is that the HTML being loaded contains angular bindings, but once loaded, these bindi ...

Finding image input loading

When working with the following code: <input type="image" ... onLoad="this.style.opacity = 1" /> Everything seemed to be functioning well in IE, but encountered an issue in Chrome where the onLoad event failed to trigger upon image load. It's ...

Problem with layout design (utilizing float properties)

I'm curious why the paragraph content gets sandwiched between two floated divs. I'd like the paragraph content to appear below the header. Also, why aren't the line breaks showing in the paragraph? Check out this JS Fiddle link for referenc ...

Is there a way to roll up the background image of the body?

body { background: url(http://andreypokrovskiy.com/image_hosting/boredom/space-bg.jpg) repeat; animation: backdrop_roll linear 100s infinite; } .enemy-animation { position: relative; z-index: 1; animation-direction: alternate; animation-durati ...

Ways to utilize jquery to limit the length of an editable div and prevent it from exceeding our specified restriction

Imagine a scenario where you have the following code snippet: <div id="editing" contenteditable onclick="document.execCommand('selectAll',false,null)">Put text here...</div> In this situation, let's say you want to impose a r ...

Perform the action when the button is clicked

I'm struggling with finding a solution to this problem. I have an input field and a submit button. My goal is to update the value of a variable to match the value in the input field when the submit button is clicked. Additionally, I need to execute a ...

What is the best way to calculate the total sum of the first element in each index of an array when using ng

I am looking to calculate the sum of the first elements from all indexes of an array using ng-repeat in AngularJS. My goal is to declare a variable and increment its value with each iteration of ng-repeat, then display that variable at the end. Below is ...

Verify the presence of both class and id before modifying the content of the h1 tag and automatically redirecting the page

I'm encountering some issues triggering my JS/JQ on an HTML5 page. Essentially, I want to verify the existence of the following ID and class: ID: page_blog CLASS: page current <section class="page current" id="page_blog" style="z-index: 99; lef ...

What is the best way to merge 60 related jquery functions into a unified function?

I designed a webpage that serves as an extensive checklist. When you click on the edit button for a checklist item, a modal window opens up. This modal window contains a radio button to indicate whether any issues were found during the check. If "Yes" is s ...

Issues with retrieving the output of a function nested within another function through an ajax request

I am attempting to use jQuery to add the results of an ajax call to a paragraph. My goal is to extract the "myResult" variable from the inner getResult function and transfer it to the outer buildParagraph function. Unfortunately, the returned value is und ...

Encountering a problem with the multi-page template structure in jQuery Mobile

As a newcomer to Phonegap, I am utilizing jQuery Mobile and HTML5 for the development of an Android app. The app consists of three pages: the first page, index.html, displays a list of contents; the second page holds text content as well as a header with b ...

Ensure that adjacent elements operate independently from one another

The code snippet provided above showcases four components: StyledBreadcrumbs, FilterStatusCode, Filter, LinkedTable. The FilterStatusCode component enables users to input search data using TagInput. If the user inputs numerous tags, this component expands ...

Tips for activating just a single event, whether it's 'change' or 'click'

I am facing an issue with a number input tag that has two event listeners 'on-click' and 'on-change'. Whenever I click on the arrow, both event listeners get triggered. I want only one event to trigger first without removing any of the ...

Reloading a page will display [object CSSStyleDeclaration]

Upon editing content and saving changes, instead of displaying my updated content when refreshing the page, it shows [object CSSStyleDeclaration]. function newElement() { let li = document.createElement("li"); let inputvalue = document.querySelector ...

The Bootstrap column class is malfunctioning

Recently, I've been diving into the world of web development. I attempted to create three divs in one row using Bootstrap col class, but unfortunately it didn't turn out as expected. Two divs ended up in a row with some blank space, while the thi ...

When the mouse hovers over, include a fade-in effect for the image

I am working with two overlapping images and have successfully implemented a function to display the second image on mouse hover. However, I am struggling to incorporate a CSS transition property for a smoother image transition effect. Currently, when the ...

What is the best way to create an interactive menu icon that includes dropdown options using MUI menu features?

i am looking to create a component similar to this https://i.sstatic.net/fwIB0.png currently, I am utilizing a material UI pop menu in my project below is the JSX code snippet <div className="navMenu" style={{ position: "relative" ...