html - 你能创建一个 css "function"或包装器吗

标签 html css

所以遇到了一点问题,并试图找出最好的方法。

所以得到了一些 css 代码(大量从比我更好的编码员那里借来的)在菜单中创建一组漂亮的箭头,大量使用 :after 和 :hover 和各种。不得不在另一个应用程序中编写它,现在想将它移动到实际页面。

问题是,我需要设置类(class),但我做错了。基本上,我决定使用“arrowrt”类将其设置为顶级菜单。但是最简单的包装方法是什么,或者包装它是否容易。想要做一些类似 .arrowrt { 加载 CSS 设置 } 的事情。

目前的代码如下所示

   <style type="text/css" media="all">

    a {
            text-decoration:none;
        }
    ul {
        margin: 20px 60px;
    }
    li {
        margin-right: 5px;
    }
    ul li {
        display: inline-block;
        font-size: 12px;
        height: 30px;
        line-height: 30px;
        width: 150px;
        margin: 5px 8px 0 0;
        text-align: center;
        position: relative;
    }

    ul li:before {
        content: " ";
        height: 0;
        width: 0;
        position: absolute;
        left: -2px;
        border-style: solid;
        border-width: 15px 0 15px 15px;
        border-color: transparent transparent transparent #fff;
        z-index: 0;
    }

     ul li:first-child:before , .dbfsrt {
        border-color: transparent;
    }
    ul li a:after {
        content: " ";
        height: 0;
        width: 0;
        position: absolute;
        right: -15px;
        border-style: solid;
        color: #000000;
        border-width: 15px 0 15px 15px;
        border-color: transparent transparent transparent #ccc;
        z-index: 10;
    }

    ul li.arractive a {
        background: orange;
        z-index: 100;
    }

    ul li.active a:after {
        border-left-color: orange;
    }

    ul li a {
        display: block;
        background: #ccc;
        color: #000000;
    }

    ul li a:hover {
        background: #000000;
        color: #ececec;
    }

    ul li a:hover:after {
        border-color: transparent transparent transparent #000000;

    }




</style>

和 HTML 部分

<ul id="arrowrt">
    <li class="arrowrt"><a  href="#">Home</a></li>
    <li class="arrowrt"><a  href="#">Back Office</a></li>
    <li class="arrowrt"><a  href="#">Cash Management</a></li>
    <li class="arrowrt arractive"><a href="#">Overdraft Management</a></li>
</ul>

最佳答案

你可以(如果使用 sass)做这样的事情:

.arrowrt li {
  display: inline-block;
  font-size: 12px;
  height: 30px;
  line-height: 30px;
  width: 150px;
  margin: 5px 8px 0 0;
  text-align: center;
  position: relative;
  &:before {
    content: " ";
    height: 0;
    width: 0;
    position: absolute;
    left: -2px;
    border-style: solid;
    border-width: 15px 0 15px 15px;
    border-color: transparent transparent transparent #fff;
    z-index: 0;
  }
  &:first-child:before {
    border-color: transparent;
  }
}

.dbfsrt {
  border-color: transparent;
}

.arrowrt li {
  a:after {
    content: " ";
    height: 0;
    width: 0;
    position: absolute;
    right: -15px;
    border-style: solid;
    color: #000000;
    border-width: 15px 0 15px 15px;
    border-color: transparent transparent transparent #ccc;
    z-index: 10;
  }
  &.arractive a {
    background: orange;
    z-index: 100;
  }
  &.active a:after {
    border-left-color: orange;
  }
  a {
    display: block;
    background: #ccc;
    color: #000000;
    &:hover {
      background: #000000;
      color: #ececec;
      &:after {
        border-color: transparent transparent transparent #000000;
      }
    }
  }
}

但您应该将 arrowrt 作为类而不是 id,以便它可以被重用。

这相当于 css:

.arrowrt li {
  display: inline-block;
  font-size: 12px;
  height: 30px;
  line-height: 30px;
  width: 150px;
  margin: 5px 8px 0 0;
  text-align: center;
  position: relative;
}
.arrowrt li:before {
  content: " ";
  height: 0;
  width: 0;
  position: absolute;
  left: -2px;
  border-style: solid;
  border-width: 15px 0 15px 15px;
  border-color: transparent transparent transparent #fff;
  z-index: 0;
}
.arrowrt li:first-child:before {
  border-color: transparent;
}

.dbfsrt {
  border-color: transparent;
}

.arrowrt li a:after {
  content: " ";
  height: 0;
  width: 0;
  position: absolute;
  right: -15px;
  border-style: solid;
  color: #000000;
  border-width: 15px 0 15px 15px;
  border-color: transparent transparent transparent #ccc;
  z-index: 10;
}
.arrowrt li.arractive a {
  background: orange;
  z-index: 100;
}
.arrowrt li.active a:after {
  border-left-color: orange;
}
.arrowrt li a {
  display: block;
  background: #ccc;
  color: #000000;
}
.arrowrt li a:hover {
  background: #000000;
  color: #ececec;
}
.arrowrt li a:hover:after {
  border-color: transparent transparent transparent #000000;
}

关于html - 你能创建一个 css "function"或包装器吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29408660/

相关文章:

javascript - HTML5 localStorage 用法

javascript - 在 HTML/JS 中点击放大和缩放图片

javascript - 选择复选框以过滤匹配所有选择的结果

Firefox 中的 css 容器高度不同?

javascript - Bootstrap 4.4.1 Accordion Scroll Behavior/Bug(?) - Chrome

javascript - 如何在响应式视频上垂直居中文本

javascript - 切换时 div 被另一个 div 插入

html - Thickbox 3.1 IE8 滚动问题

html - 用作背景的 spritesheet 图像填充容器

css - 媒体查询不适用于某些类的 Bootstrap,但适用于其他类