What is the best way to position three DIVs next to each other within another DIV while aligning the last DIV to the right?

I need help formatting a simple list item with three DIVs. The first DIV should be left justified, the second should be able to grow as needed, and the third should be right justified. I currently have them stacked side by side, but can't get the last DIV to right justify. Here's what it looks like now:

https://i.stack.imgur.com/nmPRN.jpg

<li *ngFor="let script of scripts" [class.selected]="script === selectedScript" (click)="onSelect(script)">
      <div class="container">
        <div class="left">
          <span class="badge">{{script.scriptOrderID}}</span>
        </div>
        <div class="center">
          <span>{{script.scriptName}}</span>
        </div>
        <div class="right">
          <span class="badge2">{{script.scriptID}}</span>
        </div>
      </div>
    </li>

This is how I want it to look: https://i.stack.imgur.com/Kw05U.jpg

.container { 
display: flex;
}

.left { 
width: 70px;
}

.center{ 
display: flex;
}

.right{
}

Answer №1

Check out flexbox - it's user-friendly and using flex-end will quickly position your blocks at the end.

<div class="line">
  <div class="first_block">

  </div>
  <div class="text">
      Hello
  </div>
  <div class="second_block">

  </div>
</div>


.line {
  height : 40px;
  width : 100%;
  display : flex;
  flex-flow : row wrap;
  background-color : gray;
}

.first_block, .second_block {
  background-color : blue;
  width : 40px;
}

.text {
  flex-grow : 1;
}

JSFIDDLE for a visual demonstration.

Cheers!

Answer №2

You have the option to simplify your code without using multiple divs inside each list item. Instead, you can structure your list items as tables and use spans inside them as table cells:

* { margin: 0; padding: 0; list-style: none; box-sizing: border-box; }

ul {
  width: 100%;
}

li {
  width: 100%;
  display: table;
  margin-bottom: 2px;
}
li > span {
  display: table-cell;
  background: lightgrey;
}
li > span:first-child,
li > span:last-child {
  background: black;
  color: white;
  width: 3em;
}
<ul>
  <li>
    <span>60</span>
    <span>Click Add a Printer</span>
    <span>53</span>
  </li>
  <li>
    <span>70</span>
    <span>Click "The printer I want isn't listed"</span>
    <span>54</span>
  </li>
  <li>
    <span>80</span>
    <span>Select "Add a printer using a TCP/IP address or hostname" Click Next</span>
    <span>55</span>
  </li>
</ul>

Flexbox alternative

* { margin: 0; padding: 0; list-style: none; box-sizing: border-box; }

ul {
  width: 100%;
}

li {
  width: 100%;
  display: flex;
  flex-wrap: nowrap;
  justify-content: space-between;
  margin-bottom: 2px;
}
li > span {
  background: lightgrey;
  flex-grow: 1;
}
li > span:first-child,
li > span:last-child {
  background: black;
  color: white;
  width: 3em;
  flex-grow: 0;
}
<ul>
  <li>
    <span>60</span>
    <span>Click Add a Printer</span>
    <span>53</span>
  </li>
  <li>
    <span>70</span>
    <span>Click "The printer I want isn't listed"</span>
    <span>54</span>
  </li>
  <li>
    <span>80</span>
    <span>Select "Add a printer using a TCP/IP address or hostname" Click Next</span>
    <span>55</span>
  </li>
</ul>

Answer №3

If you're looking to create a layout with fixed width divs on the sides and a center div with fluid width, you can achieve that using the code snippet provided below.

Simply have div containers for the left and right sections with specified widths, and then use a center div with a dynamic width calculated based on the total width of the container minus the sum of the left and right div widths.

.container > div {
float: left;
padding: 0 10px;
}

.left{
width: 30px;
}
.right {
width: 30px;
}

.center {
width: calc( 100% - 120px);
background: #ccc;
}
<div class="container">
        <div class="left">
          <span class="badge">23</span>
        </div>
        <div class="center">
          <span>test</span>
        </div>
        <div class="right">
          <span class="badge2">44</span>
        </div>
      </div>

Answer №4

If you utilize the power of flexbox, achieving this layout is quite simple:

.container {
  line-height: 50px;
  background: #e1e1e1;
  display: flex;
  justify-content: space-between;
}
.center {
  flex: 1;
}
.badge,
.badge2 {
  display: block;
  background: purple;
  color: white;
  text-align: center;
  padding: 0 15px;
}
<div class="container">
  <div class="left">
    <span class="badge">*</span>
  </div>
  <div class="center">
    <span>The Name will grow</span>
  </div>
  <div class="right">
    <span class="badge2">**</span>
  </div>
</div>

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

Display alternative navigation paths to the user in Angular that differ from the original routes

I am currently developing a full stack web application using Angular, Node (Express), and mySQL. I am looking to display a different route to the user than the actual one. Is there a way to achieve this? For instance, let's say this is my dashboard pa ...

Disabling specific time slots in the mat select functionality

I have a scenario where I need to display time slots at 30-minute intervals using Mat Select. export const TIME=["12:00 AM","12:30 AM","01:00 AM","01:30 AM","02:00 AM","02:30 AM","03:00 AM&qu ...

How can I align Javascript output vertically in the middle?

I am currently facing an issue with a JavaScript clock displaying in a narrow frame: <!DOCTYPE html> <HTML> <HEAD> <TITLE>Untitled</TITLE> <SCRIPT> function checkTime(i) { if (i < 10) { ...

Sharing information between Angular components

Having recently started using Angular, I'm facing an issue with passing an array to a different component that is not parent or child related. What I aim to achieve is: upon selecting rows from the first component table, a new view should open up disp ...

Unlocking the Power of Angular's Default Input Required Validation

Just aiming to utilize the default required validation functionality in HTML. This is the code I've implemented: <form (ngSubmit)="submit()"> <div class="login-container"> <ion-item> <ion-input ...

The font size on my website fluctuates up and down every time I refresh the page or navigate to a different one

I am currently in the process of updating my research lab's website using a Jekyll framework with Bootstrap. However, I have encountered an issue where the font size grows slightly whenever I navigate to a new page or refresh the current one, before r ...

Introducing HTML elements into pre-existing web pages

My interest lies in the idea of injecting HTML into existing web pages for enhanced functionality. Specifically, I am exploring the concept of creating a more efficient bookmarking system. As someone new to web development, I am unsure of how to achieve th ...

Arrange divs on the screen in a specific formation

I am exploring ways to position specific divs in the middle of the screen to create a pearl necklace-like shape. The desired shape is as follows: 0 0 0 0 0 0 0 0 0 0 My aim is to achieve this using jQuery on a mobile ...

The drop-down menu keeps flickering instead of remaining open when clicked

I am facing an issue with a dropdown menu in my webpage. The menu is initially hidden, but should become visible when the list element containing it is clicked. I have written JavaScript code to add a class that changes the visibility property to visible u ...

Sending various values via a click in an HTML link

Hello, I am attempting to pass multiple values using the HTML onclick function. I am utilizing Javascript to generate a table dynamically. var user = element.UserName; var valuationId = element.ValuationId; $('#ValuationAssignedTable').append(&a ...

View-only text area and Internet Explorer version 9 scroll bars

I'm encountering an issue with setting the readonly attribute on textareas. I am using JQuery to apply the attribute, as shown here: $("#mytextarea").prop("readonly", true); For CSS styling: textarea { width: 400px; height: 400px; } textarea[readon ...

Insert HTML elements into nested CSS classes

My goal is to utilize jQuery for looping through classes and adding text to an HTML element. I am currently working with the following example HTML: <div class="question"> <div class="title"> <strong>Here's the question ...

Ensuring that my divs remain stationary despite changes in the size of the browser window

Hey there! I'm new to web design/development and I've been working on a simple website to get the hang of things. I managed to make my webpage look good at full screen, but as soon as I resize the window, everything starts shifting around and it ...

execute the node application using the .desktop file

Hello, I am attempting to launch an application in Linux by double-clicking it. I have come across the .desktop file as a solution (I need this method because the app will be deployed on a Raspberry Pi and users prefer not to use the terminal). Here is wha ...

Trigger on the cancellation or completion of a nested observable

I'm seeking a way to detect if an inner observable was not successfully completed (due to everyone unsubscribing) and then emit a value in that scenario. Something akin to defaultIfEmpty, but the current solution isn't effective. A trigger exis ...

What is the best way to add a box shadow to just one side of an element?

Is there a way to apply an inset box-shadow to only one side of a div? I am specifically referring to an inset shadow, not the usual outer box-shadow. For instance, in the JSFiddle below, you can observe that the inset shadow is visible on all four sides, ...

Newbie: Troubleshooting Vue Errors - "Vue is not recognized"

I'm currently at the beginning stages of learning Vue and am practicing implementing it. However, I keep encountering the errors "vue was used before it was defined" and "Vue is not defined". Below are excerpts from my HTML and JS files for reference. ...

Leveraging the Power of jsPDF in Your Google Apps Scripts

Is it feasible to integrate jsPDF into a Google Apps Script project? I am attempting to create documents using templated HTML, but certain CSS styles like background-color do not translate to PDF when using the getAs() function. Given the limitations of ...

I am looking for an image search API that supports JSONP so that users can easily search for images on my website

I am currently in the process of creating a blog platform. My goal is to allow users to input keywords on my site and search for images directly within the website. This way, I can easily retrieve the URL of the desired image. ...

Refresh gif without having to reload it in Internet Explorer 11

I'm attempting to create a feature where a gif restarts when clicked by the user, without needing to reload it (due to the heavy size of the gif which is preloaded for my application). The current code functions flawlessly on Chrome and other "modern ...