h3 和锚标记样式问题

发布于 2024-11-02 09:29:18 字数 1220 浏览 6 评论 0原文

我有以下 html 片段,可以在我的页面上正常工作:

<a href="url goes here" onclick="return ! window.open(this.href);"><h3>title goes here</h3></a>

使用以下 css

h3
{
    font-family:verdana, arial, helvetica, sans-serif; 
    font-size:75%; 
    font-weight:bold; 
    font-style:normal; 
    text-decoration:none; 
    text-transform:none; 
    margin:0px; 
    padding:0px;
    color:#2C6598;
}

但是,这不是有效的 xhtml,因为

标记不应位于 内。标签。

当我将

标签移到 标签之外时,CSS 似乎不起作用,即由于某种原因它失去了样式。

例如:

<h3><a href="url goes here" onclick="return ! window.open(this.href);">title goes here</a></h3>

with:

h3
{
    font-family:verdana, arial, helvetica, sans-serif; 
    font-size:75%; 
    font-weight:bold; 
    font-style:normal; 
    text-decoration:none; 
    text-transform:none; 
    margin:0px; 
    padding:0px;
    color:#2C6598;
}

标记内的

标记的工作方式不同。

如何纠正这个问题?

I have the following html snippet which works correctly on my page:

<a href="url goes here" onclick="return ! window.open(this.href);"><h3>title goes here</h3></a>

using the following css

h3
{
    font-family:verdana, arial, helvetica, sans-serif; 
    font-size:75%; 
    font-weight:bold; 
    font-style:normal; 
    text-decoration:none; 
    text-transform:none; 
    margin:0px; 
    padding:0px;
    color:#2C6598;
}

However, this is not valid xhtml as the <h3> tag should not be within a <a> tag.

When I move the <h3> tags outside of the <a> tags, the css does not seem to work, i.e. it loses it's style for some reason.

For example:

<h3><a href="url goes here" onclick="return ! window.open(this.href);">title goes here</a></h3>

with:

h3
{
    font-family:verdana, arial, helvetica, sans-serif; 
    font-size:75%; 
    font-weight:bold; 
    font-style:normal; 
    text-decoration:none; 
    text-transform:none; 
    margin:0px; 
    padding:0px;
    color:#2C6598;
}

does not work the way it did with the <h3> tag within the <a> tags.

How can this be corrected?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

神也荒唐 2024-11-09 09:29:18

您必须在 标记上应用样式,

试试这个

h3 a
{
    font-family:verdana, arial, helvetica, sans-serif; 
    font-size:75%; 
    font-weight:bold; 
    font-style:normal; 
    text-decoration:none; 
    text-transform:none; 
    margin:0px; 
    padding:0px;
    color:#2C6598;
}

You have to apply the style on <a> tag

try this

h3 a
{
    font-family:verdana, arial, helvetica, sans-serif; 
    font-size:75%; 
    font-weight:bold; 
    font-style:normal; 
    text-decoration:none; 
    text-transform:none; 
    margin:0px; 
    padding:0px;
    color:#2C6598;
}
玉环 2024-11-09 09:29:18

这是因为锚点的样式优先。您只需将样式表更改为

h3, h3 a /** Applies to both H3 and LINK inside H3 **/
{
    font-family:verdana, arial, helvetica, sans-serif; 
    font-size:75%; 
    font-weight:bold; 
    font-style:normal; 
    text-decoration:none; 
    text-transform:none; 
    margin:0px; 
    padding:0px;
    color:#2C6598;
}

这应该可以解决问题。

This is because the styles for your anchor are taking precedence. You can simply change the stylesheet to

h3, h3 a /** Applies to both H3 and LINK inside H3 **/
{
    font-family:verdana, arial, helvetica, sans-serif; 
    font-size:75%; 
    font-weight:bold; 
    font-style:normal; 
    text-decoration:none; 
    text-transform:none; 
    margin:0px; 
    padding:0px;
    color:#2C6598;
}

This should fix the problem.

可爱咩 2024-11-09 09:29:18

我遇到了同样的问题,所以我只是简单地创建了一个新类,其中包含我希望最终文本具有的属性,所以它是这样的:

CSS:(没有“!important”就无法工作)

.myclass, .myclass a {
display: block !important;
font-size: 14px !important; 
font-weight: bold !important;
height: 32px !important;}

HTML:

<a class="myclass" "url goes here">title goes here</a>

I had the same problem, so I just simply made a new class with properties I wanted the final text to have, so it's something like that:

CSS: (it didn't work without "!important")

.myclass, .myclass a {
display: block !important;
font-size: 14px !important; 
font-weight: bold !important;
height: 32px !important;}

HTML:

<a class="myclass" "url goes here">title goes here</a>
痴意少年 2024-11-09 09:29:18

我知道这篇文章很旧,但我遇到了这个问题。您可能必须满足任何可能覆盖您的样式的伪类,例如 a:visited 等。所以您还需要做的是:

h3 a, a:visited 
{
   font-family:verdana, arial, helvetica, sans-serif; 
   font-size:75%; 
   font-weight:bold; 
   font-style:normal; 
   text-decoration:none; 
   text-transform:none; 
   margin:0px; 
   padding:0px;
   color:#2C6598;
}

I know this post is old but I encountered this problem. You may have to cater for any pseudo classes which may be overriding your styles e.g. a:visited etc. So what you would also need to to do is:

h3 a, a:visited 
{
   font-family:verdana, arial, helvetica, sans-serif; 
   font-size:75%; 
   font-weight:bold; 
   font-style:normal; 
   text-decoration:none; 
   text-transform:none; 
   margin:0px; 
   padding:0px;
   color:#2C6598;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文