What is the best way to position two divs side by side in a way that maximizes the available space?

I am facing an issue with two divs in my HTML code. The right one is set to 80px wide while the other should fill up the remaining space. I have tried various CSS configurations but it seems that the right box always ends up below the left box instead of being positioned next to it.

<!DOCTYPE html>

<html>
<head>
    <title>#{get 'title' /}</title>
    <style type="text/css">
        body {
            margin: 0;
            padding: 0;
        }

        #left {
            position: relative;
            margin-right: 80px;
            background-color: red;
        }

        #right {
            float: right;
            position: relative;
            text-align: left;
            width: 80px;
            background-color: yellow;
        }
    </style>
</head>
<body>
<div id="left">
    Left
</div>
<div id="right">
    Right
</div>
</body>
</html>

It seems like adjusting the margins or using negative values for margin-left did not produce the desired effect. Can someone please provide guidance on how to modify the CSS so that the right div appears alongside the left div?

Answer №1

Ensure the correct order with the div on the right side first.

<div id="right">
    Right
</div>
<div id="left">
    Left
</div>

View a Working Example Here

Answer №2

If you desire the LEFT div to maintain a fixed width while allowing the RIGHT div to adjust its size based on the page dimensions, implement the subsequent code:

.left {
    float: left;
    position: relative;
    width: 80px;
    background-color: red;
}

.right {
    position: relative;
    text-align: left;
    margin-left: 80px;
    background-color: yellow;
}

The corresponding HTML markup would appear as follows:

<div class="left">Left</div>
<div class="right">Right</div>

Answer №3

One reason for this behavior is that the div element is classified as a block element, which will consistently disrupt the natural flow of content on the webpage. To counteract this, consider adjusting the display property of the div to inline and utilizing the float property with a value of left.

Answer №4

To move the #right element to the top-right corner of its parent, adjust the CSS from position:relative; to position:absolute;top:0;right:0;.

For a visual demonstration, check out this example: http://jsfiddle.net/PUWYH/

Answer №5

In today's day and age, achieving this layout is possible using the flex property.

To make it happen, simply set the display property of the container (in this case, the body) to flex, and then specify the width of the left div as 100%.

body {
    margin: 0;
    padding: 0;
    display: flex;
}

#left {
    background-color: red;
    width: 100%;
}

#right {
    width: 80px;
    background-color: yellow;
}
<!DOCTYPE html>
<html>
<head>
    <title>#{get 'title' /}</title>
</head>
<body>
<div id="left">
    Left
</div>
<div id="right">
    Right
</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

How can I center an image within another image in a two-column layout?

Hey there, I'm working on a 2-column layout where I need to position an image in the center of another one in the second column. However, my attempts at styling seem to be failing as the image keeps showing up in the top left corner of my page... Her ...

Anyone able to solve this mysterious CSS alignment problem?

I have a search bar with two image buttons on a webpage I designed using ASP.NET. When I view the page in Internet Explorer 8, Google Chrome or Opera, I noticed that the textbox and image buttons are not aligned properly. The buttons seem to be positioned ...

Positioning elements to the right inside a Bootstrap 4 thumbnail behaves in a unique way when compared to Bootstrap

<div class='img-thumbnail'> <img src="a.jpg" class='img-fluid'> </div> This particular code snippet is from an HTML file using Bootstrap 4 CSS. It displays an image inside a styled box. Adding an h4 heading places ...

Challenge in WordPress Development

As a beginner in website building, I am looking to customize the background of my pages with a solid color. The current SKT Full Width theme I am using has an opaque background which is causing the text on my slider to blend in and not look appealing. All ...

Expanding Images with JQuery Accordion

I'm facing a problem where I need to include accordions on my website with images, but whenever the accordion is opened, the image ends up stretched. Is there a solution to prevent this from happening? Below is the code snippet I am using: [Fiddle] ...

How can you make an element be positioned in the center

