Properly aligning text with checkboxes using HTML/CSS and tags like <span> or <div>

My goal is to have the text displayed as a block in alignment with the checkbox, adjusting based on the sidebar's width.

For reference:

Current Layout

Preferred Layout

I have shared the code on CodePen (taking into account screen resolution and width). I've made several attempts including changing from to , and applying inline styling like "float left". The switch from to can be managed within JavaScript under "this.

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
   <link
      rel="stylesheet"
      href="https://use.fontawesome.com/releases/v5.8.1/css/all.css"
      integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf"
      crossorigin="anonymous"
    />
  <link rel="stylesheet" href="//rawgit.com/iVantage/angular-ivh-treeview/master/dist/ivh-treeview.css">
  <link rel="stylesheet" href="//rawgit.com/iVantage/angular-ivh-treeview/master/dist/ivh-treeview-theme-basic.css">
</head>
<body ng-app="bin">

  <div ng-controller="DemoCtrl as demo">
    <h3>Custom Node Templates</h3>
    <div style=width:224px>
      <div ivh-treeview="demo.stuff"
           ivh-treeview-node-tpl="demo.tpl"
           ivh-treeview-options="demo.customTreeViewOpts">
      </div>
    </div>
  </div>

  <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.8/angular.js"></script>
  <script src="//rawgit.com/iVantage/angular-ivh-treeview/master/dist/ivh-treeview.js"></script>
</body>
</html>
var stuff = [
   {
                label: "Sales",
                selected: false,
                children: [
                  {
                    label: "Sales Program",
                    selected: false,
                    children: [
                      {
                        label: "Commercial Dealer Fleet Program",
                        selected: false
                      },
                      {
                        label: "Courtesy Car Program",
                        selected: false
                      },
                      {
                        label: "VIP & Employee Purchase Program",
                        selected: false
                      },
                      {
                        label: "Policies",
                        selected: false
                      }
                    ]
                  },
                  {
                    label: "Customer Rebate Programs",
                    selected: false,
                    children: [
                      {
                        label: "Commercial Dealer Fleet Program",
                        selected: false
                      },
                      {
                        label: "Courtesy Car Program",
                        selected: false
                      },
                      {
                        label: "VIP & Employee Purchase Program",
                        selected: false
                      },
                      {
                        label: "Policies",
                        selected: false
                      }
                    ]
                  }
                ]
              },
              {
                label: "Fleet",
                selected: false
              }
];

var app = angular.module('bin', ['ivh.treeview']);

app.config(function(ivhTreeviewOptionsProvider) {


 ivhTreeviewOptionsProvider.set({
   defaultSelectedState: false,
   validate: true,
   expandToDepth: -1
 });
});


app.controller('DemoCtrl', function() {
  this.stuff = stuff;

  this.tpl = `
  <div title="{{trvw.label(node)}}" >       
    <span ivh-treeview-toggle >
      <span ivh-treeview-twistie>
      </span>
    </span>
    <span ng-if="trvw.useCheckboxes()" ivh-treeview-checkbox  >
    </span>
    <span class="ivh-treeview-node-label" ivh-treeview-toggle   >
    {{trvw.label(node)}}
    </span>
    <div ivh-treeview-children></div>
  </div>`

  this.customTreeViewOpts = {
    // useCheckboxes: false
    // twistieLeafTpl: ""
    twistieExpandedTpl: '<span class="fas fa-minus-square"></span>',
    twistieCollapsedTpl: '<span class="fas fa-plus-square"></span>',
    twistieLeafTpl: '<span class="fas fa-minus-square" style=" visibility: hidden;"></span>'
    // nodeTpl: "..."
    // onToggle: this.awesomeCallback
  };
});

Answer №1

Make sure to group the twisty, checkbox, and label together in a container with the display:flex property, while keeping the ivh-treeview-children directive separate as a sibling.

Check out the updated code sample on CodePen: https://codepen.io/jtrussell/pen/qzQNdm?editors=0010

Here's the revised node template snippet:

<div title="{{trvw.label(node)}}">       
  <div style="display:flex">
    <span ivh-treeview-toggle >
      <span ivh-treeview-twistie>
      </span>
    </span>
    <span ng-if="trvw.useCheckboxes()" ivh-treeview-checkbox  >
    </span>
    <span class="ivh-treeview-node-label" ivh-treeview-toggle>
    {{trvw.label(node)}}
    </span>
  </div>
  <div ivh-treeview-children></div>
</div>

Take a look at the screenshot of the result here:

Answer №2

To enhance the layout of the checkboxes within the node wrappers, one potential method involves adding padding to the wrappers and adjusting with a negative margin on the checkbox contents. An additional wrapper (.tree-node-adjusted-wrapper) was introduced along with a class to the outer div (.tree-node-adjusted). This technique may be considered somewhat unconventional, and the pixel offsets could potentially be calculated based on ems or another font-related metric.

Customized Template

  this.tpl = `
  <div title="{{trvw.label(node)}}" class="tree-node-adjusted" >  
    <div class="tree-node-adjusted-wrapper">
    <span ivh-treeview-toggle >
      <span ivh-treeview-twistie>
      </span>
    </span>
    <span ng-if="trvw.useCheckboxes()" ivh-treeview-checkbox  >
    </span>
    <span class="ivh-treeview-node-label" ivh-treeview-toggle   >
    {{trvw.label(node)}}
    </span>
    </div>
    <div ivh-treeview-children></div>
  </div>`

The Styling (LESS snippet used in Codepen demonstration):

.ivh-treeview .tree-node-adjusted {

  .tree-node-adjusted-wrapper {
    padding-left: 40px;

    & > span:nth-child(1) {
      margin-left: -40px;
    }
  }
}

