What is the best way to ensure that two div elements remain next to each other even when one has a fixed size?

I need to align two divs side by side, regardless of the container width when one div has a fixed size. Here is a snippet of my HTML markup:

<div class="container-fluid">
  <div class="row">
    <div class="item">
      <div class="date">
        <div class="month">APR</div>
        <div class="day">6</div>
        <div class="weekday">Mon</div>
      </div>
      <div class="details">
        <p>Kickstarter sustainable readymade Neutra viral, crucifix PBR. Migas tote bag art party, narwhal flannel hashtag YOLO XOXO polaroid. Ennui iPhone pour-over kitsch, lumbersexual stumptown gastropub flexitarian. </p>
      </div>
    </div>
  </div>
</div>

and here is the corresponding CSS code:


.date{
    display: block;
    text-align: center;
    width: 4em;
    background-image: linear-gradient(#fff,#fff 1em,#e7e7e7);
    border-radius: 5px;
}
.month{
    background-color: blue;
    color: white;
    border-top-left-radius: 5px;
    border-top-right-radius:5px;
    padding: 2px 0 2px 0;
}
.day{
    font-size: 2em;
}
.details{
    width: calc(100% - 5em);
    padding: 5px;
    border-radius: 6.5px;
    background-color: white;
    border: 1px solid rgb(204, 204, 204);
}
.item{
    border-radius: 6.5px;
    margin: 5px;
    padding: 5px;
    display: block;
    border: 0;
    background-color: rgba(0, 0, 0, 0);
    background-image: linear-gradient(rgb(242, 242, 242), rgb(242, 242, 242) 1em, rgb(255, 255, 255));
}

I am using Bootstrap and prefer not to use tables to achieve this layout. The left div has a fixed width of 4em and I want the right div to fill up the remaining space. After experimenting with different approaches, I'm reaching out to the Stack Overflow community for guidance.

Here is a Plunker demo

Answer №1

To align the date container to the left and create a margin for .details, you can simply float the date container to the left:

.date{
    display: block;
    text-align: center;
    width: 4em;
    background-image: linear-gradient(#fff,#fff 1em,#e7e7e7);
    border-radius: 5px;
    float: left;
}

Additionally, adjust the styling of .details:

.details{
    padding: 5px;
    border-radius: 6.5px;
    background-color: white;
    border: 1px solid rgb(204, 204, 204);
    margin-left: 5em;
}

It's worth noting that when using this approach, there is no need to specify a width property for .details.

Check out the updated Plunker here.

Answer №2

To create a table-like layout, you can change the display property of your date and details divs to table-cell:

.date,.details {
    display:table-cell;
}

Check out this example on Bootply

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 the best way to place tfoot at the top of the table

Below is the structure of my table: https://i.stack.imgur.com/QmlVn.png In the table, there are search input fields at the bottom of each column. I would like to move them to the top, right after the <thead> </thead>. The basic HTML code for ...

tapping on the division and sliding it to and fro

I am trying to achieve a functionality where clicking a div will move another div to the right by 0, and on clicking the div again, the second div will move to the right by -200. However, I am facing issues getting it to work properly. $(document).ready(f ...

Stop objects from shifting while easily applying a border

I have a code that adds a red border around elements when you mouseover them and removes it when you mouseout. However, the elements jump around when the border is added because it changes their dimensions. Is there a way to stop this jumping behavior? ...

Clicking on a radio button can trigger the selection of another radio button

I'm currently working on a form that includes 8 radio buttons - 4 for System A and 4 for System B. The options for the buttons are CF, ISO, ISO-B, and NW. My goal is to create a functionality where selecting a radio button in System A automatically se ...

Enhance <th> elements using CSS classes

I'm encountering some issues with HTML tables. The layout of my table is as follows: <table class="shop_table cart" cellspacing="0"> <thead> <tr> <th class="product-remove">&nbsp;</th> <th clas ...

Ensuring Consistent Item Widths in a Loop using JavaScript or jQuery

In my code, I created a function that dynamically adjusts the width of elements in a loop to match the widest element among them. // Function: Match width var _so20170704_match_width = function( item ) { var max_item_width = 0; item.each(function ...

Issues arise with the Node EJS module due to the malfunction of the include

Struggling with incorporating HTML snippets into my index.html, even after reviewing the EJS documentation. Directory Structure: project -public --assets ---css ---images ---js --Index ---index.html + index.css and index.js --someOtherPageFolder -views - ...

Tips for adding an image into a PDF document using the jsPDF library in HTML

Due to the lack of support for utf8 characters in jspdf, I'm experimenting with replacing some characters with images before exporting to pdf. To illustrate, I've created a code sample where I insert an image into an html div and then attempt to ...

What is the best way to determine if an image already exists in a database where the objects are spread across two distinct SQL tables?

Currently, I am working on a project using classic ASP and SQL. In this project, I have an input box where the image name is entered by the user after clicking the "Update" button. Upon clicking the update button, pre-existing images are displayed in a tab ...

Tips for incorporating a download button into a video player using Plyr JS

I'm using Plyr JS and I am trying to add a download option for each video. Here is what I've done so far to make the download option work: Even though I have included: controlsList="nodownload" <video controls crossorigin playsinline contro ...

Using a Do/While loop in combination with the setTimeout function to create an infinite loop

Having trouble with this script. The opacity transition works fine, but there seems to be an issue with the loop causing it to run indefinitely. My goal is to change the z-index once the opacity reaches 0. HTML <div class="ribbon_services_body" id="ri ...

HTML tag data search using Regex

I received an HTTP request and I'm attempting to extract specific data from it using a regular expression. For instance, in this particular part of the HTML: <tr><th>Continent:</th><td class='trc'>Europe (EU)</td& ...

Swapping out a code snippet within an HTML document using JavaScript

As a new member of this community, I present to you my very first problem that needs solving. It might be a piece of cake for some of you, but for me, it's proving to be quite tricky. The task at hand involves replacing or removing a string to make ev ...

Discover the following input

Can someone assist me? I am looking for a way to automatically move the cursor to the next input field after filling out the first one. Here is the code snippet I have been working with: jQuery: $('input').keyup(function(){ if($(this).val().m ...

Navigating through pages: How can I retrieve the current page value for implementing next and previous functions in Angular 7?

Greetings, I am a new learner of Angular and currently working on custom pagination. However, I am facing difficulty in finding the current page for implementing the next and previous functions. Can anyone guide me on how to obtain the current page value? ...

Tips on turning off automatic positioning in Bootstrap 4.1

My dropdown list is currently quite large. Take a look at the image linked below. Image How can I address this issue effectively? I'm considering utilizing JavaScript to reposition the menu. Any guidance on how to disable auto-positioning? Check ou ...

What steps should be taken to empty a table before adding new rows to it?

After conducting thorough research, I am still unable to find a solution to my issue. I have a table that I am populating using JQuery and while it populates properly, I cannot seem to clear it before populating it again. Below is the HTML code: $("#sea ...

What should be done if an image is not wide enough to stretch it to match the width of the window?

When the image is not full screen, it looks fine but when it's viewed in full screen, there's a white area on the right side which is likely due to the image not being large enough. Is there a way to automatically stretch the image so that its wi ...

Sort data by checking either multiple or single checkbox options in AngularJS; if none are selected, display all results

My goal is to implement a filter in my AngularJS code that will filter data based on selected checkboxes. If no checkbox is selected, all results should be displayed without any filtering. Currently, the issue I am facing is that the data is only displayed ...

In jQuery, how to create a single event that applies to two different elements simultaneously, rather than individually for each element

How can I create a toggle event for 2 different TDs within my table row that will show/hide the next table row when clicked? <table> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> & ...