Position the divs next to each other, ensuring that the right div has a minimum width

I need help aligning two div elements next to each other. However, if the right div has a minimum width and cannot fit in the available space, it should move below the left div!

<div id="sidebar">
    <div class="box">
        <div class="left"></div>
        <div class="right"></div>
    </div>
</div>

Left div - width: 250px; Right div - min-width: 200px;

If #sidebar has a width of 400px then:

left | right

If #sidebar is less than 400px then:

left
right

Test Example:

<html>
<head>
<style>
    #sidebar {width: 30%; background: #FFC;}
    .box {overflow: hidden;}
    .left {background: #F00; float: left; width: 250px;}
    .right {background: #FF0; min-width: 200px; display: inline-block;}
    .image {background: #222; width: 250px; height: 180px; display: block;}
</style>
</head>
<body>
<div id="sidebar">
    <div class="box">
        <div class="left"><span class="image"></span></div>
        <div class="right">Lorem ipsum dolor sit amet, consectetur adipiscing elit.<br />Phasellus eget semper dui. Nullam mattis gravida nisi, et viverra velit rutrum sed.</div>
    </div>
</div>
</body>
</html>

UPDATE

The #sidebar takes up 30% of the body of my website! How can I ensure that these 30% represent more or less than 400px?!

UPDATE 2

Example: https://example.com/image.png

Answer №1

To achieve the desired behavior, simply eliminate the width:400px property from the #sidebar.

If you have .left set to width:250px and .right with a minimum width of 200px, your container must be at least 450px wide to cater for both being positioned on the left | right side.

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

Tips for aligning multiple lines on the left and right in HTML

Is there a way to align text to the left and right on multiple lines, like this example: ┌─────────────────────────────────────┐ │ Tums 29  ...

Unable to conceal a DIV using React

I'm attempting to hide a div programmatically upon clicking a button. I've written the following code, but it doesn't seem to be working (the Div remains visible): import React, {useState} from 'react'; import './Button.css&ap ...

Troubleshooting a mapping problem with CSS elements

While building my website, I have successfully mapped the elements by ID for all alphabet letters except A. When clicking on the letter A, it does not go to the respective elements. Can anyone please help me find a solution? Visit this link for reference ...

I am having trouble getting my custom CSS file to be applied to my website on Github pages

I attempted to upload a custom CSS file to apply on The raw CSS file is located at: https://raw.githubusercontent.com/themomoravi/SoundOff-Website/master/styles.css I used the following as my href since it directly links to the raw CSS file: href="http ...

Is it necessary for HTML "buttons" to follow the same box-model as other elements?

I recently encountered an issue with the button element and the input element when using a type of button. In both Firefox and Chrome, I noticed a behavior that seems like a bug in recent releases. However, as form elements often have exceptions to W3 rule ...

Using HTML, CSS, and jQuery to create step-based scrollbar navigation

My current setup includes a horizontal scrollbar with products displayed inside. However, when I scroll to the right and then back, I can only see half of a product at a time. Is there a way to implement scrolling in increments of 200px using HTML/CSS/jQue ...

Designs created with shapes using HTML, CSS, and JavaScript

I've been given a challenge to reproduce the image shown below using only HTML, CSS, and JS - without utilizing any image files. Initially, I thought about creating the shapes with HTML elements and CSS, but I realize this might not be the best approa ...

Stop jQuery popups from wrapping text when resizing the browser window

Whenever a link is clicked, a jQuery popup appears. The popup functions properly in terms of opening and closing, but I would like to ensure that its position remains fixed when resizing the browser window to avoid any wrapping issues. Typically, I use a ...

Add a class if the particular class is missing from the webpage

Seeking a solution in JS, I am facing a challenge in creating a series of elements with active states, each opening a caption below when clicked. Utilizing .addClass, .removeClass, and .toggleClass in combination, only one element can be active at a time. ...

Regex: Identifying all URLs except for those with ".js" or ".css" included

I am currently working on a project where I need to change all the links in an HTML page so that they are not clickable. However, I am having trouble finding the right Regex pattern for the following condition: href="Any_URL" except for those that contain ...

Is there a way to dynamically expand and collapse all table rows, with the latest row always remaining visible, using pure JavaScript?

I have a form input field where I enter data. The entered data is then displayed in a table as a new row, with the most recent entry at the top. What I want to achieve is to show only the newest row in the table and hide all other rows. When I hover over ...

Unable to collapse the Bootstrap dropdown menu

For the life of me, I can't seem to find a solution to my problem. I'm currently working on creating a responsive navbar for my website by following a tutorial. The goal is to make it mobile-friendly with a button that expands the menu on smaller ...

Guide on attaching an arrow to a div card

I have a lineup of cards displayed in a horizontal row. By scrolling left or right, I can navigate through the rest of the cards. By selecting a card, it expands to reveal a small panel that drops down with an arrow pointing towards it. As I continue to ...

What are some ways to enhance the content using CSS?

I have explored various solutions on stackoverflow, but none of them seem to work for me. I am struggling with expanding the content of the "Section" element when the page is empty. Check out my JsFiddle Project here HTML <body> & ...

The images in my gallery refuse to hover or respond to my clicks

Ever since integrating this cropping effect (http://jsfiddle.net/BPEhD/2/) to my gallery images, I've encountered an issue - the hover state is missing and I can't click on the images either. Although the pointer cursor appears when I hover over ...

How can I arrange the ng-class based on the clicked item's order?

http://plnkr.co/edit/pUtuZy?p=preview In this scenario, there are three different border classes available: .border1 { border: 1px solid #66FFFF; } .border2 { border: 1px solid #33CCFF; } .border3 { border: 1px solid #0099FF; } The goal is to as ...

Attach image to the edge with a responsive layout using Bootstrap

I'm currently working on a project that involves the following elements: https://i.sstatic.net/rAZ0x.png I have images for the left and right sections, as well as an image of a monkey. My goal is to create a layout where the left image occupies col- ...

Where should the npm install command be executed?

I'm trying to incorporate a frosted glass effect into my project, but I'm unsure of where to place the npm install frosted-glass --save command. I use Atom as my code editor and already have Node.js installed, but I can't seem to figure out ...

What could be the reason that str_replace is failing to replace this particular string?

I am facing an issue with my PHP code that is intended to load data from a CSS file into a variable, find the old body background colour, replace it with a new colour submitted through a form, save the updated CSS file, and update the colour in a database. ...

Advancing background gradients with CSS

Looking for help with CSS to create specific effects? I have the gradient covered, but struggling with rounded corners in some areas. Any suggestions? Check out this blue background and also this blue background Appreciate any advice you can provide. Tha ...