What is the best way to center CSS slider switches without causing any damage to them?

Does anyone have ideas on how to align CSS Slider Switches?

I tried using

position: absolute; margin-left: 15em
, but the sliders need to remain non-absolute for visual effects.

A flex grid was considered, but I don't want the switches to wrap.

Using a table is an option, but I'd prefer to find an alternative.

/* toggle switch */
.switch{position:relative;display:inline-block;width:60px;height:34px}
.switch input{display:none}
.slider{position:absolute;cursor:pointer;top:0;left:0;right:0;bottom:0;background-color:#555;-webkit-transition:.4s;transition:.4s}
.slider:before{position:absolute;content:"";height:26px;width:26px;left:4px;bottom:4px;background-color:#000;-webkit-transition:.4s;transition:.4s}input:checked+.slider{background-color:#fff}input:focus+.slider{box-shadow:0 0 1px #fff}input:checked+.slider:before{-webkit-transform:translateX(26px);-ms-transform:translateX(26px);transform:translateX(26px)}
.slider.round{border-radius:34px}
.slider.round:before{border-radius:50%}
<div class="vfor-content">
  <span v-bind:id="item.name" onclick="_app.doFlipRelay(this.id)">
    <span>thing 1</span>
    <label class="switch">
      <input type="checkbox" v-model="item.value == '1'" disabled>
      <span class="slider round"></span>
    </label>
  </span>
</div>
<div class="vfor-content">
  <span v-bind:id="item.name" onclick="_app.doFlipRelay(this.id)">
    <span>thing 2 with long name</span>
    <label class="switch">
      <input type="checkbox" v-model="item.value == '1'" disabled>
      <span class="slider round"></span>
    </label>
  </span>
</div>
<div class="vfor-content">
  <span v-bind:id="item.name" onclick="_app.doFlipRelay(this.id)">
    <span>thing 3</span>
    <label class="switch">
      <input type="checkbox" v-model="item.value == '1'" disabled>
      <span class="slider round"></span>
    </label>
  </span>
</div>

Answer №1

Utilizing flex is a superior method for facilitating vertical alignment, although it may necessitate rearranging your DOM structure. If you prefer to maintain the current layout, the simplest solution would be to assign a class to your span elements like this:

<span class="switch__title">item 1</span>

Subsequently, include the following CSS:

.switch__title {
    display: inline-block;
    width: 200px;
}

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

Unreliable behavior observed with the selenium driver.find_element method

Struggling to enter a password into a text box using Python. Here is the HTML code snippet for the webpage containing the input field. <input type="password" placeholder="" name="password" id="input6" value=" ...

Is there a way to align these buttons in the middle?

I am currently designing a webpage at The challenge I'm facing is: How can I center those buttons on the page? They need to remain centered and responsive even when the page is resized. Additionally, the spacing between the buttons is inconsistent. ...

I'm facing an issue with my 'root' div on ReactJS - it doesn't stretch all the way to the top. Any suggestions on how I can make it expand properly

While I realize this issue has been discussed extensively, none of the suggested solutions have worked for me. As a final attempt to resolve it, I am turning to you all here. The point at which my root div stops is puzzling: I've explicitly defined ...

What steps should I take to ensure that my Ngrok domain is functioning properly?

Screenshot of Endpoints I've been facing challenges while attempting to get my NGROK domain operational. Despite completing all the necessary setup steps and successfully running multiple tunnels simultaneously with NSSM, as well as having a business ...

When using Owl Carousel in Chrome, the carousel unexpectedly stops after switching tabs

Hi there, I'm currently testing out the auto play feature in owl carousel. One issue I've encountered is that when I switch to another tab in Chrome and then return to my webpage with the carousel, it stops functioning unless I manually drag the ...

Guide to positioning an image absolutely within a div

I am struggling to position a small close image within a div without success. The goal is for the image to always appear on the left side of the div, regardless of the content. The content consists of a text label with a maximum length of 8 characters. Iss ...

Customizing Ngx-bootstrap Carousel Indicator, Previous, and Next Button Styles

<carousel > <a href=""> <slide *ngFor="let slide of slides"> <img src="{{slide.imgUrl}}" alt="" style="display: block; width: 100%;"> </slide> 1. Is there a way to substitute the indicators with images ...

Is it a scope issue if ng-click is not firing and the function is not working properly?

I'm facing an issue with an animation that is not working as intended. The goal is to have the search button trigger an animation to pull a search bar from the right side, allowing users to input their search query. However, the ng-click function does ...

Streamlined Boilerplate to Kickstart Your Project

Can anyone suggest a well-crafted boilerplate .less file to kickstart a new project? I'm looking for a comprehensive .less file that includes: CSS Resets styles CSS Normalizer styles CSS3 Helpers (box radius, gradients, box shadow, transition) Basic ...

Navigating through different templates in {output} with Ember

My first ember app is currently under construction, featuring CRUD operations. Here's a snippet of my index.html code: <script type = "text/x-handlebars" id = "settings"> <table> {{#each setting in controller}} <tr> < ...

Creating interactive tabs within the HTML document with AngularJS

Trying to customize the tab layout on my HTML page and I have a code similar to this: <form name="requestForm"> <div class="form-group col-md-6 md-padding"> <div class="text-primary"> <h3>Create Request</h3> ...

Alter the color scheme of Fancybox's background

I am looking to create a transparent background color for Fancybox. The typical method involves using ".fancybox-skin" in CSS, however this will affect all fancyboxes and I require it to be unique. Therefore, the setting must be sent with wrapCSS. Fancyb ...

What are some ways to utilize the pseudo elements ::before and ::after within the Material-UI framework?

Can someone please explain how to use the pseudo-element method in MUI? I couldn't find clear instructions in the documentation, so I decided to post a question here. The docs mention the following about pseudo elements: The box-sizing property is ...

Transfer the content from a specific div into another div

Within this code snippet, there are three buttons labeled P1, P2, and P3, each connected to a card div below them. On the right side, there is another div containing the text "TEXT SHOULD BE DISPLAYED HERE". The desired functionality is that clicking on an ...

Optimizing Your HTML/CSS/JavaScript Project: Key Strategies for Modular

When it comes to web frontend projects (html/css/javascript), they are often perceived as more complex to read and maintain compared to Java/C#/C/C++ projects. Is it possible to outline some optimal strategies for enhancing the readability, modularizatio ...

Is it possible to have a full-width thumbnail navigator in a full-width slider container with Jssor Slider?

I apologize if this topic has already been discussed, In a responsive full-width slider container, I am utilizing thumbnail navigator 03 with the scaling option disabled. Is it feasible to have a fixed-height, but fully (100%) width thumbnail navigator t ...

"Utilizing the power of Twitter Bootstrap and Snap.js for

I am currently working on integrating Snap.js by jakiestfu with Twitter Bootstrap. I have managed to make it work to some extent (I can slide the content and open a drawer through a click event). However, I am facing a challenge where the drawer content re ...

What is the method for shifting content as the window is resized to ensure it remains in its original position?

My page features a grid with three div elements. Each div is the size of the viewport, resulting in only one div being visible at a time, while the other two remain outside the view. This makes the grid three times larger than the viewport. When resizing ...

I am having trouble inserting a table from a JSON object into an HTML file

getJSON('http://localhost:63322/logs', function(err, data) { if (err !== null) { alert('Something went wrong: ' + err); } else { //var myObj = JSON.parse(data); // document.getElementById("demo").innerHTML = myObj.ad_soy ...

How can I use JQuery to iterate through Object data and dynamically create tr and td elements?

In previous projects, I utilized Vanilla JS to iterate through Ajax return data and construct tables. However, for this current project, I have decided to switch over to JQuery. The main reason behind this decision is that some of the td elements require s ...