Align the child element to the baseline and have it aligned to the right within an `<li>` list item

I am interested in aligning the price of coffee to the right end of the coffee name. For example, the price 1.80 should be in line with Americano and 10.00 should be in line with Macchiato.

https://i.sstatic.net/unm26.jpg

ul {
  list-style: none;
  padding: 5px;
  margin: 0;
}
ul#container {
  width: 18%;
  min-width: 100px;
  max-width: 400px;
  border: 15px solid #886633;
  border-radius: 5px;
  background-color: orange;
  box-shadow: 4px 4px 8px rgba(0, 0, 0, .3);
}
#container li {
  border-bottom: 1px dashed blue;
}
#container > li {
  font-size: 2em;
  border-bottom: 1px solid black;
}
em {
  float: right;
  position: relative;
  bottom: 0px;
}
span {
  width: 100px;
  display: inline-block;
}
<ul id="container">
  <li>DRINK MENU
  <ul>
    <li><span>Latte</span><em>2.79</em>
    </li>
    <li><span>Cappucino</span><em>2.99</em>
    </li>
    <li><span>Cafe Americano</span><em>1.80</em>
    </li>
    <li><span>Espresso</span><em>2.00</em>
    </li>
    <li><span>Carmel Macchiato</span><em>10.00</em>
    </li>
  </ul>
</li>
</ul>

In my current code implementation using relative position, it seems like it's not working correctly. Can you suggest a solution without switching to absolute positioning and with minimal changes to the existing code?
Please explain why relative position is not achieving the desired result.

Answer №1

To start, you must correct your HTML - the closing li for the DRINK MENU should come after the nested ul.

Next, I recommend utilizing the display:table CSS property:

ul {
  list-style: none;
  padding: 0;
  margin: 0;
}
ul#container {
  width: 18%;
  min-width: 100px;
  max-width: 400px;
  border: 15px solid #886633;
  border-radius: 5px;
  background-color: orange;
  box-shadow: 4px 4px 8px rgba(0, 0, 0, .3);
}
#container > li {
  padding: 5px;
}
#container ul {
  border-top: 1px solid black;
  margin-top: 5px;
}
#container ul li {
  border-bottom: 1px dashed blue;
  display: table;
  width: 100%;
}
#container span,
#container em {
  display: table-cell;
  vertical-align: bottom;
  padding: 3px 0;
}
#container em {
  text-align: right;
}
<ul id="container">
  <li>DRINK MENU
    <ul>
      <li><span>Latte</span><em>2.79</em>
      </li>
      <li><span>Cappucino</span><em>2.99</em>
      </li>
      <li><span>Cafe Americano</span><em>1.80</em>
      </li>
      <li><span>Espresso</span><em>2.00</em>
      </li>
      <li><span>Carmel Macchiato</span><em>10.00</em>
      </li>
    </ul>
  </li>
</ul>

UPDATE

If overflow is a concern, here are some solutions:

  1. Increase the minimum width of ul#container to accommodate the longest line. In this case, a width of 125px should work: Fiddle example

  2. Add table-layout:fixed to your table li and include word-wrap:break-word in the span: Fiddle example

Answer №2

You have the option to assign a class to the <em> element

HTML

<ul id="container">
<li>DRINK MENU</li>
    <ul>
        <li><span>Latte</span><em>2.79</em></li>
        <li><span>Cappucino</span><em>2.99</em></li>
        <li><span>Cafe Americano</span><em class="bottom">1.80</em></li>
        <li><span>Espresso</span><em>2.00</em></li>
        <li><span>Carmel Macchiato</span><em class="bottom">10.00</em></li>
    </ul>
</ul>

CSS:

ul{ 
    list-style: none;
    padding: 5px;
    margin: 0;
    }
ul#container{
    width: 18%;
    min-width: 200px ;
    max-width: 400px;
    border: 15px solid #886633;
    border-radius: 5px;
    background-color: orange ;
    box-shadow: 4px 4px 8px rgba(0,0,0,.3);
    }
#container  li{
    border-bottom: 1px dashed blue;
    }
