带有平行四边形形状和子菜单的CSS菜单

时间:2017-05-09 13:27:41

标签: css css3

我试图创建一个包含以下子菜单的菜单:

enter image description here

  • 选项之间的对角线边框
  • 悬停时的平行四边形形状
  • 子菜单是一个带选项的简单矩形

问题是如果我使用倾斜文本和子菜单也是倾斜的。 知道如何解决这个问题吗?

这里有一些基本代码:https://codepen.io/vichid/pen/cHnmK



@import url( 'https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css' );

nav {
    position: absolute;
    top: 50%;
    left: 0;
    bottom: 50%;
    right: 0;
}

ul.menu {
    width: 940px;
    height: 40px;
    line-height: 40px;
    position: relative;
    text-align: center;
    margin: auto;
    padding: auto;
    background-color: #AFF8C4;
    -moz-border-radius-topleft: 4px;
    -webkit-border-top-left-radius: 4px;
    border-top-left-radius: 4px;
    -moz-border-radius-topright: 4px;
    -webkit-border-top-right-radius: 4px;
    border-top-right-radius: 4px;
}

ul.menu li {
    float: left;
    width: auto;
}

ul.menu li:hover {
    background-color: #9CE0C9;
    -moz-transition-property: background-color;
    -o-transition-property: background-color;
    -webkit-transition-property: background-color;
    transition-property: background-color;
    -moz-transition-duration: 0.33s;
    -o-transition-duration: 0.33s;
    -webkit-transition-duration: 0.33s;
    transition-duration: 0.33s;
    -moz-border-radius-topleft: 4px;
    -webkit-border-top-left-radius: 4px;
    border-top-left-radius: 4px;
    -moz-border-radius-topright: 4px;
    -webkit-border-top-right-radius: 4px;
    border-top-right-radius: 4px;
}

ul.menu li:hover ul {
    width: 940px;
    background: #9CE0C9;
    visibility: visible;
    filter: progid:DXImageTransform.Microsoft.Alpha(enabled=false);
    opacity: 1;
    -moz-transition-property: opacity;
    -o-transition-property: opacity;
    -webkit-transition-property: opacity;
    transition-property: opacity;
    -moz-transition-duration: 0.33s;
    -o-transition-duration: 0.33s;
    -webkit-transition-duration: 0.33s;
    transition-duration: 0.33s;
    -moz-border-radius-bottomleft: 4px;
    -webkit-border-bottom-left-radius: 4px;
    border-bottom-left-radius: 4px;
    -moz-border-radius-bottomright: 4px;
    -webkit-border-bottom-right-radius: 4px;
    border-bottom-right-radius: 4px;
}

ul.menu li a {
    display: block;
    width: auto;
    text-decoration: none;
}

ul.menu a, ul.sub-menu a {
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    color: #0E0500;
    padding: 0 10px;
}

ul.sub-menu {
    filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=0);
    opacity: 0;
    visibility: hidden;
    display: block;
    position: absolute;
    left: 0;
    height: 40px;
    line-height: 40px;
    background-color: #9CE0C9;
}

<!-- nav>ul.menu>li*7>a[href="#"]{Item $}+ul.sub-menu>li*7>a[href="#"]{Sub-item $} -->
    
