background image and nav bar

Joseph Hedbon 161 Reputation points
2023-12-05T01:01:01.2633333+00:00

i know i'm close on this and got my background image and nav bar to overlay on the top. however when i first get testing this is the look i get:

outof size

when i resize the window i get the correct look:

correect

trying to get the background image to auto size regardless of the browser window. here is the code i have so far.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    

        <style>
            body {font-family: Arial, Helvetica, sans-serif}
* {box-sizing: border-box;}
            .bg-img {
                /* The image used */
                background-image: url("Resources/Header2.png" );
                height: 350px;
                background-position:center;
                background-repeat: no-repeat repeat;
                background-size: cover;
                position: relative;
            }
            /* Position the navbar container inside the image */
            .container {
                position: absolute;
                
                width: auto;
            }
  /* Navbar links */
.topnav a {
  float: left;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

            .topnav a:hover {
                background-color: #ddd;
                color: black;
            }
  
               
        
  

       
</style>
    <title></title>
</head>
     
<body>
   
<div class="bg-img">
  <div class="container">
    <div class="topnav">
      <a href="#home">Home</a>
      <a href="#news">News</a>
      <a href="#contact">Contact</a>
      <a href="#about">About</a>
    </div>
  </div>
</div>

Thank you in advance

Developer technologies | VB
Developer technologies | ASP.NET | Other
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2023-12-05T02:33:27.99+00:00

    Hi @Joseph Hedbon,

    First of all, your problem is that because the height of the image exceeds the height of the div, the excess part is cropped.

    You can specifically set the background size.

    background-size: 100% 350px;
    

    question i have is if i use the IMG vs background how could i float a horizontal navigation bar over it and not use the container or background image property.

    From my understanding, you are trying to set the background image using the <img> tag instead of the background image property.

    You can refer to the code below.

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <style>
            ul {
                list-style-type: none;
                margin: 0;
                padding: 0;
                overflow: hidden;
                position: relative;
                top: -350px;
                left: 0px;
            }
    
            li {
                float: left;
            }
    
                li a {
                    display: block;
                    color: white;
                    text-align: center;
                    padding: 14px 16px;
                    text-decoration: none;
                }
    
                    li a:hover {
                        background-color: #111;
                    }
    
            img {
                width: 100%;
                height: 350px;
            }
        </style>
        <title></title>
    </head>
    
    <body>
        <img src="https://learn-attachment.microsoft.com/api/attachments/07513f61-1b21-4799-a252-08be06ca99fa?platform=QnA" alt="Banner Image" />
        <ul>
            <li><a class="active" href="#home">Home</a></li>
            <li><a href="#news">News</a></li>
            <li><a href="#contact">Contact</a></li>
            <li><a href="#about">About</a></li>
        </ul>
    </body>
    </html>
    
    

    User's image

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.