Encountering an issue when trying to use SVG clip-path in Safari

I'm currently experimenting with creating a unique hexagonal border around a button. My approach involves enlarging the container element by a few pixels compared to the button size and using the same clip-mask on both elements to achieve a bordered effect. This method functions effectively in Firefox and Chrome, however, it encounters issues in Safari.

document.getElementById("button").addEventListener("click", function(){alert("foo"); return false;});
div {
  width: 9rem;
  height: 8rem;
  position: relative;
  clip-path: url("#hexagon");
  -webkit-clip-path: url("#hexagon");
  background-color: #e2e3e5;
}

button {
  position: absolute;
  cursor: pointer;
  top: 2px;
  left: 2px;
  padding: calc(8rem * 0.1) calc(9rem * 0.2);
  width: calc(9rem - 4px);
  height: calc(8rem - 4px);
  clip-path: url("#hexagon");
  -webkit-clip-path: url("#hexagon");
  background-color: white;
  border: none;
}
<div id="div">
<button id="button">The button</button>
</div>

<svg width="0" height="0" xmlns="http://www.w3.org/2000/svg">
  <defs>
    <clipPath id="hexagon" clipPathUnits="objectBoundingBox">
      <polygon points=".25 0, .75 0, 1 .5, .75 1, .25 1, 0 .5"/>
    </clipPath>
  </defs>
</svg>

When I omitted -webkit-clip-path, Safari displayed a rectangular border instead of a hexagon. Adding it caused the resulting hexagon's width to be larger than expected when viewed in other browsers. The clip path seems to be covering more of the button area than intended. Is there any workaround to address this issue and ensure compatibility with Safari?

Firefox: https://i.stack.imgur.com/F9tcXs.png Safari: https://i.stack.imgur.com/duw3ss.png

Answer №1

Instead of using the clip-path: url() property, you can opt for clip-path:polygon() instead. I have adjusted your points by multiplying them by 10 and offsetting them with 15 on the x axis and 5 on the y axis. Additionally, I have scaled the button as the border was barely visible.

document.getElementById("button").addEventListener("click", function(){alert("foo"); return false;});
div {
  width: 8rem;
  height: 6.6rem;
  position: relative;
  background-color: #ccc;
  -webkit-clip-path:polygon(40px 5px, 90px 5px, 115px 55px, 90px 105px,40px 105px, 15px 55px);
  clip-path:polygon(40px 5px, 90px 5px, 115px 55px, 90px 105px,40px 105px, 15px 55px);
}

button {
  position: absolute;
  cursor: pointer;
  text-align:center;
  width: 8rem;
  height: 6.6rem;
  -webkit-clip-path:polygon(40px 5px, 90px 5px, 115px 55px, 90px 105px,40px 105px, 15px 55px);
  clip-path:polygon(40px 5px, 90px 5px, 115px 55px, 90px 105px,40px 105px, 15px 55px);
  background-color: #fff;
  border:none;
  transform:scale(.97);
}
<div id="div">
<button id="button">The button</button>
</div>

Another alternative is to use an SVG element instead of a button. Once again, I've multiplied your points by 10 for the hexagon shape. Personally, I find this option more preferable.

document.getElementById("button").addEventListener("click", function(){alert("foo"); return false;});
#button{cursor:pointer}
<svg viewBox="-1 -1 102 102" width="6rem" role="button">
 <polygon id="button" points="25 0, 75 0, 100 50, 75 100, 25 100, 0 50" stroke="#ccc" fill="none" pointer-events="all"/>
  <text x="50" y="50" text-anchor="middle" dominant-baseline="middle" font-size="14" pointer-events="none">the Button</text>
</svg>

Alternatively, you may also consider utilizing these points:

document.getElementById("button").addEventListener("click", function(){alert("foo"); return false;});
#button{cursor:pointer}
<svg viewBox = "-50 -50 100 100" width="6rem" role="button">
  <polygon id="button" fill="none" stroke="#ccc" pointer-events="all" points = "48,0 24,41.57 -24,41.57 -48,0 -24,-41.57 24,-41.57 48,0" />

  <text text-anchor="middle" dominant-baseline="middle" font-size="14" pointer-events="none">the Button</text>
</svg>

Answer №2

Big shoutout to enxaneta for sharing the magic of clip-path: polygon with me. It was a game-changer in creating a hexagonal shape effortlessly using percentages. While I still have questions about SVG-based paths, this simple tweak in my original code did wonders:

document.getElementById("button").addEventListener("click", function() {
  alert("foo");
  return false;
});
div {
  width: 9rem;
  height: 8rem;
  position: relative;
  clip-path: polygon(25% 0, 75% 0, 100% 50%, 75% 100%, 25% 100%, 0% 50%, 25% 0);
  -webkit-clip-path: polygon(25% 0, 75% 0, 100% 50%, 75% 100%, 25% 100%, 0% 50%, 25% 0);
  background-color: #e2e3e5;
}

button {
  position: absolute;
  cursor: pointer;
  top: 2px;
  left: 2px;
  padding: calc(8rem * 0.1) calc(9rem * 0.2);
  width: calc(9rem - 4px);
  height: calc(8rem - 4px);
  clip-path: polygon(25% 0, 75% 0, 100% 50%, 75% 100%, 25% 100%, 0% 50%, 25% 0);
  -webkit-clip-path: polygon(25% 0, 75% 0, 100% 50%, 75% 100%, 25% 100%, 0% 50%, 25% 0);
  background-color: white;
  border: none;
}
<div id="div">
  <button id="button">The button</button>
