Fashionable flexbox chat design break

Can anyone explain why the image shifts to a new line when the dimensions of the bubble are restricted?

If I adjust the maximum width of the bubbles to calc(100% - 70px), the image stays on the same line, but the last word may break onto a new line, disrupting the design.

body {
  background: #f0f3f6;
}

.messages {
  width: 100%;
  margin-top: 65px;
  margin-bottom: 70px;
  padding-bottom: 15px;
}

.scrolling {
  display: inline-flex;
  flex: auto;
  overflow-y: auto;
  overflow-x: hidden;
}

.scrolled {
  flex: 0 1 auto;
  display: flex;
  flex-flow: column nowrap;
  justify-content: flex-end;
  width: 100%;
  height: 100%;
  margin: 0 15px;
}

.message {
  line-height: 1em;
  display: block;
  margin: 18px 0;
  position: relative;
}

.message .image {
  position: relative;
  display: inline-block;
  width: 25px;
}

.message img {
  display: block;
  width: 100%;
  border-radius: 150px;
}

.message .bubble {
  font-weight: 300;
  position: relative;
  vertical-align: text-top;
  display: inline-block;
  font-size: 0.9em;
  padding: 12px 14px;
  margin-top: -16px;
  margin-left: 20px;
  margin-right: 20px;
  border-radius: 4px;
  max-width: 100%;
}

.message .bubble::after {
  content: '';
  position: absolute;
  top: 12px;
  width: 0;
  height: 0;
}

.message .time {
  color: #838689;
  font-weight: 500;
  font-size: 0.7em;
  position: absolute;
  bottom: -1.9em;
  width: 100%;
}

.message-left {
  flex: 1;
  align-self: flex-start;
}

.message-left .time {
  left: 50px;
}

.message-left .bubble {
  color: #343434;
  background-color: #fff;
  margin-left: 20px;
}

.message-left .bubble::after {
  position: absolute;
  left: -5px;
  border-top: 5px solid transparent;
  border-bottom: 5px solid transparent;
  border-right: 5px solid #fff;
}

.message-right {
  flex: 0;
  align-self: flex-end;
  float: right;
}

.message-right .time {
  text-align: right;
  right: 50px;
}

.message-right .bubble {
  color: #fff;
  margin-right: 20px;
  background-color: #0084ff;
}

.message-right .bubble::after {
  position: absolute;
  right: -5px;
  border-top: 5px solid transparent;
  border-bottom: 5px solid transparent;
  border-left: 5px solid #0084ff;
}
<div ref="messages" class="messages scrolling">
  <div class="scrolled">
    <div class="message message-left">
      <span class="time">1 min ago</span>
      <div class="image">
        <img src="https://scontent-lht6-1.xx.fbcdn.net/v/t1.0-9/15492548_1291266807563287_6299338365875926813_n.jpg?oh=b2c7a59d1666247350753be9002e6884&oe=5AB71F93" />
      </div>
      <div class="bubble">
        This message has the same issue.
      </div>
    </div>

    <div class="message message-right">
      <span class="time">1 min ago</span>
      <div class="bubble">
        A brief note.
      </div>
      <div class="image">
        <img src="https://scontent-lht6-1.xx.fbcdn.net/v/t1.0-9/15492548_1291266807563287_6299338365875926813_n.jpg?oh=b2c7a59d1666247350753be9002e6884&oe=5AB71F93" />
      </div>
    </div>

    <div class="message message-right">
      <span class="time">1 min ago</span>
      <div class="bubble">
        A very lengthy message, intended to be long enough to span multiple lines.
      </div>
      <div class="image">
        <img src="https://scontent-lht6-1.xx.fbcdn.net/v/t1.0-9/15492548_1291266807563287_6299338365875926813_n.jpg?oh=b2c7a59d1666247350753be9002e6884&oe=5AB71F93" />
      </div>
    </div>
  </div>
</div>

Original JSFiddle

Answer №1

The message element has a display: block property.

By changing it to display: flex, you can ensure that the image and text stay on the same line, as the default setting for a flex container is flex-wrap: nowrap.

Check out the revised demo 1 for a visual example.

To prevent the images from shrinking, simply add flex-shrink: 0 to the items. By default, flex items have flex-shrink: 1, allowing them to shrink to fit within the container.

For another demonstration, visit revised demo 2.

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 for adjusting the hue of a single color in a JPEG image

Is there a way to transform white color to red using only CSS, while leaving the rest of the colors unaffected? This should be accomplished without changing any other elements. ...

Ways to apply attributes to specific images using a JQuery array

