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: https://i.sstatic.net/XRYuR.png

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

Angular 2 property accessor designed similar to Linq style

Many times, we come across an array (IEnumerable) property where we need to extract specific values. In C#, we can achieve this by using the following code snippet: public AssetModel PromoImage { get { return Assets.FirstOrDefa ...

Adding a dynamic style programmatically to an element created using createElement in Vue: A guide

I have a canvas element that is created programmatically and I am looking to apply a reactive style to it. canvas = document.createElement('canvas') //Apply style this.$refs.parentElement.appendChild(canvas) In a typical scenario, you would u ...

The bubble dialogue box in my picture

Looking to create a panel with images that, when clicked on, display an info window similar to Google Map's pin maker. When you click on an image, a balloon should appear containing additional information. Check out this example <a id="infowindow ...

Persistent Angular Factory Variables Showing Outdated Data - Instances Stuck with Old Values

One of the challenges I faced was setting up a resource factory to build objects for accessing our API. The base part of the URL needed to be determined using an environment variable, which would include 'account/id' path segments when the admin ...

When altering a single scope variable in AngularJS, the effect cascades to impact other scope variables as well

After uploading my code on fiddle, I noticed that changes made to the myAppObjects variable also affect another variable. Here is the link to the code: https://jsfiddle.net/owze1rcj/ Here is the HTML part of the code: <div ng-controller="MyCtrl"&g ...

When the CSS style sheet is not embedded within the HTML document, it fails

I'm facing an issue while trying to apply currency formatting to an HTML table in order to have it display correctly when opened in Excel. Initially, I tried doing this inline and it worked fine, like so: <td style="mso-number-format:$\##&bso ...

interactive switch using font awesome icons and jquery

Initially, I assumed this would be an easy task, but I've encountered some difficulties in making it function smoothly. Although I am able to toggle once using .show and .hide, I am unable to toggle back. Any assistance would be greatly appreciated. ...

Issue encountered while attempting to install datagrid library with Nuxt3

Currently, I am working on a Nuxt3 project and attempting to integrate the " @revolist/vue3-datagrid" library. Following the instructions provided in the library's documentation, I executed the command "npm i @revolist/vue3-datagrid --save". Unfortuna ...

Double submission issue with Angular form (multiple ajax requests)

My controller seems to be causing a form submission issue in AngularJS where the form is being submitted twice via a get request. Upon checking my database and the console network tab, I noticed that two submissions are logged, with the first submission sh ...

I continue to encounter the error "Unexpected token b in JSON at position 0" when attempting to parse JSON data

Having some trouble with my code that generates an HTML page. The signup function allows users to register and create a password, while the checkpassword function is supposed to verify if the correct password is entered for the given username. I seem to be ...

What is the right way to nest a SCSS rule for a specific selector within a list using the "&" symbol?

I have a question that I couldn't find the answer to in any documentation or on Stack Overflow. While this example may not be perfect, it should give you a basic understanding of what I'm trying to achieve. Illustration .btn-group { .btn ...

Assign increasing values within an array

Is there a way to efficiently update multiple values in an array by mapping through them? I want to select and change all of them simultaneously. state.formData.forEach(item => { item.EndDate = nextDayIfSelected; }); ...

Encountering an issue while attempting to assess a Meteor package within a local environment

Hello everyone! I'm a beginner in Meteor programming and currently following the online book discovermeteor.com. I recently came across a chapter that covers the creation of Meteor Packages. Excitedly, I proceeded to create my own package using the ...

Troubleshooting Issue: Failure of Ajax Script to Display Saved Data in Edit Function

Whenever I clicked on the Edit icon in the action column of my data tables, the saved data did not display as expected. I noticed that this issue was only occurring for file input types, while it worked properly for text input types. In the Blade file Ad ...

AngularJS encountered a TypeError when trying to parse the object, indicating that it is not a

I've streamlined my app to the basics, but I keep encountering an error with $parse: TypeError: object is not a function What am I doing wrong? What piece of the puzzle am I missing? Interestingly, everything seems to be working fine in this plunker ...

Combine both typescript and javascript files within a single Angular project

Is it feasible to include both TypeScript and JavaScript files within the same Angular project? I am working on a significant Angular project and considering migrating it to TypeScript without having to rename all files to .ts and address any resulting er ...

Aligning object in middle of a responsive image using Bootstrap

I'm having trouble centering a button on an image using responsive design techniques. I am currently utilizing Bootstrap 3 and have attempted to use CSS margin properties to center the button, but this method is not ideal especially on smaller screens ...

Sending JSON data stored in a JavaScript variable through a jQuery POST request

I am currently attempting to retrieve data from a remote location using a JQuery post method. It works perfectly fine when I directly input the data to be posted, but for some reason, it fails when I store the JSON in a JavaScript variable and pass it in. ...

Confirm the dimensions of an image prior to uploading using Node, Express, and Multer

Currently, I am developing a project using Nodejs with the express framework and ejs as the view engine. I have encountered an issue while working on image uploads. I am utilizing Multer for this task, but I need to implement a requirement where images wil ...

Adaptable arrow-shaped progress bar featuring sleek transparent borders

I am currently working on creating a progress bar similar to those commonly found in checkout processes. One issue I have encountered is that the borders between the arrows appear transparent, and the entire design needs to be responsive. So far, I have m ...