【问题标题】:Showing <h1> <h5> text inline using css使用 css 显示 <h1> <h5> 文本内联
【发布时间】:2013-12-26 12:15:37
【问题描述】:

我想显示“情绪分析”,这样 S 应该比 h5 文本大得多。

以下代码有什么问题:

HTML:

<h1>S</h1> <h5>entiment Analysis</h5>      

CSS:

h1 {
    margin-left: 36%;
    color:#C0C0C0;
    font-family: "calibri";
    text-decoration:underline;
    white-space: pre;
}

h5 {
    margin-left: 36%;
    color:#C0C0C0;
    font-family: "calibri";
    text-decoration:underline;
    white-space: pre;
}

Live code in CodePan

【问题讨论】:

  • 每个 H 元素都以新行开头。尝试使用 span 或其他元素。
  • 您收到了 6 个答案,但没有评论/接受。为什么?
  • 您的问题解决了吗?

标签: html css alignment html-heading


【解决方案1】:

这是一个混乱的代码。如果你只是想让第一个字母变大,你可以试试下面的方法。

Working Demo

HTML

<div>Sentiment analysis</div> 

CSS

div:first-letter {
  font-size:30px;
  font-weight:bold;
}

【讨论】:

【解决方案2】:

您正在尝试在同一行中对齐 2 个块级元素,因此您需要使用 display: inline; 或 display: inline-block(推荐)将它们设为 inline 或 inline-block,或者在内部使用 &lt;span&gt; 元素你的h1 喜欢

<h1><span>H</span>ello</h1>

你可以使用 H 来定位

h1 > span {
   font-size: 54px;
}

如果您只想定位第一个字母,最好使用:first-letter 伪,这样也可以为您节省一个元素。

Demo

注意:我在这里使用通用元素选择器,请确保您使用 class 或 id 以唯一地定位元素。

【讨论】:

  • 不推荐使用字体大小的px!用em代替px!
  • @AsrafulHaque 不是强迫症,也与问题无关:)
【解决方案3】:
Use this css rules for proper follow the css rules to render the requirement!


  <style>
  h1:first-line {
            margin-left: 36%;
            color:#C0C0C0;
            font-family: "calibri";
            font-size:1em;
            text-decoration:underline;
            white-space: pre;
            display:inline;
                }
   h1:first-letter{
            margin-left: 36%;
            color:#C0C0C0;
            font-family: "calibri";
            font-size:2em;
            text-decoration:underline;
            white-space: pre;
            display:inline;
                }
  </style>

【讨论】:

    【解决方案4】:
    <h1>Hello</h1>
    <h1>World</h1>
    
    h1:first-letter {
        font-size: 60px;
    }
    

    demo

    【讨论】:

    • 不推荐使用字体大小的px!用em代替px!
    【解决方案5】:
    <style type="text/css">
          .style1
        {
            font-size: 36px;
        }
    </style>
    </head>
    <body>
    <p>
       <span class="style1">S</span>entiment</p>
    </body>
    

    【讨论】:

      【解决方案6】:

      将display:inline 添加到h1 和h5,然后从h5 中删除 margin-left

      DEMO HERE >>

      试试这个

       h1
         {
               margin-left: 36%;
               color:#C0C0C0;
               font-family: "calibri";
               text-decoration:underline;
               white-space: pre;
               display:inline;
        }
      
      
      h5
         {
              color:#C0C0C0;
              font-family: "calibri";
              text-decoration:underline;
              white-space: pre;
              display:inline;
         }
      

      您的实现方式似乎并不复杂,即使您想使用您的代码也可以使用我的答案,否则在此处选择其他答案中的任何简单方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-04
        • 2019-08-07
        • 1970-01-01
        • 2013-11-30
        • 1970-01-01
        • 2013-08-18
        • 2018-01-05
        • 1970-01-01
        相关资源
        最近更新 更多