【问题标题】:How is font-size not working here?字体大小在这里如何不起作用?
【发布时间】:2011-02-18 12:56:56
【问题描述】:

按照The 6 Most Important CSS Techniques You Need To Know 中的建议,我将身体的font-size 设置为62.5%,将container div 的font-size 设置为1.4 em(与文章略有不同)。 p.tagsp.publishedfont-size 设置为 1em。

但是,这对我不起作用。 p.tagsp.published 中的普通文本和文本的大小相同(由 Firebug 计算的 16.8 像素)。你能解释为什么我的代码不起作用吗?我正在 Firefox 3.6.3 中进行测试。作者显示的sample page 在同一个浏览器中运行良好。

我已经复制了下面的整个页面——很抱歉它的长度,但我认为最好不要遗漏任何内容。

<html>
        <head>
                <title>Title</title>
                <style type="text/css">
                        body {
                                font-family: Georgia, "Century Schoolbook", "Times New Roman", Serif;
                                font-size: 62.5%;
                                background-color: #2B3856; /* Dark slate blue */
                        }
                        h1, h2, h3, h4, h5, h6 {
                                font-family: Verdana, Helvetica, Tahoma, "Sans Serif";
                                color: #2B3856;
                                margin-top: 2px;
                        }
                        h1 a, h2 a, h3 a, h4 a, h5 a, h6 a {
                                text-decoration: none;
                                color: #2B3856;
                        }
                        h1 a:hover, h2 a:hover, h3 a:hover, h4 a:hover, h5 a:hover, h6 a:hover {
                                text-decoration: underline;
                        }
                        div#container {
                                width: 800px;
                                font-size: 1.4em;
                                margin: 5px auto;
                                background-color: #E3E4FA; /* Lavender */
                        }
                        #sidebar {
                                width: 200px;
                                float: right;
                                margin: 0px;
                                padding: 0px;
                        }
                        #sidebar div {
                                padding: 0 5px 5px;
                        }
                        #sidebar div.shadowbox { margin-right: 5px; }
                        #content {
                                width: 600px;
                                float: left;
                                margin: 0px;
                                padding: 0px;
                        }
                        #header {
                                /*background-color: white;*/
                                background-color: #2B3856; /* #E3E4FA; Lavender */
                                margin-bottom: 5px;
                                height: 100px;
                        }
                        #header h1 {
                                color: #B93B8F; /* Plum */
                                line-height: 100px;
                                text-align: center;
                                font-size: 45px;
                        }
                        #description {
                                color: #7D1B7E /* Dark Orchid */
                        }
                        a {
                                text-decoration: underline;
                                color: #153E7E;
                        }
                        a:hover {
                                text-decoration: none;
                        }
                        div#posts {
                                padding: 0px;
                                font-size: 1.2em;
                                margin: 0px;
                        }
                        div#posts div.post {
                                padding: 5px;
                                margin: 0px 5px 15px 5px;
                        }
                        p.tags, p.published {
                                font-size: 1em;
                        }
                        .shadowbox {
                                background: repeat 0 0 url('http://www.jawsalgorhythmics.com/images/darkness-100x100-10pct.png');
                        }
                        .justifycenter { text-align: center; }
                        .floatright { float:right; }
                        .floatleft { float: left; }
                        .clearright { clear: right; }
                        .clearleft { clear:left; }
                        .clearboth { clear: both; }
                        .halfsidebarwidth { width: 82px; }
                </style>
        </head>
        <body>
                <div id="container">
                        <div id="header">
                                <h1>Odds 'n Ends</h1>
                        </div> <!-- header -->

                        <div id="sidebar">
                                <div class="shadowbox">
                                        <br /><p class="justifycenter"><img width="64" height="64" src="{PortraitURL-64}" /></p>

                                        <div class="floatleft halfsidebarwidth"><a href="/archive" class="archive">Archives</a></div>
                                        <div class="floatleft halfsidebarwidth"><a href="{RSS}" class="rss">RSS</a></div>
                                        <div class="clearboth"></div>
                                </div>
                        </div> <!-- sidebar -->

                        <div id="content">
                                <div id="posts">

                                                <div class="post shadowbox">
                                                                <span class="quote">
                                                                        "It does not matter how slow you go so long as you do not stop."

                                                                        <div class="source">Wisdom of <a href="#" title="http://en.wikipedia.org/wiki/Confucius">Confucius</a></div>
                                                                </span>
                                                        <p class="tags">Tags: #<a href="#" title="http://demo.tumblr.com/tagged/wisdom">wisdom</a>&nbsp; </p>

                                                        <p class="published">Posted: Nov 08, 2006 at 2:27 pm
                                                                &nbsp;&nbsp;<a href="#" title="http://demo.tumblr.com/post/236/it-does-not-matter-how-slow-you-go-so-long-as-you">Permalink</a>&nbsp;&nbsp; <a href="#" title="http://tumblr.com/xr06k">Short URL</a></p>
                                                </div>

                                </div> <!-- posts -->
                        </div> <!-- content -->

                        <div class="clearboth"></div>

                        <div id="footer" style="text-align: justify;">
                                <h1>The footer</h1>
                        </div>
                </div> <!-- container -->
        </body>
</html>

【问题讨论】:

  • 使用它而不是发布整个 HTML 列表:jsfiddle.net/JYfqL
  • 这对我来说似乎是一种毫无意义且可能是错误的“技术”。诸如“通过将正文字体大小设置为 62.5%,将您的字体大小设置为 10 像素”之类的陈述是错误的,我看不出它如何使网页设计更容易或有所不同。它只会导致您遇到的问题。

标签: html css layout font-size


【解决方案1】:

您的&lt;p class="tags"&gt;&lt;p class="published"&gt; 元素位于container div 中,其字体大小为1.4em,也位于posts div 中,其字体大小为1.2em。我认为将段落的字体大小设置为1em 并没有真正做任何事情,因为em 单位是累积的。因此,如果您的“根”字体大小为 62.5%,则第一个 div 将增加 40%,第二个 div 将再增加 20%。您的两个段落也将具有此字体大小,因为它们不会增加或减少大小。

基本上,如果您希望“标签”和“已发布”段落的文本更小,请将它们的大小设置为小于 1,例如0.9em。这将使它们的大小比同一父 div 中的其他元素小 10%。

【讨论】:

  • 你是对的,当然!!!!这对我来说真的很愚蠢——em的大小是相对于祖先的——所有的!!!所以当然效果是累积的。谢谢!
【解决方案2】:

格雷厄姆是对的,我确定了;

p.tags, p.published {
    font-size: 0.5em;
}

这改变了字体大小。 Updated Jsfiddle here.

EM 是根据正文字体的基本大小计算得出的。 Look here 解释这是如何工作的:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-29
    • 1970-01-01
    • 2021-12-19
    • 2014-03-06
    • 2016-01-02
    • 2018-09-07
    相关资源
    最近更新 更多