gpt4 book ai didi

html - 在窗口调整大小时,背景图像会远离导航栏

转载 作者:行者123 更新时间:2023-11-28 07:59:08 25 4
gpt4 key购买 nike

我在一个元素中深入研究了 RWD 的实践。我有一个 nav 栏和一个我应用了 CSS background-size: contain; 属性的背景图片。

当我将浏览器窗口的大小调整为视口(viewport)中较小的宽度时,nav 栏开始脱离图像并缩放背景图像。我可以应用任何其他 CSS 来使 nav 栏像磁铁一样附着在背景图像的底部吗?

ems 会帮助解决这个问题吗?

这是一个 live example -(缩小浏览器窗口宽度并观察它分开):

HTMLCSS 代码:

#aside_sect_mm {
margin: 0%;
background-image: url(../img/mm_main_tab_c.jpg);
background-size: contain;
width: 100%;
max-width: 704px;
height: 318px;
background-repeat: no-repeat;
position: relative;
}
#aside_sect_mm nav {
text-align: center;
max-width: 704px;
width: 100%;
max-height: 100%;
top: 100%;
display: block;
position: relative;
}
#aside_sect_mm nav:after {
content: "";
display: table;
clear: both;
}
#aside_sect_mm nav ul {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
position: relative;
}
#aside_sect_mm nav ul li {
font-size: 1.1em;
font-family: "Gill Sans", "Gill Sans MT", "Myriad Pro", "DejaVu Sans Condensed", Helvetica, Arial, sans-serif;
font-weight: lighter;
text-align: center;
list-style: none;
line-height: 1.1em;
position: absolute;
padding: 6.5% 0% 0% 0%;
height: 100px;
width: 60px;
color: #FCE011;
display: inline-block;
float: left;
min-width: 20%;
background-color: #004E27;
overflow: auto;
}
#aside_sect_mm nav ul li.bio_icon,
#aside_sect_mm nav ul li.stat_icon,
#aside_sect_mm nav ul li.img_icon,
#aside_sect_mm nav ul li.vid_icon,
#aside_sect_mm nav ul li.fut_icon {
position: relative;
max-height: 100px;
max-width: 60px;
width: 100%;
height: 100%;
background-repeat: no-repeat;
background-position: 50% 0%;
background-size: 100px 40px;
display: block;
overflow: auto;
}
#aside_sect_mm nav ul li.bio_icon {
background-image: url(../svg/bio_ore_ylw.svg);
}
#aside_sect_mm nav ul li.stat_icon {
background-image: url(../svg/stats_ore_ylw.svg);
}
#aside_sect_mm nav ul li.img_icon {
background-image: url(../svg/img_ore_ylw.svg);
}
#aside_sect_mm nav ul li.vid_icon {
background-image: url(../svg/vids_ore_ylw.svg);
}
#aside_sect_mm nav ul li.fut_icon {
background-image: url(../svg/fut_ore_ylw.svg);
}
#aside_sect_mm nav ul li.bio_icon a,
#aside_sect_mm nav ul li.stat_icon a,
#aside_sect_mm nav ul li.img_icon a,
#aside_sect_mm nav ul li.vid_icon a,
#aside_sect_mm nav ul li.fut_icon a {
clear: left;
position: relative;
display: block;
max-height: 60px;
text-decoration: none;
color: #F6EF1B;
overflow: auto;
}
#aside_sect_mm nav ul li:hover {
background-color: #F6EF1B;
}
#aside_sect_mm nav ul li:hover a {
color: #004E27;
cursor: pointer;
}
#aside_sect_mm nav ul li.bio_icon:hover {
background-image: url(../svg/bio_ore_grn.svg);
}
#aside_sect_mm nav ul li.stat_icon:hover {
background-image: url(../svg/stats_ore_grn.svg);
}
#aside_sect_mm nav ul li.img_icon:hover {
background-image: url(../svg/img_ore_grn.svg);
}
#aside_sect_mm nav ul li.vid_icon:hover {
background-image: url(../svg/vids_ore_grn.svg);
}
#aside_sect_mm nav ul li.fut_icon:hover {
background-image: url(../svg/fut_ore_grn.svg);
}
<!doctype html>
<html>

<head>
<meta charset="UTF-8">
<title>nav bar test</title>
<link href="css/test.css" rel="stylesheet" type="text/css">
</head>

