html - 使用 CSS 对齐两个 html 元素

标签 html css

我有两个元素,<h1><img> .我更改了两个元素的填充,以便它们在页面上对齐。除非我缩放浏览器窗口,否则这会起作用,在这种情况下,两个元素上的填充会稍微不同步。

这是html代码:

<header>
    <h1> Welcome to the SCEC </h1>
    <img id="SCEC_logo" src="images/sced_logo.png" alt="scec logo">
</header>

这是 CSS 代码:

 h1{
    padding: 2em 0em .2em .5em;
    color: #FDB813;
    font-family: Georgia;
    font-weight: 500;
    font-size: 2.5em;
    font-style: italic;
    text-align: center;
    background-color: #004A98;
    width: 60%;
    margin-left: 6.5%;
    border-top-left-radius: 15px;
    border-bottom-left-radius: 15px;
    -moz-border-top-left-radius: 15px;
    -moz-border-bottom-left-radius:15px;
    float: left;
    }

 #SCEC_logo{
    background-color: #004A98;
    padding: .93em 2.5em .93em 0;
    }

http://jsfiddle.net/isherwood/BHy3u

在浏览器的默认大小下,元素完美排列,但当我缩放它时,<h1> 上的蓝色填充底部元素比 <img> 上的填充稍长元素。

我还注意到在 IE 中,<img>元素在默认屏幕尺寸下略长。

如何调整内边距使其在任何浏览器中以任何屏幕尺寸排列?

最佳答案

似乎将样式应用于 header 会简单得多:

http://jsfiddle.net/isherwood/BHy3u/5

header {
    padding: 2em .5em;
    background-color: #004A98;
    width: 60%;
    margin-left: 6.5%;
    border-radius: 15px 0 0 15px;
    float: left;
}
header h1 {
    color: #FDB813;
    font-family: Georgia;
    font-weight: 500;
    font-size: 2.5em;
    font-style: italic;
    text-align: center;
    padding: 0; 
    margin: 0;
}
header img {
    float: right;
}

关于html - 使用 CSS 对齐两个 html 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22133958/

相关文章:

html - 将一个 div 垂直对齐到另一个 div 旁边

html - 如何将 div 的宽度绑定(bind)到 angular 2.0 中的模型?

Html Bootstrap - 包装引导轮播以适合呈现的图像

javascript - ¿如何创建文本 slider 动画?

html - CSS中使用@import的优缺点是什么

javascript - 通过 POST 调用 HTML Javascript 函数

javascript - 基金会 5 : call js function from topbar dropdown anchor

css inherit from calc property 似乎是重复计算而不是继承计算值

css - 单击时更改样式

css - 创建可以使用 html、css 填充颜色的自定义图形