Having trouble with aligning text using the span tag?

I'm attempting to create a line for pricing where the text-align is aligned differently for each item - left, center, and right.

Despite using text-align, I am unable to maintain all three items on the same line. Is there an alternative solution or workaround for this issue?

Initially, I approached it like this:

.price span {
  display: inline;
}

.previous-price {
  text-align: left;
}

.present-price {
  text-align: center;
}

.discount-price {
  text-align: right;
}
<p class="price">
  <span class="previous-price"><s>Tk. 350000</s></span>
<span class="present-price">Tk. 227500</span>
<span class="discount-price">(35% OFF)</span>
</p>

Answer №1

<span> is considered an inline element, meaning it will take on the width of its content. On the other hand, <p> is a block element, thus its width will always be that of its parent container. Consequently, applying text-align: center will only center align the <span> elements within the <p>.

If you wish to have one item aligned left, another in the center, and a third item on the right side,

  1. You can utilize the flex method mentioned above, which will position everything towards the edges.

  2. Alternatively, you can set the display to inline-block and specify width percentages in your CSS. Moreover, after setting it to inline-block with a fixed width, you can apply text-align center inside.

Answer №2

Check out my response on the issue with CSS text-align not working

    .previous-price {
        float: left;
    }
    .present-price {
        text-align: center;
    }
    .discount-price {
        float:right;
    } 
    <p class="price">
        <span class="previous-price"><s>Tk. 350000</s></span>
        <span class="discount-price">(35% OFF)</span>
        <div class="present-price">Tk. 227500</div>
        
    </p>

Additionally, it's important to ensure your HTML is correct. Using

<span class="present-price">Tk. 227500</p>
is considered invalid code. Even though most browsers may be forgiving, it's best practice to strive for accuracy in your coding.

Answer №3

An easy fix is to transform the .price element into a flex-container using these settings:

.price {
  display: flex;
  justify-content: space-between;
}
<p class="price">
  <span class="previous-price"><s>Tk. 350000</s></span>
  <span class="present-price">Tk. 227500</span>
  <span class="discount-price">(35% OFF)</span>
</p>

(By the way: You mistakenly closed your <span> tags with closing </p> tags - I corrected it in my fiddle)

Answer №4

It is recommended to avoid using display:inline for the .price span and instead assign it a fixed width, either in pixels (px) or as a percentage.

For example:

  .price{
    width:100%;
  }
  .price span {
        display: inline-block; /* also experiment with block and inline-block */
        width:150px;
    }
    .previous-price {
        text-align: left;
    }
    .present-price {
        text-align: center;
    }
    .discount-price {
        text-align: right;
    } 
    <p class="price">
        <span class="previous-price"><s>Tk. 350000</s></span>
        <span class="present-price">Tk. 227500</span>
        <span class="discount-price">(35% OFF)</span>
    </p>

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

A situation where the event onclick fails to occur within NextJS