</div>

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

Updates made to CSS are not reflecting on the error 404 page

Unable to Apply CSS Changes to 404 Error Page EDIT: page in question: Right from the start: I have ruled out any relative path issues. I have specified a base href in my header like this: <base href="https://hinnahackers.no/"> I am aware that thi ...

Positioning the comments box on Facebook platform allows users to

Need assistance, I recently integrated the Facebook comments box into my Arabic website, but I am facing an issue where the position of the box keeps moving to the left. Here is an example of my website: Could someone please suggest a solution to fix the ...

Can the line "as is" be included in HTML code?

Can I incorporate the following JavaScript and JSON expressions directly into HTML code? CONTRATE > 50 && SALINC <= 50 || RETAGE >= 50 { "CONTRATE": 0, "SALINC": 0, "MARSTATUS": "single", "SPOUSEDOB": "1970-01-01" } I want to be able to ...

Disable location prompt feature when using AngularJS with Maps API

For my web application, I developed a feature that includes a map with custom markers using Angular loops: <map data-ng-model="mymap" zoom="4" center="[38.50, -95.00]" style="heigth:375px"> <div ng-repeat="item in list"> <marker pos ...

Utilizing media queries in AMP for optimizing mobile website performance

Recently, I delved into the realm of utilizing AMP for my blog. After perusing the documentation and observing examples of its application, I must say that anything enhancing performance is a welcome addition to me. Therefore, I am keen on incorporating i ...

When using Flexbox with a column wrap and setting the height to min-content, the wrapping behavior may not

I'm facing a CSS challenge. I have divs of varying heights and I want the parent element to adjust its height based on the tallest child element, while keeping the rest of the elements aligned properly. The code provided below achieves this for rows b ...

Altering the appearance of an input field upon submission

https://jsfiddle.net/3vz45os8/7/ I'm working on a JSFiddle where I am trying to change the background color of input text based on whether the word is typed correctly or incorrectly. Currently, it's not functioning as expected and there are no e ...

Is there a way for an inline element like <a> to enclose block level elements within it?

<a href="#"> <h1>Our Premier Package</h1> <h2>The top choice among our clients</h2> <p> Enjoy expanded storage and quicker assistance to guarantee constant access to your cruci ...

Is it possible to incorporate Microdata from HTML5 into an XHTML Strict website while remaining compliant?

I have a website coded in XHTML 1.0 Strict and I am looking to incorporate Microdata for breadcrumbs so that Google can better understand them. Currently, my non-microdata marked-up breadcrumbs are structured like this: <ul> <li><a href= ...

What steps should I take to ensure that my dropdown menu functions properly?

I am struggling to get my navigation bar with dropdown menus to function properly. I suspect there might be an issue with the CSS code. Can you take a look at it? Here is the HTML code snippet: /*---------------------NAVBAR-------------------*/ ...

The Kendo Grid is refusing to show up within the popup window

I am new to using Angular 2 and Kendo UI. Currently, I am attempting to include a grid inside my pop-up window. While I have successfully displayed the pop-up, adding the grid has proven challenging. The grid is not appearing as expected ...

Achieving perfect alignment both horizontally and vertically using CSS3's viewport height of 100%

I'm trying to utilize CSS3's Viewport Height to create a fullscreen section with a height of 100vh. However, I am encountering difficulties in centering elements both horizontally and vertically within the section. My goal is to have an image and ...

The MIME type 'text/html' is incompatible with stylesheet MIME type and is not supported

I have exhausted all possible solutions for the issue, from specifying the type for <link rel="stylesheet" href="./style.css" /> to using app.use(express.static('public')), but unfortunately, none of them seem to resolve the problem. index ...

Using JQuery to retrieve part of a className results in a null value being returned

I am facing an issue with a piece of code in codesandbox where it returns null when I attempt to retrieve part of the className from a div using JQuery. How can I troubleshoot this and make it work? Check out the Codesandbox Example import React, { Compo ...

What is the best way to style multiple classes within the same element (specifically an ordered list ol

Using CSS counters, I have created various styles for an ordered list <ol> by applying different classes and adjusting the styles accordingly. Now, I want the counters to adjust based on screen size, displaying smaller numbers on smaller screens. To ...

How can the horizontal scroll bar width be adjusted? Is it possible to create a custom

Within my div, I have implemented multiple cards that scroll horizontally using the CSS property: overflow-x: scroll; This setup results in a horizontal scrollbar appearing under the div, which serves its purpose. However, I would prefer a customized scr ...

Is there a way to convert HTML into a structured DOM tree while considering its original source location?

I am currently developing a user script that is designed to operate on https://example.net. This script executes fetch requests for HTML documents from https://example.com, with the intention of parsing them into HTML DOM trees. The challenge I face arise ...

Can MUI 5 replicate the Emotions 'css' function through analog?

From what I've learned, MUI 5 utilizes Emotion for its styling framework. Emotion offers a convenient helper function called css, which generates a string - a class name that can be used as a value for the className property or any other relevant prop ...

HTML with embedded PHP script

i cannot seem to display mysql data in html format. the table i am working with is called App appid | app | version | date | apk file | 1 sample 1.0.0 2012-10-12 sample.apk //here is the mysql query ...

How can I design a search box similar to the one on the android.com website?

Is it possible to replicate the search box effect on the Android website, where the menu items fade out and the search box expands upon clicking the search icon?view image here See the difference before and after clicking the search icon ...