<body>
<section id="aside_sect_mm">
<nav>
<ul>
<li class="bio_icon"><a href="#bio">Bio</a>
</li>
<li class="stat_icon"><a href="#stats">Stats</a>
</li>
<li class="img_icon"><a href="#images">Images</a>
</li>
<li class="vid_icon"><a href="#videos">Videos</a>
</li>
<li class="fut_icon"><a href="#future">Future</a>
</ul>
</nav>
</section>

</body>

</html>

最佳答案

改用这种方法怎么样?拥有背景图像并使其具有响应性有点困难。将图像嵌入到 html 中,这将使响应更容易。

#splash-area img {
vertical-align: bottom;
width: 100%;
}
#aside_sect_mm {
margin: 0%;
width: 100%;
max-width: 704px;
position: relative;
}
#aside_sect_mm nav {
text-align: center;
max-width: 704px;
width: 100%;
max-height: 100%;
top: 100%;
display: block;
position: relative;
}
#aside_sect_mm nav:after {
content: "";
display: table;
clear: both;
}
#aside_sect_mm nav ul {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
position: relative;
}
#aside_sect_mm nav ul li {
font-size: 1.1em;
font-family: "Gill Sans", "Gill Sans MT", "Myriad Pro", "DejaVu Sans Condensed", Helvetica, Arial, sans-serif;
font-weight: lighter;
text-align: center;
list-style: none;
line-height: 1.1em;
position: absolute;
padding: 6.5% 0% 0% 0%;
height: 100px;
width: 60px;
color: #FCE011;
display: inline-block;
float: left;
min-width: 20%;
background-color: #004E27;
overflow: auto;
}
#aside_sect_mm nav ul li.bio_icon,
#aside_sect_mm nav ul li.stat_icon,
#aside_sect_mm nav ul li.img_icon,
#aside_sect_mm nav ul li.vid_icon,
#aside_sect_mm nav ul li.fut_icon {
position: relative;
max-height: 100px;
max-width: 60px;
width: 100%;
height: 100%;
background-repeat: no-repeat;
background-position: 50% 0%;
background-size: 100px 40px;
display: block;
overflow: auto;
}
#aside_sect_mm nav ul li.bio_icon {
background-image: url(../svg/bio_ore_ylw.svg);
}
#aside_sect_mm nav ul li.stat_icon {
background-image: url(../svg/stats_ore_ylw.svg);
}
#aside_sect_mm nav ul li.img_icon {
background-image: url(../svg/img_ore_ylw.svg);
}
#aside_sect_mm nav ul li.vid_icon {
background-image: url(../svg/vids_ore_ylw.svg);
}
#aside_sect_mm nav ul li.fut_icon {
background-image: url(../svg/fut_ore_ylw.svg);
}
#aside_sect_mm nav ul li.bio_icon a,
#aside_sect_mm nav ul li.stat_icon a,
#aside_sect_mm nav ul li.img_icon a,
#aside_sect_mm nav ul li.vid_icon a,
#aside_sect_mm nav ul li.fut_icon a {
clear: left;
position: relative;
display: block;
max-height: 60px;
text-decoration: none;
color: #F6EF1B;
overflow: auto;
}
#aside_sect_mm nav ul li:hover {
background-color: #F6EF1B;
}
#aside_sect_mm nav ul li:hover a {
color: #004E27;
cursor: pointer;
}
#aside_sect_mm nav ul li.bio_icon:hover {
background-image: url(../svg/bio_ore_grn.svg);
}
#aside_sect_mm nav ul li.stat_icon:hover {
background-image: url(../svg/stats_ore_grn.svg);
}
#aside_sect_mm nav ul li.img_icon:hover {
background-image: url(../svg/img_ore_grn.svg);
}
#aside_sect_mm nav ul li.vid_icon:hover {
background-image: url(../svg/vids_ore_grn.svg);
}
#aside_sect_mm nav ul li.fut_icon:hover {
background-image: url(../svg/fut_ore_grn.svg);
}
<!doctype html>
<html>

<head>
<meta charset="UTF-8">
<title>nav bar test</title>
<link href="css/test.css" rel="stylesheet" type="text/css">
</head>

<body>
<div id="splash-area"><img src="http://visionsic.github.io/DSW_II/img/mm_main_tab_c.jpg"/></div>
<section id="aside_sect_mm">
<nav>
<ul>
<li class="bio_icon"><a href="#bio">Bio</a>
</li>
<li class="stat_icon"><a href="#stats">Stats</a>
</li>
<li class="img_icon"><a href="#images">Images</a>
</li>
<li class="vid_icon"><a href="#videos">Videos</a>
</li>
<li class="fut_icon"><a href="#future">Future</a>
</ul>
</nav>
</section>

