Trying to add background image in html and css behind the nav bar so space doesn't show when scrolling

huangapple go评论81阅读模式
英文:

Trying to add background image in html and css behind the nav bar so space doesn't show when scrolling

问题

你可以尝试在CSS中的.navbar.footer样式中添加以下属性来移除顶部和底部的空白空间:

  1. .navbar {
  2. margin-top: 0;
  3. margin-bottom: 0;
  4. }
  5. .footer {
  6. margin-top: 0;
  7. margin-bottom: 0;
  8. }

这将移除导航栏和页脚的上下外边距,从而减少滚动时的空白空间。希望这对你有所帮助!

英文:

I can't figure out how to make the background image in my html and css to fill under the navbar and footer. When I scroll the space of the navbar is just a white space, I want it to scroll without space at the top and bottom. Thanks!

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

  1. * {
  2. box-sizing: border-box;
  3. }
  4. body, html {
  5. margin: 0 auto;
  6. padding: 0;
  7. }
  8. .background-container {
  9. background-image: url(&quot;http://placekitten.com/g/500/500&quot;);
  10. background-size: cover;
  11. background-position: center;
  12. position: fixed;
  13. width: 100%;
  14. height: 100%;
  15. opacity: 0.5;
  16. z-index: -1;
  17. background-repeat: no-repeat;
  18. }
  19. .logo-container {
  20. display: flex;
  21. justify-content: center;
  22. align-items: center;
  23. height: 30%; /* Adjust the height as needed */
  24. }
  25. .logo-container img{
  26. max-width: auto;
  27. max-height: 600px;
  28. text-align: center;
  29. }
  30. @media screen and (max-width: 768px) {
  31. /* Adjust background image for mobile devices */
  32. .background-container {
  33. background-position: center;
  34. height: auto;
  35. }
  36. }
  37. .footer {
  38. padding: 25px 0;
  39. background-color: #f2f2f2;
  40. bottom: 0;
  41. width: 100%;
  42. }
  43. .navbar {
  44. margin-bottom: 0;
  45. border-radius: 0;
  46. margin: 0;
  47. padding: 0;
  48. width: 100%;
  49. overflow: auto;
  50. }
  51. @media (max-width: 768px) {
  52. .logo-container {
  53. margin-top: 50px;
  54. }
  55. .footer {
  56. position: relative;
  57. }
  58. .navbar {
  59. position: relative;
  60. }

<!-- language: lang-html -->

  1. &lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js&quot;&gt;&lt;/script&gt;
  2. &lt;script src=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js&quot;&gt;&lt;/script&gt;
  3. &lt;link rel=&quot;stylesheet&quot; href=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css&quot;&gt;
  4. &lt;nav class=&quot;navbar navbar-expand-sm navbar-light bg-light&quot;&gt;
  5. &lt;a class=&quot;navbar-brand&quot; href=&quot;#&quot;&gt;The Backyard&lt;/a&gt;
  6. &lt;button class=&quot;navbar-toggler&quot; type=&quot;button&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#navbarNav&quot; aria-controls=&quot;navbarNav&quot; aria-expanded=&quot;false&quot; aria-label=&quot;Toggle navigation&quot;&gt;
  7. &lt;span class=&quot;navbar-toggler-icon&quot;&gt;&lt;/span&gt;
  8. &lt;/button&gt;
  9. &lt;div class=&quot;collapse navbar-collapse&quot; id=&quot;navbarNav&quot;&gt;
  10. &lt;ul class=&quot;navbar-nav ml-auto&quot;&gt;
  11. &lt;li class=&quot;nav-item active&quot;&gt;
  12. &lt;a class=&quot;nav-link&quot; href=&quot;#home&quot;&gt;Home&lt;/a&gt;
  13. &lt;/li&gt;
  14. &lt;li class=&quot;nav-item&quot;&gt;
  15. &lt;a class=&quot;nav-link&quot; href=&quot;#about&quot;&gt;About&lt;/a&gt;
  16. &lt;/li&gt;
  17. &lt;li class=&quot;nav-item&quot;&gt;
  18. &lt;a class=&quot;nav-link&quot; href=&quot;#events&quot;&gt;Events&lt;/a&gt;
  19. &lt;/li&gt;
  20. &lt;li class=&quot;nav-item&quot;&gt;
  21. &lt;a class=&quot;nav-link&quot; href=&quot;#contact&quot;&gt;Contact&lt;/a&gt;
  22. &lt;/li&gt;
  23. &lt;/ul&gt;
  24. &lt;/div&gt;
  25. &lt;/nav&gt;
  26. &lt;div class=&quot;background-container&quot;&gt;&lt;/div&gt;
  27. &lt;div class=&quot;logo-container&quot;&gt;
  28. &lt;img src=&quot;http://placekitten.com/g/100/100&quot; alt=&quot;Logo&quot;&gt;
  29. &lt;/div&gt;
  30. &lt;footer class=&quot;footer&quot;&gt;
  31. &lt;div class=&quot;container&quot;&gt;
  32. &lt;p&gt;The Backyard&lt;br&gt;
  33. at Boca Fiesta &amp; Palomino&lt;br&gt;
  34. 232 1/2 SE 1st st.&lt;br&gt;
  35. Gainesville, FL 32601&lt;/p&gt;
  36. &lt;/div&gt;
  37. &lt;/footer&gt;

<!-- end snippet -->

I've tried to change the code on both css and html but nothing seems to be changing the result.

答案1

得分: 0

我不确定为什么您创建了一个新元素来放置背景图像,而不是直接放在<body>标签上,但您的问题是因为在小屏幕尺寸上,您在.background-container元素上设置了height:auto

您已经创建了一个元素,将其固定位置并使其高度和宽度都为100%。当屏幕尺寸较小时,您设置height:auto,现在该元素的高度为0px,因为里面没有任何内容。

此外,您同时引入了Bootstrap 4的CSS和Bootstrap 3的JS - 这毫无意义。请选择其中一个版本,不能混用这两个版本!

英文:

I'm not sure why you have created a new element that you are putting the BG image on instead of just putting it on the &lt;body&gt; tag, but your problem is because at small screen sizes you are setting height:auto on the .background-container element.

You have created an element, made it fixed position and then made it 100% tall, 100% wide. When the screen size is small and you set height:auto now that element has a height of 0px since there's nothing inside that element.

<!-- begin snippet: js hide: false console: true babel: null -->

<!-- language: lang-css -->

  1. * {
  2. box-sizing: border-box;
  3. }
  4. body, html {
  5. margin: 0 auto;
  6. padding: 0;
  7. }
  8. .background-container {
  9. background-image: url(&quot;http://placekitten.com/g/500/500&quot;);
  10. background-size: cover;
  11. background-position: center;
  12. position: fixed;
  13. width: 100%;
  14. height: 100%;
  15. opacity: 0.5;
  16. z-index: -1;
  17. background-repeat: no-repeat;
  18. }
  19. .logo-container {
  20. display: flex;
  21. justify-content: center;
  22. align-items: center;
  23. height: 30%; /* Adjust the height as needed */
  24. }
  25. .logo-container img{
  26. max-width: auto;
  27. max-height: 600px;
  28. text-align: center;
  29. }
  30. @media screen and (max-width: 768px) {
  31. /* Adjust background image for mobile devices */
  32. .background-container {
  33. background-position: center;
  34. /*height: auto;*/ /* &lt;======= REMOVE THIS */
  35. }
  36. }
  37. .footer {
  38. padding: 25px 0;
  39. background-color: #f2f2f2;
  40. bottom: 0;
  41. width: 100%;
  42. }
  43. .navbar {
  44. margin-bottom: 0;
  45. border-radius: 0;
  46. margin: 0;
  47. padding: 0;
  48. width: 100%;
  49. overflow: auto;
  50. }
  51. @media (max-width: 768px) {
  52. .logo-container {
  53. margin-top: 50px;
  54. }
  55. .footer {
  56. position: relative;
  57. }
  58. .navbar {
  59. position: relative;
  60. }

<!-- language: lang-html -->

  1. &lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js&quot;&gt;&lt;/script&gt;
  2. &lt;script src=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js&quot;&gt;&lt;/script&gt;
  3. &lt;link rel=&quot;stylesheet&quot; href=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css&quot;&gt;
  4. &lt;nav class=&quot;navbar navbar-expand-sm navbar-light bg-light&quot;&gt;
  5. &lt;a class=&quot;navbar-brand&quot; href=&quot;#&quot;&gt;The Backyard&lt;/a&gt;
  6. &lt;button class=&quot;navbar-toggler&quot; type=&quot;button&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#navbarNav&quot; aria-controls=&quot;navbarNav&quot; aria-expanded=&quot;false&quot; aria-label=&quot;Toggle navigation&quot;&gt;
  7. &lt;span class=&quot;navbar-toggler-icon&quot;&gt;&lt;/span&gt;
  8. &lt;/button&gt;
  9. &lt;div class=&quot;collapse navbar-collapse&quot; id=&quot;navbarNav&quot;&gt;
  10. &lt;ul class=&quot;navbar-nav ml-auto&quot;&gt;
  11. &lt;li class=&quot;nav-item active&quot;&gt;
  12. &lt;a class=&quot;nav-link&quot; href=&quot;#home&quot;&gt;Home&lt;/a&gt;
  13. &lt;/li&gt;
  14. &lt;li class=&quot;nav-item&quot;&gt;
  15. &lt;a class=&quot;nav-link&quot; href=&quot;#about&quot;&gt;About&lt;/a&gt;
  16. &lt;/li&gt;
  17. &lt;li class=&quot;nav-item&quot;&gt;
  18. &lt;a class=&quot;nav-link&quot; href=&quot;#events&quot;&gt;Events&lt;/a&gt;
  19. &lt;/li&gt;
  20. &lt;li class=&quot;nav-item&quot;&gt;
  21. &lt;a class=&quot;nav-link&quot; href=&quot;#contact&quot;&gt;Contact&lt;/a&gt;
  22. &lt;/li&gt;
  23. &lt;/ul&gt;
  24. &lt;/div&gt;
  25. &lt;/nav&gt;
  26. &lt;div class=&quot;background-container&quot;&gt;&lt;/div&gt;
  27. &lt;div class=&quot;logo-container&quot;&gt;
  28. &lt;img src=&quot;http://placekitten.com/g/100/100&quot; alt=&quot;Logo&quot;&gt;
  29. &lt;/div&gt;
  30. &lt;footer class=&quot;footer&quot;&gt;
  31. &lt;div class=&quot;container&quot;&gt;
  32. &lt;p&gt;The Backyard&lt;br&gt;
  33. at Boca Fiesta &amp; Palomino&lt;br&gt;
  34. 232 1/2 SE 1st st.&lt;br&gt;
  35. Gainesville, FL 32601&lt;/p&gt;
  36. &lt;/div&gt;
  37. &lt;/footer&gt;

<!-- end snippet -->

Also you are including Bootstrap 4 CSS and Bootstrap 3 JS - this makes no sense at all. Pick one of the other, you can't mix versions like this!

答案2

得分: 0

我建议在您的问题评论中将背景图应用于body,但我想不出如何以这种方式应用不透明度。所以,只需将&lt;div class=&quot;background-container&quot;&gt;&lt;/div&gt;移到页面顶部。

  1. * {
  2. box-sizing: border-box;
  3. }
  4. body, html {
  5. margin: 0 auto;
  6. padding: 0;
  7. }
  8. .background-container {
  9. background-image: url("http://placekitten.com/g/500/500");
  10. background-size: cover;
  11. background-position: center;
  12. position: fixed;
  13. width: 100%;
  14. height: 100%;
  15. opacity: 0.5;
  16. z-index: -1;
  17. background-repeat: no-repeat;
  18. }
  19. .logo-container {
  20. display: flex;
  21. justify-content: center;
  22. align-items: center;
  23. height: 30%; /* 根据需要调整高度 */
  24. }
  25. .logo-container img {
  26. max-width: auto;
  27. max-height: 600px;
  28. text-align: center;
  29. }
  30. @media screen and (max-width: 768px) {
  31. /* 调整移动设备的背景图像 */
  32. .background-container {
  33. background-position: center;
  34. height: auto;
  35. }
  36. }
  37. .footer {
  38. padding: 25px 0;
  39. background-color: #f2f2f2;
  40. bottom: 0;
  41. width: 100%;
  42. }
  43. .navbar {
  44. margin-bottom: 0;
  45. border-radius: 0;
  46. margin: 0;
  47. padding: 0;
  48. width: 100%;
  49. overflow: auto;
  50. }
  51. @media (max-width: 768px) {
  52. .logo-container {
  53. margin-top: 50px;
  54. }
  55. .footer {
  56. position: relative;
  57. }
  58. .navbar {
  59. position: relative;
  60. }
  61. }
  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
  2. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
  3. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
  4. <div class="background-container"></div>
  5. <nav class="navbar navbar-expand-sm navbar-light bg-light">
  6. <a class="navbar-brand" href="#">The Backyard</a>
  7. <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
  8. <span class="navbar-toggler-icon"></span>
  9. </button>
  10. <div class="collapse navbar-collapse" id="navbarNav">
  11. <ul class="navbar-nav ml-auto">
  12. <li class="nav-item active">
  13. <a class="nav-link" href="#home">Home</a>
  14. </li>
  15. <li class="nav-item">
  16. <a class="nav-link" href="#about">About</a>
  17. </li>
  18. <li class="nav-item">
  19. <a class="nav-link" href="#events">Events</a>
  20. </li>
  21. <li class="nav-item">
  22. <a class="nav-link" href="#contact">Contact</a>
  23. </li>
  24. </ul>
  25. </div>
  26. </nav>
  27. <div class="logo-container">
  28. <img src="http://placekitten.com/g/100/100" alt="Logo">
  29. </div>
  30. <footer class="footer">
  31. <div class="container">
  32. <p>The Backyard<br>
  33. at Boca Fiesta & Palomino<br>
  34. 232 1/2 SE 1st st.<br>
  35. Gainesville, FL 32601</p>
  36. </div>
  37. </footer>
英文:

I suggested in a comment on your question to apply the background image to body, but I can't think of how you would apply opacity in that manner. So, just move &lt;div class=&quot;background-container&quot;&gt;&lt;/div&gt; to the top of the page.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

  1. * {
  2. box-sizing: border-box;
  3. }
  4. body, html {
  5. margin: 0 auto;
  6. padding: 0;
  7. }
  8. .background-container {
  9. background-image: url(&quot;http://placekitten.com/g/500/500&quot;);
  10. background-size: cover;
  11. background-position: center;
  12. position: fixed;
  13. width: 100%;
  14. height: 100%;
  15. opacity: 0.5;
  16. z-index: -1;
  17. background-repeat: no-repeat;
  18. }
  19. .logo-container {
  20. display: flex;
  21. justify-content: center;
  22. align-items: center;
  23. height: 30%; /* Adjust the height as needed */
  24. }
  25. .logo-container img{
  26. max-width: auto;
  27. max-height: 600px;
  28. text-align: center;
  29. }
  30. @media screen and (max-width: 768px) {
  31. /* Adjust background image for mobile devices */
  32. .background-container {
  33. background-position: center;
  34. height: auto;
  35. }
  36. }
  37. .footer {
  38. padding: 25px 0;
  39. background-color: #f2f2f2;
  40. bottom: 0;
  41. width: 100%;
  42. }
  43. .navbar {
  44. margin-bottom: 0;
  45. border-radius: 0;
  46. margin: 0;
  47. padding: 0;
  48. width: 100%;
  49. overflow: auto;
  50. }
  51. @media (max-width: 768px) {
  52. .logo-container {
  53. margin-top: 50px;
  54. }
  55. .footer {
  56. position: relative;
  57. }
  58. .navbar {
  59. position: relative;
  60. }

<!-- language: lang-html -->

  1. &lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js&quot;&gt;&lt;/script&gt;
  2. &lt;script src=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js&quot;&gt;&lt;/script&gt;
  3. &lt;link rel=&quot;stylesheet&quot; href=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css&quot;&gt;
  4. &lt;div class=&quot;background-container&quot;&gt;&lt;/div&gt;
  5. &lt;nav class=&quot;navbar navbar-expand-sm navbar-light bg-light&quot;&gt;
  6. &lt;a class=&quot;navbar-brand&quot; href=&quot;#&quot;&gt;The Backyard&lt;/a&gt;
  7. &lt;button class=&quot;navbar-toggler&quot; type=&quot;button&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#navbarNav&quot; aria-controls=&quot;navbarNav&quot; aria-expanded=&quot;false&quot; aria-label=&quot;Toggle navigation&quot;&gt;
  8. &lt;span class=&quot;navbar-toggler-icon&quot;&gt;&lt;/span&gt;
  9. &lt;/button&gt;
  10. &lt;div class=&quot;collapse navbar-collapse&quot; id=&quot;navbarNav&quot;&gt;
  11. &lt;ul class=&quot;navbar-nav ml-auto&quot;&gt;
  12. &lt;li class=&quot;nav-item active&quot;&gt;
  13. &lt;a class=&quot;nav-link&quot; href=&quot;#home&quot;&gt;Home&lt;/a&gt;
  14. &lt;/li&gt;
  15. &lt;li class=&quot;nav-item&quot;&gt;
  16. &lt;a class=&quot;nav-link&quot; href=&quot;#about&quot;&gt;About&lt;/a&gt;
  17. &lt;/li&gt;
  18. &lt;li class=&quot;nav-item&quot;&gt;
  19. &lt;a class=&quot;nav-link&quot; href=&quot;#events&quot;&gt;Events&lt;/a&gt;
  20. &lt;/li&gt;
  21. &lt;li class=&quot;nav-item&quot;&gt;
  22. &lt;a class=&quot;nav-link&quot; href=&quot;#contact&quot;&gt;Contact&lt;/a&gt;
  23. &lt;/li&gt;
  24. &lt;/ul&gt;
  25. &lt;/div&gt;
  26. &lt;/nav&gt;
  27. &lt;div class=&quot;logo-container&quot;&gt;
  28. &lt;img src=&quot;http://placekitten.com/g/100/100&quot; alt=&quot;Logo&quot;&gt;
  29. &lt;/div&gt;
  30. &lt;footer class=&quot;footer&quot;&gt;
  31. &lt;div class=&quot;container&quot;&gt;
  32. &lt;p&gt;The Backyard&lt;br&gt;
  33. at Boca Fiesta &amp; Palomino&lt;br&gt;
  34. 232 1/2 SE 1st st.&lt;br&gt;
  35. Gainesville, FL 32601&lt;/p&gt;
  36. &lt;/div&gt;
  37. &lt;/footer&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月30日 05:49:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定