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

In Vue, when you want to display text after reaching a height of 50px, any additional text will automatically be replaced by five full

https://i.stack.imgur.com/mp0YJ.png >>>>>>>>>Explore Sandbox Example <div style="height:50px"> ...

Retrieve and manipulate custom headers from a webview within an AngularJS app

I have implemented an angular application within a webview using the following code: WebView.loadUrl(String url, Map<String, String> additionalHttpHeaders) In order to manage the content and display it appropriately, I am maintaining a state in my ...

There is a space above the second div when using display inline-block

I'm encountering an issue with the display inline block property. I have two divs that I want to position next to each other, but there's a strange gap above the second div (#store_header_text) that I can't seem to explain. I've tried r ...

PHP image retrieval is not functioning correctly and the image is not appearing as it

I'm facing some challenges when it comes to displaying images with PHP. I attempted to use this solution from Stack Overflow, but unfortunately, the image is still not appearing correctly. Within my PHP file that deals with the HTML layout, the code ...

What is the best way to constrain the ratio of a full-width image display?

I am in need of a solution for displaying an image at full width inside a frame with a 16:9 aspect ratio. The image needs to be centered within the frame and adjust responsively when resizing the screen. My preference is to achieve this using just CSS. ...

Acquire HTML information using the Robot framework and xpath techniques

My first task is to visit the website www.google.de. I aim to extract the word Deutschland from this section of the HTML code: <div class="logo-subtext">Deutschland</div> Since the id in this div class is not available, I must utilize xpath. ...

Creating a dynamic full-width background image that adjusts its height according to the content: a step-by-step guide

Currently, I'm attempting to apply a background image to a webpage that spans 100% width in order to adjust to the viewport size. The method I am using involves placing an image directly after the body tag with the subsequent styling: img#background ...

What is the method for executing a for loop in AngularJS without relying on HTML elements?

I'm creating an application in AngularJS where the structure requires me to execute a for each loop without using any HTML elements or div tags with ng-repeat. In Django, we typically use the following syntax: {% for item in list %} {{ item }} {% ...

Altering documents post Bower installation

I took the initiative to manually download the css and js files for an angular module (here). In order to make a small adjustment, I modified the css (specifically changing max-width in the media query): Original: @media only screen and (max-width: 800px ...

Troubleshooting issue: Failure in proper functionality of Jquery's slideDown

My form is divided into 3 sections, with the latter two initially hidden using CSS. When users click the next button in the first section, the second section is supposed to slide down into view. However, I'm experiencing an issue where the animation s ...

Error 403 occurs after submitting the form

Recently, I encountered a 403 error when attempting to submit the form on this page. Interestingly, when I directly accessed the new page by typing the URL into my browser, it loaded without any issues. The PHP code in the initial page looks like this: & ...

The issue of the Angular checkbox not properly updating the model is causing confusion

Having trouble updating a checkbox value based on another checkbox value and seeing no effect in the model. Check out this code example showcasing the issue: http://plnkr.co/edit/eFOn3cejwKU01LkKNC1s?p=preview Here's the HTML file snippet: <!DOC ...

Email sending through PHP AJAX can be done without refreshing the page. The email action is handled in email.php and incorporated into

I am having trouble sending this with AJAX. The error message I keep getting is: Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more assistance, please visit @ jquer ...

Creating a stylish gradient text color with Material-UI's <Typography /> component

Is there a way to apply a gradient font color to a <Typography /> component? I've attempted the following: const CustomColor = withStyles({ root: { fontColor: "-webkit-linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)", }, })(T ...

Switching Over to Burger Menu

I created a burger menu using HTML, CSS, and jQuery that switches from a full-width menu to a burger menu. However, I'm facing an issue with toggling the dropdown when the menu collapses. Here's my code: <!DOCTYPE html> <html> < ...

Is there a way to verify the phone number input field on my registration form along with the country code using geolocation

I'm currently working on a registration form that includes an input field for telephone number. I'd like to implement a feature where, upon filling out the form, the telephone input field automatically displays the country code by default. Would ...

How to efficiently pass multiple inputs from an HTML form to a controller in AngularJS using a single ng

Currently, I have multiple input text boxes in my HTML and I need to send their values to my controller to add them to an object. The challenge I'm facing is figuring out how to pass more than one value using just one ng-model. In my HTML code, I have ...

What is the best way to compare JavaScript arrays with the same items but in a different order?

Within my product groups, each group is identified by a set of product_type_ids. I am looking for a way to match different groups based on their product_type_ids. The order of the ids may vary within the group, but as long as the sets of ids match, I cons ...

The justify-position attribute does not have any impact on the positions that are set

Here is the JSX code that resembles HTML but uses ClassName instead of class: <div className="snavbarcontainer"> <div className="toplefticon"> <a class="test" href=" ...

Exploring AngularJS Protractor End-to-End Mocking

In my Angular SPA, I am retrieving data from a node backend that is fully covered with tests. To mock the Angular HTTP calls, I want to implement something like this: Api = $injector.get('Api'); sinon.mock(Api, 'getSomethingFromServer' ...