Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Square with rounded corners and indented curved border

I was wondering if it is possible to make a square with round corners and a indented border in pure CSS.

Currently I have this:

#custom-square {
     position: relative;
     display: block;
     width: 75px;
     height: 75px;
     border: 2px solid #8A6EF1;
     border-radius: 10px;
     background-color: white;
}

Squarez with rounded corners and indented border

like image 687
vweltje Avatar asked Dec 18 '15 15:12

vweltje


People also ask

What is a square with rounded corners called?

A squircle (FIGURE 1) is a square with rounded corners that, when incorporated into stencil aperture designs for small area arrays, provides better print quality than either a square or a circle alone.

What property is used to create rounded corners on a border?

The border-radius CSS property rounds the corners of an element's outer border edge. You can set a single radius to make circular corners, or two radii to make elliptical corners.

What is border corner shape?

Border Corner Shape allows us to manipulate element corners further. While we can create rounded corners by using border-radius , the Border Corner Shape allows us to form beveled corners, scoop-style corners, and rectangle-notch corners.


3 Answers

Considering the hassle and amount of code needed to align double curves with CSS, SVG seems way more appropriate. A few other reasons to go for svg here are :

  • control of the path (color, width, curve...)
  • control the fill with a plain color, gradient or image
  • less code
  • you can display it over a non plain background (gradient or image)
  • maintain the boundaries of the shape for user interactions (hover, click...)

Here is a basic example using an inline svg with a path element.
The curves are drawn with Cubic Bezier curves :

svg{width:30%;}
<svg viewbox="0 0 10 10">
  <path d="M1.5 0.5 Q5 1 8.5 0.5 Q9.5 0.5 9.5 1.5 Q9 5 9.5 8.5 Q9.5 9.5 8.5 9.5 Q5 9 1.5 9.5 Q0.5 9.5 0.5 8.5 Q1 5 0.5 1.5 Q0.5 0.5 1.5 0.5z" 
        fill="none" stroke-width="0.2" stroke="#8A6FF2" />
</svg>
like image 99
web-tiki Avatar answered Oct 13 '22 00:10

web-tiki


Another pure CSS approach for creating this border would be to make use of border-image property. All that is required is create an image with the required border shape and set it to an element using the border-image-source property.

.shape.large {
  height: 300px;
  width: 300px;
  border-image-source: url(http://i.stack.imgur.com/Qkh6A.png);
  border-image-width: 34px; /* the width of the border portions in the image - refer to image at the end of the answer for the exact portion details*/
  border-image-slice: 34; /* equal to border-image-width */
  border-width: 34px; /* equal to border-image-width */
}
.shape.small {
  height: 100px;
  width: 100px;
  border-image-source: url(http://i.stack.imgur.com/Mra4B.png);
  border-image-width: 14px;
  border-image-slice: 14;
  border-width: 14px;
}
.shape.small.fill {
  background: aliceblue content-box;
  border-image-source: url(http://i.stack.imgur.com/Ovj03.png);
  border-width: 14px;
}

/* Just for demo */

body {
  background: url(http://lorempixel.com/800/800/abstract/2);
}
.shape.small {
  float: left;
}
.shape.large {
  clear: both;
}
<div class='shape small'>Some content</div>
<div class='shape small fill'>Some content</div>
<div class='shape large'>Some content</div>

At present this method is definitely not much advantageous compared to SVG but it is an option and in my opinion is better than the other CSS only approaches that are possible.

The advantages of this approach are:

  • Very minimal and low complexity code.
  • Better control over the curves and their radii (like with SVG) because the image with the required border curvature can be created separately.
  • Can be placed on top of an image or a gradient background.
  • Can be made to degrade gracefully (into a solid square border) in browser's that don't support it.

The drawbacks are:

  • The container is still a square and so hover effects will not be restricted to the boundaries of the shape unlike with SVG.
  • Adding solid color fill to the shape is possible (by using a filled version of the image) but adding a gradient or image fill is tricky because borders are still blocks (that is, there are transparent areas on either side of the curves).
  • The output is responsive but as dimensions increase or decrease beyond a threshold, the shape starts to look a bit compressed or stretched. So, this is more suited for break-point based design.
  • The browser support is not bad but is not great either. It works in Chrome, Firefox, Safari, Opera and IE11+.

Calculation of Border Image Width:

The width or height of border area (which becomes the border-image-width) is nothing but the width of the portion highlighted in the below image.

Highlighted portion defines border-image-width

like image 31
Harry Avatar answered Oct 13 '22 02:10

Harry


This draft mock up is as close as i could get it to pure CSS, but still requires a nested div. You would need to tweak the sizing / radius for the before / after circles.

Pen

div {

  position: absolute;
  top: 100px;
  left: 100px;
  width: 100px;
  height: 100px; 
  border: 4px solid purple;
  border-radius: 30px;
  //overflow: hidden;
  box-sizing: border-box;

  &:before {
    position: absolute;
    top: -4px;
    left: -94px;
    content: ' ';
    width: 100px;
    height: 100px;
    border: 4px solid purple;
    border-radius: 50px;
    box-sizing: border-box;
    background-color: white;
    clip: rect(0px, 100px, 100px, 90px);
  }

  &:after {
    position: absolute;
    top: -4px;
    right: -94px;
    content: ' ';
    width: 100px;
    height: 100px;
    border: 4px solid purple;
    border-radius: 50px;
    box-sizing: border-box;
    background-color: white;
    clip: rect(0px, 10px, 100px, 0px);
  }
}

div > div {
  position: absolute;
  top: -4px;
  left: -4px;
  transform: rotate(90deg);
  border-color: transparent;
}
like image 24
plarner7 Avatar answered Oct 13 '22 00:10

plarner7