html - 如何让我的 css 仅应用于一个页面而不是所有页面

标签 html css

我正在努力学习 css,我制作了一个小的愚蠢的网站,但我不知何故搞砸了,它将第二页的边框应用到第一页。 这是使用边框的部分的html

<div class="rickmorty">
    <p>The rick and morty copypasta</p>
</div>

这是它也适用于的 html

<div class="box-1">
        <p>Test<br />Test again<br /></p>
</div>

这是 html 应用的 css

.rickmorty p{
    /*margin:50px;
    padding:20px;*/
    border: 20px #007700 solid;
    margin:50px;
    padding:10px;
    font-weight: bold;
}
.box-1{
    background-color: #222
}
.box-1 p{
    font-weight: 80px;
}

如果是别的here is my github repository of the site

最佳答案

如果您希望将样式应用到特定页面——并使其易于管理——只需向页面的主体添加一个类引用,该页面是唯一的。

例如

<body class="some-page-reference">
    <p>Foo bar</p>
</body>

然后在你的 CSS 中:

body.some-page-reference p {
    border: 1px solid red;
}

虽然这是一个偏好问题,但我个人认为这种方法比使用 ID 更好。

或者,如果您只想在特定页面上使用 CSS,您可以添加 <style>标记到 <head>并放置你的 CSS。但是,通常需要将 CSS 与 HTML 分开。

关于html - 如何让我的 css 仅应用于一个页面而不是所有页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46967751/

相关文章:

html - 如何在图像的垂直中间显示图像前后的文本?

html - 在没有绝对定位的情况下将标题图像左对齐?

css - 如何将媒体查询添加到样式组件中的非组件变量?

Javascript - 如何在 html 页面上显示下周三的日期

html - $index,在表中,不是顺序的... angularjs

html - 调整表格水平和垂直居中的问题

javascript - 打开和关闭节点时更改jstree小三 Angular 形图标

html - 如何减少 Bootstrap 中输入类型的宽度

jquery - 使用 JQuery 删除选中的复选框

html - 在打开模态窗口后隐藏 html 溢出