<nav>
        <ul class="menu">
            <li>
                <a href="#">Item 1</a>
                <ul class="sub-menu">
                    <li><a href="#">Sub-item 1</a></li>
                    <li><a href="#">Sub-item 2</a></li>
                    <li><a href="#">Sub-item 3</a></li>
                    <li><a href="#">Sub-item 4</a></li>
                    <li><a href="#">Sub-item 5</a></li>
                    <li><a href="#">Sub-item 6</a></li>
                    <li><a href="#">Sub-item 7</a></li>
                </ul>
            </li>
            <li>
                <a href="#">Item 2</a>
                <ul class="sub-menu">
                    <li><a href="#">Sub-item 1</a></li>
                    <li><a href="#">Sub-item 2</a></li>
                    <li><a href="#">Sub-item 3</a></li>
                    <li><a href="#">Sub-item 4</a></li>
                    <li><a href="#">Sub-item 5</a></li>
                    <li><a href="#">Sub-item 6</a></li>
                    <li><a href="#">Sub-item 7</a></li>
                </ul>
            </li>
            <li>
                <a href="#">Item 3</a>
                <ul class="sub-menu">
                    <li><a href="#">Sub-item 1</a></li>
                    <li><a href="#">Sub-item 2</a></li>
                    <li><a href="#">Sub-item 3</a></li>
                    <li><a href="#">Sub-item 4</a></li>
                    <li><a href="#">Sub-item 5</a></li>
                    <li><a href="#">Sub-item 6</a></li>
                    <li><a href="#">Sub-item 7</a></li>
                </ul>
            </li>
            <li>
                <a href="#">Item 4</a>
                <ul class="sub-menu">
                    <li><a href="#">Sub-item 1</a></li>
                    <li><a href="#">Sub-item 2</a></li>
                    <li><a href="#">Sub-item 3</a></li>
                    <li><a href="#">Sub-item 4</a></li>
                    <li><a href="#">Sub-item 5</a></li>
                    <li><a href="#">Sub-item 6</a></li>
                    <li><a href="#">Sub-item 7</a></li>
                </ul>
            </li>
            <li>
                <a href="#">Item 5</a>
                <ul class="sub-menu">
                    <li><a href="#">Sub-item 1</a></li>
                    <li><a href="#">Sub-item 2</a></li>
                    <li><a href="#">Sub-item 3</a></li>
                    <li><a href="#">Sub-item 4</a></li>
                    <li><a href="#">Sub-item 5</a></li>
                    <li><a href="#">Sub-item 6</a></li>
                    <li><a href="#">Sub-item 7</a></li>
                </ul>
            </li>
            <li>
                <a href="#">Item 6</a>
                <ul class="sub-menu">
                    <li><a href="#">Sub-item 1</a></li>
                    <li><a href="#">Sub-item 2</a></li>
                    <li><a href="#">Sub-item 3</a></li>
                    <li><a href="#">Sub-item 4</a></li>
                    <li><a href="#">Sub-item 5</a></li>
                    <li><a href="#">Sub-item 6</a></li>
                    <li><a href="#">Sub-item 7</a></li>
                </ul>
            </li>
            <li>
                <a href="#">Item 7</a>
                <ul class="sub-menu">
                    <li><a href="#">Sub-item 1</a></li>
                    <li><a href="#">Sub-item 2</a></li>
                    <li><a href="#">Sub-item 3</a></li>
                    <li><a href="#">Sub-item 4</a></li>
                    <li><a href="#">Sub-item 5</a></li>
                    <li><a href="#">Sub-item 6</a></li>
                    <li><a href="#">Sub-item 7</a></li>
                </ul>
            </li>
        </ul>
    </nav>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

您可以在悬停时使用li上的伪元素创建形状,然后将其倾斜。

&#13;
&#13;
@import url('https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css');
nav {
  position: absolute;
  top: 50%;
  left: 0;
  bottom: 50%;
  right: 0;
}

ul.menu {
  width: 940px;
  height: 40px;
  line-height: 40px;
  position: relative;
  text-align: center;
  background-color: #AFF8C4;
  -moz-border-radius-topleft: 4px;
  -webkit-border-top-left-radius: 4px;
  border-top-left-radius: 4px;
  -moz-border-radius-topright: 4px;
  -webkit-border-top-right-radius: 4px;
  border-top-right-radius: 4px;
}

ul.menu li {
  float: left;
  position: relative;
}

ul.menu > li:after {
  content: '';
  display: none;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  transform: skew( -15deg);
  z-index: 0;
  background-color: #9CE0C9;
  -moz-transition-property: background-color;
  -o-transition-property: background-color;
  -webkit-transition-property: background-color;
  transition-property: background-color;
  -moz-transition-duration: 0.33s;
  -o-transition-duration: 0.33s;
  -webkit-transition-duration: 0.33s;
  transition-duration: 0.33s;
  -moz-border-radius-topleft: 4px;
  -webkit-border-top-left-radius: 4px;
  border-top-left-radius: 4px;
  -moz-border-radius-topright: 4px;
  -webkit-border-top-right-radius: 4px;
  border-top-right-radius: 4px;
}

ul.menu > li:hover::after {
  display: block;
}

ul.menu > li:hover ul {
  width: 940px;
  background: #9CE0C9;
  visibility: visible;
  filter: progid:DXImageTransform.Microsoft.Alpha(enabled=false);
  opacity: 1;
  -moz-transition-property: opacity;
  -o-transition-property: opacity;
  -webkit-transition-property: opacity;
  transition-property: opacity;
  -moz-transition-duration: 0.33s;
  -o-transition-duration: 0.33s;
  -webkit-transition-duration: 0.33s;
  transition-duration: 0.33s;
  -moz-border-radius-bottomleft: 4px;
  -webkit-border-bottom-left-radius: 4px;
  border-bottom-left-radius: 4px;
  -moz-border-radius-bottomright: 4px;
  -webkit-border-bottom-right-radius: 4px;
  border-bottom-right-radius: 4px;
}