Explore a working example in Codepen

(An alternate solution might involve positioning the nodes with relative placement along with left padding, and employing absolute positioning for the checkboxes – potentially offering greater precision).

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

Is there a way to modify the CSS of the sequential number element when it is clicked on?

I've successfully utilized jQuery to create sequential numbering for my menu items. Upon clicking, the hyperlink text changes color to red. However, I'm facing an issue where I want the corresponding number to also change to red when the hyperli ...

Footer not sticking to bottom using bootstrap version 5.2.2

I am currently working on a project using Laravel Inertia Vue and bootstrap 5.2.2 as part of my dependencies. After loading the css to the tag, I attempted to create a sticky footer. However, I found that the footer is not sticking to the bottom as expect ...

Troubleshooting the problem of fast rotation and scrolling of text in the middle but slow movement at the end with

Currently, I am utilizing the jquery animate function for animating text while also aiming to rotate the text if a specific degree is passed. In order to achieve this, I have implemented the following code snippet: var leftCss = "-"+$('#mydiv'). ...

Having trouble with clearTimeout and clearInterval functions not functioning properly?

Currently, I've set up a countdown using both setInterval and setTimeout functionalities, and it seems to be running smoothly. However, I encounter an issue when trying to stop the countdown upon clicking a certain button; it pauses only after complet ...

CSS: choose all elements starting from the n-th element and apply styles to them

Is it possible to choose specific child elements beyond the first few in a parent element? To clarify, let's say there's a div containing 7 spans. How can I select only the spans starting from the 3rd element onwards - so specifically span 4, 5, ...

Complete a bootstrap row and begin a new row after every nth div element

I have a grid layout in Bootstrap that I will be filling with blog post thumbnails. <section class="container"> <div class="row thumbs"> <div class="col-sm-3">content</div> <div class="col-sm-3">content</div> ...

Can someone provide guidance on utilizing the index correctly within this v-for to prevent any potential errors?

I am encountering an issue with using index in a v-for loop to implement a function that deletes items from an array. The linter is flagging "index is defined but never used" as an error. I am following the instructions provided in a tutorial, but I am un ...

If the <option> "anyTableName" </option> is chosen, then display the column names of the selected table (PHP, MySQL)

Hey there, I'm a newbie on stackoverflow so feel free to correct me if I'm off base ;) Here's my current dilemma: I have a text.php file that contains 2 <select> elements. The first one allows me to choose a table (like "accounts", "c ...

Developing custom validation with Angular 1.5 based on external data inputs

Is it possible to create a custom validation directive in Angular 1.5 that takes into account values other than the bounded one? For example: I have an array of objects of type Type1 with fields Field1, Field2, and Field3. Using ng-repeat, I display the ...

Issues with 'floating' unordered lists

.menu li { float: left; color: #fff; font-weight: bold; } .menu li a { display: block; height: 20px; min-width: 110px; text-decoration: none; border-radius: 3px; padding: 4px; padding-left: 6px; padding-right: 6p ...

What steps should I take to make sure that the types of React props are accurately assigned?

Dealing with large datasets in a component can be challenging, but I have found a solution by creating a Proxy wrapper around arrays for repeated operations such as sorting. I am looking to ensure that when the data prop is passed into my component as an ...

Unable to execute specific php function using ajax

I have created a script that utilizes an ajax request. This script is triggered when a user clicks a button on the index.php page, like so: Risorse.php <form method='post'> Name <input type='text' name='nome&apo ...

What causes my Excel file to become corrupted when inputting new data?

My intention with this code is to insert "ABC" into cell B3 of an existing Excel document. However, when the file is written, its size decreases significantly and Excel is unable to open it. const excel = require("exceljs"); const template = "./myexcel.xl ...

What could be causing the issue of React not showing the messages of "hello" or "goodbye"?

I have a page with a single button that is supposed to display either "hello world" or "goodbye world" when clicked. However, I am facing issues as the messages are not showing up as expected. Below is a screenshot of what the menu items look like when ca ...

How do I send a 404 error in Node JS Express when a third party API receives a bad request?

I've set up a Node JS server with a route handler that sends a request to a third-party API to retrieve a username: app.get('/players/:player', apiLimiter, function(request, response) { const player = request.params.player; const api_url = ...

Attempting to route a selector, yet on the second attempt, the entity is successfully transferred

I am currently working on developing a function that will switch the image every half second for 10 iterations. During the final iteration, the image src will be set to the actual value. Everything seems to work fine for the first loop, however, when the s ...

The absence of parameters in the Express.js middleware object

const application = express(); let routerInstance = require('express').Router({mergeParams: true}); const payloadMiddlewareFunction = (request, response, next) => { console.log('A:', request.params); const {params, query} = reque ...

How can an input field with a checkbox type be properly placed within a table data tag in a table nested within an AngularJS modal?

Below is the code for my modal: <!-- modal for viewing permissions --> <div id="modal-user-permissions" class="modal"> <div class="modal-content"> <h4 id= ...

Need help with resetting a value in an array when a button is clicked?

Using Tabulator to create a table, where clicking on a cell pushes the cell values to an array with initial value of '0'. The goal is to add a reset button that sets the values back to '0' when clicked. component.ts names = [{name: f ...

What is the reasoning behind setting the perspective to 1000 for carousel items in Bootstrap?

While working on customizing the bootstrap carousel, I came across this code snippet: .carousel-inner > .item { -webkit-transition: -webkit-transform .6s ease-in-out; -o-transition: -o-transform .6s ease-in-out; transit ...