gpt4 book ai didi

java - CSS 在运行程序时不起作用,但在我不运行时它确实起作用

转载 作者:行者123 更新时间:2023-11-28 00:43:12 25 4
gpt4 key购买 nike

当我运行我的代码时,我的 html 的 CSS 不适用。

Page with code running

但是当我在 intellij 中为 chrome 使用内置的 HTML 显示器时,它确实有效。

Page without code running

我们正在用 java 编程并使用 Spring 框架制作网站。一直在尝试我能找到的关于这个主题的一切。

但如果我不使用样式表而是直接在 html 中实现样式,它确实工作正常,但我不想用样式使所有 html 页面困惑。

在我的 html 中,我使用了:

<head>
<link rel="stylesheet" type="text/css" href="static/style.css" />
</head>

我还测试了其他可用的 CSS 样式,看看它是否有帮助,所以我知道不是那样。

编辑:

我的 Controller 中的代码

@GetMapping("/")
public String home(Model model) {

if (currentUser==null) {
return "home";
} else {
model.addAttribute("userOnline",currentUser);
return "adminView/aHome";
}
}

CSS 不适用于任何页面,但我从首页开始:

<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="/static/style.css" />
</head>

<body>
<div class="navbar">
<a href="home">Home</a>
<a href="portfolio">Portfolio</a>
<a href="contact">Contact</a>
<a href="login">Login</a>
</div>
<div class="main">
<h1>Fixed Top Menu</h1>
</div>

</body>
</html>

The structure looks like this

当我在 http://localhost:8888/ 上运行我的程序时它不起作用,但 intellij 有一个功能,可以向我显示 chrome 中的页面,它将我发送到 http://localhost:63342/demo/templates/home.html?_ijt=kd3vqt392jk8ggqv8ctthtemsi#contact ,当它这样做时,它就起作用了。

编辑 2:

我尝试过的都不起作用,但现在它起作用了,不知道为什么。

最佳答案

这很可能与文件路径有关。如果您使用的是 HTML5,请执行以下操作:

<head>
<link rel="stylesheet" href="static/style.css">
</head>

如果这不起作用,请检查 CSS 文档的文件路径。可能是您缺少 //static/style.css../static/style.css 等。如果不看代码等,很难解决问题,但这是我现在能想到的唯一解决方案。

关于java - CSS 在运行程序时不起作用,但在我不运行时它确实起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53691488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com