index.js: function Home () { return <div> <html> <head> <title>Site</title> </head> <body> <div class= 'v5_3' onclick = "func_click()"></div> </b ...

Troubleshooting: Django project not functioning properly post transfer from Windows to Mac

Using Django 3.1.7 on a MacBook Pro now, I recently moved the Django project from a Windows 10 machine. Despite setting up the database and superuser again, running pipenv sync didn't result in serving any of my URLs or Admin page CSS styles. The serv ...

Optimizing Title Tags for Static Pages with Header Inclusion

Working on developing static web pages for a client who preferred not to use a content management system. These pages all have a shared header and footer php files. I am in need of creating unique title and meta description tags for each page, but I am un ...

Adjust the dimensions of the initial cell

I need to adjust the size of the initial "generated" cell in a grid. The grid is not present in the HTML markup until JavaScript prints RSS information on it, making it difficult to target specific rows or cells directly. Note: The first element is hidden ...

How to hide offcanvas navigation bar with one click in Bootstrap 5

There was an issue with a Bootstrap 5 project where the offcanvas menu would remain open after clicking on an anchor link. Is there a way to automatically close the offcanvas menu after clicking on an anchor link? <nav class="navbar fixed-top py ...

What exactly is the significance of "hash" in terms of Internet Explorer style

After downloading a sample CSS file, I noticed the following entry: #text-align: right; The comment beside the entry mentioned that this expression is specifically for justifying it in IE, while text-align: right; works for Safari and Chrome. My questi ...

Utilizing React JSX within HTML structures

Attempting to integrate React into a basic HTML file, I came across an official guide on this website. However, I am struggling to grasp how to render a JSX-encoded component and how to nest one component within another: class LikeButton extends React.Co ...

The margin-top property in CSS is not functioning as intended for a specific pixel value when applied to

I'm having trouble with positioning an icon using margin-top: -35px; under #menu. When I click on the icon, the side navigation bar doesn't work properly. However, if I adjust it to -15px, the side navigation bar displays correctly. Can someone h ...

Having issues with Inline-Block functionality?

I am facing alignment issues with an image and a container placed next to each other. The rest of my website uses inline-block without any problems. If someone could provide guidance on how to resolve this, it would be greatly appreciated. Below is the s ...

What is the best method for adding a background image to a jumbotron in my Django project?

I have been struggling with this issue for some time now. Currently, I am working on a Django project and I need to add an image as the background in the jumbotron section. home.html {% extends 'blog/base.html' %} {% load static %} {% bl ...

Tips on embedding an HTML page within another HTML page by utilizing a custom directive in AngularJS

I am looking to utilize custom directives in order to insert one HTML page into another. How can I achieve this? The custom directive code is as follows: (here is the .js file) fbModule.directive('fbLogin', function(){ return { ...

Bizarre actions with jQuery append in Internet Explorer 8

Issue with adding elements to a div in IE8: The element is not added until the button is clicked twice, resulting in two new elements being added at once. Here's the code snippet in question: $(options.addButton).click(function(event) { var proto ...

Tips for utilizing the font-family style to span multiple columns

I recently discovered that you can set the background color across table columns using the <colgroup> tag. table { border: 2px solid; border-collapse: collapse; } td { border: 2px solid; } <ta ...

Passing dynamic scope from Angular to a directive is a seamless process

I am working with a directive that retrieves an attribute value from an http call in the following manner: Controller app.controller("HomeController", function($scope){ $http.get("/api/source").then(function(res){ $scope.info = res.data }); }); ...

6 Steps to Extract Data from a POST Request Using JavaScript

I have a form that includes a text field and a select field. When the form is submitted, I am posting it. Can someone guide me on how to extract the form data on the server using JavaScript? <form id="createGameForm" action="/createGame" method="post" ...

Retrieve the file name of the webpage from the URL bar

Is there a way to retrieve the page name from the address bar using jquery or javascript instead of PHP? I am working on an HTML website and would prefer not to use PHP for this specific task. For example, if the address is www.mywebsite.com/hello.htm, ho ...

Need inspiration for testing web content with Protractor?

Exploring new possibilities for testing web content with Protractor. Any fresh ideas on what else can be tested? So far, I've checked links to ensure they redirect correctly. I've also verified text color and decoration changes when hovering ove ...

The overflow of CSS text selection color spills beyond the boundaries of the box

I'm just starting to learn CSS and was wondering if there is a way to prevent the text selection color from extending into the 'gutter' (I believe that's the correct term) like shown here: It may seem trivial, but I've observed th ...

Control the button's activation status by accepting or unaccepting the two checkbox conditions

In my search through stackoverflow for help on enabling/disabling a button conditionally, I found some assistance but nothing quite matched what I needed. My situation involves two checkbox conditions rather than just one. The button should only be enable ...

Creating a CSS template for organizing sidebar and footer divisions on a webpage

I am facing an issue with the layout of my website. I have a container that is positioned relatively and houses various divs for header, sidebar, main-content, and footer. The problem lies with the sidebar and footer components. The sidebar has been posit ...