如何在一个语句中在HTML中设置不同的颜色? [英] How to set different colors in HTML in one statement?

查看:110
本文介绍了如何在一个语句中在HTML中设置不同的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑在一行中使用不同颜色的文本.怎么可能呢?

I'm thinking of having different color of text in one line. How could that be possible?

<p style="color:#4C4C4C;font-weight:bold;font-family:Calibri;font-size:20"> My Name is: 

 <"color:#FF0000;font-weight:bold;font-family:Tahoma;font-size:20"> Tintincute </p>

我想为Tintincute使用不同的颜色,但是与此相关的问题是,名称下降了一个空格.

I would like to have a different color for Tintincute but in one line the problem with this, the name went down one space.

这是代码示例:

<p style="color:#4C4C4C;font-weight:bold">All fields marked with   <style="color:#FF0000;font-weight:bold">*</color> <style="color:#4C4C4C;font-weight:bold">are required</p>

更新

@Phil:我尝试使用代码,但是没有用.代码本身已显示在页面上.这是我的方法:

Update

@Phil: I tried using the code, but it didn't work. The code itself was shown on the page. This is how I did it:

<div style="color:red">[+validationmessage+]</div>
p.detail {color:#4C4C4C;font-weight:bold;font-family:Calibri;font-size:20 }
span.name {color:#FF0000;font-weight:bold;font-family:Tahoma;font-size:20 }
<p class="detail">My Name is: <span class="name">Tintincute</span> </p>

推荐答案

您可以为此使用CSS并为元素创建类.所以你会有这样的东西

You could use CSS for this and create classes for the elements. So you'd have something like this

p.detail { color:#4C4C4C;font-weight:bold;font-family:Calibri;font-size:20 }
span.name { color:#FF0000;font-weight:bold;font-family:Tahoma;font-size:20 }

然后您的HTML将显示为:

Then your HTML would read:

<p class="detail">My Name is: <span class="name">Tintinecute</span> </p>

与内联样式表相比,它整洁了很多,更易于维护并提供了更多的重用性.

It's a lot neater then inline stylesheets, is easier to maintain and provides greater reuse.

以下是完整的HTML,可以演示我的意思:

Here's the complete HTML to demonstrate what I mean:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <style type="text/css">
    p.detail { color:#4C4C4C;font-weight:bold;font-family:Calibri;font-size:20 }
    span.name { color:#FF0000;font-weight:bold;font-family:Tahoma;font-size:20 }
    </style>
</head>
<body>
    <p class="detail">My Name is: <span class="name">Tintinecute</span> </p>
</body>
</html>     

您将看到我在标题的样式标签中包含样式表类,然后仅将这些类应用于代码中,例如<p class="detail"> ... </p>.遍历w3schools教程,只需要几个小时,当涉及到样式化HTML元素时,它将真正使您转身.如果将其剪切并粘贴到HTML文档中,则可以编辑样式,并查看它们在浏览器中打开文件时的效果.这样的实验是学习的好方法.

You'll see that I have the stylesheet classes in a style tag in the header, and then I only apply those classes in the code such as <p class="detail"> ... </p>. Go through the w3schools tutorial, it will only take a couple of hours and will really turn you around when it comes to styling your HTML elements. If you cut and paste that into an HTML document you can edit the styles and see what effect they have when you open the file in a browser. Experimenting like this is a great way to learn.

这篇关于如何在一个语句中在HTML中设置不同的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