design floating elements

I am struggling with a seemingly simple issue that I can't seem to solve.

My goal is to create two boxes, one on the left and one on the right, each taking up 50% of the space and sitting beside each other.

Here is my current CSS code:

#left {
  text-align: right;
  width: 50%;
  padding-right: 10%;
  float: left;
}

#right {
  width: 50%;
  text-align: left;
  padding-left: 10%;
}

#footer {
  clear: both;
}

The corresponding HTML looks like this:

<div id='left'>
  <h1>Left</h1>
  <ul>
    <li>Some Listing</li>
  </ul>
</div>

<div id='right'>
  <h1>Stuff</h1>
  <p>
    Stuff right
  </p>
</div>

<div id='footer'>
</div>

Despite my efforts, the layout is not working as intended. However, the desired outcome is quite straightforward.

Answer №1

When working with percentages in CSS, it's important to consider the impact of padding and margins. If you set a <div> element to be 50% wide but also add padding, it may cause the <div> to wrap to the next line. To avoid this, you can either remove the padding or adjust the width percentage to something like 45%. Experimenting with these values will help you achieve the desired layout.

Answer №2

Two key adjustments were necessary to ensure functionality:

1) The combined width and padding of each div should not exceed 50%. In the original code, they totaled 60%, resulting in a combined total of 120%, which exceeded the body's 100% width capacity.

2) Additionally, floating the second div to the left or applying overflow: hidden to both divs was essential for proper display.

(Further investigation is ongoing to determine the necessity of step 2)

Answer №3

To ensure you steer clear of any issues highlighted by XSaint, a complete style reset is essential. Margins, borders, and padding are crucial factors to consider.

It's important to set these elements as follows:

div {
  margin: 0;
  padding: 0;
  border: 0;
}

If you do decide to include padding and borders, remember to adjust the width of the elements accordingly. The box model is a valuable resource that visually explains this concept in detail:

http://www.w3.org/TR/CSS2/box.html

Beneath the diagram on that page, it clarifies that the width property specifically impacts the size of the content box, not the overall box with padding, borders, or margins. This distinction is crucial for proper layout design.

Answer №4

Consider following XSavior's advice and either implement his suggestion or try removing the padding from the #left division. Instead, you could create a new #context division with the desired padding inside the #left division. For example:

Answer №5

According to Xsaint and Danny Staple, they provided the most exceptional answers up to this point.

In addition to their responses, you can also incorporate a property called "box-sizing" to ensure accurate calculations.

I strongly suggest including this property in your CSS reset, as Webkit, IE, Opera, and Mozilla tend to utilize different box models.

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

What is preventing this button from having a border-radius?

The red border is just for visual reference. I attempted using overflow: hidden but it didn't yield the desired outcome. My goal is to achieve the same border radius as the red border. Is there something amiss in my approach? Appreciate your assistanc ...

CSS failing to reach the full width of the container

My navigation tab is currently only occupying half of the width that I need it to. I have tried various solutions found on this site and through Google, but none seem to work as expected. The black background of the navigation should fill up 100% of the wi ...

Center align for drop-down menu

I'm currently working on styling an asp:DropDownList element using custom CSS. I have successfully set the height, but I am facing a challenge with getting the text to align in the middle of the element rather than at the bottom. The vertical-align:mi ...

Choosing a class using CSS

Can someone please help me with editing this HTML code using CSS? I'm trying to target the coupon divs using #coupon but it doesn't seem to be working. Any assistance would be greatly appreciated. <div class="coupon"> <h1>Classic ...

what is the best way to position a glyphicon next to a form?

Forgive me for asking such a basic question, but I struggle with aligning and positioning elements. Here is my issue: https://i.sstatic.net/Uy6XB.jpg https://i.sstatic.net/TTs6V.jpg All I want is for the glyphicon to be displayed next to the textfield, ...

Mastering the Art of Positioning Arrows in Post Car

