Trouble getting inline SVG to scale properly with its parent

Attempting to utilize SVG as an alternative to clippath due to limited cross-browser support has proven challenging. Each time I make the attempt, I face the same issue: the SVG functions properly but appears small and does not scale to fit the designated element. Despite numerous efforts, I have been unable to pinpoint the error.

<div id="container">  
  <div id="target">
    <div id="Box_Collectors">      
      <div class="blackBox">
        <h1>TITLE</h1>
        <div>One morning, when Gregor Samsa woke from troubled dreams...
      </div>

       <div class="blackBox">
        <h1>TITLE</h1>
        <div> One morning, when Gregor Samsa woke from troubled dreams...
      </div>

       <div class="blackBox">
        <h1>TITLE</h1>
        <div>One morning, when Gregor Samsa woke from troubled dreams...        
      </div>      
    </div>
  </div>


<svg xmlns="http://www.w3.org/2000/svg" width="0" height="0"
     xmlns:xlink="http://www.w3.org/1999/xlink"
     xmlns:ev="http://www.w3.org/2001/xml-events"
     class="" viewBox="0 0 100 100" clipPathUnits="objectBoundingBox">
      <defs>
        <clipPath id="blackBox-clip">
          <polygon points="100 0, 100 100, 0 100, 0 35, 15 25, 0 15, 0 0"></polygon>          
      </clipPath>
      </defs>
    </svg>

</div>

The CSS is provided below:

