html - 如何将 Logo 背景图像和文本在标题中居中?

标签 html css

我被一些感觉应该很简单的东西难住了!

我正在尝试将 Logo 置于 <header> 的中心。该标志由<a>组成。背景图像是 Logo 图标, anchor 文本是 Logo 名称。我的 Logo 图标使用 margin: 0 auto; 居中关于<h1>但找不到一个好的解决方案将图标和文本作为一个整体居中。

<header>
  <h1>
    <a href="#" class="logo">Logo Name</a>
   </h1>
</header>

CSS:

header h1 {
  height: 54px;
  width: 54px;
  background: url('../img/logo.png') no-repeat left;
  margin: 0 auto;
}

a.logo {
  font-size: 33px;
  margin: 0 auto;
  display: block;
  padding-top: 20px;
}

有什么想法吗?

最佳答案

我通常这样做:

<style>
    .logo{
        margin: 0px auto;
        padding-left: 120px; /* the space between the start of your logo and your text */
        padding-top: 30px; /* the vertical space between the top and your text, to center the logo*/
        display: block; /* very important since normally anchors are inline */
        background: url(path/to/logo.png) top left no-repeat; /* obviously :) */
    }
</style>

它确实不需要位于 h1 内。

当您看到结果时,您可能看不到它居中,那么,测量您的单位,并在上面的 .logo 规则内指定宽度和高度。

关于html - 如何将 Logo 背景图像和文本在标题中居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10433935/

相关文章:

html - 将 div 放置在绝对定位的 div 下方,无需嵌套

html - 样式化的无序列表中出现奇怪的换行符?

jquery - 如何根据屏幕分辨率调整按钮大小

javascript - 覆盖图像属性

html - 延迟加载 (data-src2) Jssor Slider 未根据 w3 标准进行验证

css - 如何在生产中编译CSS React Rails(5.2.3)

javascript - 如何在网站不闪烁/重新加载的情况下在 React 中动态注入(inject)样式表?

html - 使用图像作为要点

php - 在检查元素中显示 0*0 像素的图像

html - 使用标签和单选按钮作为 CSS 钩子(Hook)是否适合 HTML?