I am currently facing an issue with my post carousel. The carousel has two arrows on the side, and I have designed a background in the shape of circles for these arrows. However, I can't seem to figure out how to center the arrows inside the circles. ...

Implement jQuery to toggle a class on click for added functionality

I am attempting to create a box that changes color when clicked. When the box is first clicked, it will turn red by adding the class red, and if clicked again, it will change to blue. The colors alternate with each click, but I am unsure of how to achieve ...

Set the background color of a webpage depending on the data retrieved from the database when a specific radio button

I am facing a challenge with my radio buttons that are set to light up green when the page loads. However, I am working on an "order system" where I need a specific button or "locker" to appear red instead of green when the page loads. While I have succes ...

The issue of drop shadows causing links to not work properly in Internet Explorer

I am currently working on a website design that features a fixed menu positioned behind the body. When the menu icon is clicked, some jQuery code shifts the body to the left. To create the effect of the fixed menu being positioned underneath, I have added ...

Bootstrap 4 carousel feature that allows for smooth responsive design with breakpoints and column widths that cater to various screen sizes

Any idea why the second slide is causing such a drastic change or shift on desktop view? It seems to be related to adding d-md-flex align-items-md-center to the enclosing div. Am I missing something here, or should I try a different approach? <div cl ...

Prioritize the Menu display

Is there a way to bring the menu's black background in front of the body text in the image below? Here is the JSFiddle link for reference: https://jsfiddle.net/johnking/j58cubux/6/ The issue occurs when scrolling down and the menu is not clearly vi ...

Tips for seamlessly transitioning the background of Google Maps into view as you scroll down the page

I have a unique background with a Google Map. It loads perfectly in the right place when I open the page, but as I scroll down, it disappears from view. Is there a way to keep the Google map constantly in the background, even when scrolling? This is my cu ...

Organizing Files with Identical Names in Separate Folders Using Gulp.js

I'm currently working on a forum and need to convert my CSS files to SASS. Here is the project hierarchy: app/ components/ 404/ controllers/ ...

Adaptable triple-column design

After completing the layout of my website, everything seems to be functioning well. However, I would like to ensure that I haven't overcomplicated it or used poor techniques. The structure involves a main DIV with a nested table DIV, inside of which ...

Attempting to optimize a webpage design for compatibility with Google's mobile-friendly testing tool

After much searching on the forum, this is my first post, so I kindly ask for patience! I am dealing with a 20-year-old website that urgently needs to be optimized for mobile devices. With almost 2200 pages, manual editing is out of the question, so I nee ...

Top button disappears in Chromium browser after fade-in effect

I've encountered a very peculiar issue. Whenever I use the page down or fragmented identifier to jump to a specific div, my "go to top" image disappears. Here is the code snippet: HTML: <a title="Go to top" href="#" class="back-to-top"></ ...

Differences in sub column heights within Bootstrap grid system

I am facing an issue with maintaining consistent heights for all my columns. I typically use Bootstrap to adjust their sizes, but the problem arises when using sub divs. Click here to view a DEMO showcasing the issue The first "table" is borrowed from an ...

Customize hover effects for MuiCheckbox

For some reason, my checkboxes are displaying a circle on hover that I want to remove. https://i.sstatic.net/62TLL.png I attempted to make overrides in MuiTheme: export const MUI_THEME = createMuiTheme({ overrides: { MuiCheckbox: { root: { ...

CSS classes designed to mimic JavaScript object attribute-value pairs

I stumbled upon some interesting css class-value combinations within HTML tags. It seems like a specific JavaScript code is interpreting this, but it's something I haven't encountered before. I came across this on www.woothemes.com/flexslider/ (y ...

The carousel spun around, each section moving to the side on its own

One issue I'm facing is that on my page, I have multiple carousel rows. However, when I click on the "next" or "prev" button to navigate through the items in the carousel, it affects all carousels instead of just the one I clicked on. I've attem ...