HTML 文本覆盖图像

标签 html css

我试图在一张图片上放置 4 个标题,但是我给出的代码发生了一些奇怪的事情:左边的 2 个标题被抛出了 div。

引用图片:

如您所见,div 有一个红色边框,但出于某种原因文本会跳出它。

代码:

@font-face {
    font-family: Head;
    src: url('HeadFont.ttf');
}
@font-face {
    font-family: SpecifyLight;
    src: url('SpecifyLight.ttf');
}
@font-face {
    font-family: SpecifyMedium;
    src: url('SpecifyMedium.ttf');
}

@font-face {
    font-family: Android;
    src: url('Android.ttf');
}

BODY {
    background-image: url("bg.jpg");
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-position: center;
    width: 75%;
    margin:auto;
}

/*Navigatie bar styling*/
ul {
    list-style-type: none;
    margin: auto;
    margin-top: 0;
    padding: 0;
    overflow: hidden;
    background-color: black;
}

.ulMain {
    background-image: url("menu.png");
    background-position: center;
    background-position: top;
    background-size: 100% auto;
    border-radius: 0px 0px 20px 20px;
}

li {
    float: left;
    border-right: 1px solid #bbb;
}

    li a {
        display: block;
        color: white;
        text-align: center;
        padding: 14px 16px;
        text-decoration: none;
    }

        li a:hover:not(.active) {
            color: #B8371A;
            background-color: white;
        }

    li:last-child {
        border-right: none;
    }

.active {
    background-color: darkred;
}

h1 {
    width: 100%;
    font-family: Head;
    font-size: 80px;
    margin-top: 0;
    text-align: center;
    color: black;
    padding: 0;
}

h2 {
    width:100%;
    font-family: Head;
    font-size: 50px;
    text-align: center;
    padding: 0;
}

h3 {
    width:100%;
    font-family: Head;
    font-size: 30px;
    text-align: left;
    padding: 0;
}

h4 {
    width:100%;
    font-family: Head;
    font-size: 40px;
    margin: 10px;
    text-align: left;
    color: red;
}

h5 {
    width:100%;
    font-family: Head;
    font-size: 40px;
    text-align: right;
    color: red;
}

header {
    width: 100%;
    margin: auto;
    height: auto;

}

.menu {
    width: 100%;
    margin: auto;
    height: auto;
}

footer {
    width: 100%;
    margin: auto;
    height: 200px;
    background-color: #B8371A;
    border-radius: 0px 0px 20px 20px;
}

.content {
    font-family:SpecifyLight;
    font-size: 20px;
    width: 100%;
    margin: auto;
}

.bioFoto {
    float: right;
    width: 20%;
    height: auto;
    margin-right: 100px;
    border: solid;
    border-color: black;
    border-radius: 20px;
}

.website {
    width: 100%;
    margin:auto;
    background-color: lightgray;
}

.headerFoto {
    display:block;
    width: 100%;
    margin: auto;
    height: auto;
}

.mainFoto {
    width: 100%;
    margin: auto;
    height: auto;
}

.homePage {
    width:inherit;
    margin:auto;
    border:solid;
    border-color:red;
}
<!DOCTYPE html>

<html>
<head>
    <title>Atlas</title>
    <link rel="stylesheet" type="text/css" href="siteStyle.css">
    <link rel="shortcut icon" type="image/png" href="Logo.png" />
</head>
<body>
    <div class="website" style="background-color:black">
        <div class="menu">
            <ul>
                <li class="active"><a href="index.html">Home</a></li>
                <li><a href="bio.html">Biografie</a></li>
                <li><a href="projecten.html">Projecten</a></li>
                <li><a href="galerij.html">Galerij</a></li>
                <li><a href="contact.html">Contact</a></li>
            </ul>
        </div>
        <h2 style="color:white">Klik op de opties hier beneden</h2>
            <div class="homePage">
                <img src="grootLogo.png" class="mainFoto">
                <h4 style="position: absolute; top:50%;">Biografie</h4>
                <h5 style="position: absolute; top:50%;">Projecten</h5>
                <h4 style="position: absolute; top:75%;">Galerij</h4>
                <h5 style="position: absolute; top:75%;">Contact</h5>
                <br />
            </div>
    </div>
    <footer></footer>
</body>
</html>

最佳答案

简答

设置position: relativehomePage .

少简答

来自 MDN :

The absolutely positioned element is positioned relative to nearest positioned ancestor (non-static). If a positioned ancestor doesn't exist, the initial container is used.

在您的情况下,因为所有元素都是 position: static (这是默认行为),这意味着您的 4 个绝对元素是相对于 <html> 的标记(例如您的初始容器)。
设置position: relative (或任何不是 static 的值)在它们的父级上通过使任何 top|right|bottom|left 来修复它们的定位相对于它。

关于HTML 文本覆盖图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39896275/

相关文章:

javascript - 如何在 HTML 中使用列表样式(正方形、圆形等)在一行中打印事物列表?

javascript - IE8 使用 JavaScript 加载和移动广告

jquery - IE 中的圆 Angular 使用 ui-corner-all

javascript - 获取整个 DOM 包装边界客户端矩形

javascript - 动画滚动 100%(水平)?

javascript - css 随机修复 JS 和多个 div

javascript - 在 Jquery 中搜索无法显示 'data-name' 的结果

jquery - AJAX调用返回JSON,如何渲染正确格式的数据

javascript - 旋转(360 度)圆形 div 内的文本选框动画

css - 菜单周围不需要的边距