Suppose I have a series of links like: <a>foobar</a><a>foobar</a><a>foobar</a><a id="center">foobar</a><a>foobar</a> If one of them has an id="center", is it possible to position it in the cente ...

"Customizing the website's font: A step-by-step guide to replacing the default Roboto font with a custom font

Despite my efforts to globally override the font, I am still encountering instances where the Roboto font is not being replaced, particularly on MUI select and autocomplete components. import { createTheme } from '@material-ui/core/styles'; // A ...

Transforming font sizes in mobile dp units to desktop px measurements

Yes, it's true - using px in css for specifying font-size is not ideal. However, in my case, I have a unique situation. I am currently developing a browser-based editor that is specifically designed for creating mobile content visually. The editor fe ...

Having trouble selecting the first td within a tr using CSS3 selectors

I'm encountering an issue with selecting the first td in the tr of a dynamically generated table. I've attempted to do this using: .record_list tbody tr td:first-child { text-align: center; } However, the styles are not being applied as ex ...

The Eclipse software is unable to detect the external CSS files

Whenever I apply external CSS to my screen, the JSP page seems to be unaware of the Bootstrap CSS. https://i.sstatic.net/DYGKQ.png https://i.sstatic.net/QUYnI.png ...

Issue with Chrome causing linear-gradient background to break when zoomed out

My current design involves utilizing a gradient background to create an alternating pattern for rows that are absolutely positioned. However, I have run into an issue when zooming out in Chrome. The sizing calculations for the gradient background are not ...

Is there a way to manipulate a button's toggle state using Javascript as the page loads?

I am in the process of developing a straightforward web application that allows users to customize their settings. On the settings page, there are several toggle buttons that need to have their state changed during the page load to align with the preferenc ...

How can you animate a MUI v4 Grid element using CSS transitions?

I was exploring the potential of using breakpoints in the MUI v4 component to control the visibility of items in my Grid System. How can I create a smooth CSS transition for b, transitioning from 0px to a defined breakpoint size of 3 for xl? Using % works ...

How can I align Javascript output vertically in the middle?

I am currently facing an issue with a JavaScript clock displaying in a narrow frame: <!DOCTYPE html> <HTML> <HEAD> <TITLE>Untitled</TITLE> <SCRIPT> function checkTime(i) { if (i < 10) { ...

I'm attempting to include a home glyphicon in my navigation bar using a bootstrap class. Unfortunately, it doesn't appear on the output page. Any suggestions for resolving this issue?

I'm having trouble adding a home icon using Bootstrap glyphicon; it's not appearing in the output. Can someone assist me with this? I've already attempted to increase the font size and change the font color, but neither solution worked. &l ...

How can I use CSS to make a child element in a grid layout stretch horizontally to full width?

I am currently working on designing a grid layout that will display a child element when clicked. Here is what I have in mind: [ link1 ] [ link2 ] [ link3 ] [ link4 ] [ link4 ] [ link4 ] When clicked, it will turn into: [ link1 ] [ link2 ] ...

javascript method to apply styling

I'm puzzled by how these two javascript/css codes can yield different results: 1st: prev.setAttribute('style', 'position:absolute;left:-70px;opacity:0.4;border-radius: 150px;-webkit-border-radius: 150px;-moz-border-radius: 150px;&apos ...

What is the method to modify the text color of an <option> in a <select> tag?

I am attempting to change the color of only the text "select one option" to grey, but I am having trouble making it work. Here is my code: .grey_color { color: #ccc; font-size: 14px; } <select id="select"> <option selected="selected"> ...

The speed at which Laravel loads local CSS and JS resources is notably sluggish

Experiencing slow loading times for local resources in my Laravel project has been a major issue. The files are unusually small and the cdn is much faster in comparison. Is there a way to resolve this problem? https://i.stack.imgur.com/y5gWF.jpg ...

Top method for customizing w2ui grid appearance

While utilizing the w2ui grid to showcase data in a table, I've found it to be quite effective. However, I'm not entirely happy with the appearance of the table itself. Are there any methods available for styling the table that don't involve ...