#container > li{
    font-size: 2em;
    border-bottom: 1px solid black;
    }
em{
    float: right;
    position: relative;
    bottom: 0px;
    }
.bottom {
    position: relative;
    top:15px;
}
span{
    width: 100px;
    display: inline-block ;
    }

DEMO

Another approach could be (potentially the preferred method):

CSS:

li:nth-child(3) > em, li:nth-child(5) > em{
    position: relative;
    top:16px;
}

DEMO

Answer №3

Your feedback has been invaluable in shaping the response to your queries.

Initially, there were discrepancies in your HTML structure. The nested list was incorrectly formatted, which I have rectified in this updated answer for you.

Addressing your initial question...

How can I align the prices at the baseline?

Utilizing absolute positioning offers a solution without compromising the responsiveness of your price card across various browsers and devices. It adapts seamlessly within its containing element. Testing your code is crucial to ensure its functionality.

Keep in mind that for position: absolute to function correctly, the parent element must be set to position: relative. This ensures that the absolutely positioned element moves relative to its closest positioned ancestor (e.g., the li in this case). Without a positioned ancestor, it defaults to positioning relative to the <body>. Therefore, remember:

To position a child element absolutely, designate the parent element as position: relative.

Below is an illustration integrating your code.

DEMO

https://i.sstatic.net/4dZw3.png

HTML

<!-- Corrected with adjustment to nested list -->
<div id="container">
    <h2>DRINK MENU</h2>
    <ul>
        <li><span>Latte</span><em>2.79</em></li>
        <li><span>Cappuccino</span><em>2.99</em></li>
        <li><span>Cafe Americano more text more text more text more text</span>
        <em>1.80</em></li>
        <li><span>Espresso</span><em>2.00</em></li>
        <li><span>Caramel Macchiato more text more text more text more text</span>
        <em>10.00</em></li>
     </ul>
</div>

CSS

/* Based on your original code */

#container {
    width: 200px;
    border: 15px solid #886633;
    background-color: orange;
    box-shadow: 4px 4px 8px rgba(0, 0, 0, .3);
    padding: 5px;
}
h2 {
    width: 99%;
    border-bottom: 1px solid black;
    margin: 0;
}
ul {
    list-style: none;
    padding: 5px;
    margin: 0;
}
#container ul li {
    font-size: 1em;
    font-weight: bold;
    border-bottom: 1px dashed blue;
    position: relative;
}
span {
    width: 100px;
    display : inline-block;
}
em {
    position: absolute;
    bottom: 0;
    right: 0;
}

Regarding your second query...

Why isn't relative positioning effective?

In reality, relative positioning is functioning properly. In the normal flow of content, it aligns appropriately within the layout. The line breaks occurring in your description are due to the constrained margin settings in your span.

Nonetheless, the em element can still be adjusted using position: relative. Experiment with different values apart from 0. By altering these values, your prices will collectively shift up or down based on positive or negative inputs.

Your CSS rule:

em {
  float: right;
  position: relative;
  bottom: 0px;

/* Experiment with these individually:
bottom: 25px;
bottom: -25px;
right: 25px;
right: -25px */
}

For further insights into positioning techniques, refer to the position article on MDN.

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 Custom CSS Within a Parent CSS Element

After implementing a Wordpress Theme, I encountered the need to customize the styles of one of its elements. (This particular element displays our tours automatically) Currently, the style is as follows: .tourmaster-tour-grid .tourmaster-tour-content-wrap ...

Discovering the method to access a file without the standard .html or .php extension on the local host at 127.0.0

Is there a way to access my file without the .html or .php extension? My folder structure looks like this: C:\Apache24\htdocs\search-html\test search-html test low.html high.html ...

Establishing a connection between Python and MySQL on a Windows operating system using

Here is the source code I am working with: import pymysql import socket conn = pymysql.connect(host='127.0.0.1', unix_socket='/tmp/mysql.sock', user='user', passwd=None, db='extractor') cur = conn.cursor() cur.execu ...

Detecting media queries for the Samsung Galaxy S3 and S4 devices

