How to align a list item to the right using CSS

I'm having trouble aligning items in a list to the right and adjusting their width based on content length. Unfortunately, I haven't been able to make it work despite multiple attempts.

Take a look at my code snippet below:

.messages {
  overflow: hidden;
  list-style: none;
  padding: 0 0 50px 0;
  margin: 0;
}
.messages .sent {
  background: white;
  color: #424242;
}
.messages .received {
  background: dodgerblue;
  color: white;
}
.messages .sent,
.messages .received {
  padding: 5px 8px;
  margin-bottom: 8px;
  max-width: 80%;
}
.messages * .message {
  margin-bottom: 5px;
}
.messages .sent .message .p {
  text-align: left;
}
.messages * .message p {
  word-wrap: break-word;
  text-align: left;
  font-size: 18px;
  padding: 0;
  margin: 0;
}
<ul class="messages">

  <li class="sent">
    <div class="message">
      <p>Hey</p>
    </div>
  </li>

  <li class="received">
    <div class="message">
      <p>Hey man!</p>
    </div>
  </li>

  <li class="sent">
    <div class="message">
      <p>What's up?</p>
    </div>

    <div class="message">
      <p>How are ya?</p>
    </div>
  </li>

  <li class="received">
    <div class="message">
      <p>Not bad</p>
    </div>

    <div class="message">
      <p>Not bad at all</p>
    </div>
  </li>

</ul>

Here's how the page looks with the current code:

However, here's a simple example of what I'm trying to achieve:

I've experimented with adding display: inline-block; under the .messages * .message selector and text-align: right; under .messages .sent, but unfortunately, it messed up the entire page layout.

I've spent quite some time trying to achieve the desired alignment as shown above, but nothing seems to be effective. How can I align specific items to the right in a list while keeping others aligned to the left?

Thank you!

Answer №1

The text container with a background color should be set to display inline-block, while the next container should align either left or right.

Check out this example on JSFiddle

To summarize:

.messages .message {
   display:block;
}

.messages .message p{
   display:inline-block;
}

.messages .sent{
  text-align:right;
}

Answer №2

To resolve the issues regarding text alignment, it is recommended to eliminate the current text alignments in .messages .sent .message .p and .messages * .message .p, and substitute them with the following code:

.messages .received {
    text-align: left;
    clear: both;
}

.messages .sent {
    width: 20%;
    float: right;
    clear: both;
}

Please note that messages sent should be floated to the right, and both sent and received messages require a 'clear: both'. The background colors should be assigned to messages .received .message and messages .sent .message, each with specific widths:

.messages .received .message {
    width: 40%;
    background: dodgerblue;
}

.messages .sent .message {
    background: white;
    color: #424242;
}

The padding adjustments should also be made by transferring them from

.messages .sent, .messages .received
to .messages * .message p. For your convenience, all these modifications have been implemented in a new fiddle provided below:

Check out the updated design on this fiddle: https://jsfiddle.net/Lbyyk5d7/6/

Answer №3

hopefully this meets your needs.

.conversations {
    overflow: hidden;
    list-style: none;
    padding: 0 0 50px 0;
    margin: 0;
    background: #efeeec;
}

.conversations .sent {
  text-align:right;
}

.conversations .sent p {
    background: white;
    color: #424242;
}

.conversations .received p {
    background: dodgerblue;
    color: white;
}

.conversations .sent p,
.conversations .received p{
    display:inline-block;
    padding: 5px 8px;
        margin: 0px;
}

.conversations .message {
   display:block;
}

.conversations .sent .message .p {
    text-align: left;
    display:inline;
}
<ul class="conversations">

  <li class="sent">
    <div class="message">
      <p>Hey</p>
    </div>
  </li>

  <li class="received">
    <div class="message">
      <p>Hey there!</p>
    </div>
  </li>

  <li class="sent">
    <div class="message">
      <p>How's it going?</p>
    </div>

    <div class="message">
      <p>What's new?</p>
    </div>
  </li>

  <li class="received">
    <div class="message">
      <p>Pretty good</p>
    </div>

    <div class="message">
      <p>Can't complain</p>
    </div>
  </li>

</ul>

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

Embedding a picture logo within a CSS-designed content box

I am trying to create a unique layout for my website that reveals content using labelled radio buttons, without the use of scripts. Instead of using <a href='#'>, I want to achieve a specific layout like the one shown in this example. When ...

Center the first column in jQuery Datatables

