Search code examples
cssalignmentmaterializeright-align

Right-aligned Switch in a Sidenav


I am trying to show two switches in a Sidenav, with labels of different length. Here is my original version, without trying to align the switches:

<html>
  <head>
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/css/materialize.min.css">
  </head>
  <body>
    <ul id="slide-out" class="sidenav">
      <li><a>
        Label
        <span class="switch">
          <label>
            <input type="checkbox"></input>
            <span class="lever"></span>
          </label>
        </span>
      </a></li>
      <li><a>
        Long long label
        <span class="switch">
          <label>
            <input type="checkbox"></input>
            <span class="lever"></span>
          </label>
        </span>
      </a></li>
    </ul>

    <script>
      document.addEventListener('DOMContentLoaded', function() {
        M.Sidenav.init(document.querySelectorAll(".sidenav"), { edge: "right" })[0].open();
       });
    </script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/js/materialize.min.js"></script>
  </body>
</html>

This renders exactly as expected, with the two switches appearing horizontally wherever the labels end:

Unaligned

I thought I could fix this by adding the right class to the switches, i.e. changing both <span class="switch"> to <span class="switch right">; however, this then completely breaks the vertical alignment of the switches:

Right-aligned

What is the correct way of right-aligning these switches in a sidenav?


Solution

  • You can make a flex container out of your divs and align them space-between. You can affect them if you change the width, this so you can tweak your result.

    Hope this helps. The HTML is unchanged below.

    .sidenav li>a {
      align-items: center;
      display: flex!important;
      justify-content: space-between!important;
    }
    <html>
      <head>
        <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/css/materialize.min.css">
      </head>
      <body>
        <ul id="slide-out" class="sidenav">
          <li><a>
            Label
            <span class="switch">
              <label>
                <input type="checkbox"></input>
                <span class="lever"></span>
              </label>
            </span>
          </a></li>
          <li><a>
            Long long label
            <span class="switch">
              <label>
                <input type="checkbox"></input>
                <span class="lever"></span>
              </label>
            </span>
          </a></li>
        </ul>
    
        <script>
          document.addEventListener('DOMContentLoaded', function() {
            M.Sidenav.init(document.querySelectorAll(".sidenav"), { edge: "right" })[0].open();
           });
        </script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/js/materialize.min.js"></script>
      </body>
    </html>