简体   繁体   English

如何使CSS仅适用于一页而不是所有页面

[英]How do I get my css to apply only to one page and not all pages

I'm working on learning css and I made a small dumb site, but I screwed up somehow and it's applying the border of the second page to the first page. 我正在学习CSS,并且制作了一个小型哑站点,但是我搞砸了,并且将第二页的边框应用于第一页。 Here's the html of the part using the border 这是使用边框的部分的html

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

Here is the html it is also applying to 这是它也适用于的html

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

And here is the css the html is applying 这是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;
}

If it's something else here is my github repository of the site 如果还有其他问题, 这是我的站点的github存储库

Add an ID to the elements that appear only on that unique page by doing something like: 通过执行以下操作,将ID添加到仅在该唯一页面上显示的元素:

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

Then in CSS do: 然后在CSS中执行:

#unique-box-1
{
   color: red; //just an example
}

Then the red color will be applied only to the element with that ID . 然后,红色将仅应用于具有该ID的元素。 Adjust the code accordingly. 相应地调整代码。

If you wish to apply styles to specific pages — and make it easy to manage — simple add a class reference to the body that is unique to the page. 如果您希望将样式应用于特定页面,并且使其易于管理,则只需向该页面唯一的正文中添加一个类引用即可。

Eg 例如

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

Then in your CSS: 然后在您的CSS中:

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

Although it's a matter of preference I personally find this method better than using IDs. 尽管这是优先考虑的问题,但我个人发现此方法比使用ID更好。

Alternatively, if you want to use CSS on a specific page only you can add <style> tags to the <head> and put your CSS. 另外,如果只想在特定页面上使用CSS,则可以将<style>标记添加到<head>并放置CSS。 However, it is often desirable to keep CSS separate from the HTML. 但是,通常希望将CSS与HTML分开。

Instead of class, try to give id for an div.. 而不是类,尝试给一个分区的ID。

The priority of id is greater than class. id的优先级大于class。

Ex: 例如:

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

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

#rickmorty p{
    /*margin:50px;
    padding:20px;*/
    border: 20px #007700 solid;
    margin:50px;
    padding:10px;
    font-weight: bold;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的CSS样式表仅影响了我4页中的3页。 如何更新第4页? - My CSS style sheet only affected 3 out of my 4 pages. How do I update that 4th page? 我如何得到我的 <nav> 栏始终使用CSS3始终位于页面中心? - How do I get my <nav> bar to be centered in the page at all times using CSS3? (html / css)如何删除页眉中出现的边距,并将其仅应用于徽标? - (html/css) How do I remove the margin appearing from my header, and have it only apply to my Logo? 如何将此侧边栏应用到所有使用 CSS 的页面? - How to apply this sidebar to all pages using CSS? 如何在一页上使用vh和vw CSS没有滚动响应式网页? - How do I use vh and vw CSS for one page no scroll responsive web pages? 如何编写母版页并将其链接到CSS样式表和其他网页? - How do i code a master page and link it to my css style sheet and other web pages? 为什么将我的样式表加载到除一页之外的所有其他页面上,我在该页面上获取样式表文件的404 - why my style sheets are loaded for all other pages except one page, i get 404 for stylesheet files on that page 如何设置一个包含<header></header>并且可以链接到所有页面? - How do I set up one page that containers the <header></header> and can be linked to all pages? 如何使所有页面采用用户定义的浏览器缩放级别(css / html)? - How do I get all pages to adopt the user-defined browser zoom level (css/html)? 如何获得CSS文件以正确地应用于我的PHP / HTML代码? - How do I get the CSS file to apply correctly to my PHP/HTML code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM