The table toggle feature seems to be malfunctioning in Safari, whereas it works perfectly in Chrome

My table includes a td element that serves as a toggle switch, transitioning between 3 states flawlessly in Chrome. However, I am facing issues with its functionality in Safari and seek assistance in rectifying the issue to ensure cross-browser compatibility.

Attached are both an image and the relevant code snippet.

Grateful for any insights or solutions shared.

M.

https://i.sstatic.net/8kTgU.png

highlight {
    background-color: #86C440;
    color: white;
}

th {
  font-size: 30px;
}

tr {
  text-align: left;
}

td {
  font-size: 20px;
  background-color: #d4d6d3;
  cursor: pointer;
}

.center {
  margin-left: auto;
  margin-right: auto;
}

tr {
  position:relative;
  transform:scale(1,1);
}

td.last{
  position:fixed;
  border: 5px solid #d4d6d3;
  background-color: #4eafef;
  left: -46px;
  top: -46px;
  height: 7px;
  width: 7px;
  line-height: 7px;
  cursor: pointer;
}
    <html>
        
    <body>
<div id="switch">1</div>
<br><br><br><br><br><br><br>

<style>

  table          {border-collapse: collapse; border: 5px solid white; padding: 5px;}
  table td       {text-align: center; color: black; border: 5px solid white; padding: 10px; height: 66px; width: 66px;}
  
</style>


<table class="center">
 <tr>
   <td>1</td>
   <td>2</td>
   <td id="toggle" class="last" onclick="toggle1()"></td>
 </tr>
    <tr>
   <td>3</td>
   <td>4</td>
 </tr>
    <tr>
   <td>5</td>
   <td>6</td>
 </tr>
</table>

</body>

    <script>

    function toggle1() {

                var key = document.getElementById("switch").innerHTML;

              if (key == 1) {

                      document.getElementById("toggle").style.backgroundColor = "white";
                      document.getElementById("toggle").style.backgroundImage = "url('')";
                      document.getElementById("toggle").style.borderStyle = "solid";
                      document.getElementById("toggle").style.borderColor = "#d4d6d3";
                      document.getElementById("toggle").style.borderWidth = "5px";
                      document.getElementById("switch").innerHTML = 2;

                            //console.log("toggle clicked 1st time!");

              } else if (key == 2) {

                      document.getElementById("toggle").style.backgroundColor = "#a370f0";
                      document.getElementById("toggle").style.backgroundImage = "url('')";
                      document.getElementById("toggle").style.borderStyle = "solid";
                      document.getElementById("toggle").style.borderColor = "#d4d6d3";
                      document.getElementById("toggle").style.borderWidth = "5px";
                      document.getElementById("switch").innerHTML = 3;

                            //console.log("toggle clicked 2nd time!");

              } else if (key == 3) {

                      document.getElementById("toggle").style.backgroundColor = "#4eafef";
                      document.getElementById("toggle").style.backgroundImage = "url('')";
                      document.getElementById("toggle").style.borderStyle = "solid";
                      document.getElementById("toggle").style.borderColor = "#d4d6d3";
                      document.getElementById("toggle").style.borderWidth = "5px";
                      document.getElementById("switch").innerHTML = 1;

                            //console.log("toggle clicked 3rd time!");

              }

    }

    </script>

    </html>

Answer №1

When comparing the behavior of Chrome/Edge to Safari, there are several issues that may lead to differences.

One problem is the presence of a style declaration within the body element instead of the head element, which can be flagged as invalid HTML by an HTML validator. The solution provided below moves this style declaration to the head element.

Another issue involves a setting for tr elements that applies a transform, causing Chrome to position the fixed element relative to the table's beginning rather than relative to the viewport. While I do not have a complete explanation for this behavior, it seems that any kind of transform triggers this change and creates a new stacking context.

A warning from the validator arises due to a third cell in the first row but not in subsequent rows. It is unclear how the table layout should appear under these circumstances, especially considering the fixed setting that visually removes the third cell from the flow. To address this, the snippet removes the third cell and adds a button before the table, resulting in consistent behavior across Safari and Chrome/Edge.

Adjustments might be needed for the button's positioning – one potential solution is placing the button and the table inside a container with specified positioning based on the desired layout requirements.

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

Creating dynamic stripes on MUI LinearProgress for lively animations

I'm looking to add animation to MUI LinearProgress. I have the animation keyframes code, but I'm not sure how to implement the stripes effect. Any advice would be appreciated. Here is the code snippet I have: import React, { FunctionComponent } ...

Tips for maintaining the height of the outer div consistent with that of the inner div

