首页 > 解决方案 > 为什么这个LESS代码在复制粘贴后不起作用?

问题描述

我从一个免费网站复制了这段代码,它完全可以在这个网站上运行,但是当我在我的网站上使用它时,它给了我这样的错误;意外的令牌和'}'预期.. 我将在下面添加 HTML 和 CSS 代码,我还将添加我复制代码的原始导航,提前感谢您,非常感谢您的时间和考虑。

这是我获取代码的网站,请看一下:

https://codepen.io/bonkalol/pen/adHBz

/* CSS Code  */

* {
  box-sizing: border-box;
}

@import url(https://fonts.googleapis.com/css?family=PT+Sans);

html, body {
  margin: 0;
  padding: 0;
}

body {
  font-family: 'PT Sans', Arial, Verdana;
  background-color: #eee;
}

h1 {
  text-align: center;
  font-size: 48px;
  text-transform: uppercase;
  letter-spacing: 3px;
  color: #222;
}

.menu {
  list-style: none;
  margin: 0;
  padding: 0;
  width: 100%;
  height: 120px;
  margin: auto;
  position: relative;
  background-color: #2c3e50;
  z-index: 7;

  li {
    float: left;
    width: 25%;
    height: 100%;
    margin: 0;
    padding: 0;
  }

  a {
    display: flex;
    width: 100%;
    height: 100%;
    justify-content: center;
    align-items: center;
    color: #fff;
    text-decoration: none;
    position: relative;
    font-size: 18px;
    z-index: 9;
  }
}

a.active {
  background-color: #ffc312;
  pointer-events: none;
  color: black;

}

li.slider {
  width: 25%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background-color: #e74c3c;
  z-index: 8;
  transition: left .4s, background-color .4s;
}

.menu li:nth-child(1):hover,
.menu li:nth-child(1):focus,
.menu li:nth-child(1):active {
  & ~ .slider {
    left: 0;
    background-color: #3498db;
  }
}


.menu li:nth-child(2):hover,
.menu li:nth-child(2):focus,
.menu li:nth-child(2):active {
  & ~ .slider {
    left: 25%;
    background-color: #9b59b6;
  }
}

.menu li:nth-child(3):hover,
.menu li:nth-child(3):focus,
.menu li:nth-child(3):active {
  & ~ .slider {
    left: 50%;
    background-color: #e67e22;
  }
}

.menu li:nth-child(4):hover,
.menu li:nth-child(4):focus,
.menu li:nth-child(4):active {
  & ~ .slider {
    left: 75%;
    background-color: #16a085;
  }
}

/* HTML Code */

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>

    <title>Login</title>

    <meta charset="UTF-8">
    <title>BootStrap</title>

    <link rel="stylesheet" href="css/bootstrap.css">
    <link rel="stylesheet" type="text/css" href="Header.css";
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">


</head>
<body>

<header>


    <ul class="menu">
        <li><a href="#" class="active">Home</a></li>
        <li><a href="#">About</a></li>
        <li><a href="#">Contact</a></li>
        <li><a href="#">Signup/Signin</a></li>
        <li class="slider"></li>
    </ul>

</header>

标签: htmlcss

解决方案


如果您直接从 CodePen 网站复制了 CSS,那么格式不仅仅是纯 CSS,而是作为样式表预处理器的Less。这意味着您可以用另一种方式编写 CSS,让工具为您构建和处理(转换)成 CSS。在 Mozilla Developers Network阅读有关CSS 预处理器的更多信息。

如果您希望代码直接在您的 HTML 文件中运行,您应该选择 CSS 窗格右上角的下拉菜单,然后选择选项View compiled CSS。然后您可以将结果复制到您的 HTML 文件中并将其粘贴到您的<style>声明中。

CodePen - 查看已编译的 CSS

有关 CodePen 示例的结果,请参见下文。

* {
  box-sizing: border-box;
}
@font-face {
  font-family: 'PT Sans';
  font-style: normal;
  font-weight: 400;
  src: local('PT Sans'), local('PTSans-Regular'), url(https://fonts.gstatic.com/s/ptsans/v11/jizaRExUiTo99u79D0KEwA.ttf) format('truetype');
}
html,
body {
  margin: 0;
  padding: 0;
}
body {
  font-family: 'PT Sans', Arial, Verdana;
  background-color: #eee;
}
h1 {
  text-align: center;
  font-size: 48px;
  text-transform: uppercase;
  letter-spacing: 3px;
  color: #222;
}
.menu {
  list-style: none;
  margin: 0;
  padding: 0;
  width: 100%;
  height: 120px;
  margin: auto;
  position: relative;
  background-color: #2c3e50;
  z-index: 7;
}
.menu li {
  float: left;
  width: 25%;
  height: 100%;
  margin: 0;
  padding: 0;
}
.menu a {
  display: flex;
  width: 100%;
  height: 100%;
  justify-content: center;
  align-items: center;
  color: #fff;
  text-decoration: none;
  position: relative;
  font-size: 18px;
  z-index: 9;
}
a.active {
  background-color: #e74c3c;
  pointer-events: none;
}
li.slider {
  width: 25%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background-color: #e74c3c;
  z-index: 8;
  transition: left 0.4s, background-color 0.4s;
}
.menu li:nth-child(1):hover ~ .slider,
.menu li:nth-child(1):focus ~ .slider,
.menu li:nth-child(1):active ~ .slider {
  left: 0;
  background-color: #3498db;
}
.menu li:nth-child(2):hover ~ .slider,
.menu li:nth-child(2):focus ~ .slider,
.menu li:nth-child(2):active ~ .slider {
  left: 25%;
  background-color: #9b59b6;
}
.menu li:nth-child(3):hover ~ .slider,
.menu li:nth-child(3):focus ~ .slider,
.menu li:nth-child(3):active ~ .slider {
  left: 50%;
  background-color: #e67e22;
}
.menu li:nth-child(4):hover ~ .slider,
.menu li:nth-child(4):focus ~ .slider,
.menu li:nth-child(4):active ~ .slider {
  left: 75%;
  background-color: #16a085;
}
<h1>Responsive Menu Effect
hover on menu items!
</h1>

<ul class="menu">
  <li><a href="#" class="active">Home</a></li>
  <li><a href="#">About</a></li>
  <li><a href="#">Portfolio</a></li>
  <li><a href="#">Contact</a></li>
  <li class="slider"></li>
</ul>


推荐阅读