html - 在背景图像顶部添加徽标
问题描述
我正在努力在模板网站上的背景上添加徽标。徽标要么位于背景图像之间,要么根本不出现。
<!doctype html>
<html>
<head>
<title> Group Coursework </title>
<link REL="StyleSheet" TYPE="text/css" HREF="example.css">
<style>
body,
html {
height: 100%;
}
* {
box-sizing: border-box;
}
.bg-image {
/* Full height */
height: 100%;
/* Center and scale the image nicely */
background-position: center;
background-repeat: no-repeat;
background-size: cover;
background-attachment: fixed;
}
/* Images used */
.img1 {
background-image: url("https://images3.alphacoders.com/914/thumb-1920-914671.png");
filter: blur(4px);
-webkit-filter: blur(4px);
}
.img2 {
background-image: url("https://hdwallpapersmafia.com/wp-content/uploads/2018/11/baby-groot-playing-basketball-lk-3840x2160-1.jpg");
}
/* Center and scale the image nicely */
background-position: center;
background-repeat: no-repeat;
background-size: cover;
background-attachment: fixed;
}
.header {
width: 100%;
height: 100%;
}
.navbar {
width: 100%;
padding: 20px;
position: fixed;
top: 0px;
text-align: center;
transition: .5s;
}
.navbar ul li {
list-style-type: none;
display: inline-block;
padding: 10px 50px;
color: white;
font-size: 32px;
font-family: cursive;
cursor: pointer;
border-radius: 10px;
transition: .5s;
}
li a,
.dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rbga (0, 0, 0, 0.5);
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: grey;
}
.dropdown:hover .dropdown-content {
display: block;
}
.logo {
position: relative;
}
.logo:after {
content: "";
display: block;
width: 100%;
height: 100%;
position: absolute;
top: 15%;
left: 50%;
background-image: url("https://www.pngkey.com/png/detail/44-447166_cavalier-logo-with-sword-and-flag-cleveland-cavaliers.png");
background-size: 100px 100px;
background-position: 30px 30px;
background-repeat: no-repeat;
opacity: 0.7;
}
</style>
</head>
<body>
<div class="bg-image img1"></div>
<div class="logo"></div>
<div class="bg-image img2"></div>
<div class="header">
<div class="navbar" id='nav'>
<ul>
<li><a href="http://google.co.uk">Home</a></li>
<li><a href="http://standard.co.uk">News</a></li>
<li><a href="mailto:">Contact</a></li>
<li><a href="tutorial3.html">About</a></li>
<li class="dropdown">
<a href="javascript.void(0)" class="dropbtn">Sports Websites</a>
<div class="dropdown-content">
<a href="https://www.nba.com">NBA</a>
<a href="https://www.nfl.com">NFL</a>
<li><a href="reviews.html" onclick="javascript:void window.open('reviews.html','1552802326220','width=1200,height=500');return false;">Reviews</a></li>
</div>
</ul>
</div>
</div>
</body>
<script src="javascript.js"></script>
</html>
我将其全部保存到 html,因为如果分隔为 html / css,它会显示错误。我要做的就是让徽标显示在顶部 15% 和中心 50% 上。任何可能有帮助的建议?
解决方案
您需要从中移除position: relative
,.logo
因为它被现有背景遮挡,而现有背景存在于更高的堆叠上下文中。删除相对定位后,absolute
伪内容可以在正确的上下文中创建提升其堆叠顺序。
此外,为了.logo
更好地居中,您不能只放置它left: 50%
。您需要占背景图像大小宽度的一半,即100px / 2
或50px
。
因此,left
值变为:
left: calc(50% - 50px);
演示
<!doctype html>
<html>
<head>
<title> Group Coursework </title>
<link REL="StyleSheet" TYPE="text/css" HREF="example.css">
<style>
body,
html {
height: 100%;
}
* {
box-sizing: border-box;
}
.bg-image {
/* Full height */
height: 100%;
/* Center and scale the image nicely */
background-position: center;
background-repeat: no-repeat;
background-size: cover;
background-attachment: fixed;
}
/* Images used */
.img1 {
background-image: url("https://images3.alphacoders.com/914/thumb-1920-914671.png");
filter: blur(4px);
-webkit-filter: blur(4px);
}
.img2 {
background-image: url("https://hdwallpapersmafia.com/wp-content/uploads/2018/11/baby-groot-playing-basketball-lk-3840x2160-1.jpg");
}
/* Center and scale the image nicely */
background-position: center;
background-repeat: no-repeat;
background-size: cover;
background-attachment: fixed;
}
.header {
width: 100%;
height: 100%;
}
.navbar {
width: 100%;
padding: 20px;
position: fixed;
top: 0px;
text-align: center;
transition: .5s;
}
.navbar ul li {
list-style-type: none;
display: inline-block;
padding: 10px 50px;
color: white;
font-size: 32px;
font-family: cursive;
cursor: pointer;
border-radius: 10px;
transition: .5s;
}
li a,
.dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rbga (0, 0, 0, 0.5);
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: grey;
}
.dropdown:hover .dropdown-content {
display: block;
}
.logo:after {
content: "";
display: block;
width: 100%;
height: 100%;
position: absolute;
top: 15%;
left: calc(50% - 50px);
background-image: url("https://www.pngkey.com/png/detail/44-447166_cavalier-logo-with-sword-and-flag-cleveland-cavaliers.png");
background-size: 100px 100px;
background-position: 30px 30px;
background-repeat: no-repeat;
opacity: 0.7;
}
</style>
</head>
<body>
<div class="bg-image img1"></div>
<div class="logo"></div>
<div class="bg-image img2"></div>
<div class="header">
<div class="navbar" id='nav'>
<ul>
<li><a href="http://google.co.uk">Home</a></li>
<li><a href="http://standard.co.uk">News</a></li>
<li><a href="mailto:">Contact</a></li>
<li><a href="tutorial3.html">About</a></li>
<li class="dropdown">
<a href="javascript.void(0)" class="dropbtn">Sports Websites</a>
<div class="dropdown-content">
<a href="https://www.nba.com">NBA</a>
<a href="https://www.nfl.com">NFL</a>
<li><a href="reviews.html" onclick="javascript:void window.open('reviews.html','1552802326220','width=1200,height=500');return false;">Reviews</a></li>
</div>
</ul>
</div>
</div>
</body>
<script src="javascript.js"></script>
</html>
推荐阅读
- sql - 如何安排 google-bigQuery 以便每天将其结果导入电子表格?
- python - 有没有办法解决这个json解码错误?
- r - 如何仅选择合并数据框中两个数据框的共同观察值?
- oracle - 不理解 Live SQL 中给出的错误
- mysql - 如何将来自不同 MQTT 主题的每个数据发送到不同列中的 Mysql 数据库
- excel - 仅从共享工作簿发送一次自动电子邮件
- wpf - 为什么 Crystal Report 公式无法对数据进行分组
- android - 将 ArrayList 值一一保存在 Room 中 - Android
- xml - 如何使用 xslt 对 xml 标签进行排序
- c++ - 没有找到 Boost 库,尽管智能感知可以看到它们