Tips for creating a corner border using svg illustrations

I've created a div with a dynamic size and border but I'm looking to add corner borders, including one skewed corner, using svg. However, I'm running into an issue where the vertical and horizontal lines are not appearing at the same size.

I've tried adjusting the preserveAspectRatio attribute in different ways but haven't been successful in achieving equal line sizes for both directions within the div.

If anyone has suggestions on how to make the vertical and horizontal lines the same size in svg while filling the div, please feel free to share.

.container {
  position: relative;
  background-color: black;
  border: 1px solid red;
  padding: 25px;
  color:white;
}

.container > svg {
  position: absolute;
  left: 0;
  top: 0;
}

.container > svg > path {
  stroke: red;
}
 <div class="container">
      <svg width='100%' height='100%' viewBox='0 0 100 100' preserveAspectRatio='none'>
        <path
          d='M0,0 H25 M0,0 V25'
          fill='none'
          stroke-width='5'
          vector-effect='non-scaling-stroke'
        />
        <path
          d='M90,0 L75,0 M90,0 L100,10 M100,10 L100,25'
          fill='none'
          stroke-width='5'
          vector-effect='non-scaling-stroke'
        />
        <path
          d='M100,100 L75,100 M100,100 L100,75'
          fill='none'
          stroke-width='5'
          vector-effect='non-scaling-stroke'
        />
        <path
          d='M0,90 L0,75 M0,90 L10,100 M10,100 L25,100'
          fill='none'
          stroke-width='5'
          vector-effect='non-scaling-stroke'
        />
      </svg>
      <p>fancy text here</p>
    </div>

Answer №1

If you want to achieve this effect using CSS, you can use the following code:

.container { 
  --c: red;  
  --b: 20px; 
  --t: 3px;  
  --l: 40px; 
  
  --g:linear-gradient(red 0 0);
  background: 
    var(--g) 0 0 / var(--l) var(--t), 
    var(--g) 0 0 / var(--t) var(--l), 
    var(--g) 100% 0 / var(--l) var(--t), 
    var(--g) 100% 0 / var(--t) var(--l), 
    var(--g) 100% 100% / var(--l) var(--t), 
    var(--g) 100% 100% / var(--t) var(--l), 
    var(--g) 0 100% / var(--l) var(--t), 
    var(--g) 0 100% / var(--t) var(--l),
    linear-gradient(  45deg,red calc(50% + var(--t)),#0000 0) 0    100%/var(--b) var(--b),
    linear-gradient(-135deg,red calc(50% + var(--t)),#0000 0) 100% 0   /var(--b) var(--b),
    lightblue;
  background-repeat: no-repeat;
  clip-path:polygon(0 0,calc(100% - var(--b)) 0,100% var(--b),100% 100%,var(--b) 100%,0 calc(100% - var(--b)));
  padding: 50px 25px;
  position:relative;
}
<div class="container">
  <p>your fancy text goes here</p>
</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

The custom directive containing ng-switch isn't being reevaluated when the value is updated

I have developed a unique directive that acts as a reusable form. The form includes an error message display section which utilizes ng-switch: <div ng-switch="status"> <span ng-switch-when="Error" class="text-error">An Error occurred</spa ...

JavaScript is not designed to run a second time

Currently, I have a script set up using $(document).ready(). It works perfectly upon initial loading. However, I also need this script to execute whenever the user changes an HTML select control. Ideally, upon page load, I want the filter and sort functio ...

Why won't the audio tag play any audio on IE10 Windows7?

Encountering issues with the audio tag on Internet Explorer 10/Windows 7. Discovered a test page where IE is unable to display any audio tags. Interestingly, when the file is saved and tested locally, it works perfectly (see image below). Confirming that ...

Incorporate a jQuery userscript on Firefox that includes a link to a form

Attempting to incorporate a search link into an online form using a userscript with jQuery, specifically for Firefox. I typically work in Chrome, so I'm encountering some challenges with compatibility in Firefox. However, I need this to be functional ...

What is the most effective method for centering the title text in a div while keeping the remaining text left-aligned?

I am trying to create a layout with 6 divs arranged in two rows of 3. Each div should have a centered title, a centered image, and text aligned to the left underneath. What is the best approach to achieve this layout? Is it possible to center the title u ...

Step by step guide on incorporating two background images in a single header

I'm looking to create a header with a background image that appears twice, like this: To achieve the effect, I added opacity of 0.5 to one of the images, but it is affecting everything in my header including text and logo. Is there a way to prevent t ...

Tips for retrieving HTML file content as a string using JavaScript

I'm looking to retrieve the contents of an HTML file using JavaScript and store it as a string. I tried writing some code for this purpose, but unfortunately, I encountered an error: Error: Cannot find module 'xmlhttprequest' Could someone ...

A website with two distinct pages, where only the first page is refreshed

I have split the content of my HTML page into 2 separate subpages, based on the references provided below: Multiple distinct pages in one HTML file This is how my code is structured: ... <script> function show(shown, hidden) { document.getEle ...

The background image is not adjusting properly to the browser when resized

I'm having trouble keeping my background image fitted to the browser when I resize the window. Can someone please assist me with this issue? Here are links to images that illustrate what's happening: View images. The first image shows how it shou ...

What is the best way to have my items fill the entire column space using flexbox?

Having 2 containers and wanting them to have the same height has been a challenge. I've been experimenting with flexbox and the flex-direction: column (flex-column) property, but I can't seem to get each element to divide the available container ...

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 is the process for setting a background image in a React application?

I need help figuring out how to add a background image to my React app. I've searched for solutions on StackOverflow, but I'm still unsure. The image I want to use is named background.jpeg and it's located in the same folder as my App.js fil ...

When interacting with elements with the "user-select: none" property, WkWebView still allows text selection

When using an iOS iPad app with a single fullscreen WKWebView, an issue arises when long-pressing on an area with user-select:none - the first text in the next available area without user-select:none gets selected instead of being ignored. Is there a way t ...

Challenges in accurately capturing canvas drawing masks using HTML5 and JavaScript for image inpainting

I have been developing an image inpainting tool that allows users to mark areas they want to remove on a canvas. These drawn masks are then sent to the server for processing. However, I am facing an issue where the produced mask does not accurately align w ...

Ways to conceal a div using ng-hide

Is there a way to make a div disappear using ng-hide? I have this code and I'm trying to hide the div with id="ApagarNA". The code displays a table with values, but some rows contain N.A in all 4 columns, and I want to hide those specific rows. For e ...

Permanent Visibility of Bootstrap 4 Navbar Toggler Icon

I am facing an issue with my navbar-toggler as it remains visible even on a large screen where it is not needed. To view the file, click here. <div class="navbar-header"> <a href="#" class="navbar-brand">Logo</a> </di ...

Encountering issues with basic login functionality using Node.js and MongoDB - receiving a "Cannot

I'm experiencing some difficulties trying to set up a login for my website. I have a user registered in my database with the email: [email protected] and password 123. The issue arises when I attempt to use the POST method, as it returns the fo ...

Unable to adjust the text color to white

As someone who is new to Typescript, I'm facing a challenge in changing the text color to white and struggling to find a solution. I'm hoping that someone can guide me in the right direction as I've tried multiple approaches without success ...

The malfunctioning Bootstrap navbar dropdown menu is causing a disruption in user experience by unexpectedly logging the user

I'm having an issue with the dropdown menu on my angular app. Every time I click on the Profile link to access the Account and Logout options, it automatically logs me out. <nav class="navbar navbar-expand-lg bg-dark navbar-fixed-top" ...

Troubleshooting Issue with Angular Property Binding for Image Dimensions

Currently, I am exploring property binding in Angular through an example. The idea is to use an image and bind its width, height, and src attributes using property binding. Surprisingly, even though there are no errors and the image renders successfully vi ...