I am encountering an issue with an inner div that is used for overlay and set to a min-height property. Despite changes in the overlay's height, the height of the outer div remains unaffected. #outer { width: 70px; height: 70px; background-co ...

Is there a way to display the form values on the table once it has been submitted?

I'm struggling with my form input not showing up under the table headings after submission. I've reviewed the code multiple times, but can't figure out what's causing the issue. If you have any suggestions on how to write the code more ...

What is the best way to alter the font size in an SVG?

I have incorporated an SVG icon into my website and obtained the following code from Adobe Illustrator: <svg id="Livello_1" data-name="Livello 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448.05 436.7"><path d="M190.5,66.9l22.2-22.2a23.9, ...

Is it possible to send an entire HTML table to the server and then update the database table with it?

Recently, I encountered an issue that has me stumped. Suppose I have a database table A with multiple columns and the server (PHP script) renders this data into an HTML table for the web client. Now, the challenge lies in allowing users to add/delete rows ...

Embracing Elegance with jQuery

Is there a way to customize the appearance of my list (<ul>) in the following code snippet? $.widget( "custom.catcomplete", $.ui.autocomplete, { _renderMenu: function( ul, items ) { var self = this, currentCategory = ""; ...

The Carousel feature functions properly with 3 or more slides, but malfunctions when there are only 2 slides present

After implementing a minimal carousel, I discovered that it functions perfectly with 3 or more slides. However, as soon as I remove some slides, issues start to arise. Some of the problems I encountered include: The sliding animation is removed on ' ...

Is there a way for me to obtain the present value of the chosen button in the list below?

I am working with a group of three buttons labeled English, Hindi, and Urdu. How can I retrieve the value of the selected button in a JavaScript function? <div class="btn-group" data-toggle="buttons"> <label class="btn btn-primary active"> ...

Unable to removeClass and addClass as expected

Each time I click on a div, it gets encased in a black line. However, my goal is for the previously selected div to lose the black outline whenever I click on a different div. Only the current div should have the border. Below is the code snippet that I a ...

Modify the text and purpose of the button

I have a button that I would like to customize. Here is the HTML code for the button: <button class="uk-button uk-position-bottom" onclick="search.start()">Start search</button> The corresponding JavaScript code is as follows: var search = n ...

Creating a Drop-down Menu Item using React Material UI

I am experiencing an issue with displaying a MenuItem in my Dialog2 within my React app. Despite setting the zIndex: 1900 to a higher value than the two dialogs, the MenuItem remains hidden behind the dialogs instead of appearing at the front. Please revi ...

What are some ways to increase the scalability of an HTML form?

My login page is easy to read on larger screens like laptops or tablets, but on mobile devices, users have to zoom in to see the text clearly. I want to make it more scalable, but I'm struggling to figure out how. Here's the HTML code: <!DO ...

Animate a dotted border with CSS

How can I make a text block with a dotted style border move like a gif image using CSS at runtime? ...

Align the emoji in the center of the absolute div horizontally

I'm currently working on a project involving placing emojis at different coordinates within a webpage. At times, the emoji is larger than its container. For instance, here is an example of HTML code that positions a house with a font size of 100px ins ...

What is the best way to retrieve the height of the parent element in my specific situation

I am facing an issue in my directive where I need to access the height of the parent element. Here is a snippet of my code: (function(window, angular) { var app = angular.module('myApp'); app.directive('testElem', [ fu ...

The margin-left and margin-right in HTML CSS are not cooperating to center a div

I'm currently diving into the world of CSS and HTML, but I've hit a roadblock. The margin-left and margin-right in the ".logo" div class are refusing to center the div as expected. Despite conducting research and meticulously checking over the co ...

Eliminating gaps between elements

I recently delved into the world of web design, and although I have a basic understanding of HTML and CSS, I am determined to enhance my skills and knowledge by creating a standard home page. So far, I managed to create a standard navigation bar, and now ...

Has anyone come across an XSLT that can effectively convert Apple Pages files into high-quality HTML?

When Apple’s word processor/DTP app Pages saves its files, it does so as zipped folders with an XML file containing the content and attachments like images stored as separate files. I am interested in converting this content into HTML format, using < ...

When hovering over the element, child elements remain unaffected by the wrapping behavior

My anchor tag contains a span and an image, but I am having trouble making them hover together. <a href=""> <img src="image"/> <span>Shopping Cart</span> </a> a :hover { opacity: 0.6; } Check out the fiddle ...

Dragging the identical li element from the div for the second time should not trigger a drag and drop action

After successfully dragging and dropping an element from <div id="catalog" > into a box, specifically <div id="dialogIteration">, on the first attempt everything works as expected. However, upon attempting to drag and drop the same element for ...