Align the inner div in the center of the outer div, even when the outer div does not

I'm attempting to make sure the tail of the speechbubble aligns with the center of the image, regardless of their respective heights. Essentially, I want the image to always be centered within the current height of the wrapper, similar to how the speechbubble tail adjusts its position based on the speechbubble height.

Despite trying various methods, none seem successful so far. How can I accomplish this?

Here is the code I am currently working with:

@text: black;
@bkg: white;
@accent: rgba(75, 75, 75);

.wrapper {
    padding: 2rem 0;
}

.wrapper, .wrapper * {
    white-space: pre-line;
}

.bubble {
    background: gray;
    min-height: 225px;
    padding:10px; 
    max-height:auto; 
    width: 60%;
    border-radius: 10px;
    color: black; 
      position: relative;
    text-align:center;
    display: table;
    
}

.bubble:after {
    content: '';
    position: absolute;
    right: 0;
    top: 50%;
    width: 0;
    height: 0;
    border: 20px solid transparent;
    border-left-color: gray;
    border-right: 0;
    border-bottom: 0;
    margin-top: -10px;
    margin-right: -20px;
}

.bubble span {
    display: table-cell;
    vertical-align: middle;
}

.image {
    min-height:10px;
    position: absolute;
    right: 0;
    width: 35%;
}

.image img {
    max-width: 100%;
}
<div class="wrapper">
  <div class="image">
    <img src="https://images.squidge.org/images/2024/04/09/biscuit_portrait.png" class="bbcode_image" loading="lazy">
  </div>
  <div class="bubble">
    <span style="font-size:10pt">
            elit enim in ex......content truncated for brevity....
          ...tincidunt eget a nunc. 
    </span>
  </div>
</div>

Answer №1

To achieve this layout without a fixed size and absolute position, I suggest utilizing display: flex and align-items: center on the wrapper. You can also try changing the order of elements in the HTML or using flex-direction: row-reverse.

@text: black;
@bkg: white;
@accent: rgba(75, 75, 75);

.wrapper {
    padding: 2rem 0;
    display: flex;
    align-items: center
}

.wrapper, .wrapper * {
    white-space: pre-line;
}

.bubble {
    background: gray;
    min-height: 225px;
    padding:10px; 
    width: 60%;
    border-radius: 10px;
    color: black; 
    position: relative;
    display: flex;
    align-items: center;
}

.bubble:after {
    content: '';
    position: absolute;
    right: 0;
    top: 50%;
    width: 0;
    height: 0;
    border: 20px solid transparent;
    border-left-color: gray;
    border-right: 0;
    border-bottom: 0;
    margin-top: -10px;
    margin-right: -20px;
}

.bubble p {
    font-size: .625rem;
    margin: 0;
}

.image {
    min-height:10px;
    right: 0;
    width: 35%;
}

.image img {
    max-width: 100%;
}
<div class="wrapper">

  <div class="bubble">
    <p>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin vel sollicitudin eros. Suspendisse potenti. Integer ac velit non metus suscipit fringilla et id nisi. Donec euismod ex at magna fermentum, in vestibulum arcu porttitor. Aenean cursus varius bibendum. In ut augue pretium, gravida lectus quis, dictum turpis. Vestibulum vehicula condimentum erat, quis efficitur neque sodales vitae. Quisque accumsan volutpat urna nec faucibus. Nulla fringilla dictum felis, sed imperdiet justo consequat a. Aliquam erat volutpat. Maecenas varius interdum nisl ac convallis.
   </p>
  </div>
    <div class="image">
    <img src="https://example.com/image.jpg" class="bbcode_image" loading="lazy">
  </div>
</div>

Additionally, I simplified the code and recommend using Flexbox and Grid over display: table for better layout control. Flexbox and Grid are more modern and versatile choices for web design layouts.

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

Transferring mouse events from iframes to the parent document

Currently, I have a situation where my iframe is positioned over the entire HTML document. However, I am in need of finding a way to pass clicks and hover events from the iframe back to the main hosting document. Are there any potential solutions or alter ...

Can I retrieve the element of any DOM element I'm currently hovering over using JavaScript?

Suppose I have this HTML snippet: <body> <div id="1"> <span class="title">I'm a title!</span> </div> <div id="2">I'm the first element!</div> <div ...

Is it better to use multiple external style sheets?

Hello! I am looking to implement a pop-up login screen by integrating downloaded code. The issue I am facing is that the CSS file that accompanies it clashes with my current one. Is there a way to have a specific style sheet only affect certain div tags, ...

