带有相交线的圆角矩形

时间:2017-05-30 10:47:08

标签: html css flexbox

我正在尝试使用flexbox制作带相交线的矩形。这就是我想做的事情

enter image description here

HTML

<div id="nav-bar">
    <div class="item">
        <button>TEXT</button>
    </div>
    <div class="item">
        <button>TEXT</button>
    </div>
    <div class="item">
        <button>TEXT</button>
    </div>
    <div class="item">
        <button>TEXT</button>
    </div>
</div>

Here is fiddle

2 个答案:

答案 0 :(得分:2)

您应该可以使用几个绝对定位的伪元素来执行此操作:

&#13;
&#13;
html,
body {
  padding: 0;
  margin: 0;
  background-color: #e24d3d;
}

button {
  font-family: 'Raleway', sans-serif;
  font-size: 30px;
  font-weight: 700;
  text-transform: uppercase;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: transparent;
  border: none;
  color: white;
  height: 62px;
  width: 130px;
}

#nav-bar {
  max-width: 300px;
  height: 150px;
  margin: 10px auto;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  flex-direction: column;
  background-color: rgba(255, 255, 255, .1);
  border: 1px solid #FFF;
  border-color: rgba(255, 255, 255, 0.70);
  border-radius: 10px;
  
  /* add these styles */
  position: relative;
  overflow: hidden;
}

.item {
  margin: 0px auto;
  background-color: transparent;
  color: #000;
  display: flex;
  text-align: center;
  align-items: center;
  height: 50%;
  width: 50%;
}

/* added the below */

#nav-bar:before,
#nav-bar:after {
  content: '';
  display: block;
  position: absolute;
}

#nav-bar:before {
  left: 0;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
  border-top: 1px solid white;
}

#nav-bar:after {
  top:-5px; 
  bottom:-5px; 
  left:50%; 
  transform:rotate(10deg);
  border-left: 1px solid white;
}
&#13;
<div id="nav-bar">
  <div class="item">
    <button>TEXT</button>
  </div>
  <div class="item">
    <button>TEXT</button>
  </div>
  <div class="item">
    <button>TEXT</button>
  </div>
  <div class="item">
    <button>TEXT</button>
  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

使用绝对定位伪,将它们设置在左侧/顶部item,它们将随文本缩放。

Fiddle demo

Stack snippet

&#13;
&#13;
html, body {
  margin: 0;
  background-color: #e24d3d;
}
button {
    font-family: 'Raleway', sans-serif;
    font-size: 30px;
    font-weight: 700;
    text-transform: uppercase;
    display: flex;
    flex-direction: column;
    justify-content: center;
    background-color: transparent;
    border: none;
    color: white;
    min-height: 62px;
    min-width: 130px;
    margin-left: 10px;
}
#nav-bar {
  max-width: 500px;
  min-width: 300px;
  height: 150px;
  margin: 10px auto;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  flex-direction: column;
  background-color: rgba(255, 255, 255, .1);
  border: 1px solid #FFF;
  border-color: rgba(255, 255, 255, 0.70);
  border-radius: 10px;
  overflow: hidden;
}
.item {
  position: relative;
  background-color: transparent;
  color: #000;
}
.item button {
  display: flex;
  justify-content: center;
  align-items: center;
}
.item:nth-child(1):before, .item:nth-child(1):after {
  content: '';
  position: absolute;
  background-color: #000;
}
.item:nth-child(1):before {
  left: 0;
  top: 100%;
  width: 200%;
  height: 1px;
}
.item:nth-child(1):after {
  left: 100%;
  top: -25%;
  width: 1px;
  height: 250%;
  transform: rotate(10deg);
  transform-origin: center center;
}
&#13;
<div id="nav-bar">
  <div class="item"><button>TEXT</button></div>
  <div class="item"><button>Some large text</button></div>
  <div class="item"><button>TEXT</button></div>
  <div class="item"><button>TEXT</button></div>
</div>
&#13;
&#13;
&#13;