Incorporating a technique for aligning text towards the right as the number of characters or digits expands

I'm designing a game user interface with a money indicator. Let's imagine the player has 0 dollars in their wallet, it should appear like this:

https://i.stack.imgur.com/35eoh.png

Please note that it is located in the upper right corner.

The position of the text is as follows:

right: 12.00vw;
top: 1.7vw;

Now let's go unrealistic and assume the player has 50,000,000 dollars in their pocket. It would look like this instead: https://i.stack.imgur.com/Wugop.png

However, it should actually appear like this:

https://i.stack.imgur.com/quw3s.png

So, my question is, how can I make the text move towards the right direction when the amount of money in the pocket increases?

Here's the code snippet:

<div class="moneyhud"></div>
    <div ID="hud">
        <div id="top">
        <span id="text4">50000000 $</span>  <!-- Money -->      
    </div>
 </div>

#text4 {
    font-size: 1.5vw;
    color: rgb(53, 143, 73);
    position: absolute;
    right: 12.00vw;
    top: 1.7vw;
    text-shadow: 4px 4px 10px #313131;
}

.moneyhud {
    position: absolute;
    background-image: 
    url("https://img.rehmann.online/a/fastlife/gruengeld.png");
    background-repeat: no-repeat;
    right: 1.00vw;
    top: 2vw;
    z-index: -1;
    height: 30px;
    width: 288px;
    color: #fff;
}

Answer №1

#text2 {
    font-size: 1.2vw;
    color: rgb(255, 0, 0);
    position: absolute;
    right: 15.00vw;
    top: 2vw;
    text-shadow: 2px 2px 5px #000000;
}

.boxhud {
    position: relative;
    background-image: 
    url("https://img.rehmann.online/a/fastlife/bluebox.png");
    background-repeat: no-repeat;
    right: 2.00vw;
    top: 3vw;
    z-index: -1;
    height: 20px;
    width: 200px;
    color: #fff;
}
<div class="boxhud">
    <div id="hud">
        <div id="top">
          <span id="text2">$100</span>  
        </div>
    </div>
 </div>

That is the code you currently have. Notice how you're relying on hard-coded values to display dynamic content. Try something like this:

#text2 {
    font-size: 14px;
    color: rgb(255, 0, 0);
    text-shadow: 2px 2px 5px #000000;
}


.boxhud {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    position: absolute;
    top: 25px;
    right: 12px;
    height: 20px;
    width: 200px;
    background-image: 
    url("https://img.rehmann.online/a/fastlife/bluebox.png");
    background-repeat: no-repeat;
    color: #fff;
}
<div class="boxhud">
    <span id="text2">$100</span> 
 </div>

This should give you a rough idea of how to flex-box it

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

Encountering issues with custom.css.scss not being properly overridden in Rails while using Bootstrap

My Configuration I made modifications to the .col-lg-4 class in Bootstrap for Rails and added them to my custom.css.scss file after the import statements to remove padding completely. The Issue at Hand I am trying to override the styling of .embed-respo ...

What are the benefits of utilizing the useStyles MaterialUI function instead of traditional CSS?

As I develop my MERN application, I am utilizing Material UI components along with inline attributes. However, I find myself questioning the necessity of creating a JavaScript file just to import the makeStyles function, which essentially outputs an obje ...

The error "relative error: invalid property value" occurs when setting the width and height of a parent element

I'm having trouble getting my h2 text to appear above my images when hovered over. I keep receiving an error message in Chrome Developer Tools saying that "display: relative" is not a valid property value, but I can't figure out why. I've se ...

Add flair to the ButtonBase element within a Tab component

I'm currently exploring how to override Material UI styles with a nested component. For instance, what if I want to increase the bottom border height on an active Tab, which is applied by the underlying ButtonBase. Below is the style definition: con ...

I need to remove the mobile view of my Angular application because it is not optimized for smaller screens

I have implemented Angular in my web application, but unfortunately it is not mobile responsive. In order to address this issue, I would like to disable the mobile view and instead display a message informing users that mobile view is not supported for t ...

Internet Explorer is automatically inserting extra vertical space after the first list item in a basic menu design

Does anyone else have trouble with IE? (Or is it just me?) I'm creating a list with a background. The first and last LI elements are 5px high, while the others are 16px high. However, in IE there's a strange blank space between the first and se ...

What is the best way to split two divs in this manner?

Is there a way to split two divs like this in such a manner using TailwindCSS? Alternatively, what would be the CSS code for dividing these two divs? I am struggling with this concept and cannot wrap my head around how it can possibly be achieved. ...

Struggling to select the accurate end date from the datepicker while using a Python script

I'm currently using the Google Chrome browser and running a Python script to automate date selection in a datepicker. However, I'm having an issue with selecting the correct end date. The script keeps choosing "02/01/2022" as the end date, wherea ...

Determining height of an element in AngularJS directive prior to rendering the view

As I dive into the world of Angular, any assistance is greatly welcomed. My goal is to vertically align a div based on screen size (excluding the header and footer) when the page loads, the window resizes, and during a custom event triggered by the user ex ...

`Count the number of rows needed to accommodate text line breaks within a div element`

Is there a method to determine the number of lines that text breaks into using the CSS property word-break: break-all? For example, if I have a div like this: <div>Sample text to check how many lines the text is broken into</div> And the corr ...

Each time a nested collapse occurs, it triggers its parent collapse

I have come across some similar answers, but they all pertain to accordions, which I am not utilizing in this case. In my code, I have nested collapse items. You can view the code snippet here on CodePen $('#collapseOutter').on('show.bs. ...

The function getSelection().focusNode does not function properly within a specified ID

My current code allows for text to be bolded and unbolded using Window.getSelection(). I found the initial solution here: Bold/unbold selected text using Window.getSelection() It works perfectly without any issues. However, when I tried to modify the code ...

Always ensure that the horizontal scrollbar is visible without the need for vertical scrolling

I'm attempting to create a data grid-style layout where the left side is fixed and only the right side is scrollable. However, the entire div needs to be vertically scrollable. The concept is functioning correctly, but the horizontal scrollbar appears ...

Text area capacity

Is there a limit to the maximum capacity of a textarea for accepting text? The HTML page functions correctly when the text is limited to around 130-140 words. However, if the text exceeds this limit, it causes the page to hang without any response. The tex ...

Using jQuery's .html() method will remove the selected attribute from the dropdown menu option

When generating HTML dynamically, a select element is included. After choosing an option inside it, everything works fine. For example: <div id="test"> <select> <option value="1">1</option> <option value="2" selec ...

Achieving nested classes in CSS: a comprehensive guide

I am encountering an issue with my CSS while trying to create a dropdown on hover. The problem seems to be related to nested classes inside a div structure. It appears that the dropdown list is not displaying properly, leading me to believe there might be ...

Vuetify's data table now displays the previous and next page buttons on the left side if the items-per-page option is hidden

I need help hiding the items-per-page choices in a table without affecting the next/previous functionality. To achieve this, I've set the following props: :footer-props="{ 'items-per-page-options':[10], &apo ...

Difficulty viewing image in Firefox using HTML <img> tag

I'm encountering an issue with rendering an img tag in an HTML page. The img source is a file located on a remote server. Surprisingly, when attempting to display the image in Firefox using: file://///server/folder1/folder2/name.jpg it shows up prop ...

What causes the parent element's click event to trigger on its own when a child textbox is clicked or focused in Safari?

Describing my HTML setup [Checkbox] [Text] [Input element (Display if check-box is marked)] <span class='spGrpContainer'> <label id='stepGH_Group1' class='lblStep GHSteps lblGroupheader' stepid='1' st ...

`Can you guide me on the proper path for designing this website layout?`

As I embark on creating my first website, I have a specific vision in mind. I want the full display on a computer screen to show all the information and links without any need for scrolling. However, when the window is resized to smaller screens, I would l ...