Applying CSS to align the first line to the left and right, while ensuring that wrapped lines are aligned to the

Struggling with CSS to align drink names and descriptions on a bar's menu. Want the name left aligned and the description right aligned, even if wrapped lines are also right aligned.

Current progress can be seen at http://jsfiddle.net/H96XQ/

CSS:

.alignleft {
   font-weight: 700;
   text-transform: uppercase;
   float: left;
   text-align:left;
}
.alignright {
   font-weight: 400;
   font-style: italic;
   float: right;
   text-align:right;
}

HTML:

<div id="textbox">
   <p class="alignleft">Old Fashioned</p>
   <p class="alignright">Bulleit Bourbon, Raw Sugar, Luxardo Maraschino Cherries, Soda, Orange</p>
   <div style="clear: both;"></div>
   <p class="alignright">Jameson Irish Whiskey, Coffee</p>
   <div style="clear: both;"></div>
</div>

The layout works for single line descriptions like Irish Coffee, but wider content doesn't wrap correctly - each part gets its own line.

A workaround is using consecutive "alignright" classes manually where wrapping should happen, which is tedious and not scalable with menu changes.

<div id="textbox">
   <p class="alignleft">Old Fashioned</p>
   <p class="alignright">Bulleit Bourbon, Raw Sugar,</p>
   <p class="alignright"> Luxardo Maraschino Cherries, Soda, Orange</p>
   <div style="clear: both;"></div>
   <p class="alignleft">Irish Coffee</p>
   <p class="alignright">Jameson Irish Whiskey, Coffee</p>
   <div style="clear: both;"></div>
</div>

Seeking assistance to achieve desired alignment without manual workarounds. Any help is highly appreciated.

Answer №1

VIEW LIVE DEMO

HTML Snippet

<p>Desired Output:</p>
<div id="textbox">
        <h3>Old Fashioned</h3>
        <p >Bulleit Bourbon, Raw Sugar, Luxardo Maraschino Cherries, Soda, Orange</p>
        <h3>O'Henry</h3>
        <p>Buffalo Trace Bourbon, Benedictine, Liqueur, Fever Tree Ginger Beer</p>
        <h3>Hemingway</h3>
        <p>Aged White Rum, Fresh Lime &amp;
         Grapefruit Juice, Maraschino Liqueur, Lucardo Maraschino Cherries</p>
        <h3>Irish Coffee</h3>
        <p>Jameson Irish Whiskey, Coffee</p>
    </div>

CSS Styling

* {
    margin: 0;
    padding: 0;
}
#textbox {
    width: 350px;
    margin: 10px auto;
    padding: 10px;
    border: 1px solid #cccccc;
}
#textbox h3 {
    font-family: 'Roboto Condensed', sans-serif;
    font-size:15px;
    font-weight: 700px;
    text-transform: uppercase;
    float: left;
    text-align:left;
}
#textbox p {
    font-family: 'Roboto Condensed', sans-serif;
    font-weight: 400;
    font-style: italic;
    color: #333;
    text-align:right;
}

Answer №2

To properly align elements with the classes alignleft and alignright, simply include the CSS rule display:inline;. Check out this Fiddle for a demonstration.

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

Sequentially iterate through elements based on their Data attributes

Assume you are working with HTML elements like the ones shown below <span class="active" data-id="3"> Test 3 </span> <span class="active" data-id="1"> Test 1 </span> <span class="activ ...

The system considers the resource as a script, yet it was transmitted with the MIME type of text

I am encountering an issue when trying to integrate AngularJS/JavaScript into my HTML document. Each time I try to load my index.html file, the following error message appears: Resource interpreted as Script but transferred with MIME type text/html: "http ...

Activating/Deactivating a button with just a press

In my SQL data display, each row has an "Add To Zip" button that is initially enabled. I want to disable the button in the current row when it's clicked and later re-enable them using a different PHP file. What's the best way to achieve this with ...

Attempting to utilize a Jquery Ajax post method to retrieve information from my specified URL and display it within my HTML Div element

Seeking assistance in fetching real-time data using a jQuery Ajax Post method. My experience lies mainly in SQL and C#, so coding in this language is relatively new to me. I was advised to use this script, which seemed straightforward. However, despite my ...

Issues with concealing the side menu bar in Vue.js

I've been attempting to conceal the side menu bar, with the exception of the hamburger icon when in the "expanded" state. Despite my efforts to modify the CSS code, I am still struggling to hide the small side menu bar. The following images represent ...

Sass does not compile for optimization

My Sass compiler generated an overly large file for me, but it could be much smaller!! I checked the compiler settings and didn't find anything wrong! The issue might be with the compiler itself. It's also possible that there is a problem with my ...

Choose children input textboxes based on the parent class during the onFocus and onBlur events

How can I dynamically add and remove the "invalid-class" in my iti class div based on focus events in an input textbox, utilizing jQuery? <div class="form-group col-md-6"> <div class="d-flex position-relative"> & ...

What is the best way to select an HTML tag element using the :v-deep() selector?

When I utilize the following: ::v-deep img { ... } it functions but triggers a deprecation warning: [@vue/compiler-sfc] ::v-deep usage as a combinator has been deprecated. Use :deep(<inner-selector>) instead. How can I achieve the same effect using ...

Effortlessly Styling Children Elements in Emotion JS When Parent Element is Hovered

This particular query seems to have been posed and resolved in various ways, but the solutions I've come across either do not pertain to Emotion or the responses related to Emotion haven't yielded results for me. I am currently using @emtion/[ema ...

Resolve issues with vue js checkbox filtering

As I embark on my Vue journey, I came across some insightful queries like this one regarding filtering with the help of computed(). While I believe I'm moving in the right direction to tackle my issue, the solution has been elusive so far. On my web ...

Line up with miniature stature

How can I prevent the image in this row from getting a margin at the bottom when I resize the window? <div class="row-fluid"> <div class="span2"> <img src="https://s3.amazonaws.com/media.jetstrap.com/SLgRUEHSyGwQVfG4x6ed_curso_a ...

Guide to generating a dropdown menu and linking it with data received from a server

I am completely new to Angular and recently received a project involving it. My task is to create a nested dropdown list for Json data retrieved from a server using Rest Api calls. The data contains a Level attribute that indicates the hierarchy level of ...

Adapting the colors of various elements throughout the entire webpage

My goal is to incorporate color-switch buttons on my webpage. When these buttons are clicked, I want the existing colors to change to different shades. However, my challenge lies in the fact that these colors are used for various elements such as backgro ...

Issues with the navigation and logo design in bootstrap mode

1: How can I adjust the size of the logo and name in my Bootstrap navigation? 2: My navigation menu is not showing up correctly. Can anyone help me troubleshoot? Navbar: <nav class="navbar navbar-expand-lg py-4 fig-nav"> <div class=&q ...

What is preventing images from displaying in my slider within baguetteBox?

Currently, I am in the process of incorporating a slider into my website using the baguetteBox library. After consulting the documentation, I successfully implemented an example that is functioning smoothly without any issues. In order to display a series ...

In JavaFX, when adjusting the transparency of a popup window, only the brightness changes while the solid color remains unchanged, effectively concealing

Is it possible to have a transparent popup window (errorDialog.fxml) on top of the main window (mainWindow.fxml) with 50% opacity while still showing the content of the main window underneath? My attempts at setting the background color of the overlay insi ...

The picture won't stay within the confines of the container

As I work on my fitness site, I wanted to incorporate a sponsor section. While exploring some code that matched the style of my website, I discovered this snippet below. However, being new to CSS, I struggled with fixing it to ensure that the images fit ...

What are the steps to transforming shorthand CSS into longhand?

Is there a tool available that can automatically convert shorthand CSS to longhand? I am in need of this functionality because I would like to utilize SmartSprites, which is not compatible with shorthand formatting. Additionally, if there is a tool that c ...

What are the steps to incorporate an iframe containing uniquely designed XML content?

I'm currently working on a website project for a client who specifically wants to include news from the BBC. Despite spending 3 hours searching online, I haven't been able to successfully insert an XML file into an iframe and style it to the clie ...

difficulty in obtaining information submitted through a form

I'm having an issue with my contact form. I tried testing to see if I can retrieve the values from the form, but it's giving me back: name: undefined I'm not sure why it's not working! Here is the code for the form: <form met ...