What is the best way to position an image in the bottom right corner of a fluid div container?

I am struggling to position this icon image at the bottom right hand corner of this div, so that it appears as if the message is originating from the icon's head. Despite my efforts, I have yet to find a definitive solution.

https://i.stack.imgur.com/XkHz1.jpg

Below is the relevant portion of CSS code:

.messageContainer{
    width:100%;
    position: relative;
}
.message.to {
    border-radius: 3em 3em .5em 3em;
    background-color: black;
    color: #fff;
    float:right;
    margin-right: 30px;
    margin-left: 12em;

}

img.to{
        position: absolute;
        right: 0px;
        bottom: 0px;
        float:right;
        width: 24px;
        height: 24px;
        content:url("https://cdn2.iconfinder.com/data/icons/ui-1/60/05-512.png");
    }

Next, the relevant snippet of HTML using Angular:

<ng-container *ngFor="let message of messages | async">
    <div class="messageContainer">
         <img class="icon" [ngClass]="{ 'from': message.sentBy === 'bot',
                                    'to':   message.sentBy === 'user' }"/>
        <div class="message" [ngClass]="{ 'from': message.sentBy === 'bot',
                                    'to':   message.sentBy === 'user' }" [innerHTML]=message.content>
         </div>
    </div>
</ng-container>

Here is the suggested HTML structure without Angular:

<div class="messageContainer">
         <img class="icon to">
        <div class="message to" >example message where i want the icon to be in the bottom right hand corner of the div</div>
    </div>

Answer №1

It is important to always set the parent div with a position of relative and the child div (such as an image container) with a position of absolute:

.chat-layout {
  position: relative;
  display: inline-block 
}

.corner-image{
  position : absolute;
  display: inline-block;
  right: 0;
  bottom: 0;
}

Remember to consider any padding or z-index in the containing text div...

Answer №2

To achieve the desired effect, one simple solution is to create a space on the right side of the parent element and set the parent's position value to relative. Then, utilize position: absolute; bottom: 0; and adjust right: 0 (or even use a negative value to move the child outside the parent's background area).

Here is a basic example:

.container {
  background-color: #eee;
  margin: 1rem 4rem 1rem .5rem;
  position: relative;
  min-height: 4rem;
  padding: 1rem;
  box-sizing: border-box;
  border-radius: 1rem 1rem 0;
}
.child {
  width: 3rem;
  height: 3rem;
  position: absolute;
  background-color: #999;
  border-radius: 1.5rem;
  bottom: 0;
  right: -3.5rem;
}

/* Additional styling for aesthetics */ 
body {
  background-color: #f9f9f9;
}
.child {
  background-color: white;
  box-shadow: 0 1px 3px 0 rgba(0,0,0,.1), 0 1px 1px 0 rgba(0,0,0,.07), 0 2px 1px -1px rgba(0,0,0,.06);
  transition: box-shadow .3s cubic-bezier(.4,0,.2,1), bottom .3s cubic-bezier(.4,0,.2,1);
}

.container:hover {
  background-color: #e9e9e9;
}
.container:hover .child {
  bottom: .2rem;
  box-shadow: 0 3px 5px -1px rgba(0,0,0,.1), 0 6px 10px 0 rgba(0,0,0,.07), 0 1px 18px 0 rgba(0,0,0,.06);
}
<div class="container">
  <div class="child"></div>
  Lorem ipsum dolor sit amet.
</div>
<div class="container">
  <div class="child"></div>
  LoLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Elit ut aliquam purus sit amet. Nisl nisi scelerisque eu ultrices. Elementum integer enim neque volutpat ac tincidunt vitae semper. Vestibulum morbi blandit cursus risus at ultrices mi. Ut diam quam nulla porttitor massa id neque. Praesent elementum facilisis leo vel fringilla est ullamcorper eget. Nisl purus in mollis nunc sed id semper risus. Tellus in hac habitasse platea dictumst vestibulum rhoncus est. Lorem ipsum dolor sit amet. Cursus metus aliquam eleifend mi. Dictumst quisque sagittis purus sit. Vel pharetra vel turpis nunc eget lorem dolor sed.
</div>
<div class="container">
  <div class="child"></div>
  Lorem ipsum dolor sit amet.
</div>
<div class="container">
  <div class="child"></div>
  Nisl purus in mollis nunc sed id semper risus. Tellus in hac habitasse platea dictumst vestibulum rhoncus est. Lorem ipsum dolor sit amet. Cursus metus aliquam eleifend mi. Dictumst quisque sagittis purus sit. Vel pharetra vel turpis nunc eget lorem dolor sed.
</div>
<div class="container">
  <div class="child"></div>
  Volutpat lacus laoreet non... //

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

Angular with PHP Integration Success

I'm navigating the realms of Angular and PHP, understanding that Angular is client-side while PHP operates on the server. I'm curious if there's a way to merge these two technologies, as it's all quite unfamiliar territory for me. For ...