Looking to center-align only the first column named "Status": $("#TransactionTable").DataTable({ ajax: { url: '/Transaction/SearchMockup', type: 'POST', data: { ...

The website does not support the zoom out or scrolling features on iOS devices

A portion of our website has been optimized for mobile devices, however, we have decided to direct iPhone users to the desktop version when accessing the Blogs section (all PHP content) at this time. While this functions properly on an iPad, we have encou ...

An easy way to divide an array into a table

I've obtained these arrays from a JSON file "Works": [ {"TypeOfWork": "scaffolding", "Status": "done"}, {"TypeOfWork": "painting", "Status": "on-going"} ] My go ...

"Exclusive Mui sx styles will be applied only when a specific breakpoint

As I update my old mui styling to utilize the sx attribute, I've noticed the ability to specify styles for different breakpoints using sx = {{ someCssProp: { xs: ..., md: ... and so on. Prior to this, I had been using theme.breakpoints.only for some ...

Utilize jQuery to automatically assign numbers to <h1-h6> headings

Looking to develop a JavaScript function that can automatically number headings (h1- h6) for multiple projects without relying on CSS. Currently achieving this with CSS: body { counter-reset: h1; } h1 { counter-reset: h2; } h2 { counter-reset: h3; } ... T ...

Pictures failing to load within the canvas

I am currently in the process of creating a very basic canvas element. The idea is to allow users to choose between two images and have their selection appear on the canvas when clicked. However, I am facing some challenges with making it work. I have ver ...

`Unable to activate Bootstrap dropdown feature`

Having an issue with the dropdown menu on my Bootstrap navbar - when I click it, nothing happens. I've tried various solutions found online, such as moving the <script type="text/javascript" src="js/jquery-3.3.1.min"></script> file to the ...

Show the content of a div inside a tooltip message box in an MVC application

I have a MVC application where I need to show tooltip messages when hovering over my HTML div. HTML: <li class="Limenu" data-placement="right"> <span class="LessMenuspan btn-primary" pid="@i.ConsumerNo_" onmouseover="FacebookprofileTip(1, 0,thi ...

HTML - PHP, navigating the history of the page with the browser's back button in real-time

My current project involves the development of a website using HTML and PHP. On one of my HTML pages, I have a form with certain fields marked as "REQUIRED". To ensure that all required fields are filled before submission, I have implemented a feature wher ...

Achieving perfect alignment of two images within a block and maintaining their center position even when resizing

While working within a Wordpress post, my goal is to insert two images side by side with the block of images centered, one aligned left and the other aligned right (which I have achieved). As the page size decreases, I want the images to stack on top of e ...

Sending emails to multiple groups in Opencart can be easily achieved with the

I am interested in customizing Opencart's admin panel to allow for the selection of multiple customer groups when sending emails. However, I am unsure of where and how to make these modifications as I am relatively new to this type of task. ...

Ways to conceal the picture

Check out the JSfiddle link here for the full code. I'm having trouble with my slider as the last picture keeps collapsing underneath and is not hidden as it should be. I suspect this issue is causing the slider to malfunction. HTML <div class=" ...

Conceal the div with ID "en" if the value matches $en

Looking for assistance with language settings on my page. I have a menu where I can select English, Croatian, or German. Below is the code to manage language changes: <?php class home_header_language { protected $_DBconn; ...

Break down and simplify the code into individual components

<input type='button' class="myButton" onclick="document.body.style.cssText+=';background-image: url(img01.jpg);background-repeat: no-repeat; -moz-background-size: cover; -o-background-size: cover; background-size: cover;';" value="S ...

What is preventing this jQuery from altering the opacity?

Currently, I have a piece of code that is intended to change the opacity of a div element from 0 to 1 as soon as the user initiates scrolling on the body. However, it appears to be malfunctioning for reasons unknown. $("bod").load(function(){ document ...

What is the best way to make the block rotate each time the button is clicked?

Specifically, I am having trouble getting the block to rotate properly. Currently, the block only rotates once when clicked, but I want it to rotate each time I click in both directions. Any suggestions on how to achieve this? var now = 10; $('. ...

Ways to utilize CSS for capturing user-inputted text in a text field

I have a question about incorporating user input text into CSS styling. Specifically, I am looking to create a color background option (#ffffff) where the designer can input their own color and have it displayed once saved in the body background style. Wh ...

Displaying MongoIds in HTML output for the user

Currently, I am in the process of developing a web application that utilizes MongoDB as its database management system. One challenge I am facing is finding a way to accurately identify which object the user has selected from a list on the screen, and the ...

Troubleshooting: Bootstrap 5.0 Navbar Dropdown Issue Unresolved

My navbar dropdown isn't working when clicked. I've searched for solutions without success. Is there a script missing? layout.html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> ...