Limit selection choices in select element

Similar Question: Prevent select dropdown from opening in FireFox and Opera In my HTML file, I have a select tag that I want to use to open my own table when clicked. However, the window of the Select tag also opens, which is not desirable. Is there a ...

Issues arise when attempting to utilize jQuery to print a string once a button is pressed

I am trying to display a string on the webpage when the user clicks a button. Below is the HTML code: <p> <h1 class="text-center" id="notif"> </h1> </p> Here is the relevant part of the script: $("#btn_My").click(functio ...

Navigating on the horizontal axis within a container with overflow properties

I am trying to insert multiple children-divs into a parent-div. The parent div has a fixed width. The children are set to float left and will overflow the container. I need the ability to scroll along the x-axis. However, when I attempt to do this, the c ...

I'm curious as to why the web browser is showing a timestamp below the image I have on my HTML page

Issue at Hand: I am currently working on coding a website that involves displaying an image. However, when the image is loaded, a timestamp appears underneath it in the web browser. Query: Could you please shed some light on why a timestamp is showing up ...

Creating dynamic buttons based on a list: A step-by-step guide

Is there a way to dynamically generate and display buttons based on the number of elements in a list? I currently have a hardcoded implementation, but sometimes my list only contains two elements (button 1 and button 2) so I don't need the additional ...

The HTML file contains the complete version number of Firefox

If you're an expert in HTML, take a look at my expandable start page. I know there's room for improvement, and I'm seeking advice on automatically updating the Firefox browser version number in line 106 of the code snippet below. (Linux Mint ...

JQuery Mobile fails to apply consistent styling to user input check items within a list

I have successfully implemented the functionality to add user input items to a checklist. However, I am facing an issue where the newly added items are not adhering to Jquery Mobile's styling. Here is a screenshot showcasing the problem: Below is th ...

Revolutionary newspaper design utilizing asp.net's C# web forms

Trying to create a dynamic newspaper layout using ASP.NET C# web form. Struggling to set the page layout size inside a slider and display specific news content on a new page when clicking on an area of the newspaper using Repeater or another suitable contr ...

What is the JavaScript method for updating an HTML5 datalist to show new options?

When populating options dynamically into an HTML5 datalist, I'm facing an issue where the browser tries to display the datalist before all the options have loaded. As a result, the list either does not show up completely or only partially shows up. Is ...

Unable to position a div alongside a fluid div with minimum and maximum widths

Within this container, there is a div. #leftBanner { height: 100%; background-color: #CCC; width: 22%; min-width: 245px; max-width: 355px; float: left; } This particular div doesn't cooperate when I try to float another div next to it. While I could ...

The flex grow animation fails to activate when the Bootstrap 4 style sheet is added

Trying to implement transitions with flex grow has been a challenge. It works smoothly without any issues, but as soon as I add the bootstrap 4 style sheet, the transitions stop working. I haven't even started using the bootstrap 4 classes yet, it&apo ...

Labels are overlapping each other and being aligned to the right side

Working with the most up-to-date version of Bootstrap, I am currently designing a search bar featuring a dropdown menu preceding the search button. Upon clicking the dropdown, various controls are revealed. Below is the HTML code for this configuration: ...

Enhancing button functionality using jQuery and JavaScript for toggling behavior

I am facing a challenge with this code. The goal is to have a single script that can handle multiple audio elements on the page and toggle between PLAY and PAUSE buttons by adding or removing classes. Can anyone suggest any changes or additions that coul ...

How can SQL data be loaded following the selection from a dropdown menu?

I have a pressing query that I need assistance with. I am aiming to achieve a certain task, but due to lack of prior experience in this area, I am unsure about how to proceed. My current situation involves populating a dropdown menu with names retrieved f ...

Adjustable div height: reduce until reaching a certain point and then begin expanding once more

Incorporating a hero section to display content is my current approach. The design adapts responsively utilizing the padding-bottom percentage strategy, along with an inner container that is absolutely positioned for central alignment of the content. The ...

Using AngularJS, I can bind the ng-model directive to asynchronously update and retrieve data from

I am currently working with Angular to develop a preferences page. Essentially, I have a field in a mysql table on my server which I want to connect my textbox to using ng-model through an asynchronous xhr request for setting and fetching data. I attempt ...

Revamp the Bootstrap Form Upload feature

I am looking to redesign the Bootstrap Form Upload field. The main reason behind this is that in Bootstrap, the file upload field is treated as one single component, making it impossible to separate the file upload button from the file upload text. The fi ...