【问题标题】:text-align: center; not working文本对齐:居中;不工作
【发布时间】:2015-05-17 18:13:32
【问题描述】:

我在 JSfiddle 中重新创建了它,但仍然无法让它工作。我错过了什么?

这是 HTML

<div class="row">
<div class="6u 12u$(small)">    <span class="image fit"><img src="images/pic01.jpg" alt=""></span>

    <h2green class="h2green">HIDE IN PLAIN SIGHT</h2green>
    <p>Escape to the big city of your choice; top floor of the tallest skyscraper with a champagne bottle in the bucket filled with ice, this might be just what you need .</p>
</div>
<div class="6u$ 12u$(small)">   <span class="image fit"><img src="images/pic02.jpg" alt=""></span>

    <h2blue>ESCAPE TO THE COUNTRYSIDE</h2blue>
    <p>You deserve a relaxing break and a quality experience. We strive to make it that way by finding you the right property and making sure you receive the very best of service before and during your stay.</p>
</div>

这是 CSS

h2green {
text-align: center;
font-family: Steelfish;
color: #80c342;
font-size: 3em;
}

h2blue {
text-align: center;
font-family: Steelfish;
color: #0080c6;
font-size: 3em;
}

所有文本总是向左对齐,我需要它居中。

https://jsfiddle.net/m19f35nb/

我只想让所有 H2 和 Ps 居中。

【问题讨论】:

    标签: html css center jsfiddle text-align


    【解决方案1】:

    在 HTML 中没有 &lt;h2green&gt; 或 &lt;h2blue&gt; 这样的东西。为了从错误中恢复,浏览器正在生成内联元素(text-align 属性不适用于这些元素)。

    使用真正的 HTML (&lt;h2 class="blue"&gt;),然后更改 CSS 以匹配:h2.blue。

    【讨论】:

    • 好吧,现在我觉得自己很愚蠢。我怎么能指定哪个

      是紫色的,哪个是绿色的,哪个是蓝色的。

    【解决方案2】:

    这里是 HTML 和 CSS 示例代码

     <h2 class="blue">ESCAPE TO THE COUNTRYSIDE</h2>
     <h2 class="green">ESCAPE TO THE COUNTRYSIDE</h2>
    
    h2.green {text-align: center; font-family: Steelfish;color: #80c342;font-size: 3em;}
    
    h2.blue { text-align: center; font-family: Steelfish; color: #0080c6; font-size: 3em; }
    

    【讨论】:

    • 谢谢@Javid。我所要做的就是从各处移除多余的 h2 元素。现在可以享受美食了。谢谢
    猜你喜欢
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多