css - HTML 5 中的垂直居中 Logo 和标题

标签 css html responsive-design html-head

我正在尝试创建我的投资组合网站的响应式版本,并将我的 Logo 图像和标题嵌套在 header 标签中。旧版本使用 div 将 Logo 和标题 float 在页眉的左右两侧。

这就是我在不使用 divs 的情况下尝试分隔 imgh1 时遇到的问题。 标题位于右上角,但我不知道如何在不使用旧方法的情况下将 imgh1 垂直居中。

目前的布局

Current Layout

HTML

<header>
    <img src="images/logo.png" width="203" height="41" alt="logo for Design415.com" class="logo">
    <h1 class="pagetitle">PORTFOLIO TITLE</h1>
</header>

CSS

header {
    position: relative;
    width: auto;
    max-width: 900px;
    height: 60px;
    margin: 0 auto 20px auto;
    background-color: #333333;
    overflow: hidden;
}

.logo {
    position: absolute;
    left: 0;
    margin: 0;
}

.pagetitle {
    position: absolute; 
    right:0; 
    font-family: 'Century Gothic', Helvetica, sans-serif; 
    font-size: 1.40em; 
    font-weight: bold;
    margin: 0;
    color: rgb(240, 240, 240);
}

最佳答案

要使文本垂直居中,请添加 line-height: 60px:

.pageTitle {
    // Your styles here
    line-height: 60px;
}

要使图像垂直居中,请更改顶部边距:

.logo {
    // Your styles here
    margin-top: 9px;
}

这是一个 working JSFiddle .

关于css - HTML 5 中的垂直居中 Logo 和标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22750371/

相关文章:

javascript - 使用附加 div 时 MVC 5 View 看不到 html 图像

jquery - Safari 在使用 jQuery Masonry 时忽略边距

CSS - 在按钮内定位图标

css - IE8 div 下推

javascript - 如何根据条件将 DIV 滚动到浏览器窗口的顶部?

html - 旋转 div 在 IE 上产生黑色背景,我该如何删除它?

php - 响应式设计 - Android 不缩放

html - 如何在同一行显示 2 个 div,同时在最后一个 div 中保持水平滚动?

css - @media 在 980px 宽度以下不工作

image - 如何以响应模式在移动/桌面上交换图像