代码之家  ›  专栏  ›  技术社区  ›  A Salcedo

如何将文本居中放置在图像上?

  •  1
  • A Salcedo  · 技术社区  · 17 年前

    我正在尝试在按钮图像上放置一些文本。我设法在按钮上从左到右对齐它的中心,但是垂直对齐对其余的部分不起作用。CSS中是否有一种方法可以使该文本垂直居中?

    alt text

    .selectedTabOnPlacementMenu
    {
        background: url('/Content/Images/Blank_Yellow.png') no-repeat;
        width:88px;
        height:28px;
        text-align:center;
        font-weight:bold;
    }
    
    3 回复  |  直到 17 年前
        1
  •  5
  •   Emily    17 年前

    如果是单行文本,则可以设置行高。

    <div style="height:50px;background-color:#ccc;">
        <p style="line-height:50px;">test</p>
    </div>
    

    将多行文本垂直居中要困难得多。

        2
  •  1
  •   MCardinale    17 年前

    只需输入代码

    line-height:28px;
    

    在你们班上。

        3
  •  0
  •   Petrogad    17 年前

    您可以在DIV中使用Margin Top:5px;或padding Top:5px;(取决于DIV的设置方式),在该DIV中您可以将文本向下推。

    干杯