Looking to ensure that a div with no content adjusts to the height of the neighboring div?

Currently working on a Reactjs project. I'm trying to set the height of the colorTab div to match that of the content div, while making it responsive. The height of content should adapt dynamically based on the text content in title and description, which can vary in length, as well as the width of the window.

However, when I remove the min-height property from the CSS of colorTab and only use height: 100%;, the colorTab disappears. Adding back the min-height makes it visible again but doesn't adjust to the height of content, which is the main objective here. How can I solve this issue?

JSX:

<div className="wrapper">
  <div className="colorTab" style={color}>

  </div>
  <div className="content">
    <tr>
      <td className="title">
        <a href={link}>{title}</a>
      </td>
    </tr>
    <tr>
      <td className="description">
        {description}
      </td>
    </tr>
  </div>
</div>

CSS:

.wrapper {
  min-height: 48px;
  overflow: hidden;
}

.colorTab {
  float: left;
  position: relative;
  width: 5px;
  min-height: 48px;
  height: 100%;
  border-radius: 5px;
  margin-left: 15px;
}

.title {
  padding-top: 0 !important;
  padding-bottom: 0 !important;
}

.description {
  padding-top: 0 !important;
  padding-bottom: 0 !important;
}

Answer №1

Utilizing Flexbox can provide the necessary functionality.

Add display: flex to your container class and flex: 1 to your content div. This will ensure that regardless of the amount of content in the content div, the colorTab div will match its height.

Here is an example using pure HTML/CSS (no React):

.wrapper {
  overflow: hidden;
  display: flex;
}

.colorTab {
  position: relative;
  width: 5px;
  border-radius: 5px;
  background: red;
}

.content {
  flex: 1;
}
<div class="wrapper">
  <div class="colorTab">
  
  </div>
  <div class="content">
      <div class="title">
        <a>Your Title</a>
      </div>
      <div class="description">
        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Rem nam perspiciatis aperiam mollitia obcaecati molestiae, consequuntur saepe repellendus cumque aliquid. Ullam reiciendis praesentium repellendus ipsam, qui illum. At, aliquid quidem. Reprehenderit eligendi voluptatem maiores deleniti id nulla, pariatur ipsa ducimus accusantium! Unde ea nostrum eligendi suscipit impedit, laborum adipisci accusamus ducimus temporibus eius inventore optio officia reiciendis porro eos assumenda numquam velit obcaecati. Perferendis, ipsum! Facilis fuga dolorum nobis nihil illo nam, voluptate suscipit excepturi sunt non. Modi perferendis ex illum eaque pariatur laudantium saepe accusantium vel, blanditiis, aperiam odit! Suscipit ullam, necessitatibus est distinctio obcaecati, odio ipsa blanditiis consequatur.
      </div>
  </div>

Answer №2

If you're considering using the flexbox as suggested in a previous answer, there's still a catch...

For those stubborn old browsers that refuse to support modern techniques (yes, looking at you, grandpa with your ancient IE), it might be wise to offer additional fallback options.

Therefore, for the sake of completeness: You can also resort to utilizing the trusty old tables.

.wrapper {
  display: table;
}

.description {
  display: table-cell;
}

.colorTab {
  display: table-cell;
  width: 5px;
  border-radius: 5px;
  background: red;
}

// optional, but for aesthetic purposes
.content {
  position: relative;
  left: 10px;
}
<div class="wrapper">
  <div class="colorTab">
  </div>
  <div class="content">
      <div class="title">
        <a>Your Title</a>
      </div>
      <div class="description">
        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Rem nam perspiciatis aperiam mollitia obcaecati molestiae, consequuntur saepe repellendus cumque aliquid. Ullam reiciendis praesentium repellendus ipsam, qui illum. At, aliquid quidem. Reprehenderit eligendi voluptatem maiores deleniti id nulla, pariatur ipsa ducimus accusantium! Unde ea nostrum eligendi suscipit impedit, laborum adipisci accusamus ducimus temporibus eius inventore optio officia reiciendis porro eos assumenda numquam velit obcaecati. Perferendis, ipsum! Facilis fuga dolorum nobis nihil illo nam, voluptate suscipit excepturi sunt non. Modi perferendis ex illum eaque pariatur laudantium saepe accusantium vel, blanditiis, aperiam odit! Suscipit ullam, necessitatibus est distinctio obcaecati, odio ipsa blanditiis consequatur.
      </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

Tips on attaching a class to elements in a loop when a click event occurs

In my HTML, I am displaying information boxes from an array of objects that are selectable. To achieve this, I bind a class on the click event. However, since I am retrieving the elements through a v-for loop, when I select one box, the class gets bound to ...

Blending an HTML jQuery toggle