I'm struggling to add attributes to multiple images using a jQuery array. Can anyone provide some assistance? <div id="result"> <img src="Image1" width="300"/> <img src="Image2" width="300"/> <img src="Image3" width="300"/> ...

Center the element vertically if its height is less than the container's height, but let its height become 100% if the container's height is

I am trying to achieve a layout where an element (.figure) is centered horizontally when it is shorter than its container (.timeline-content), but matches the height of its container when longer. The height of the container changes based on its parent. T ...

Customize Angular Material's Mat-Dialog background blur/darkening effect

Greetings, dear community members, I am currently utilizing angular along with angular material in my projects. By default, when a material dialog is opened, it slightly darkens the background. However, I am interested in having a blurred background inst ...

Using PHP and an HTML form to insert data into a MySQL database

I'm having an issue with inserting data into a MySQL database using PHP and an HTML form. After hitting submit, I receive a message stating that the item was inserted successfully. However, when I check phpMyAdmin, the table appears to be empty. The ...

drawing tool with JavaScript and HTML5

Can you help me figure out why my sketchpad isn't working in Processing.js? I've checked my code and there are no errors, but the canvas is not showing up. Any suggestions? Take a look at the code below: function createSketchPad(processing) { ...

The modal window does not display a scroll bar

I'm currently facing an issue where the scroll bar is not appearing on my page when more elements are added. In addition, I have a modal that covers the entire page when clicking on an item. However, changing the CSS position: fixed property affects ...

Is the navigation item only clickable within its designated area?

Apologies in advance for the confusion, but I'm facing an issue with the navigation bar on my website. It works perfectly on the Home page when hovered over, but on the login and register pages, it requires hovering over a specific spot under the item ...

Issue with Selenium WebDriver: Expectation failure while waiting for element visibility at specified location(..)

I'm facing a little issue with my automated test. Whenever the test encounters an error like this: Expected condition failed: waiting for visibility of element located by(...) I find it difficult to pinpoint the exact problem. @BeforeMethod pub ...

Zebra lines overlooking unseen details

Imagine having a table where rows can be dynamically assigned classes such as .hidden, which hide those rows using CSS. The rows are styled with alternating colors, like this: tr:nth-child(even) { background-color: $light-grey; } But here's the ...

Is there a way to remove the ring shadow in TailwindCSS?

Here is a visual representation of the issue I'm facing (refer to the image): Check out the Image After inspecting with Chrome, it seems like the problem is connected to --tw-ring-shadow. I attempted to use classes like ring-0 and ring-offset-0 (men ...

How to extract the content of a child HTML element using jQuery

Is there a way to extract the text from the h4 tag with the class .media-heading in the code snippet below when a user clicks on the link? I attempted to use the closest method but it didn't yield the desired result. Check it out: <li class="media ...

Make a diagonal border using CSS styling

Is it possible to create a slanted border like the one shown in this image (red line) using CSS/CSS3 or JavaScript? <div id="DIV_1"> <img src="/uploads/2016/01/logo.jpg" width="250px" id="IMG_2" alt='' /> <nav id="NAV_3"& ...

Tips for integrating Bootstrap JavaScript into a React project

I am encountering an issue while trying to incorporate Bootstrap JavaScript into my web app. I have successfully included the CSS, but I am facing difficulties with Bootstrap JavaScript for components like modals and alerts. My package.json includes: "boo ...

Creating a website without access to the internet

When I'm working on my laptop in locations without internet access, I need to build a website. How can I assess the progress of my pages without an active browser? ...

How can you stop text in a textarea from spilling over into the padding?

When I add padding to a textarea element and type more than four lines of content, the content overflows into the padding. Is there a way to prevent this? I have looked at the suggestions in this thread and this one (specifically for google-chrome), but t ...

Is there a way to adjust the height overflow using a percentage?

I have a coding dilemma here where I am attempting to make the SearchLocationCards overflow. It seems to work fine with 'px' and 'vh' values, but the '%' value doesn't seem to have any effect. How can I adjust the height ...

The navbar expands in height when resized

I've been working on creating a responsive navbar, but I'm facing an issue where the height of the bar doubles between phone screen size and full window size. Despite trying various Bootstrap classes, I haven't had any success in fixing this ...

Only output to the console if the data returned from an AJAX request has been

Here is a script that I created: <script type="text/javascript> $('.storage').html(""); setInterval(function(){ $.get('./playcommand.php', function(data) { if($('.storage').html() !== data){ ...

issue with jQuery not properly selecting options based on text

I have three select elements in my code, each containing 3 or 4 options. An "Apply All" button is placed on the first file row. When a user selects a sheet name on the first file and clicks the Apply All button, it should automatically select the same she ...