Update the toggle switch position in HTML/CSS

I am facing an issue with positioning a toggle switch on my webpage. Despite adding 'top' and 'left' elements under .switch{}, the button remains stuck in a small space. Increasing the numbers doesn't move the button as desired. Is there a solution to this problem? Modifying the position elements in other classes results in misalignment of the toggle switch and text. I want to be able to relocate the button seamlessly.

html:

<label class="switch">
<input type="checkbox" id="togBtn">
<div class="slider round">
   <!--ADDED HTML -->
   <span class="on">ON</span>
   <span class="off">OFF</span>
   <!--END-->
</div>
</label>

css:

.switch {
  position: absolute;
  display: inline-block;
  width: 90px;
  height: 34px;
  top: 500;
  left: 600;
}

.switch input {display:none;}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ca2222;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked + .slider {
  background-color: #2ab934;
}

input:focus + .slider {
  box-shadow: 0 0 1px #2196F3;
}

input:checked + .slider:before {
  -webkit-transform: translateX(55px);
  -ms-transform: translateX(55px);
  transform: translateX(55px);
}

.on
{
  display: none;
}

.on, .off
{
  color: white;
  position: absolute;
  transform: translate(-50%,-50%);
  top: 50%;
  left: 50%;
  font-size: 10px;
  font-family: Verdana, sans-serif;
}

input:checked+ .slider .on
{display: block;}

input:checked + .slider .off
{display: none;}


.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;}

Answer №1

It is important to remember that when defining top and left values, units such as pixels (px), rems, or percentages (%) are necessary. It raises the question of why position: absolute was included in the .switch class. While it may be suitable for certain cases, utilizing absolute positioning could lead to an improper solution for placing the switch.

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

Positioning Bootstrap 4 Dropdown Menus

I am currently utilizing Bootstrap 4 and I am facing a situation where I need to create a website header containing dropdown categories. Here is how it appears in normal view: https://i.sstatic.net/N90KR.png And here is how it looks when hovering/clickin ...

trouble with image tag not displaying correct source URL

Currently, my views.py in Django is rendering a list of objects to my html template successfully. Each object has an associated object ID, and I have named the images in the 'static/images/' directory accordingly. For example, image 1.jpg corres ...

html carousel not updating in popover

I've been attempting to create a popover with a bootstrap carousel, where the carousel items are dynamically generated and appended from a script. Despite my efforts, I have successfully displayed the carousel but struggle to append the items. I' ...

Use a variable to determine which image to display on various pages

As a PHP beginner, I have created a loop on my page to display images in a gallery format. Each image has a title displayed below it, which is actually a link leading to a second page where only that specific image is shown. The issue I'm facing is th ...

Exploring the root of an abnormal behavior on Bootstrap 4

Currently, I am encountering an issue with a dropdown menu that I created by modifying Bootstrap's example. I had to replace buttons with links in my version. You can view the code on jfiddle. Here is the code snippet: <a class="nav-link nav-link ...

Two liquid level divs within a container with a set height

I hesitated to ask this question at first because it seemed trivial, but after spending over 3 hours searching on stackoverflow and Google, I decided to give it a shot. The issue I'm facing can be found here: http://jsfiddle.net/RVPkm/7/ In the init ...

What is the best way to incorporate several elements within a <li> tag without disrupting the line spacing?

In the section labeled Pastas on the right side, there is a standard group of <li> elements within a <ul>. <ul></ul> <li>item</li> <li>item</li> <li>item</li> <ul></ul> On the ...

send the user to a different page while transferring post data using Javascript

Currently, I have an HTML form that utilizes the post method to send data to a PHP page for processing into a MySQL database. However, before the user submits the form, there is a button that allows them to view a PDF of the entered data on a new page. Whi ...

Why is my array not conforming to the CSS styles I have set?

I have been working on a code to create custom links based on user input, and have faced some challenges along the way. Despite using snippets from various online sources, everything seems to be functioning correctly except for two issues: The generated ...

Transferring a document specified by a URL into a byte array

In my HTML form, there is a field where users can input a URL that points to a potentially binary file, such as an image. Sometimes this URL may refer to a resource that the server cannot access directly, so it must be fetched by the client. My main concer ...

What is the method for creating bootstrap columns within a single row that scroll horizontally instead of wrapping to the

I am looking to create rows with columns that total more than 12, arranged in a way that allows the columns to scroll horizontally. Here is an example of what I have attempted: <div class="container-fluid"> <div class="row" ...

The HTML5 range input is consistently defaulting to the minimum value and is not triggering the onChange event for text input

My goal is to implement a feature where an input type text is used along with an input type range with three specific use cases: 1. Default case: When the page loads, the slider should point to the value specified in the text input field. 2. When a user e ...

"What is the best way to prevent a button from being enabled in Angular if a list or array is not

I am incorporating data from my service in the component: ngOnInit() { this._sharedService. getReceiptItem().takeUntil(this.ngUnsubscribe). subscribe(products => this.receiptItems = products); } Is there a way to deactivate a button if there ...

Pressing the Google Scholar Button using Scrapy

I have been experimenting with web scraping data from Google Scholar using the scrapy library, and here is my current code: import scrapy class TryscraperSpider(scrapy.Spider): name = 'tryscraper' start_urls = ['https://scholar.googl ...

Organize rows in the table while maintaining reactivity

A challenge I'm facing with a web app (Angular SPA) is that it displays a large table without the ability to sort. To work around this issue, I've managed to implement sorting via the console. However, re-inserting the rows after sorting causes t ...

The combination of Spring Boot and Angular's routeProvider is a powerful

I have been working on a project using Spring Boot, REST, and AngularJS. While I successfully implemented the back end with REST, this is my first time using AngularJS. Despite watching numerous tutorials and following them step by step, I am still facing ...

Removing the Arrow on a Number Input Field Using Tailwind CSS

Is there a way to remove the default increment/decrement arrows that come with an input of type number using Tailwind CSS? I've searched for a solution but haven't found one yet. input type="number" placeholder="Phone number" className="border ...

Verify whether the element within an iFrame contains any content

After conducting extensive research, I have been unable to find a satisfactory answer to my question. Therefore, I am reaching out to see if anyone has the knowledge I seek. The Goal: I aim to check the contents within an iFrame and determine whether it ...

Tips for incorporating component templates into your Nuxt application

I am currently working on creating a dynamic header using Vue within a Nuxt application. However, I have encountered a couple of challenges: 1) I am struggling to populate the template in the original DOM with the content from an external file, which was ...

How can we ensure an image fills up the entire screen while still being contained within a Bootstrap 4 container?

I have a unique setup on my Wordpress page template. The content from the parent template is displayed first, followed by the content of child templates, including featured images. I want the content to appear in the container class, while the featured ima ...