I have been working on implementing media queries to show text boxes with different styles for iPhone 5, iPhone 6, Samsung Galaxy SIII, and Samsung Galaxy S4. Initially, everything was working fine for iPhone 5 and iPhone 6, but when I switched from an i ...

How to prevent table row from highlighting on hover in Angular Bootstrap

In my project, I have a table that showcases various details and allows users to select certain rows. Some of these rows have child rows which can also be selected. The requirement is for the parent rows to be selectable only if they do not have any child ...

How float elements can affect z-index through opacity

While troubleshooting the alignment of my floated images, I came across an unexpected issue related to opacity: http://jsfiddle.net/tshwbnLo/ It appears that when an element has opacity, it does not adhere to the usual behavior and instead overlaps the f ...

Show information from an array

On the index.php page, there is a script that retrieves data from demo.php and presents the outcome in a div. <div class="leftbox"> <?php echo "<div id='proddisplay'>"; echo "</div>"; ?> </div&g ...

Adjusting Picture Sizes to Fit the Dimensions of a Photo Frame

Currently, I am in the process of developing a website that incorporates two distinct image types: Portrait photos https://i.sstatic.net/rmIXQ.jpg Landscape photos https://i.sstatic.net/Z7mr3.jpg As you can see, there are two different categories of ...

Exploring the art of beautifying React.js with SCSS

I have been working on creating the homepage layout for a project using SCSS to style Reactjs with the assistance of an online course. However, I am facing an issue where my layout looks different from the one presented in the course even though I have app ...

What could be causing ReactNative Dimensions component to display lower resolutions?

Currently, I am developing an application using React Native v0.45.1 and testing it on my S6 device. Despite setting a background image that matches the resolution of my phone, it appears excessively large on the screen, cutting off most of the content. T ...

Dynamically obtaining the screen width in JSP using a variable

Could someone provide assistance on how to resize an image using an img src="blah.jpg?width=x" so that my page can be displayed at various sizes? I just need x (width) as a JSP variable. Update 2021: It's been 9 years since this question wa ...

A single button that performs multiple actions with just one click

Summary: Seeking assistance in implementing a button that triggers three different actions upon being clicked thrice. Detailed description: On one page of my website, there is an introduction with text and images. I currently have a button that switches t ...

CSS - setting the background-image property is not displaying the image

.main_banner { background-image: url("../images/main_banner.png"); width: 100%; height: auto; } <!DOCTYPE html> <html> <heading> <title> ********** </title> <link rel="sty ...

identify when the bottom of a container is reached during scrolling through a window

On my website, I have a section with products displayed in the center of an HTML page. I am looking to implement an AJAX call that will load additional products when the user reaches the bottom of this product container by scrolling down. How can I detec ...

Issue with CSS3 animations

.button { background-color: #4CAF50; /* Green */ border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; ...

Include content within the navigation bar of Bootstrap 3.3

I am trying to insert text at the end of the header in a jumbotron template: <div id="navbar" class="navbar-collapse collapse"> <form class="navbar-form navbar-right"> <div class="form-group"> <input type="text ...

Tables lacking borders where rowspans are present

I'm currently using Firefox and have the following code snippet: html, body { width: 100%; height: 100%; } * { box-sizing: border-box; } body { padding-left: 20px; padding-right: 20px; } .visitentabelle { border: 2px solid black; ...

Differences between encoding URL variables in HREF and using JS window.location for onclick events

For some reason, this particular hyperlink is not functioning properly. I have a Javascript redirect (window.opener.location) where I pass several variables through the URL. The problem arises when these variables contain apostrophes. In PHP, I am using UR ...

Show the total sum of a specific column in a datatable and enable the option to export the data to an Excel

When exporting a datatable as an Excel file with 4 columns, the third column contains product prices. After the export, I would like to see an additional row at the end of the table labeled "Total" that displays the sum of all values in column 3. I initia ...

What is the best way to expand and collapse a mat-expansion-panel using a button click

Is it possible to expand a specific mat-expansion-panel by clicking an external button? I've attempted linking to the ID of the panel, but haven't had any luck... <mat-expansion-panel id="panel1"> ... </> ... <button (click)="doc ...