如何用html改变字体颜色?[英] How can I change my font color with html?

本文是小编为大家收集整理的关于如何用html改变字体颜色?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在制作一个网页,我希望字体的颜色在段落中为红色,但我不确定如何做.

我以前正在使用Frontpage来构建网页,因此此HTML的内容对我来说确实是新的.什么是最好的方法?

推荐答案

<p style="color:red">Foo</p>

或优先:

<p class="error">Foo</p>

在您的样式表中定义了"错误":

.error {
    color: red;
}

其他推荐答案

首选做到这一点的方法是 cascading样式表(CSS) .这使您可以编辑网站的视觉方面,而无需处理HTML代码本身.

说明:

<[tag] style="[css]"> Content </[tag]>

[标签]可以是什么.例如" p"(段落),"跨度"," div"," ul"," li" ..等等.

以及[CSS]是任何有效的CSS.例如"颜色:红色;字体大小:15px; font权重:粗体"


将样式添加到HTML元素中的推荐方法是将其分配为"类"(可以在文档上重复的标识符)或" ID"或" ID"是文档中不得重复的唯一标识符./p>

例如:

<[tag] id="element1" class="red"> Content </[tag]>
<[tag] id="element2" class="red"> Content </[tag]>

标签为任何HTML有效标签. id是一个独特的任意名称,类是可以重复的任意名称.

然后在CSS中(文档的标签内):

<style type="text/css">

.red {
    color:red;
}

#element1 {
    background-color:black;
}

</style>

对于此示例,为了使新用户保持简单,我将类命名为"红色".但是,class=" red"并不是如何命名的最佳示例.最好以其语义含义命名CSS课程,而不是它们实现的样式.因此,class=" error"或class=" hilight"可能更合适. (感谢Grant Wagner指出了这一点)


简介CSS说明:

由于您得到的大多数答案都在提到CSS,因此我会添加一个有关其工作原理的小指南:

在哪里放置CSS

首先,您需要知道应该在文档标签中添加CSS.用于定义CSS在哪里的标签:

<style type="text/css"> <!-- Your CSS here --> </style>

这称为嵌入式CSS,因为它在文档内部.但是,更好的做法是通过使用以下标签直接从外部文档链接"包含"它:

<link href="file.css" media="all" rel="stylesheet" type="text/css"/> 

file.css是要包含在文档中的外部文件.

使用"链接"标签的好处是您不必编辑在线CSS.因此,可以说如果您有10个HTML文档,并且要更改字体的颜色,则只需在外部CSS文件上执行.

包括CSS的两种方法是最建议的方法.但是,还有另一种方法是进行在线CSS调整,例如:

<[tag] style="<!-- CSS HERE -->"> Content </[tag]>

CSS通用结构

代码编写CSS时,您需要知道的第一件事是什么是类,什么是ID.由于我已经提到了他们在上面的工作,所以我将解释如何使用它们.

写CSS时,您首先需要告诉您要"选择"的元素,例如:

可以说,我们有一个带有 class "基本"的" div"元素,我们希望它具有黑色背景颜色,白色字体和灰色边框.

为此,我们首先需要"选择"元素:

.[identifier] { }

由于我们正在使用类,因此我们使用"".在这种情况下为"基本"的标识符的前面,因此看起来像这样:

.basic { }

这不是唯一的方法,因为我们告诉任何具有"基本"类的元素将被选中,所以可以说我们只想要" div"元素.为此,我们使用:

[html-tag].[identifier] { }

因此,对于我们的例子,它看起来像这样:

div.basic { }

现在,我们选择了" div"班级"身体".现在,我们需要应用我们希望的视觉样式.我们在括号内执行此操作:

div.basic {
    background-color:black;
    color:white;
    border:1px solid gray;
}

这样,我们只是成功地将视觉样式应用于所有" div"元素,这些元素具有"基本"类.

请记住,这不仅适用于"类",还适用于" ID",但在这里进行了略有更改,在这里是最终代码的示例,但我们只是说这是" ID"

#unique-basic {
    background-color:black;
    color:white;
    border:1px solid gray;
}

有关CSS的完整指南,您可以访问此链接: http://www.w3schools.com/css/css/

记住:

保持HTML代码清洁并使用CSS修改所需的任何视觉样式. CSS确实很强大,它可以节省很多时间.

其他推荐答案

