Positioning two labels within a Div container

Struggling to align two labels within a div using ASP.NET, where the length of the data displayed can vary.

The challenge I'm facing is getting the first label to align to the left and the second label to the right. Since the data displayed is queried from a database and can change in length, it's causing alignment issues.

Here is the code snippet:

ASP.NET/HTML

<div class="results" id="divResult1" runat="server" visible="false">
        <asp:Label ID="lblResult1Desc" runat="server" Text="" CssClass="lblResults1"></asp:Label>
        <asp:Label ID="lblResult1Val" runat="server" Text="" CssClass="lblResults2"></asp:Label>
    </div>

CSS

.results {
    height: 30px;
    background-color: #ffffff;
    vertical-align: middle;
    line-height: 30px;
    margin-top: 10px;
    border: solid 1px white;
    border-radius:10px;
}

.lblResults1 {
    font-size: 1.1em;
    color: #000000;
    float: left;
    width: 50%;
    margin-left: 10px;

}

.lblResults2 {
    font-size: 1.1em;
    color: #000000;
    float: right;
    padding-right: 25px;
    width: 50px;

}

In the image provided below, you can see how a long value for the second label (under description B) causes overlap at the end of the div. On the other hand, if there is a short value like in description A, there appears to be excessive white space after it.

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

Considering that the lengths of descriptions can vary but should easily fit into the available space up to the £ sign, I need assistance with CSS to achieve left-aligned descriptions with some padding and right-aligned values with additional padding.

Answer №1

.results {
  display: block;
  box-sizing: border-box;
  background-color: #F4F4F4;
  margin: 10px;
  border-radius: 15px;
}
.results::after {
  content: '';
  display: table;
  clear: both;
}

.result {
  font-size: 1.1em;
  color: #000;
  width: 50%;
  float: left;
  padding: 10px;
  box-sizing: border-box;
}
.lblResults1 {
  width: 70%;
  float: left;
}
.lblResults2 {
  float: right;
  width: 30%;
  text-align: right;
}

.result--ellipsis {
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}
<div class="results" id="divResult1">
  <label ID="lblResult1Desc" class="result lblResults1">Lorem ipsum dolor sit amet lorem ipsum dolor sit amet lorem ipsum dolor sit amet</label>
  <label ID="lblResult1Val" class="result lblResults2">€ 12.735.430,9</label>
</div>

<div class="results" id="divResult1">
  <label ID="lblResult1Desc" class="result lblResults1 result--ellipsis">WITH ELLIPSIS Lorem ipsum dolor sit amet lorem ipsum dolor sit amet lorem ipsum dolor sit amet</label>
  <label ID="lblResult1Val" class="result lblResults2">€ 12.735.430,9</label>
</div>

Answer №2

increase padding in the result class

.result {
padding: 10px; /*this rule will also affect top and bottom */
}

eliminate left and right margins from lblResults1 and lblResults2

Answer №3

The problem lies in the fixed width set on .lblResults2, which is causing the issue. If your content exceeds the specified width - indicated by width: 50px; in your code, it will be cut off and prevent the padding from taking effect. Consider removing this width specification to resolve the issue.

Answer №4

One way to organize your layout is by using a flexbox design like the one shown below:

.results {
height: 30px;
background-color: #ffffff;
vertical-align: middle;
line-height: 30px;
margin-top: 10px;
border: solid 1px white;
border-radius:10px;
display: flex;
justify-content: space-between;
}
.lblResults1 
{
font-size: 1.1em;
color: #000000;
}
.lblResults2 {
font-size: 1.1em;
color: #000000;
}

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

Problems Arising with Javascript Animation Functionality

I've created a script for an interactive "reel" that moves up or down when clicking on specific arrow buttons. However, I'm encountering two issues: 1) The up arrow causes it to move downward, while the down arrow moves it upward. 2) After exe ...

CSS: Adjusting the position of tooltips to align with the triggering element (rest of code is fully functional)

Looking to create a custom tooltip without relying on plugins. The goal is to align the tooltip vertically to the right of the triggering element, in this case, an input field. The current setup (see Fiddle demo) achieves the desired layout and content fo ...

Incorporating sweetalert2 with the latest Bootstrap 4 framework

I am currently utilizing sweetAlert2 and attempting to integrate bootstrap 4 for button styling by implementing the following properties: buttonsStyling: false, confirmButtonClass: 'btn btn-primary btn-lg', cancelButtonClass: 'btn btn-lg&ap ...

What is the best way to invoke an HTML file using a jQuery function in a JavaScript file?