ul.menu li a {
  display: block;
  width: auto;
  text-decoration: none;
  position: relative;
  z-index: 1;
}

ul.menu a,
ul.sub-menu a {
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  color: #0E0500;
  padding: 0 10px;
}

ul.sub-menu {
  filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=0);
  opacity: 0;
  visibility: hidden;
  display: block;
  position: absolute;
  left: -5px;
  height: 40px;
  line-height: 40px;
  background-color: #9CE0C9;
}
&#13;
<!-- nav>ul.menu>li*7>a[href="#"]{Item $}+ul.sub-menu>li*7>a[href="#"]{Sub-item $} -->

<nav>
  <ul class="menu">
    <li>
      <a href="#">Item 1</a>
      <ul class="sub-menu">
        <li><a href="#">Sub-item 1</a></li>
        <li><a href="#">Sub-item 2</a></li>
        <li><a href="#">Sub-item 3</a></li>
        <li><a href="#">Sub-item 4</a></li>
        <li><a href="#">Sub-item 5</a></li>
        <li><a href="#">Sub-item 6</a></li>
        <li><a href="#">Sub-item 7</a></li>
      </ul>
    </li>
    <li>
      <a href="#">Item 2</a>
      <ul class="sub-menu">
        <li><a href="#">Sub-item 1</a></li>
        <li><a href="#">Sub-item 2</a></li>
        <li><a href="#">Sub-item 3</a></li>
        <li><a href="#">Sub-item 4</a></li>
        <li><a href="#">Sub-item 5</a></li>
        <li><a href="#">Sub-item 6</a></li>
        <li><a href="#">Sub-item 7</a></li>
      </ul>
    </li>
    <li>
      <a href="#">Item 3</a>
      <ul class="sub-menu">
        <li><a href="#">Sub-item 1</a></li>
        <li><a href="#">Sub-item 2</a></li>
        <li><a href="#">Sub-item 3</a></li>
        <li><a href="#">Sub-item 4</a></li>
        <li><a href="#">Sub-item 5</a></li>
        <li><a href="#">Sub-item 6</a></li>
        <li><a href="#">Sub-item 7</a></li>
      </ul>
    </li>
    <li>
      <a href="#">Item 4</a>
      <ul class="sub-menu">
        <li><a href="#">Sub-item 1</a></li>
        <li><a href="#">Sub-item 2</a></li>
        <li><a href="#">Sub-item 3</a></li>
        <li><a href="#">Sub-item 4</a></li>
        <li><a href="#">Sub-item 5</a></li>
        <li><a href="#">Sub-item 6</a></li>
        <li><a href="#">Sub-item 7</a></li>
      </ul>
    </li>
    <li>
      <a href="#">Item 5</a>
      <ul class="sub-menu">
        <li><a href="#">Sub-item 1</a></li>
        <li><a href="#">Sub-item 2</a></li>
        <li><a href="#">Sub-item 3</a></li>
        <li><a href="#">Sub-item 4</a></li>
        <li><a href="#">Sub-item 5</a></li>
        <li><a href="#">Sub-item 6</a></li>
        <li><a href="#">Sub-item 7</a></li>
      </ul>
    </li>
    <li>
      <a href="#">Item 6</a>
      <ul class="sub-menu">
        <li><a href="#">Sub-item 1</a></li>
        <li><a href="#">Sub-item 2</a></li>
        <li><a href="#">Sub-item 3</a></li>
        <li><a href="#">Sub-item 4</a></li>
        <li><a href="#">Sub-item 5</a></li>
        <li><a href="#">Sub-item 6</a></li>
        <li><a href="#">Sub-item 7</a></li>
      </ul>
    </li>
    <li>
      <a href="#">Item 7</a>
      <ul class="sub-menu">
        <li><a href="#">Sub-item 1</a></li>
        <li><a href="#">Sub-item 2</a></li>
        <li><a href="#">Sub-item 3</a></li>
        <li><a href="#">Sub-item 4</a></li>
        <li><a href="#">Sub-item 5</a></li>
        <li><a href="#">Sub-item 6</a></li>
        <li><a href="#">Sub-item 7</a></li>
      </ul>
    </li>
  </ul>
</nav>
&#13;
&#13;
&#13;