Search code examples
htmlcsscss-shapes

How to bevel the corner of a block div?


I have the following HTML document:

<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="utf-8">
        <title>Тег DIV</title>
        <style type="text/css">
            .block1 { 
                width: 200px; 
                background: #ccc;
                padding: 5px;
                padding-right: 20px; 
                border: solid 1px black; 
                float: left;
            }
        </style> 
    </head>
    <body>
        <div class="block1">Text Content</div>
    </body>
</html>

How do I describe the style to get the following? enter image description here


Solution

  • Until CSS4 border-corner-shape property is in danger! and it's not implemented, This can be done by using CSS3 transforms (to keep border property free for further usage):

    HTML:

    <div class="box">
      Text Content
    </div>
    

    CSS:

    .box {
      width: 200px; 
      height: 35px;
      line-height: 35px;
      padding: 0 5px;
      background-color: #ccc;
      padding-right: 20px;
      border: solid 1px black;
      border-right: 0;  
      position: relative;
    }
    
    .box:after {
      content: "";
      display: block;
      background-color: #ccc;
      border: solid 1px black;
      border-left: 0;
      width: 35px;
      height: 35px;
      position: absolute;
      z-index: -1;
      top: -1px; /* pull it up because of 1px border */
      right: -17.5px; /* 35px / 2 */
      transform: skew(-45deg);
      -o-transform: skew(-45deg);
      -moz-transform: skew(-45deg);
      -webkit-transform: skew(-45deg);
    }
    

    Here is JSBin Demo.

    NOTE: There's another div in example above has class attribute of box2, which implemented without using CSS3 declarations that you might consider using it ;)