Ways to determine the number of overlapping elements and adjust their widths

My layout conundrum involves 3 buttons within a flexbox placed in a single table cell. I am trying to adjust the width of each button so that they collectively fill the horizontal space as much as possible. The issue arises when the buttons overlap and their sizes do not adjust accordingly, resulting in some buttons being hidden. Using flexbox prevents this overlapping but causes all buttons to dynamically shrink even if not overlapping on the y-axis (the cell itself is 42rem tall).

Problem Illustration:

https://i.sstatic.net/GHJIi.png

The red cell beneath the Monday column should span the full width, which it currently does not.

Question: Is there a way to dynamically alter these widths only when they overlap? If no overlap occurs, the buttons should expand to fill the total width available. One challenge is the potential addition of more events over time, making fixed percentage values unreliable.

Alternatively: Can I determine the exact number of overlaps so JavaScript can readjust widths whenever new items are added (albeit less elegant)?

Check out the codepen for reference: https://codepen.io/samuel-solomon/pen/oNbKeRm?editors=1100 Note: Vertical positioning must be maintained as it relates to a calendar timeline.

.day_Block {
  position: relative;
}

.flexbox_Edits {
  top: 0;
  position: relative;
  width: 100%;
  height: 42rem;
}

.event_Button {
  border-width: 2px;
  max-width: 100%;
  border-radius: 8px;
  border-style: inset solid solid;
  align-items: flex-start;
  align-self: stretch;
  position: relative;
  width: 100%;
  float: left;
  left: 0;
  overflow: hidden;
  -webkit-box-sizing: border-box;
  /* Safari/Chrome, other WebKit */
  -moz-box-sizing: border-box;
  /* Firefox, other Gecko */
  box-sizing: border-box;
  /* Opera/IE 8+ */
}

