是否可以在 xhtml 文件中内联 CSS 的类定义?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7434869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to inline a class definition of CSS inside an xhtml file?
提问by Mr.Eddart
Is it possible to inline a class definition of CSS inside an xhtml file?
是否可以在 xhtml 文件中内联 CSS 的类定义?
I mean, to put someting like:
我的意思是,把一些东西:
p.first{ color: blue; }
p.second{ color: red; }
Inside my page, not in a separate CSS file.
在我的页面中,而不是在单独的 CSS 文件中。
回答by Joseph Silber
I think you're trying to put your CSS in the HTML page, not inline.
我认为您正在尝试将 CSS 放在 HTML 页面中,而不是内联。
You can put CSS in an HTML page (usually in the head
) by surrounding it in style
tags:
您可以将 CSS 放在 HTML 页面中(通常在 中head
),方法是将其用style
标签括起来:
<style type="text/css">
p.first{ color: blue; }
p.second{ color: red; }
</style>
回答by FAtBalloon
Sure, here's an example. However, it is best practice to keep your styles in a separate css file.
当然,这是一个例子。但是,最佳做法是将您的样式保存在单独的 css 文件中。
<html>
<head>
<title>Classes</title>
<link rel="stylesheet" type="text/css" href="css/styles.css"/>
<style type="text/css">
img {
padding:10px;
margin:5px;
border:1px solid #d5d5d5;
}
div.thumb {
float:left;
}
div.caption {
padding-left:5px;
font-size:10px;
}
</style>
</head>
<body>
<div>your page code etc..</div>
</body>
</html>
回答by shridhar
You can also put css inside the p tag.
您也可以将 css 放在 p 标签中。
<html>
<body>
<p class="first" style="color:blue;"></p>
<p class="second" style="color:red;"></p>
</body>
</html>
回答by Hammad Khan
The nice thing about CSS is it works in any file not just an HTML,XML file. You just need to define the syle block like this anywhere in the page
CSS 的好处在于它适用于任何文件,而不仅仅是 HTML、XML 文件。你只需要在页面的任何地方定义这样的 syle 块
<style type="text/css">
<all my styles goes here>
</style>
In HTML and HTML/XHTML, the standard is, you will put this block in the head section. If it is other type of file for example .aspx, or .php, the block still works, even it is not in head block.
在 HTML 和 HTML/XHTML 中,标准是,您将此块放在 head 部分中。如果它是其他类型的文件,例如 .aspx 或 .php,该块仍然有效,即使它不在 head 块中。
Example
例子
<?php
/* mytest.php file */
<style>
<my styles>
</style>
?>
?>
the same is true for ASPX file.
ASPX 文件也是如此。
You can also define inline CSS which means CSS goes right in the element tag. The syntax is
您还可以定义内联 CSS,这意味着 CSS 位于元素标签中。语法是
<p style="<all my styles>"> My paragraph contain inline CSS</p>
回答by moey
Yes, you can insert CSS styles in the HTML file. For example:
是的,您可以在 HTML 文件中插入 CSS 样式。例如:
<p>...</p>
<style type="text/css">
p.first { ... }
</style>
<div>...</div>
As you'll find in the literature, it's not considered a good practice though.
正如您将在文献中发现的那样,虽然这不被认为是一种好的做法。