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

Bringing in typefaces to enhance your email signature

I am trying to design a unique email signature with a custom Adobe font. My email account is hosted by Strato, a German mail server. However, when I attempted to insert the signature using Apple Mail, I realized that the CSS <style> part was not bein ...

Troubleshooting material design CSS problem in AngularJS routing module

Attempting to incorporate material design with routing in angular js, but encountering issues with CSS design not working. Interestingly, when using bootstrap CSS, it functions properly. Check out the Plnker Demo here However, upon trying this approach, ...

Tips for avoiding the display of the overall scrollbar while utilizing grid with an overflow:

In my react-redux application, I am utilizing the material ui Grid component for layout design. The structure of my code is as follows: <div> <Grid container direction="row" spacing={1}> <Grid item xs={3}> ...

The file size exceeds the server's upload limit, despite making changes to the php.ini file

I've encountered a problem trying to upload an .OBJ file to the server, resulting in an 'Error 1' message. The uploaded file surpasses the upload_max_filesize directive specified in php.ini This error is detailed on this page - http://ph ...

What steps should I follow to activate Ivy in Angular 8 or 9?

Can you guide me through the process of enabling Ivy on an Angular 8 or 9 project? Ivy is the exciting new rendering engine designed for Angular that brings a wealth of useful features while still maintaining compatibility with existing Angular projects. ...

I am seeking to showcase an image in a window, and upon the image being clicked, execute the code in a separate window

I am looking to showcase the image provided below. <img src="http://gfx.myview.com/MyView/skins/livesample/image/livesample.gif" alt="" border="0"><a/> Once the image is clicked, I want it to execute the following code. How can I ensure that ...

Issues with properly functioning collapse button in Bootstrap 5.1.3 Navigation Bar

When I initially click on the collapsed nav-bar button, it expands to show the nav-bar contents. However, upon clicking the button again, it fails to collapse. Below is the corresponding HTML code snippet. *<!-- CSS Stylesheet -->* <link href= ...

Troubleshooting overflow problems when centering a UL menu with an unspecified width

Trying to create a demo where the scrollbars are removed when resizing smaller, but adding overflow: hidden causes issues with sub-menus. Demo Link HTML <nav> <ul> <li><a href="#">Menu Item</a></li> ...

Tips for sending an optional parameter to @Directives in Angular 2 using TypeScript

Here is a helpful guide on passing parameters to Angular 2 directives. <p [gridGroup]="gridGroup"></p> My goal is to have the parameter as optional so that it doesn't have to be included in every class referencing the html source. Curre ...

Interact with jQuery to trigger events upon clicking on a list item, excluding checkboxes within the list

I'm in the process of developing a straightforward web application. I have a dynamically generated list on one section: Subsequently, I set up an event that triggers when any element within the list is clicked: $(document).on('click', &apo ...

Aligning an SVG icon at the center of a button

I have elements that are currently being used as buttons, but I want to switch them out for actual button tags. This would improve accessibility and allow keyboard navigation using the tab key to focus on my buttons. Each button contains a centered SVG ima ...

Is there a way to partially conceal the H2 text using CSS?

Is there a way to partially hide H2 text using CSS? I have the following code: HTML: <div class="twocol-box1 superflex-content-6-12"> <h2 data-content="My - Text&amp;nbsp;<span style=&quot;float:right;&quot;>M ...

Adding an HTML element to the DOM in an AngularJS directive without using jQuery

Looking to enhance my AngularJS directive by including an svg tag within the current element, currently using jQuery for this purpose. link: function (scope, iElement, iAttrs) { var svgTag = $('<svg width="600" height="100" class="svg">< ...

Having trouble with blueprintjs icons not appearing as they should on certain pages

I have recently updated an older React application from blueprintjs version 1 to version 4 by following the documentation. However, I am now facing an issue where the icons are not displaying correctly as shown in the image below. Could someone please poi ...

PHP - designate a separate folder for script and style resources

There seems to have been some discussion about this issue before, but I am struggling to find a solution. I want to add css, js, and asset files to my php framework. However, when calling these files, the path must always match the folder structure like t ...

How to center text both horizontally and vertically in HTML

I am struggling with centering a background image along with a 1-line text vertically and horizontally inside a div. Although I have successfully centered the image, the text remains misaligned. I attempted using vertical-align: middle without success. Be ...

What is the preferred method of compiling Sass: utilizing the Live Sass Compiler extension in VS Code, or setting up and running Sass through npm? (Including guidance on transitioning from node-sass to dart-sass)

As I delve into an online course focused on Advanced CSS and Sass, I have noticed that the techniques being taught seem a bit outdated. The course heavily relies on node-sass in its dependencies, which is now considered deprecated. An alternative solution ...

Is there a way to turn off the "defer" feature in an Angular build?

After compiling my Angular project, I noticed that the compiler automatically adds the "defer" attribute to the script tag in my "index.html" file. However, I need to disable this feature. Is there a way to do it? I am currently working with Angular versi ...

When Chrome DevTools are opened, some elements of a webpage undergo a transformation in their style

I've recently started working on a static webpage using VueJS/NuxtJS and Buefy as the UI library, although I am still learning about these technologies. One issue that I have encountered is that certain elements on the page change style when I open C ...

Thymeleaf is responsible for fetching the static resources by referencing the REST URI

I am currently utilizing thymeleaf as my chosen template engine for a spring boot project. Here is the directory structure: src/main/ |- java | - UserAdministrationController.java |- resources | - static | - admin | - css ...