<p style="color:red">Your Text here</p>

但是,正如其他人现在所说的那样:甚至比以上更好的话是使用类或ID并将CSS-Attributes分配给它而不是使用内联样式.

本文地址:https://www.itbaoku.cn/post/627316.html

问题描述

I'm making a web page where I want the color of the font to be red in a paragraph but I'm not sure how to do this.

I was using FrontPage for building web pages before so this HTML stuff is really new to me. What is the best way to do this?

推荐答案

<p style="color:red">Foo</p>

Or preferrably:

<p class="error">Foo</p>

Where "error" is defined in your stylesheet:

.error {
    color: red;
}

其他推荐答案

The preferred way to do this is using Cascading Style Sheet (CSS). This allows you to edit the visual aspects of the site without having to deal much with the HTML code itself.

Explanation :

<[tag] style="[css]"> Content </[tag]>

Where [tag] can be anything. For example "p" (paragraph), "span", "div", "ul", "li".. etc.

and where [css] is any valid CSS. For example "color:red; font-size:15px; font-weight:bold"


The recommended way to add style to a html element is by assigning it a "class" (a identifier that can be repeated on the document) or a "id" a unique identifier that shall not be repeated in the document.

For example:

<[tag] id="element1" class="red"> Content </[tag]>
<[tag] id="element2" class="red"> Content </[tag]>

Where tag is any html valid tag. id is a unique arbitrary name and class is an arbitrary name that can be repeated.

Then in the CSS (inside the tags of your document):

<style type="text/css">

.red {
    color:red;
}

#element1 {
    background-color:black;
}

</style>

For this example and to keep it simple to new users I named the class "red". However class="red" isn't the best example of how to name . Better to name CSS classes after their semantic meaning, rather than the style(s) they implement. So class="error" or class="hilight" might be more appropriate. ( Thanks to Grant Wagner for pointing that out )


Brief CSS Explanation :

Since most of the answers you're getting are all mentioning CSS, I'll add a small guide on how it works:

Where to put CSS

First of all, you need to know that CSS should be added inside the tags of your document. The tags used to define where the CSS is going to be are:

<style type="text/css"> <!-- Your CSS here --> </style>

This is called embedded CSS since it's inside the document. However, a better practice is to link "include it" directly from an external document by using the following tags:

<link href="file.css" media="all" rel="stylesheet" type="text/css"/> 

Where file.css is the external file you want to include into the document.

The benefits of using the "link" tag is that you don't have to edit in-line CSS. So lets say if you have 10 HTML documents and you want to change the color of a font you just need to do it on the external CSS file.

This two ways of including CSS are the most recommended ways. However, there's one more way that's by doing in-line CSS adjustments, for example:

<[tag] style="<!-- CSS HERE -->"> Content </[tag]>

CSS General Structure

When you code write CSS, the first thing you need to know is what are classes and what are id's. Since I already mentioned what they do above I'm going to explain how to use them.

When you write CSS you first need to tell which elements you're going to "select", for example:

Lets say we have a "div" element with the class "basic" and we want it to have a black background color, a white font, and a gray border.

To do this we first need to "select" the element:

.[identifier] { }

Since we're using a class we use a "." in front of the identifier which in this case is: "basic", so it will look like this:

.basic { }

This is not the only way, because we're telling that ANY element that has the class "basic" will be selected, so lets say we JUST want the "div" elements. To do this we use:

[html-tag].[identifier] { }

So for our example it will look like this:

div.basic { }

Now we've selected the "div" with the class "body". Now we need to apply the visual style we wish. We do this inside the brackets :

div.basic {
    background-color:black;
    color:white;
    border:1px solid gray;
}

With this, we just applied successfully a visual style to all "div" elements that have the "basic" class attached.

Remember this doesn't just apply for "class" it also applies for "id" but with a slight change, here an example of the final code but instead of a class we'll just say it's a "id"

#unique-basic {
    background-color:black;
    color:white;
    border:1px solid gray;
}

For a complete guide to CSS you can visit this link: http://www.w3schools.com/css/

Remember:

Keep your HTML Code clean and use CSS to modify ANY visual style that's needed. CSS is really powerful and it'll save you a lot of time.

其他推荐答案

<p style="color:red">Your Text here</p>

But as others have by now said in more and better words: Even better than the above would be to use classes or IDs and assign the CSS-attributes to that instead of using the inline style.