When I call an HTML file from a jQuery function using require(), I am encountering an issue where I am unable to link the CSS with the HTML file. After running the file, only plain HTML is displayed without any styling. The structure of my HTML file is as ...

Experiencing the 'zone not authorized for dns server' error while trying to utilize aspnet_regiis -i

Attempting to launch a .NET 4.5.1 asp.net application on my work computer (Windows 7 Enterprise) for the first time has been a challenge. Visual Studio prompted me to configure ASP.NET to utilize .NET 4.5, which led me to run aspnet_regiis -i in an adminis ...

My code gets disrupted when I switch between ids and classes

I'm currently learning about javascript and jquery, attempting to implement various scripts. I successfully executed the following script: jQuery(document).ready(function($) { var scroll_start = 0; var startchange = $('#homepage-header' ...

What is the best way to link CSS files from libraries that are downloaded using npm?

For instance, let's say I installed a package: npm install --save package and it gets saved in ./node_modules/package/ Inside that folder, there might be a directory named styles and within that directory, you could find style.css. How can I link ...

Steps for updating images and text on a live webpage after launching the web application

I added an image to a jsp file. The image, named kid.jpg, is downloaded and stored in the /resources/images folder as shown in the code snippet below. <li style="background-image: url('resources/images/kid.jpg');"> Now that my web app is ...

What are Google's thoughts on links that lack text but only have a background?

Here is an example: <a href="/" id="logo"></a> Alternatively, should I do this and adjust the font-size to zero: <a href="/" id="logo">Home</a> Update: It seems like nobody understands my question. Let me also share some CSS cod ...

What is the reason why the 'hide menu onscroll' JavaScript function doesn't perform flawlessly on Safari web browser?

Compatibility with different browsers: works on Firefox, Chrome, and Edge. However, on Safari, the menu (navbar) behaves strangely. It appears when scrolling up but hides (floats up beyond the window) when scrolling to the top of the page without any downw ...

Trouble with Updating InnerHTML

xmlHttp = new XMLHttpRequest(); xmlHttp.open( "GET", "myurlhere.php", true ); xmlHttp.send(); var display = document.getElementById("display"); display.innerHTML = xmlHttp.response; This snippet of code is part of a function triggered by a button click. ...

Styling is applied by Bootstrap to inputs in a form that are not required

I am currently working on validating a form for empty fields using Bootstrap. When submitting and validating the form with the form.checkValidity() method, I noticed that even the non-required fields are being styled as if they are required. Is this normal ...

Displaying SQL data in a table using PHP

I've encountered a dilemma where the category name is being outputted as the same for both offered and wanted categories in PHP. This issue arises when trying to display categoryName differently for each type of category in a table. Despite attempting ...

Troubleshooting the non-functioning collapse feature of Bootstrap 5's "Nav-bar"

Basic code snippet: <?php session_start(); if ((!isset($_SESSION['valid']) == true)) { unset($_SESSION['valid']); header('location:index.php'); } ?> <!DOCTYPE html> <html lang="pt-br"> <h ...

Determine the width of the window and adjust the positioning of jQuery UI tooltips accordingly

Struggling to adjust the jQuery UI tooltip position based on screen width, but can't seem to figure it out. Can someone assist me in detecting the browser's width and changing the tooltip position accordingly? [fiddle] $(function () { $(doc ...

I am experiencing a 404 error when attempting to import a local JS file in Angular

After creating a new project with "ng new xxx", all you need to do is add one line of code in index.html: <!doctype html> <html lang="en> <head> <meta charset="utf-8> <title>Bbb</title> <base href="/&g ...

Utilizing AJAX with jQuery to transmit information to a PHP script

I am trying to extract data from HTML using JavaScript, send it to PHP, and then receive the result back in JavaScript. After searching extensively, I have been unable to find a code snippet that works for my specific case. Here is the JavaScript code I ...

The JavaScript-set value in a form field is not being transmitted to the PHP script within the $_POST array

Struggling to pass a JavaScript value to a .php script, and then on to a .txt script. It works fine with regular numbers, but when trying with the variable it fails, leaving the .txt file blank. Despite extensive research online, I can't seem to get i ...

`The functionalities of classList.add and classList.remove aren't behaving as anticipated.`

I'm currently working on a list of items (ul, li) that have a class applied to them which adds a left border and bold highlight when clicked. My goal is to reset the style of the previously clicked item back to its original state when a new item is c ...

Passing values between a JavaScript function and a PHP script - Here's how!

I am looking to display the output of a PHP script in a div tag without having to refresh the page. I have a collection of .c files that users can compile on the server, and I want to list them in a dropdown like this: <option value="" selected="select ...