How to make angled tab like this using css?

2019-01-23 13:39发布

问题:

I need to make like this. Is it possible with pure css?

回答1:

Using border-radius, :before and transform: skew(...);

body {
  background-color: #000;
  margin: 0;
  padding: 0;
  font-family: sans-serif;
  font-size: 16px;
}
.tab {
  height: 50px;
  width: 150px;
  border-radius: 15px 15px 0px 0px;
  background-color: #FFF;
  position: relative;
  top: 10px;
  left: 1px;
  display: inline-block;
  z-index: 2;
}
.tab:before {
  height: 50px;
  width: 70px;
  border-radius: 10px 10px 0px 0px;
  background-color: white;
  content: "";
  position: absolute;
  left: 104px;
  top: 0px;
  -webkit-transform: skewX(40deg);
  transform: skewX(40deg);
  z-index: -1;
}
.tab:nth-of-type(2) {
  background-color: #555;
  top: 10px;
  left: 30px;
  z-index: 1;
  color: #EEE;
}
.tab:nth-of-type(2):before {
  background-color: #555;
}
.tab:nth-of-type(2):hover,
.tab:nth-of-type(2):hover:before {
  background-color: #159;
  transition: 0.3s ease-out;
}
span {
  display: inline-block;
  width: 160px;
  text-align: center;
  height: 50px;
  line-height: 50px;
  z-index: 3;
}
#page {
  background-color: white;
  height: calc(100vh - 120px);
  width: calc(100vw - 61px);
  position: relative;
  top: 10px;
  left: 1px;
  padding: 30px;
}
<nav id="tabs">
  <div class="tab">
    <span>Tab 1</span>
  </div>
  <div class="tab">
    <span>Tab 2</span>
  </div>
</nav>
<div id="page">
  Lorem Ipsum dolor sit amet ...
</div>



回答2:

With CSS3, use border-radius to make the curved tab corner, then create a triangle with a lower z-index.

The HTML:

<div class="tab">
    <div class="arrow"></div>
</div>

The CSS:

body
{
    background-color: #666;    
}
.tab
{
    height: 50px;
    width: 150px;
    border-radius: 10px 10px 0px 0px;
    background-color: #FFF;
    position: relative;
}

.arrow
{
  border-color: transparent transparent #FFF #FFF;
  border-style: solid;
  border-width: 23px 23px 23px 23px;
  height:0;
  width:0;
  position:absolute;
  bottom:0px;
  right:-43px;
}

The result: http://jsfiddle.net/P3P3Z/2/

It's not perfect and it may render differently on different browsers, but should get you started. :) Some things have to be tweaked a little so it looks nice.



回答3:

This question intrigued me so here is a working example.

<html>

<style type="text/css">

body {
    background: #000000;
    }
#header {
    height: 29px;
    overflow: hidden;
    }
#content {
    background: #ffffff;
    min-height: 200px;
    padding: 10px;
    border-top-right-radius: 10px;
    border-top-right-radius: 10px;
    }

#on {
    background: #ffffff;
    display: inline-block;
    padding: 5px;
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
    vertical-align: top;
    }

#off {
    background: #888888;
    display: inline-block;
    padding: 5px;
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
    vertical-align: top;
    margin-left: -40px;
    }

#seperatoron {
    border: none;
    border-left: 25px solid white;
    border-bottom: 0px;
    height: 0px;
    width: 0px;
    display: inline-block;
    position: relative;
    margin-top: 10px;
    border-right: 25px solid transparent;
    border-top: 25px solid transparent;
    margin-top: 4px;
    margin-left: -2px;
    }

#seperatoroff {
    border: none;
    border-left: 25px solid #888888;
    border-bottom: 0px;
    height: 0px;
    width: 0px;
    display: inline-block;
    position: relative;
    margin-top: 10px;
    border-right: 25px solid transparent;
    border-top: 25px solid transparent;
    margin-top: 4px;
    margin-left: -2px;
    }

</style>


<body>

<div id="header">
<div id="on">Tab 1</div><div id="seperatoron"></div>
<div id="off">Tab 2</div><div id="seperatoroff"></div>
</div>

<div id="content">

Document content section.

</div>

</body>
</html>


回答4:

The rounded border in the upper-left corner is possible with the border-radius property (not in old IE versions). However, the right side of each tab should be done with an image, svg or a rotated div with a css3 transform, but that will give you a headache for sure.

If I were you, I would go for a border radius for topleft and a background-image aligned to the right:

border-top-left-radius: 10px;
background: white url(tab.gif) right top no-repeat;

I think you should also give the second tab a negative margin-left.



回答5:

Here is the window typed tab buttons with css

.tablist{
    padding:0;
    margin:0;
    list-style:none;
    display:flex;
    justify-content:center;
}
.tablist>li{
}
.tablist >li>a{
    position: relative;
    display: block;
    color: red;
    text-decoration: none;
    line-height: 1.4;
    border-top: 1px solid red;
    border-radius: 6px 6px 0 0;
    padding: 3px 10px;
    margin: 0 5px;
}
.tablist >li>a:before{
    position: absolute;
    content: "";
    width: 9px;
    height: 29px;
    background: white;
    transform: rotate(21deg);
    border-left: 1px solid red;
    left: -5px;
    top: 2px;
    z-index: -1;
    border-radius: 2px 0 0 0px;

}
.tablist >li>a:after{
    position: absolute;
    content: "";
    width: 9px;
    height: 29px;
    background: white;
    transform: rotate(-21deg);
    border-right: 1px solid red;
    right: -5px;
    top: 2px;
    z-index: -1;
    border-radius: 2px 0 0 0px;

}

And the html will be like this

<ul class="tablist">
<li><a href="jasscript:void(0)">Home</a></li>
<li><a href="jasscript:void(0)">About</a></li>
<li><a href="jasscript:void(0)">Contact us</a></li>
<li><a href="jasscript:void(0)">Blog</a></li>
<li><a href="jasscript:void(0)">Home</a></li>
</ul>