Two adjacent divs positioned next to each other, where the right-hand div occupies the remaining space within its

I am facing a common issue with two side-by-side divs, which I usually solve without any problems by floating both divs left and adding a clear:both div after them.

However, my requirements have made this problem more complicated to solve...

What I would like is for the left-hand div to act as a column, occupying the entire left side of the containing div. This left div will hold a number (e.g., '1.').

Additionally, I want the right-hand div to occupy the remaining space on the right side of the left div. The most important aspect is that it should not drop below the left-hand div when there isn't enough space for it to fit. Instead, I want the right-hand div to stay in its position and for the text inside to wrap around, staying to the right of the left-hand div. It seems simple!

I do not want to set arbitrary width values because the length of the number in the left-hand div can vary, affecting the distance between the number and the right-hand text.

Here is an example HTML code:

<div class="popup-container"> // set to width: 300px; in css
    <div class="popup-text">
        <div class="float-left">
            <h3>2.<.h3>
        </div>
        <div class="float-left">
            <h3>Example text here, long enough to wrap around<.h3>
        </div>
        <div class="clear"></div>
    </div>
</div>

And the CSS code:

.popup-container {
       width: 300px;
}

.popup-text h3 {
    line-height: 1.25;
    padding: 0px 8px 0px 0px;
}

.float-left {
    float: left;
}

.clear {
        clear: both;
}

That's all there is to it! If anyone knows how to accomplish making the left div act as a column, with the text in the right-hand div remaining justified left (instead of dropping below the left-hand div), I would greatly appreciate it.

EDIT Thank you for all the answers. It is worth noting that this solution should work in IE8. Although it may seem outdated, we are working with a large organization that has not yet updated its machines.

Answer №1

Both Flexbox and CSS Tables have the capability to achieve this.

Compatibility

Flexbox works on IE10 and above.

CSS Tables are compatible with IE8 and above.


FLEXBOX

.popup-container {
       width: 300px;
       border:1px solid grey;
}

.popup-text {
  display: flex;
}

.popup-text h3 {
    line-height: 1.25;
    padding: 0px 8px 0px 0px;
}

.left {
  flex: 0 0 auto;
  background: #c0ffee;
}

.right {
  flex:1;
  background: yellow;
}
<div class="popup-container"> 
    <div class="popup-text">
        <div class="left">
        <h3>2.</h3>
        </div>
        <div class="right">
        <h3>Example text here, long enough to wrap around</h3>
        </div>
    </div>
</div>

CSS Tables

.popup-container {
       width: 300px;
       border:1px solid grey;
}

.popup-text {
  display: table
}

.popup-text h3 {
    line-height: 1.25;
    padding: 0px 8px 0px 0px;
}

.left {
  background: #c0ffee;
  display: table-cell;
}

.right {
  background: yellow;
    display: table-cell;
}
<div class="popup-container"> 
    <div class="popup-text">
        <div class="left">
        <h3>2.</h3>
        </div>
        <div class="right">
        <h3>Example text here, long enough to wrap around</h3>
        </div>
    </div>
</div>

Answer №2

Apply the CSS property of display:flex;

.container-pop {
  width: 300px;
}
.container-pop .text-popup {
  display: flex;
}
.text-popup h3 {
  line-height: 1.25;
  padding: 0px 8px 0px 0px;
}
.floating-left {
  float: left;
}
.clearance {
  clear: both;
}
<div class="container-pop">
  <!-- set to a width of 300px in CSS -->
  <div class="text-popup">
    <div class="floating-left">
      <h3>2.</h3>
    </div>
    <div class="floating-left">
      <h3>An example text goes here, Lorem ipsum dolor sit amet</h3>
    </div>
    <div class="clearance"></div>
  </div>
</div>

Answer №3

Check out this neat solution utilizing the display: flex property:

.modal-wrapper {
  width: 500px;
  background-color: teal;
}
.modal-content {
  display: flex;
}
.modal-content div.section-two {
  flex: 1;
  background-color: lavender;
}
.modal-content h2 {
  line-height: 1.25;
  padding: 6px;
}
<div class="modal-wrapper">
  <!-- set to width: 500px; in css -->
  <div class="modal-content">
    <div class="section-one">
      <h2>3.</h2>
    </div>
    <div class="section-two">
      <h2>Some random text here, long enough for scrolling</h2>
    </div>
  </div>
</div>

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

A guide on grouping an entire CSS file under one class umbrella