.event_Button_Text {
  word-wrap: break-word;
  text-align: left;
  float: left;
  line-height: 1.25rem;
  font-family: 'Montserrat';
  font-weight: 550;
  letter-spacing: 0.06rem;
  display: inline-flex;
  font-size: 0.8em !important;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<section class="container on_Left from_Top table-responsive">
  <table class="calendar mt-5" id="calendar_Table">
    <thead class="masthead">
      <tr>
        <th class="time_Col days"></th>
        <th>
          <str>Monday</span>
        </th>
        <th><span>Tuesday</span></th>
        <th><span>Wednesday</span></th>
        <th><span>Thursday</span></th>
        <th><span>Friday</span></th>
      </tr>
    </thead>
    <tbody id="calendar_Body">
      <tr id="Time">
        <td class="time_Col">
          <div class="time_Block">8am</div>
          <div class="time_Block">9am</div>
          <div class="time_Block">10am</div>
          <div class="time_Block">11am</div>
          <div class="time_Block">12pm</div>
          <div class="time_Block">1pm</div>
          <div class="time_Block">2pm</div>
          <div class="time_Block">3pm</div>
          <div class="time_Block">4pm</div>
          <div class="time_Block">5pm</div>
          <div class="time_Block">6pm</div>
          <div class="time_Block">7pm</div>
          <div class="time_Block">8pm</div>
          <div class="time_Block">9pm</div>
        </td>
        <td class="day_Block">
          <div class="d-flex flexbox_Edits" id="M">
            <button class="btn event_Button_Text event_Button event_Button_CS_2" style="height: 12rem; top: 0rem; background-color: rgb(83, 94, 235);">M8:00 - 12:00<br>CS 2<br></button>

            <button class="btn event_Button_Text event_Button event_Button_CS_1" style="height: 21rem; top: 18rem; background-color: rgb(254, 44, 84);">M2:00 - 9:00<br>CS 1<br></button>
            <button class="btn event_Button_Text event_Button event_Button_CS_1" style="height: 21rem; top: 18rem; background-color: rgb(19, 202, 145);">M2:00 - 9:00<br>CS 1<br></button>
          </div>
        </td>
        <td class="day_Block">
          <div class="d-flex flexbox_Edits" id="T"></div>
        </td>
        <td class="day_Block">
          <div class="d-flex flexbox_Edits" id="W"></div>
        </td>
        <td class="day_Block">
          <div class="d-flex flexbox_Edits" id="R"></div>
        </td>
        <td class="day_Block">
          <div class="d-flex flexbox_Edits" id="F"></div>
        </td>
      </tr>
    </tbody>
  </table>
</section>

Answer №1

It appears that you are utilizing display flex along with top:18rem. The top value seems to be causing a shift in their position, moving them down from their original row placement. Perhaps dividing the buttons into subcategories based on their timeslots and applying display:flex individually might resolve this issue.

<div class="d-flex flexbox_Edits" id="M">
  <button class="btn event_Button_Text event_Button event_Button_CS_2" style="height: 12rem; top: 0rem; background-color: rgb(83, 94, 235);">M8:00 - 12:00<br>CS 2<br></button>
</div>
<div class="d-flex flexbox_Edits" id="M">
  <button class="btn event_Button_Text event_Button event_Button_CS_1" style="height: 21rem; top: 18rem; background-color: rgb(254, 44, 84);">M2:00 - 9:00<br>CS 1<br></button>
  <button class="btn event_Button_Text event_Button event_Button_CS_1" style="height: 21rem; top: 18rem; background-color: rgb(19, 202, 145);">M2:00 - 9:00<br>CS 1<br></button>
</div>
Feel free to explore the updated code pen here: https://codepen.io/rohinikumar4073/pen/MWKNXzP?editors=1100

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

Guide on implementing various styles for a class in Tailwind CSS when transitioning to dark mode with the help of Next.js 13.4 and next-theme

Is it possible to apply unique styles for a class in Tailwind CSS when transitioning to dark mode using Next.js 13.4 and next-theme? I am currently setting up a dark theme in Tailwind CSS with Next.js 13.4 utilizing next-theme. Within my globals.css file, ...

Generating an Xpath for the selected element with the help of Javascript or Jquery - here's how!

On my website, I have implemented a click event on an element. When a user clicks on this element, I want to generate the XPath of that particular element starting from either the html or body tag, also known as the Absolute Xpath. For example, if I click ...

Steps for extracting HTML content from a beautiful soup object

My current situation involves a bs4 object listing: >>> listing <div class="listingHeader"> <h2> .... >>> type(listing) <class 'bs4.element.Tag'> I am aiming to retrieve the raw html as a string. Initially, ...

Can flex-basis be used with percentage in IE11?

Encountering an issue with flexbox and IE10/11. Trying to utilize min-width/max-width values, but IE11 is not cooperating. Discovered flex-basis and used a percentage value, which functions as intended in Chrome but fails in IE11. Experimented with adding ...

The floated div is disrupting the flow due to its margin

Two divs are causing some alignment issues: one floated left and the other floated right. The margin on the right div is pushing it down, causing the left div to appear lower than desired. I want both divs to be aligned at the top of the page. HTML: < ...

Bizarre actions with jQuery append in Internet Explorer 8

Issue with adding elements to a div in IE8: The element is not added until the button is clicked twice, resulting in two new elements being added at once. Here's the code snippet in question: $(options.addButton).click(function(event) { var proto ...

Error encountered in loop for concatenating html elements: identifier unexpectedly found (jquery + html + php)

I am attempting to concatenate HTML inside a variable within a loop and then return it populated. However, I am encountering an error: Uncaught SyntaxError: Unexpected token += in my code. <a style="cursor: pointer" id="addmore">More Entree ?</ ...

Discovering the largest value with arrays in javascript

My code is functioning as expected for single-digit numbers but encounters issues with two or three-digit numbers. I want to stick to this approach without utilizing the math max function. The primary issue lies in only considering the first digit of a tw ...

I am experiencing difficulties with my focusin not functioning properly for the input element that I added

I am working on a program that includes a dynamic table with the following structure: <table id="selectedItems"> </table> I plan to use jQuery to append elements to it like so: var i=0; $("#selectedItems").html(''); $("#sel ...

To activate two separate click events, one for an absolute element and another for the element positioned behind it, simply click on the desired absolute element to trigger both actions

Is it possible to trigger click events for both of these elements? position: relative container ELEMENT 1: standard div ELEMENT 2: position: absolute div In real life, element 2 is a transparent backdrop designed to capture clicks on top of the header ...

Caution: Additional server attributes detected: style

Alert in my Next.js project, I encountered the following message: Notice: Server is sending additional attributes: style I am uncertain about the source of this warning and therefore unable to provide any code snippet. ...

Utilizing the power of SimpleHTMLDom to retrieve high-resolution images through an advanced Google search

I want to display a large image that was retrieved from Google using simple_html_dom.php include_once( "simple_html_dom.php" ); $key="unique"; $html = new simple_html_dom(); $html=file_get_html('http://images.google.com/images?as ...

Is there a way to modify the browser's user agent using JavaScript or HTML?

I currently have an application that is running on IE7. However, an analytics tool I rely on has recently been updated and now requires a minimum of IE8. Is there a way to modify the User Agent in IE7 using JavaScript or HTML to trick it into being recogni ...

Encountering difficulties with the functionality of Google Places API

I am attempting to incorporate the autocomplete functionality of Google Places API into a text field. Below is the code I have implemented: <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script> <script ...

transforming a div to behave similarly to an iframe

I am trying to load content from my page into a div element using the following function: function loadmypage(DIV, pageURL) { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } e ...

I designed an innovative contact form utilizing mail function, yet unfortunately, the emails generated from it persistently land in spam folders. Below is the code I

I have created a contact form using the mail function, but all the emails are ending up in the spam folder. Here is my code: <?php // CODE FOR SENDING EMAILS if(isset($_POST['submit'])){ $to = "<a href="/cdn-cgi/l/email-protectio ...

Tips for adjusting the button spacing on a bootstrap navbar

As someone who doesn't work on front-end tasks frequently, I have encountered an issue while creating a navbar using Bootstrap. The problem arises when trying to add multiple buttons as it causes the spacing between them to appear strange. Does anyon ...

Determine the number of XML elements and display the results for each XML element counted

I need assistance with a problem I am facing. I am extracting data from an XML API using PHP. Below is my code snippet: <?php $gigatools = simplexml_load_file('http://gigs.gigatools.com/u/A2LTD.xml'); echo "<table id='gigs_parse&apos ...

I continue to encounter an error every time I attempt to place an HTML nested div on a separate line

When I structure the HTML like this, I don't encounter any errors: <div class="game-card"><div class="flipped"></div></div> However, if I format it differently, I receive an error message saying - Cannot set property 'vi ...

Optimizing responsiveness and side margins for high-resolution displays

Struggling with creating a website design with Bootstrap. My goal is to add margins on the sides when the screen size increases, but once I achieved it, the responsiveness of the page got disrupted. To incorporate the side margins, I enclosed the entire b ...