You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

复制粘贴后的LESS代码无法运行,请求排查原因

解决LESS代码在网站中出现语法错误的问题

你的问题很典型——你从Codepen复制的是LESS预处理器代码,但浏览器只能识别标准CSS语法,直接把LESS代码当作CSS引入就会触发unexpected token}expected这类解析错误,因为LESS的嵌套选择器、父选择器&都是CSS原生不支持的特性。

快速解决方案:使用编译后的标准CSS

我已经把你提供的LESS代码编译成了浏览器能直接识别的CSS,替换你Header.css里的内容即可:

* {
  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;
}
.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: #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 ~ .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;
}

额外优化点

你的HTML里有两个重复的<title>标签,建议合并成一个,避免语义冲突:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>BootStrap | Login</title>
  <meta charset="UTF-8">
  <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>
</body>
</html>

后续开发建议

如果之后你还想使用LESS编写样式,推荐两种更高效的方式:

  • 本地快速编译:用VS Code的Easy LESS插件,保存LESS文件时自动生成对应的CSS文件
  • 工程化编译:用Webpack、Vite这类前端构建工具集成LESS编译,适合中大型项目的样式管理

内容的提问来源于stack exchange,提问作者Kurdish Learner

火山引擎 最新活动