Is there a way to encapsulate a large number of CSS declarations so they only apply when the parent has a specified class? div { color: #ffffff; } ... a { color: #000000; } Can it be structured like this, with a parent class containing all the CSS r ...

Selenium Scrolling: Improving Web Scraping Efficiency with Incomplete Data Extraction

I have been attempting to extract product data from a website that utilizes JavaScript to dynamically render HTML content. Despite using Selenium, implementing scrolling functionality to reach the end of the page, and allowing time for the page to reload, ...

Clicking on the button has no effect whatsoever

I'm currently dealing with a button on my webpage that seems to be causing me some trouble: <script> function changeMap() { container.setMap(oMap); } </script> <button onClick="changeMap"> Click here </button> Upon inspe ...

Is there a way to restore a minimized min.css file?

Recently, I attempted to utilize an online tool for unminifying my CSS code. However, when I replaced the minified code with the unminified version and accessed the URL, I discovered that the entire website had lost its layout. Unfortunately, relying sole ...

Enhancing Your Vue Experience: A Guide to Highlighting and Selecting HTML Elements on Mouse Hover

Incorporating a navigation header using Vue, I utilize v-html to display it seamlessly. Check out the demo - here <section class="text-gray-700 font-heading font-medium relative bg-gray-50 bg-opacity-50"> <nav class="flex justify ...

Customize Zurb Foundation: Applying styles to specific child elements based on current screen size (large, medium, small)

For wide displays, I aim to give a distinct border to each 7th element. However, on smaller screens, I wish to apply these styles to every 4th element instead. Is there a way for me to nest my styles within .small, .medium, and .large classes? ...

Learn the process of sending information to a MySQL table using a select tag in PHP

I am attempting to use a select tag to submit data to a MySQL table using PHP. I have been successful in inserting data using the text type with the following code: <td> <label class="control-label">Image Type </label> </td> &l ...

Tips for using jQuery to dynamically apply CSS styles to new content added to a webpage

I have encountered this question multiple times on stackoverflow and through google search, but unfortunately none of the suggested solutions have worked for me. My issue involves a page that loads an HTML page with empty sections, which are then dynamica ...

Encountering an error while attempting to load the jQuery script: TypeError - $.fn.appear.run is not a

I have included an animation script for CSS in my markup, but I am encountering the following error: TypeError: $.fn.appear.run is not a function Does anyone know why this is happening and how I can resolve it? /* * CSS3 Animate it * Copyright (c) 2 ...

Update the styling of each div element within a designated list

Looking for a way to assist my colorblind friend, I am attempting to write a script. This is the layout of the page he is on: <div class="message-pane-wrapper candy-has-subject"> <ul class="message-pane"> <li><div style=" ...

Horizontal centering using Bootstrap 4 media

How can I horizontally center a media element? Here is the code for my media: <div class="container"> <div class="row"> <div class="col-12 col-sm-12 col-md-12 col-lg-12 col-xl-12"> <div class="media"> ...

Is there a way to conceal a div class on the Payment page in Shopify?

I have encountered an issue with the code on the Shopify checkout (Payment) Page. Unfortunately, I am unable to directly edit this page within Shopify, but I have discovered that it is possible to add custom CSS or HTML code within the checkout settings of ...

Execution of Ajax call fails to occur synchronously

I have created a unique weather website that utilizes the flickr API as well as the yahoo API to gather weather data. However, I am facing an issue where the ajax call from the yahoo API does not retrieve the necessary data in time for the content of the p ...

Avoid the resetting of chosen value following a POST or submission

I am currently working on creating a "web dashboard" that requires a year_from parameter from an HTML form. Despite setting up my Flask backend to return data within this specified range, I am encountering a problem where every time I hit submit, the selec ...

"the content does not occupy the entire space of the parent

My TableRow expands across the width of the screen, and within it is a ListView with a layout_width set to match_parent. However, the ListView does not expand properly. This issue arose when I added two buttons in the next TableRow. Even after setting the ...

Trigger a function when the browser automatically populates an input field

I am attempting to trigger a function that can detect if the browser has autofilled a field and then add a specific class to it. After finding a thread with a solution that mostly works, mentioned here: Here is how I implemented it: $.fn.allchange = fun ...

Seeking assistance in creating custom tabs for integration with a jQuery tabs plugin

Attempting to navigate the world of CSS as a newbie, I find myself struggling with creating tabs. Our current tab setup can be viewed here, but unfortunately, these tabs are created using an unattractive <table> tag. Below is the code responsible fo ...

How can Node.js and Express be used to conceal Javascript code on the backend?

I'm a beginner when it comes to Node and Express. I have a query regarding how to securely hide Javascript code on the backend. Currently, I am working with Node.js and Express. My goal is to prevent users from easily accessing the code through browse ...

A guide to injecting HTML banner code with pure Vanilla Javascript

Looking for a solution to incorporate banner code dynamically into an existing block of HTML on a static page without altering the original code? <div class="wrapper"><img class="lazyload" src="/img/foo01.jpg"></div> <div class="wrapp ...