Is there a way to make the current headline fade out while simultaneously fading in a new one when switching to the next div using a jQuery .html switch? Check out the codepen example: https://codepen.io/balke/pen/JpNNve $(window).scroll(function() { ...

Convert the HTML content into a PDF while retaining the CSS styles using either JavaScript or Django

Looking to create a PDF of an HTML element with background color and images. Seeking a solution, either client-side or server-side using Django/Python. Tried jsPDF on the client side, but it does not support CSS. Considering ReportLab for Django, but uns ...

Drawing intersecting lines on a javascript canvas and clearing them using the lineto method

I am working with a canvas where lines are drawn based on mouse movement. I am trying to achieve the effect of the line only lasting for a few seconds before disappearing, similar to swirling a ribbon with a set length. I have been using lineTo to draw the ...

The CSS styling is not applying correctly to a particular page

Having a minor CSS issue here. I am attempting to change something for a specific page or post, like in the case of wanting to change the background color of the entry content of page number 2458. I've managed to get that working with the following co ...

Sorting in reverse order within a table

Is there a way to reverse the order in which my function renders table rows with incremental id? (first row with id=n, last with 1) Here is the code snippet I am using: renderRows = () => { const { receipts } = this.props const reversedReceipt ...

Using the CSS :not selector to style certain input elements

Is there a way to style ALL input elements except for specific types without using the :not selector? Here's an example of what I tried: input, input:not(type='email') { background: red; } Unfortunately, this approach didn't work ...

Adjust the properties within the component's styles using Angular 2

In this project, the objective is to dynamically change the background-color based on different routes. The goal is to display a specific color for UpcomingComponent while keeping the background-color consistent for all other routes. The approach involves ...

How can I alter the color of the top bar in Foundation framework?

Hey there! I'm relatively new to using Foundation and I've been trying out different solutions I found online for various issues, but I'm still struggling with this one. The section highlighted in red just won't change to the same pink ...

Grab the content from a contenteditable HTML Element using React

Currently, I am developing an EditableLabel React component using Typescript in conjunction with the contenteditable attribute. My goal is to enable the selection of the entire text content when the user focuses on it, similar to the behavior showcased in ...

What is the best way to rearrange the order of divs when the screen is being resized?

My task involves rendering a page with react-bootstrap on desktop, structured like this: <Row> <Col xs={12} md={8} className="a"> </Col> <Col xs={6} md={4} className="b"> </Col> <Col xs={6} md={4} className ...

What is the best way to incorporate both images and text in PHP code?

I'm currently working on creating a large image call-to-action (CTA) for my new WordPress website. I've set up a new field group with the type "group" in ACF, and added some functions in PHPStorm. However, none of my images, text, or links are ap ...

A step-by-step guide on dividing items in an array with React

user.js import React, { useState, useEffect } from 'react'; import Axios from 'axios'; const RecommendationDetail = () => { const [images, setImages] = useState([]); useEffect(() => { fetchRecommendations(); }, []); ...

Is there a way to use CSS animation and transform to create a flip effect on two different divs, flipping them up and then back down with pauses and looping in between?

I have a query about CSS animation and transform. I am looking to create a flipping effect on two divs - one flips up from the bottom, then back down, followed by the second div flipping up and back down. I want this sequence to repeat indefinitely with pa ...

Issues with Google maps are causing multiple maps to malfunction

After incorporating some jquery code to create multiple maps upon window load, I noticed a peculiar issue with the maps - they all display the same location despite having different latitudes and longitudes set. Upon inspecting the code responsible for cr ...

Customizing hover color with tailwind CSS

How can I change the color of an icon on mouseover using Tailwind CSS? I have tried to go from this https://i.sstatic.net/ObW6C.png to this https://i.sstatic.net/Tagp3.png, but it's not working. .btn { @apply agt-h-10 agt-w-10 agt-bg-zinc-100 agt- ...

List items that are not in any particular order spilling over onto the next

Having an issue with an unordered list where the list items aren't all on the same line. The 5th element is dropping to the next line even though each item is set to 20% of the container. The math adds up, so not sure what's causing the problem. ...

The Asp.net MVC Navigation Bar

I'm facing an issue with the navbar in asp.net mvc https://i.sstatic.net/QJmPW.jpg Here is my code snippet <li class="nav-item nav-link" role="presentation"> <a class="nav-link" @Html.ActionLink("Cust ...

The potential for an 'undefined' object in TypeScript React is a concern that should be addressed

Currently, I am honing my skills in using TypeScript with React and retrieving data from an API that I set up a few days back. The API is functioning properly as I am able to fetch data for my front-end without any issues. However, when I attempt to util ...

The MUI text input element fails to correctly update the component's state

I have a functioning app that utilizes various inputs. Initially, the dataset is populated with data from an API request as shown below: const [userData, setuserData] = useState([]) const companyuser = useSelector(state=>state.companyuser.currentU ...