如何将 CSS 和 HTML 代码放在同一个文件中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7177881/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 10:14:04  来源:igfitidea点击:

How can I put CSS and HTML code in the same file?

htmlcss

提问by Nielsou Hacken-Bergen

I'm a profane in CSS, I don't know anything about it. I need to put HTML code and the CSS formatting for it in the same string object for an iPhone program.

我是 CSS 的亵渎者,我对此一无所知。我需要将 HTML 代码和它的 CSS 格式放在 iPhone 程序的同一个字符串对象中。

I have the HTML code and the CSS code, but I don't know how to mix them together. Could you help me?

我有 HTML 代码和 CSS 代码,但我不知道如何将它们混合在一起。你可以帮帮我吗?

The HTML code:

HTML代码:

<html>
<head>
??? <link type="text/css" href="./exemple.css" rel="stylesheet" media="all" />
</head>
<body>
??? <p>
??? <span class="title">La super bonne</span>
??? <span class="author">proposée par Jérém</span>
??? </p>
</body>
</html>

The CSS styles:

CSS样式:

.title {
??? color: blue;
??? text-decoration: bold;
??? text-size: 1em;
}

.author {
??? color: gray;
}

回答by Prisoner

<html>
<head>
    <style type="text/css">
    .title {
        color: blue;
        text-decoration: bold;
        text-size: 1em;
    }

    .author {
        color: gray;
    }
    </style>
</head>
<body>
    <p>
    <span class="title">La super bonne</span>
    <span class="author">proposée par Jérém</span>
    </p>
</body>
</html>

On a side note, it would have been mucheasier to just do this.

在一个侧面说明,这本来是太多容易只是做这个

回答by Mark

You can include CSS styles in an html document with <style></style>tags.

您可以在带有<style></style>标签的 html 文档中包含 CSS 样式。

Example:

例子:

<style>
  .myClass { background: #f00; }

  .myOtherClass { font-size: 12px; }
</style>

回答by Phil

Is this what you're looking for? You place you CSS between styletags in the HTML document header. I'm guessing for iPhone it's webkit so it should work.

这是你要找的吗?您将 CSSstyle放在 HTML 文档标题中的标记之间。我猜 iPhone 是 webkit,所以它应该可以工作。

<html>
<head>
    <style type="text/css">
    .title { color: blue; text-decoration: bold; text-size: 1em; }
    .author { color: gray; }
    </style>
</head>
<body>
    <p>
    <span class="title">La super bonne</span>
    <span class="author">proposée par Jérém</span>
    </p>
</body>
</html>

回答by Nabin

Or also you can do something like this.

或者你也可以做这样的事情。

<div style="background=#aeaeae; float: right">

</div>

We can add any CSS inside the styleattribute of HTML tags.

我们可以在HTML 标签的style属性中添加任何 CSS 。

回答by JordinB

Two options: 1, add css inline like style="background:black" Or 2. In the head include the css as a style tag block.

两个选项: 1,添加 css inline like style="background:black" 或者 2. 在头部包含 css 作为样式标记块。

回答by PandemoniumSyndicate

There's a style tag, so you could do something like this:

有一个样式标签,所以你可以做这样的事情:

<style type="text/css">
  .title 
  {
    color: blue;
    text-decoration: bold;
    text-size: 1em;
  }
</style>