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

Is it possible to filter JavaScript Array while ensuring that select options do not contain duplicate IDs?

I am utilizing two datatables with drag and drop functionality. Each row in the datatables contains IDs. When I drag a row from table 1 to table 2, the data is stored in an Array. I have an addArray function that pushes the IDs into the Array, filters out ...

Tips for showcasing two pieces of content next to each other across the entire webpage using a combination of Bootstrap, CSS, and Laravel

I am facing a challenge with displaying two contents side by side on a full page and stacking them vertically when the screen size is small (mobile). I have managed to achieve the side by side display on a full page, but they stack on top of each other on ...

The correlation between dynamic pricing and surge pricing in relation to PX

I'm exploring the translation of measurements from dp and sp to pixels within Google's new material design for a website I'm working on. Usually, my web designs have used pixel-based measurements for both grid and font sizing over the four y ...

links contained within a single span inside an anchor tag

I am attempting to place all three spans within an anchor tag, inline. I am not certain what is missing from the code: a.facebook-button { float: none; display: inline-block; margin-bottom: 5px; left: auto; text-shadow: 0 -1px 1px rgba(0, 0, 0 ...

The document type is not compatible with the element "BR" in this location; it is assumed that the "LI" start-tag is missing

How can I create a validated list with HTML 4.01 standards? I tried the following code, but it's giving me an error: "document type does not allow element 'BR' here; assuming missing 'LI' start-tag" <tr> <td colspan="2" ...

Tips for creating an HTML table that fills the entire width of its parent element:

One of the rows in my table contains a nested table. I want both tables to fill up 100% of the width of the parent element. How can I achieve this? Check out the demo here Here's the HTML code: <div class="container"> <table> <tr ...

Adjusting the height of a row in Bootstrap can sometimes result in column content overflowing

.row-2 { padding: 20px; margin: 100px auto; box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px; border-radius: 10px; height: 300px; } .fa-solid { ...

What is the best way to insert an image between two sections using CSS?

As I work on a new webpage, I find myself stuck (yes, I'm a beginner :-) ). I have a header followed by a section, and I would like to place images in between the two. However, despite my attempts to position the images, they keep getting hidden "und ...

Unusual CSS inconsistencies between running on VS Web Server and IIS

My current dilemma involves a discrepancy in the appearance of my site when viewed locally through the visual studio web server (http://localhost:3452/) compared to when accessed on IIS7 (http://server/myproject/). Initially, I suspected a CSS issue causi ...

Are there any reliable PHP classes available to generate HTML tables efficiently?

Searching for an advanced PHP class that can effortlessly create intricate HTML tables, including the ability to handle colspan/rowspan and assign specific CSS classes to rows, columns, and cells. ...

When the React button is clicked, it displays a stylish blue border

Hey there, I'm currently working on a project using React and facing an issue with some buttons I've styled. Whenever I click on them, they show a strange blue border that I can't seem to get rid of. I've tried setting border:none !impo ...

Tips for avoiding word fragmentation in <pre> code blocks

pre { white-space: pre-wrap; /* CSS 3 */ white-space: -moz-pre-wrap; /* Mozilla, since 1999 */ white-space: -pre-wrap; /* Opera 4-6 */ white-space: -o-pre-wrap; /* Opera 7 */ word-wrap: break-word; /* Internet Explorer 5.5+ */ overflo ...

The collapsing z-index menu in Bootstrap 4 is experiencing CSS issues and is not functioning correctly

I have a bootstrap 4 menu with a slide effect that is functioning correctly, but there seems to be an issue with the z-index value I have set for it. Specifically, there is some content within a parallax jumbotron that is overlapping the menu links. I need ...

What is the best method for utilizing the CSS :target selector in order to generate a versatile modal box?

I am looking for a way to display my vast collection of proverbs from an endangered language without overcrowding the page. I have implemented a modal box solution using CSS :target selector, which expands a hidden div element when clicked on. However, I n ...

The PurgeCSS CLI does not generate CSS files beyond the command line interface

I'm struggling to successfully extract my CSS using purgecss from the command line and save it to a separate file. The instructions on PurgeCSS.com are vague: By default, the CLI only outputs the result in the console. To save the purified CSS files ...

Can you answer this straightforward query about CSS positioning?

I am currently facing a challenge in creating a small div (header class) that overlays a main banner image div (banner class). When I maximize my browser window, the positioning is correct. However, upon resizing the browser, the header div maintains its m ...

"Bootstrap - creating a dynamic effect with a repeating pattern overlay and vibrant background color in

I'm having trouble adding a repeating background image to my jumbotron that already has a rebecca-purple background color. I've tried multiple approaches, but can't seem to get it right. Here's the code snippet I'm working with: . ...

What is the best way to eliminate duplicate values from a column in a data set

Time To Status Off-Track On-Track 10:28 AM gf 6233 4 4 10:32 AM dd 3835 7 10:40 AM ss 3235 4 10:43 AM ww 6621 5 11:06 AM zz 3837 ...

Tips for preserving a circular element shape while accommodating dynamic content

I'm attempting to generate a circular shape using the ::after pseudo element, which adjusts its size automatically based on the content inside. .container { display: flex; flex-direction: row; } #dividerHost2 #left { flex: 1 1 auto; ...

Easily Update Your Div Content by Simply Clicking a Single Link/Button

I am in need of assistance here. My goal is to display dynamic content within a div. Below you will find the code I currently have: <script type="text/javascript"><!-- function AlterContentInContainer(id, content) { var container = documen ...