如何在CSS中使用对角网格制作响应式菜单?

时间:2017-10-16 07:47:43

标签: html css html5 css3 user-interface

我正在为网站创建模板。在那里我想创建一个这样的菜单 enter image description here

我发现以下代码作为解决方案

但是我无法将div分成4个部分并以响应方式添加文本。可以使用position:absolute来完成,但它没有响应。如何以响应方式使用css实现这一目标?



.background {
  background-color: #BCBCBC;
  width: 100px;
  height: 50px;
  padding: 0;
  margin: 0
}

.line1 {
  width: 112px;
  height: 47px;
  border-bottom: 1px solid red;
  -webkit-transform: translateY(-20px) translateX(5px) rotate(27deg);
  position: absolute;
  /* top: -20px; */
}

.line2 {
  width: 112px;
  height: 47px;
  border-bottom: 1px solid green;
  -webkit-transform: translateY(20px) translateX(5px) rotate(-26deg);
  position: absolute;
  top: -33px;
  left: -13px;
}

<div class="background">
  <div class="line1"></div>
  <div class="line2"></div>
</div>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:4)

您可以使用 CSS Flexbox &amp; CSS转换属性实现此目的。请看下面的代码段:

&#13;
&#13;
body {
  padding: 20px;
}

.menu-cover {
  width: 360px;
  height: 360px;
  overflow: hidden;
  background: #eee;
}

.menu {
  list-style: none;
  margin: -70px 0 0 -70px;
  padding: 80px;
  display: flex;
  flex-wrap: wrap;
  width: calc(600px - 100px);
  height: calc(600px - 100px);
  position: relative;
  transform: rotate(45deg);
  transform-origin: center;
}

.menu:before {
  content: '';
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 1px;
  height: 100%;
  background-color: #aaa;
}

.menu:after {
  content: '';
  position: absolute;
  top: 50%;
  left: 0;
  transform: translateY(-50%);
  width: 100%;
  height: 1px;
  background-color: #aaa;
}

.item {
  width: 50%;
}

.item a {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  transform: rotate(-45deg);
}
&#13;
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet"/>
<div class="menu-cover">
  <ul class="menu">
    <li class="item">
      <a href="#">Bussiness</a>
    </li>
    <li class="item">
      <a href="#">Team</a>
    </li>
    <li class="item">
      <a href="#">Talent</a>
    </li>
    <li class="item">
      <a href="#">Group</a>
    </li>
  </ul>
</div>
&#13;
&#13;
&#13;

希望这有帮助!

答案 1 :(得分:0)

请检查以下代码。您可以以相同的方式实现您的要求。我添加了<span>标签来显示文字。

&#13;
&#13;
.background {
  background-color: #BCBCBC;
  width: 100px;
  height: 50px;
  padding: 0;
  margin: 0
}

.line1 {
  width: 112px;
  height: 47px;
  border-bottom: 1px solid red;
  -webkit-transform: translateY(-20px) translateX(5px) rotate(27deg);
  position: absolute;
  /* top: -20px; */
}

.line2 {
  width: 112px;
  height: 47px;
  border-bottom: 1px solid green;
  -webkit-transform: translateY(20px) translateX(5px) rotate(-26deg);
  position: absolute;
  top: -33px;
  left: -13px;
}
&#13;
<div class="background">
  <span style="float:left;padding-left:20%">Business
  </span>
    <span style="float:left;padding-left:5%">Team
  </span>
    <span style="float:right;padding-left:5%">Talent
  </span>
    <span style="float:left;padding-left:20%">Group
  </span>
  <div class="line1"></div>
  <div class="line2"></div>
</div>
&#13;
&#13;
&#13;

https://jsfiddle.net/t3z8q2k4/

答案 2 :(得分:0)

您可以使用beforeafter等伪元素。请查看更新的答案。希望它对你有所帮助。

&#13;
&#13;
.background {
  background-color: #BCBCBC;
  width: 100px;
  height: 100px;
  padding: 0;
  margin: 0;
  position: relative;
  overflow: hidden;
}

.background:before {
  width: 1px;
  height: 500px;
  background :red;
  -webkit-transform: rotate(45deg);
  position: absolute;
  content:'';
  left: 0;
  right: 0;
  margin:-200px auto 0;
}

.background:after {
  width: 1px;
  height: 500px;
  background :green;
  -webkit-transform: rotate(-45deg);
  position: absolute;
  content:'';
  left: 0;
  right: 0;
  margin:-200px auto 0;
}
&#13;
<div class="background">
</div>
&#13;
&#13;
&#13;