</body>

</html>

#aside_sect_mm {
margin: 0%;
background-image: url(../img/mm_main_tab_c.jpg);
background-size: contain;
width: 100%;
max-width: 704px;
height: 318px;
background-repeat: no-repeat;
position: relative;
}
#aside_sect_mm nav {
text-align: center;
max-width: 704px;
width: 100%;
max-height: 100%;
top: 100%;
display: block;
position: relative;
}
#aside_sect_mm nav:after {
content: "";
display: table;
clear: both;
}
#aside_sect_mm nav ul {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
position: relative;
}
#aside_sect_mm nav ul li {
font-size: 1.1em;
font-family: "Gill Sans", "Gill Sans MT", "Myriad Pro", "DejaVu Sans Condensed", Helvetica, Arial, sans-serif;
font-weight: lighter;
text-align: center;
list-style: none;
line-height: 1.1em;
position: absolute;
padding: 6.5% 0% 0% 0%;
height: 100px;
width: 60px;
color: #FCE011;
display: inline-block;
float: left;
min-width: 20%;
background-color: #004E27;
overflow: auto;
}
#aside_sect_mm nav ul li.bio_icon,
#aside_sect_mm nav ul li.stat_icon,
#aside_sect_mm nav ul li.img_icon,
#aside_sect_mm nav ul li.vid_icon,
#aside_sect_mm nav ul li.fut_icon {
position: relative;
max-height: 100px;
max-width: 60px;
width: 100%;
height: 100%;
background-repeat: no-repeat;
background-position: 50% 0%;
background-size: 100px 40px;
display: block;
overflow: auto;
}
#aside_sect_mm nav ul li.bio_icon {
background-image: url(../svg/bio_ore_ylw.svg);
}
#aside_sect_mm nav ul li.stat_icon {
background-image: url(../svg/stats_ore_ylw.svg);
}
#aside_sect_mm nav ul li.img_icon {
background-image: url(../svg/img_ore_ylw.svg);
}
#aside_sect_mm nav ul li.vid_icon {
background-image: url(../svg/vids_ore_ylw.svg);
}
#aside_sect_mm nav ul li.fut_icon {
background-image: url(../svg/fut_ore_ylw.svg);
}
#aside_sect_mm nav ul li.bio_icon a,
#aside_sect_mm nav ul li.stat_icon a,
#aside_sect_mm nav ul li.img_icon a,
#aside_sect_mm nav ul li.vid_icon a,
#aside_sect_mm nav ul li.fut_icon a {
clear: left;
position: relative;
display: block;
max-height: 60px;
text-decoration: none;
color: #F6EF1B;
overflow: auto;
}
#aside_sect_mm nav ul li:hover {
background-color: #F6EF1B;
}
#aside_sect_mm nav ul li:hover a {
color: #004E27;
cursor: pointer;
}
#aside_sect_mm nav ul li.bio_icon:hover {
background-image: url(../svg/bio_ore_grn.svg);
}
#aside_sect_mm nav ul li.stat_icon:hover {
background-image: url(../svg/stats_ore_grn.svg);
}
#aside_sect_mm nav ul li.img_icon:hover {
background-image: url(../svg/img_ore_grn.svg);
}
#aside_sect_mm nav ul li.vid_icon:hover {
background-image: url(../svg/vids_ore_grn.svg);
}
#aside_sect_mm nav ul li.fut_icon:hover {
background-image: url(../svg/fut_ore_grn.svg);
}
<!doctype html>
<html>

<head>
<meta charset="UTF-8">
<title>nav bar test</title>
<link href="css/test.css" rel="stylesheet" type="text/css">
</head>

<body>
<section id="aside_sect_mm">
<nav>
<ul>
<li class="bio_icon"><a href="#bio">Bio</a>
</li>
<li class="stat_icon"><a href="#stats">Stats</a>
</li>
<li class="img_icon"><a href="#images">Images</a>
</li>
<li class="vid_icon"><a href="#videos">Videos</a>
</li>
<li class="fut_icon"><a href="#future">Future</a>
</ul>
</nav>
</section>

</body>

</html>

关于html - 在窗口调整大小时,背景图像会远离导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29907891/

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