* { color: #fff }

.blackBox {
  background: #000;
  position: relative;
  z-index: 100;
  height: 300px;
  width: 300px;
  margin: auto;
  padding-left: 60px;
  -webkit-clip-path: url("#blackBox-clip");
  clip-path: url("#blackBox-clip");
}

View the demonstration of the issue here.

Answer №1

To achieve the desired effect, there are two key elements needed to make it work seamlessly. Firstly, clipPathUnits must be included within the clipPath element, and the units designated for objectBoundingBox should represent fractions of the bounding box. Implementing the following code snippet will produce the desired outcome:

<clipPath id="blackBox-clip" clipPathUnits="objectBoundingBox">
        <polygon points="1 0, 1 1, 0 1, 0 0.35, 0.15 0.25, 0 0.15, 0 0"></polygon>
</clipPath>

* {
  color: #fff
}
.blackBox {
  background: #000;
  position: relative;
  z-index: 100;
  height: 300px;
  width: 300px;
  margin: auto;
  padding-left: 60px;
  /*-webkit-clip-path: polygon(100% 0, 100% 100%, 0 100%, 0 35%, 15% 25%, 0 15%, 0 0);
clip-path: polygon(100% 0, 100% 100%, 0 100%, 0 35%, 15% 25%, 0 15%, 0 0);*/
  -webkit-clip-path: url("#blackBox-clip");
  clip-path: url("#blackBox-clip");
}
<div id="container">
  <div id="target">
    <div id="Box_Collectors">
      <div class="blackBox">
        <h1>TITLE</h1>
        <div>One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin. He lay on his armour-like back, and if he lifted his head a little he could see his brown belly, slightly domed and divided
          by arches into stiff sections. The bedding was hardly able to cover it and seemed ready to slide off any moment. His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked.</div>
      </div>

      <div class="blackBox">
        <h1>TITLE</h1>
        <div>One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin. He lay on his armour-like back, and if he lifted his head a little he could see his brown belly, slightly domed and divided
          by arches into stiff sections. The bedding was hardly able to cover it and seemed ready to slide off any moment. His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked.</div>
      </div>

      <div class="blackBox">
        <h1>TITLE</h1>
        <div>One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin. He lay on his armour-like back, and if he lifted his head a little he could see his brown belly, slightly domed and divided
          by arches into stiff sections. The bedding was hardly able to cover it and seemed ready to slide off any moment. His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked.</div>
      </div>
    </div>
  </div>


  <svg xmlns="http://www.w3.org/2000/svg" width="0" height="0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ev="http://www.w3.org/2001/xml-events" class="" viewBox="0 0 100 100">
    <defs>
      <clipPath id="blackBox-clip" clipPathUnits="objectBoundingBox">
        <polygon points="1 0, 1 1, 0 1, 0 0.35, 0.15 0.25, 0 0.15, 0 0"></polygon>
      </clipPath>
    </defs>
  </svg>

</div>

Answer №2

While using Safari version 10.0.2 (12602.3.12.0.1), I am experiencing a strange issue where only the first box is rendered. Interestingly, on my work-in-progress website, the boxes reappear when I scroll up to a specific point on the page.

Do you have any insights into what might be causing this behavior?

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

Top-notch loading icon using jquery

Recently, I embarked on the journey of learning Javascript and jQuery simultaneously, and to my surprise, it feels quite manageable. As I delve deeper into creating functions, I am currently working on displaying a loading image. While I initially used on ...

Establish a buffering system for the <video> element

Currently, I am facing an issue with playing videos from a remote server as they take an extended amount of time to start. It appears that the entire video must be downloaded before playback begins. Is there a way to configure the videos so they can begi ...

Backdrop styling for Material-UI dialogs/modals

Is there a way to customize the semi-transparent overlay of a material-ui dialog or modal? I am currently using material-ui with React and Typescript. https://i.stack.imgur.com/ODQvN.png Instead of a dark transparent overlay, I would like it to be transp ...

Generating HTML content using Angular 8 and JSON data

Currently, I am managing an Angular Storybook that consists of various components. Within the stories.ts file of a component, there is a JSON snippet containing properties such as the content of a DIV element, shown below... { "accordionLink": ' ...

Is there a way to align both the horizontal and rotated lines to intersect at a common point in HTML and CSS?

As a beginner, I'm looking to create a structure resembling a thigh, leg, and ankle using thick lines. My aim is to rotate them with animation, ensuring that the joints between the lines mimic those of a knee joint and hip joint. Below is the code sn ...

Tips for retrieving JSON data from an AJAX call and displaying it pre-filled in an input field

Here is the code snippet I used to receive a response in JSON format, but when I try to display the response using alert(response.Subject);, it shows as "undefined". HTML: <input type="text" id="subject" value='Subject'> Javascript: $.a ...

Seamless movements to the exact midpoint

I am trying to find a method to create a smooth transition when removing the center class from p.title.class, allowing it to smoothly move to its new position. JSfiddle Here is an excerpt of my HTML: <body> <div class="wrapper-top"> ...

Retrieving information from SQL and transferring it to PHP

I'm having trouble accessing data from mySQL to display on an HTML Page. Can someone assist? Here are the necessary details: Host: 127.0.0.1:8889 username: root password: root database-name: gibsonek table-name: events Below is the code I'm cu ...

Incorporating a header include on every page throughout my website

Is there a way to include headers and footers in your HTML web pages without having to duplicate code on every page? I understand that this can be done using PHP, but what if you are creating a website solely with HTML? If there is no solution to avoid r ...

Achieving proper stacking of a table with other div elements

I've come across some similar inquiries, but none seem to fully address the specific issue at hand. If there's an existing thread on this that I overlooked, I apologize in advance. So, here's a multi-part problem: my solution for the first ...

What is the best way to convert HTML into a format where the nesting implied by header levels is made clear and explicit?

Every time I attempt web scraping, I encounter a recurring issue in different forms that I can't seem to overcome. Essentially, the problem lies in the structure of HTML which has a somewhat flat organization with implicit nesting. My goal is to make ...

Methods for activating touch-like scrolling through the use of mouse grabbing and dragging

Let me show you a simple illustration of the task I'm attempting to accomplish: <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> ...

CSS Toggle failing to show updated styles

Initially, I successfully changed the font and color of text on Google using a simple code. However, when I attempted to incorporate it into a toggle on / off switch, the changes did not take effect. To address this issue, I sought assistance from ChatGPT ...

Loading an animated SVG sprite file in real-time

Recently, I received an SVG sprite file from our designers to use in my app. The specified convention is to load the sprite at the top of the <body> element and then render icons using a specific code snippet: <svg class="u-icon-gear-dims"> ...

Having trouble with Bootstrap 4: I want to rotate an image, but every time I try, it ends up overwriting the CSS properties

I am currently working on a website using bootstrap 4, and I'm facing an issue with the image I selected. It seems to rotate to the right and is not displaying correctly. click here for image preview The main problem lies in rotating the image witho ...

Creating a button with multiple background gradients using CSS

When working on a button element that already boasts a gradient effect, my aim was to integrate a small play image directly in the center. However, achieving this without compromising the current layout and ensuring compatibility across various browsers pr ...

What are the steps to create a dynamic navigation menu in Angular 2?

I have successfully implemented this design using vanilla CSS and JS, but I am encountering challenges when trying to replicate it in Angular 2. Setting aside routing concerns, here is the current state of my component: navbar.component.ts import { Comp ...

What modifications were implemented in Bootstrap 5 to cause controls to automatically assume their maximum size?

After upgrading NodeJs to version 16.13.0 from 14.x, one of the modules that was updated was bootstrap. This caused me to remove my Jumbotron control due to the upgrade, although I don't believe that is directly related to the issue I am experiencing. ...

Gmail not displaying image - troubleshoot with Django Python-Postmark

Having trouble displaying static images in Gmail when using django python-postmark for mailing. Gmail is appending a url proxy to the src of the images, which causes them not to display properly. What might I be missing here? How can I solve this issue? Th ...

PHP function with JSON response encountered an error during the AJAX call

I am currently working on creating a News Ticker that utilizes PHP, Javascript, and AJAX. The first step involved creating a PHP function called getFeed(), which gathers data from various news websites into an Array. This data is then returned in JSON form ...