html - 具有 "viewport-width"属性的 CSS 文本定位

标签 html css

当我创建一个包含 h1div 时,我将 margin-left 设置为 h1 55vw(viewport-宽度)。我的 1378px 宽度显示器没问题,但当我决定调整浏览器 h1 的大小时,文本开始超越/超过图像。如何在任何尺寸的屏幕(非常高/低分辨率除外)中将其设置在我需要的位置。

HTML代码:

<div class="logo-container">
        <h1 class="text">
            Netherlands
        </h1>
    </div>

它的 CSS:

<style>
    .logo-container {
        background: url(https://www.google.com/images/srpr/logo9w.png) no-repeat center;
        padding: 28px 0 14px;
    }
    .text {
        margin-top: 62px;
        margin-left: 55vw;
        font: 16px Arial;
        font-weight: 700;
    }
</style>

最佳答案

Try this Fiddle

Logo 容器水平居中,并带有自动边距。文本相对于 Logo 容器绝对定位。这样,无论 Logo 位于何处,文本都绝对会位于正确的位置。

HTML

<div class="logo-container">
    <h1 class="text">
        Netherlands
    </h1>
</div>

CSS

.logo-container {
    background: url(https://www.google.com/images/srpr/logo9w.png) no-repeat;
    height: 95px;
    width: 269px;
    margin: 0 auto;
    position: relative;
}
.text {
    position: absolute;
    top: 60px;
    left: 210px;
    font: 16px Arial;
    font-weight: 700;
}

关于html - 具有 "viewport-width"属性的 CSS 文本定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29755263/

相关文章:

css - Angular Material - 删除空格

jquery - 本地托管站点的文件路径

jquery - 根据单元格值为HTML/jquery表单元格设置颜色格式

c# - 如何在dll中嵌入html文件并稍后编辑内容?

css - 有没有更简单的方法来防止元素滚动到页面顶部之外?

css - 非粗体文本占用粗体文本空间

javascript - 使用 jQuery 在段落开头和特定链接之间获取文本

javascript - 来自文本框的动态进度条

javascript - 声明 z-index 后无法单击按钮?

html - 无法在 IE 中单击菜单