【问题标题】:Styling email link / href="mailto:" with CSS使用 CSS 样式化电子邮件链接 / href="mailto:"
【发布时间】:2012-05-24 20:06:35
【问题描述】:

感谢 StackOverflow,我终于找到了一种设置电子邮件链接样式的方法,但我想知道为什么如果没有我在这里找到的解决方案它就无法工作。

由于链接是属性类“about”的跨度的一部分,它定义了字体大小和样式,电子邮件链接不应该以 11px 和无衬线显示吗?

a[href^="mailto:"]

{ 
  font-family: sans-serif;
  color: black;
  font-size: 11px;
}

效果很好,只要我尝试将其更改为

.about a[href^="mailto:"]

{ 
  font-family: sans-serif;
  color: black;
  font-size: 11px;
}

它也没有按预期运行。

标签不听跨度格式或类嵌套吗?

    <!DOCTYPE html>
<html>
<head>
<style type="text/css">

html {
    height:100%;
}

body {
    height: 100%;
    margin-left: 20px;
    margin-top:0px;
}

.bottom-left {

    position: absolute;
    font:sans-serif;
    bottom: 15px;
    left: 15px;
}


.bold {
    font-family: serif;
}


.about {
    font-size: 11px;
    font-family: sans-serif;
}


/*a[href^="mailto:"]

{ 
  font-family: sans-serif;
  color: black;
  font-size: 11px;
}*/



.address {
font-size: 11px;
border-bottom: 1px grey dotted;
}




</style>

<title>TEMP</title>

</head>
<body>


<div class="bottom-left">
        <span class="about">
            <span class="bold">XYZ</span> is a project space .&nbsp;&nbsp;&#124;&nbsp;
            <span="address">Website Information</span> &mdash; <a href="mailto:info@info.eu">info@info.eu</a>
        </span>
</div>



</body>
</html>

【问题讨论】:

    标签: css email href mailto


    【解决方案1】:

    您好,实际上您已经评论了您的电子邮件链接 css:-

    所以现在像这种方法一样编写css它工作正常......

    a[href^="mailto:"]
    { 
      font-family: sans-serif;
      color: red;
      font-size: 11px;
    }
    

    查看演示:- http://jsbin.com/ijofoq/edit#html,live

    更新

    现在它工作正常...编辑您的 HTML 并添加您的 HTML

    <div class="bottom-left">
        <div class="about">
            <span class="bold">XYZ</span> is a project space .&nbsp;&nbsp;&#124;&nbsp;
            <span="address">Website Information</span> &mdash; <a href="mailto:info@info.eu">info@info.eu</a>
        </div>
    

    基本上你必须从 .about 类中删除 span 标签。

    检查这个:- http://jsbin.com/ijofoq/2/edit

    【讨论】:

    • 谢谢谢兰德!我知道它是这样工作的,我想问为什么没有这个解决方案它就不能工作,为什么 a href 元素不能嵌套到 .about 类中。
    • 对我来说似乎是一个错误,它适用于 div 而不是 span
    【解决方案2】:

    我认为.about 优先于a。 参看。 Css Rule Specificity.

    基本上,一个 css 规则集被分配一个优先级,就像这样的版本号:

    {#id}.{#class}.{#element}.{order}
    

    • {#id} : id 选择器计数
    • {#class} :类、伪类、属性的计数
    • {#element} : 元素计数,伪元素
    • {order} : 此规则在所有文件中的索引

    所以,我们有以下顺序:

    1. 0.2.1.* .about a[href^="mailto:"] (0 id, 1 class + 1 attr, 1 element)
    2. 0.1.1.a span.about (0 id,1 类,1 元素)
    3. 0.1.1.b a[href^="mailto:"] (0 id, 1 attr, 1 element)
    4. 0.1.0.* .about (0 id, 1 class, 0 element)

    span.abouta[href^="mailto:"] 具有相同的特殊性(1 个类或属性,以及 1 个元素),所以顺序很重要,最后胜出。

    如果您删除 span,那么规则就不那么具体和松散了。

    (另外,区分直接应用于元素的规则和其他从父元素继承的规则......)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-01
      • 1970-01-01
      • 2011-04-12
      • 1970-01-01
      • 2013-03-24
      • 1970-01-01
      • 2015-07-26
      相关资源
      最近更新 更多