Encountered an issue locating the stylesheet file 'css/style.css' that is supposed to be linked from the template. This error occurred when trying to integrate Bootstrap with Angular

<link rel="stylesheet" href="plugins/bootstrap/bootstrap.min.css"> <link rel="stylesheet" href="plugins/owl-carousel/owl.carousel.css"> <link rel="stylesheet" href="plugins/magnific-pop ...

Transform Default Buttons to Resemble Normal Buttons

I currently have a JavaFX button that is set as the Default Button, allowing users to select it with the Enter key. The button has a blue background: However, I would like to change its appearance to that of a normal button: After reviewing the JavaFX CS ...

Combining two arrays filled with objects to form a collection of Objects on a Map

In my JavaScript code, I'm receiving data from a WebService that looks like this: { "fire": { "totalOccurence": 2, "statsByCustomer": [ { "idCustomer": 1, "occurence": 1 }, { "idCustomer": 2, ...

Troubleshooting imagejpeg() Function Failure in PHP Server

I've been working on implementing image cropping functionality for my website. I've managed to send an array of cropped image dimensions (x, y, width, height) to my PHP script. On my localhost, the script successfully crops the image, but unfort ...

Removing and shattering data from a JSON file at a designated location in AngularJS

I have received json data that is structured like this (and unfortunately cannot be modified): Desc: First data - Second data My current method of displaying this data involves using the following code: <div ng-repeat="b in a.Items">{{b.Desc}}< ...

Utilizing HTML documents instead of images in Owl Carousel 2

I am currently utilizing owl carousel 2 to construct a basic sliding carousel. However, I am only using images at the moment and would like to incorporate HTML files instead. These HTML files contain multiple divs where images can be inserted, and instead ...

Setting up button alignment in Bootstrap

In my current template, I am utilizing Bootstrap and need to position three buttons accordingly. The first button should be all the way to the left, the second in the middle, and the third (final) one on the right within a container element. While attempt ...

What could be the reason for the clone function not duplicating the data within the table

Recently, I utilized jQuery's clone method to duplicate and add an HTML table. The rows and cells of the table were added using jQuery. However, despite using clone, it only replicated the headers and CSS, not the appended data. This raises the ques ...

Having trouble getting Tailwind CSS to work with React components?

I'm having trouble getting Tailwind CSS to work properly. I've made sure to update to the latest version, but for some reason Tailwind is not functioning and there are no errors showing up in the console. Here is a link to my header component an ...

Conceal the scrollbar and enable horizontal swiping using CSS

I have set up a container where I want the content to scroll horizontally from left to right on mobile devices, and I would like to be able to swipe to navigate while hiding the scrollbar. You can view the implementation on this JSfiddle link Do you think ...

Is it possible to include a submenu within a md-tooltip?

I have set up an md-tooltip for a sidebar, but I would like to enhance it by enabling a submenu option. For instance, if there is a Contacts submenu under the Profile menu, I want to be able to access Contacts when hovering over the Menu Icon. The tooltip ...

HTML/CSS - Troubleshooting Animation Issues

I am currently experimenting with different website animations and implementing responsive design. However, I encountered an issue when trying to apply these changes to my header, which also affects the .heading element. It seems that there is a problem w ...

Differentiating CSS translate in front versus behind another div

I'm having some trouble translating an SVG graphic in the y-axis using CSS transforms. The translation itself is working fine: transform: translate3d(0, -100px, 0); However, when I move the graphic 100px up in the Y direction, it ends up behind its ...

Discovering elements that are currently visible in an Angular list

Suppose there is a variable named users which represents an array We have the following ng-repeat statement: ng-repeat="user in users | filterUser: searchUser: assignedUsers: selectedDivision" After filtering the users using searchUser and selectedDivis ...

JQuery is failing to locate elements that have dynamic data titles assigned to them

I've been attempting to locate an element using a jQuery regex comparison in the data title. My situation involves having divs with the class .textbox, each containing a dynamically generated data title. To target specific boxes with that particular d ...

When the if-else statement is executed, it showcases two strings:

Learning Experience: Unveiling My Lack of Cleverness Update: It appears that utilizing PHP in my current setup is not possible. As a result, I'll take some time to contemplate while banging my head against a wall. Despite that, thank you all for your ...

What could be causing the web service's returned data to not show up in the table?

I am utilizing angularjs version 1.6 in my project. My project involves reading data from a web service and I need to showcase the returned data in a table. Below is the http ajax call I'm using: $http.get("../../Services/ReportDepartmentService.as ...

Utilizing Firebase in place of .json files for the AngularJS Phonecat application

I am currently exploring firebase and attempting to retrieve data using this service from firebase instead of json files. However, I have encountered some challenges in getting it to function properly. This is inspired by the angularjs phonecat example .f ...

Use Angular UI Directive Typeahead to direct to a specific link

Currently, I am utilizing the Angular UI Bootstrap typeahead directive for a specific project in which I aim to redirect to a dynamic URL depending on the user's selection in the typeahead. Essentially, I want to utilize the typeahead as a search bar. ...