gpt4 book ai didi

html - 如何在不使用绝对位置的情况下使我的 h1 位于图像背景的中心?

转载 作者:太空宇宙 更新时间:2023-11-04 07:56:44 24 4
gpt4 key购买 nike

/*General*/
@import url('https://fonts.googleapis.com/css?family=Oxygen');

.wrapper {
max-width: 630px;
margin: 0 auto;
}

/*Header*/
#header .navbar .navbar-header a {
color: white;
font-size: 25px;
font-family: 'Oxygen', sans-serif;
}
#header .navbar-inverse {
background-color: #2C3E50;
border-color: #2C3E50;
}
#header .navbar {
border-radius: 0;
}
#header .navbar-nav > li > a {
color:white;
}
#header .navbar {
margin-bottom: 0;
}
.nav.navbar-nav.navbar-right li a {
color: white;
}

/*Content*/
#content .content-content .responsive-image {
width: 100%;
background-size: 100% 100%;
z-index:0;
}
#content .content-content h1 {
font-family: 'Open Sans', sans-serif;
color: #2c3e50;
font-size: 50px;
font-weight: bold;
z-index: 2;
text-align: center;
}

/*Footer*/

/*Responsiveness*/

/*Desktop*/
@media screen and (min-width: 992px)
{
#content .content-content .responsive-image {
background-image: url('https://wallpaperscraft.com/image/board_black_line_texture_background_wood_55220_1920x1080.jpg');
}
}

/*Tablet*/
@media screen and (min-width: 768px) and (max-width: 991px)
{
#content .content-content .responsive-image {
background-image: url('https://wallpaperscraft.com/image/board_black_line_texture_background_wood_55220_1920x1080.jpg');
}
}

/*Mobile*/
@media screen and (max-width: 767px)
{
#content .content-content .responsive-image {
background-image: url('https://wallpaperscraft.com/image/board_black_line_texture_background_wood_55220_1920x1080.jpg');
}
}

/*Custom*/
@media screen and (max-width: 574px)
{
#content .content-content .responsive-image {
background-image: url('https://wallpaperscraft.com/image/board_black_line_texture_background_wood_55220_1920x1080.jpg');height:250px;
}
}
<!DOCTYPE html>
<html>
<head>
<title>Magic Gate</title>
<link rel="stylesheet" type="text/css" href="Custom.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<div id="header">
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Magic Gate</a>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav navbar-right">
<li>
<a href="#">Play</a>
</li>
<li>
<a href="#">Log Out</a>
</li>
</ul>
</div>
</div>
</nav>
</div>
<div id="content">
<div class="content-content">
<img src="https://wallpaperscraft.com/image/board_black_line_texture_background_wood_55220_1920x1080.jpg" class="responsive-image">
<div class="wrapper">
<h1>Welcome to Magic Gate</h1>
</div>
</div>
</div>
<div id="footer">
</div>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</html>

我不想使用绝对定位,因为它会破坏横幅中心的对象/控件的居中。你能帮我解决这个问题吗,除非它会响应,否则不要将 img 更改为 div 背景,因为一切都是响应的。我的目标是将 h1 文本置于横幅中间。

最佳答案

像你问的那样,我添加了 position:relative; top:-250px;h1 。但这是一种丑陋的做法,也是一种非常糟糕的代码。 position:absolute;transform:transle() 更好

/*General*/
@import url('https://fonts.googleapis.com/css?family=Oxygen');
.wrapper {
max-width: 630px;
margin: 0 auto;
}


/*Header*/
#header .navbar .navbar-header a{color: white;font-size: 25px;font-family: 'Oxygen', sans-serif;}
#header .navbar-inverse {background-color: #2C3E50;border-color: #2C3E50;}
#header .navbar {border-radius: 0;}
#header .navbar-nav > li > a {color:white;}
#header .navbar {margin-bottom: 0;}
.nav.navbar-nav.navbar-right li a {color: white;}




/*Content*/

#content .content-content .responsive-image{width: 100%;background-size: 100% 100%;z-index:0;}
#content .content-content h1{font-family: 'Open Sans', sans-serif;color: #2c3e50;font-size: 50px;font-weight: bold;z-index: 2;text-align: center;position:relative;top:-250px;}


/*Footer*/

/*Responsiveness*/

/*Desktop*/
@media screen and (min-width: 992px)
{
#content .content-content .responsive-image{background-image: url('https://wallpaperscraft.com/image/board_black_line_texture_background_wood_55220_1920x1080.jpg');}
}

/*Tablet*/
@media screen and (min-width: 768px) and (max-width: 991px)
{
#content .content-content .responsive-image{background-image: url('https://wallpaperscraft.com/image/board_black_line_texture_background_wood_55220_1920x1080.jpg');}
}

/*Mobile*/
@media screen and (max-width: 767px)
{
#content .content-content .responsive-image{background-image: url('https://wallpaperscraft.com/image/board_black_line_texture_background_wood_55220_1920x1080.jpg');}
}

/*Custom*/
@media screen and (max-width: 574px)
{
#content .content-content .responsive-image{background-image: url('https://wallpaperscraft.com/image/board_black_line_texture_background_wood_55220_1920x1080.jpg');height:250px;}
}
<!DOCTYPE html>
<html>
<head>
<title>Magic Gate</title>
<link rel="stylesheet" type="text/css" href="Custom.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<div id="header">
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Magic Gate</a>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav navbar-right">
<li><a href="#">Play</a></li>
<li><a href="#">Log Out</a></li>
</ul>
</div>
</div>
</nav>
</div>
<div id="content">
<div class="content-content">
<img src="https://wallpaperscraft.com/image/board_black_line_texture_background_wood_55220_1920x1080.jpg" class="responsive-image">
<div class="wrapper">
<h1>Welcome to Magic Gate</h1>
</div>
</div>
</div>
<div id="footer">
</div>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</html>

关于html - 如何在不使用绝对位置的情况下使我的 h1 位于图像背景的中心?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47265968/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com