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

Rearrange the middle column to be the top column on smaller screens

I am trying to create a layout with 3 columns in a row. The challenge is to prioritize the middle column on top when viewed on small screens, with the left and right columns below it. To clarify, on large screens the order should be 1 2 3, while on small s ...

Automatically advance to the next input field and go back when the backspace key is pressed after reaching the maximum length

Creating a credit card input field that switches cursor after reaching maximum number length and focuses on previous field when deleting with backspace. (function ($) { $(".nmipay-input-field").keyup(function () { if (this.value.l ...

Interactive SVG viewport dimension

Check out my "hamburger button" made in SVG, shown below. body { margin: 0; text-align: center; } svg#ham-btn { margin: 2rem; border: 1px solid black; fill: #383838; } <svg id="ham-btn" width="107.5px" height="80px" viewBox="0 0 80 80" pre ...

What steps should I take to ensure that my Ngrok domain is functioning properly?

Screenshot of Endpoints I've been facing challenges while attempting to get my NGROK domain operational. Despite completing all the necessary setup steps and successfully running multiple tunnels simultaneously with NSSM, as well as having a business ...

Reducing the Size of Sprite Images on Websites

I am working on a web page that contains an image file called sprites.png. This file holds multiple images within it. Specifically, there is one image in the file that is 48px x 48px. In my CSS, I am referencing this image like so: .cell-back { height:3 ...

html Comparison of Documents

My goal is to compare HTML documents to see if they have the same tags in the same arrangement, regardless of different inner text and attribute values. I am only interested in comparing the general tag structure, such as: <html> <head> </h ...

Issue in Firefox: Footer is wrapping around the main content

Hey, I've been dealing with a pesky bug that just won't go away. We're currently working on a redesign for www.petpoint.com - The footer looks perfectly fine in every browser except for Firefox. It gets all jumbled up for some reason. I&a ...

Ways to modify the text color in a CSS animation without using any external libraries?

When using this pure CSS loading animation, the fore color is set to white which may not be visible on a white background. How can the fore-color be changed? .lds-hourglass { display: inline-block; position: relative; width: 120px; height: 120 ...

Challenging design with CSS shapes

Can this specific shape be created using just one HTML element? I am looking to use it for image cropping purposes, so having only one element would make the process easier ...

Stop users from being able to select or highlight text within a content editable div

Is there a method to develop a content-editable div where users are unable to select or highlight content, but can still input information? I'm interested in creating an interface that forces users to delete and enter data character by character, with ...

Instructions for implementing this script in HTML and JavaScript: utilize the clone() function

I came across this code snippet here function displaytickets(){ var $panel = $('<div/>').addClass('col-xs-3 panel panel-default') $panel.append($('<div><h3 class="panel-title">Title</h3></div>&a ...

What is the best way to ensure that my header remains responsive across all devices and screen sizes?

The header is not responding despite inputting width: 100%. How can I ensure that the header is responsive on all browsers, as well as iPads and phones? .head-wrap { background: url(http://envisionmediallc.com/prodigy/wp-content/uploads/2014/02/We-are-p ...

Issues arise when the sub-menu does not function properly and overlaps with other elements of

I've been working with a client on their website and encountered an issue. On the mobile version of our menu, when I hover over a menu item that has several sub-menu items, those items display properly, but there is an element below them showing thro ...

A step-by-step guide on how to implement a window scroll-controlled color transition

I've implemented jQuery code to change the opacity of my navbar's background as the user scrolls, transitioning from transparent to blue. Here's the snippet: $(window).scroll(function(){ var range = $(this).scrollTop(); var limit = 45 ...

Div Boxes at the Center

I've searched extensively for a solution to my unique problem, but haven't been able to find one that fits. I have 4 div boxes with a max width of 50% and a min width of 400px. When the site is viewed on a smaller screen, I want the boxes to alig ...

Make sure all Bootstrap elements have square edges

Can someone help me with using bootstrap3 to achieve square corners for all buttons, menus, and navs? I have tried setting the edges to be square using this code snippet, but my specific requirement is to only have square corners for the buttons, menus, a ...

Utilize JavaScript and jQuery to extract the selected text's context from a webpage

I'm looking to extract the contextual information surrounding a selected text on a web page, specifically the 25 words before and after it. I've tried using JavaScript and jQuery with the following code snippet but unfortunately, it doesn't ...

Issue with Google Chart overlapping drop down menu in CSS紿orizontal scrollingyan

I've exhausted all my options. I've experimented with positioning and z-indexes, but no matter what I try, the drop-down menu remains hidden behind the google chart. It functions properly on Firefox, but not on Chrome or IE. Any helpful advice wo ...

Clicking on a select box causes the scrollbar area to vanish, shifting all the page content to the right

I have successfully implemented overflow-y: scroll; to keep the scrollbar space always reserved and prevent content from shifting when the scrollbar appears. However, I noticed that when I click on a select element, this scrollbar space is lost. How can I ...

Caution: It is important for every child in a list to have a distinctive "key" prop value. How can a shared key be used for multiple items?

When I click on a header from my list, an image should appear. However, I'm encountering an error that needs to be resolved. I've been struggling to find a solution for adding unique keys to multiple elements in order to eliminate the error. Des ...