My website's logo is not appearing on the navigation bar in the HTML code

Hi there! I recently tried adding a logo to my navigation bar in an HTML file, but for some reason, the logo doesn't appear when I run the file. I'm currently learning web development through a course on Coursera, and I am quite new to programming. Can anyone help me spot what's wrong with my code?

Here is the code snippet:

body {
  font-size: 16px;
  color: #fff;
  background-color: #61122f;
  font-family: 'Oxygen', sans-serif;
}

#header-nav {
  background-color: #f6b319;
  border-radius: 0;
  border: 0;
}

#logo-img {
  background: url("https://www.crushpixel.com/big-static7/preview4/restaurant-logo-140120.jpg");
  height: 150px;
  margin: 10px 15px 10px 0;
}
<!-- Bootstrap 5.0.x -->
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ed8f8282999e999f8c9dadd8c3ddc3df">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">


<!-- original markup -->
<header>
  <nav id="header-nav" class="navbar navbar-default">
    <div class="container">
      <div class="navbar-header">
        <a href="index.html" class="">
          <div id="logo-img" alt="logo image"></div>
        </a>
      </div>
  </nav>
</header>

Answer №1

Kindly ensure that you specify the relative width value using the width parameter and include parameters such as "background-size: cover;" and "background-repeat: no-repeat;".

Adding an image to your website can be done by using the 'background-image: url('path-name');' method.

/* custom CSS */
#logo-img {
  width: 100px;
}


/* default CSS */
body {
  font-size: 16px;
  color: #fff;
  background-color: #61122f;
  font-family: 'Oxygen', sans-serif;
}

#header-nav {
  background-color: #f6b319;
  border-radius: 0;
  border: 0;
}

#logo-img {
  background-image: url("https://www.crushpixel.com/big-static7/preview4/restaurant-logo-140120.jpg");
  height: 150px;
  margin: 10px 15px 10px 0;
  width: 200px;
  background-repeat: no-repeat;
  background-size: cover;
}
<!-- Using Bootstrap 5.0.x -->
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e88a87879c9b9c9a8998a8ddc6d8c6da">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">


<!-- original HTML markup -->
<header>
  <nav id="header-nav" class="navbar navbar-default">
    <div class="container">
      <div class="navbar-header">
        <a href="index.html" class="">
          <div id="logo-img" alt="logo image"></div>
        </a>
      </div>
  </nav>
</header>

Answer №2

  1. Make sure to include the width property for your image (div#logo-img).
  2. When applying a background image using CSS, remember to use the following properties:
    background: url(foo) center/cover no-repeat;
body {
  font-size: 16px;
  color: #fff;
  background-color: #61122f;
  font-family: 'Oxygen', sans-serif;
}

#header-nav {
  background-color: #f6b319;
  border-radius: 0;
  border: 0;
}

#logo-img {
  background: url("https://www.crushpixel.com/big-static7/preview4/restaurant-logo-140120.jpg") center/cover no-repeat;
  width:100px;
  height: 150px;
  margin: 10px 15px 10px 0;
}
<!-- Bootstrap 5.0.x -->
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="07656868737473756677473229372935">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

<!-- original markup -->
<header>
  <nav id="header-nav" class="navbar navbar-default">
    <div class="container">
      <div class="navbar-header">
        <a href="index.html" class="">
          <div id="logo-img" alt="logo image"></div>
        </a>
      </div>
  </nav>
</header>

Answer №3

The problem arises from using a background-image to include the image via CSS. An anchor's width is determined by its content. Since there is no content included, the width defaults to 0. To fix this, provide it with a fixed width greater than 0.

/* customized CSS */
#logo-img {
  width: 100px;
}


/* original CSS */
body {
  font-size: 16px;
  color: #fff;
  background-color: #61122f;
  font-family: 'Oxygen', sans-serif;
}

#header-nav {
  background-color: #f6b319;
  border-radius: 0;
  border: 0;
}

#logo-img {
  background: url("https://www.crushpixel.com/big-static7/preview4/restaurant-logo-140120.jpg");
  height: 150px;
  margin: 10px 15px 10px 0;
}
<!-- Bootstrap 5.0.x -->
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="debcb1b1aaadaaacbfae9eebf0eef0ec">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">


<!-- original markup -->
<header>
  <nav id="header-nav" class="navbar navbar-default">
    <div class="container">
      <div class="navbar-header">
        <a href="index.html" class="">
          <div id="logo-img" alt="logo image"></div>
        </a>
      </div>
  </nav>
</header>

A better solution is:

Avoid adding the logo via CSS as a background image. Instead, use the <img> tag directly in the HTML:

/* updated CSS */
#logo-img {
  height: 150px;
  margin: 10px 15px 10px 0;
  display: block;
}

/* original CSS */
body {
  font-size: 16px;
  color: #fff;
  background-color: #61122f;
  font-family: 'Oxygen', sans-serif;
}

#header-nav {
  background-color: #f6b319;
  border-radius: 0;
  border: 0;
}
<!-- Bootstrap 5.0.x -->
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0a6865657e797e786b7a4a3f243a2438">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">


<!-- original markup -->
<header>
  <nav id="header-nav" class="navbar navbar-default">
    <div class="container">
      <div class="navbar-header">
        <a href="index.html" class="">
          <img id="logo-img" src="https://www.crushpixel.com/big-static7/preview4/restaurant-logo-140120.jpg" alt="logo image">
        </a>
      </div>
  </nav>
</header>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I'm struggling to understand why the jQuery find() method isn't functioning as expected

UPDATE: In a twist of events not caused by syntax errors, but rather by a loading issue with the html template, it turns out that the checkbox update was happening prematurely. So if you're searching for an element that seems to be missing, it may sim ...

Issue with a responsive CSS circle element that holds an image

I'm struggling to figure out how to create 9 responsive CSS circles in a row, with each circle containing an img tag rather than a background image. The goal is to center the img and resize it based on the size of its parent circle div. These 9 circle ...

Is JavaScript Gallery Acting Up? Possible Layer Glitch!

Seeking assistance with a website issue. I have an index.php file set up with a sideshow script in the head that appears on all pages. Additionally, within the index.php file, there is a portfolio.html page that displays a gallery script when loaded. The p ...

What are the risks associated with allowing user-generated HTML or Javascript code on a website?

Is it really dangerous to allow users to edit HTML with Jinja2 templates and access some server-side variables that will be rendered later? I know Google uses Caja Compiler to sanitize and sandbox HTML served from Google Apps Script. Should I be concerned ...

Scrollbar malfunction in Angular and Bootstrap主 The main scrollbar in Angular and Bootstrap is un

I am currently facing an issue with my Angular app using Bootstrap 5. The main html/body scrollbar does not work when elements overflow the view. I have tried various solutions such as setting a fixed height, adjusting overflow-y to scroll or auto for body ...

Overflowing Collection of Hidden CSS Assets

Here is a unique question. I'm currently implementing some CSS lasers for a futuristic website design: https://codepen.io/jefferymills/pen/xpmDK The code below showcases how to incorporate the lasers using HTML: <div class="laser-beam"></di ...

Is it possible to seamlessly incorporate a square image into a bootstrap background image slider without having to crop the image?

I've exhausted all the different solutions I've come across on other platforms to resolve this issue, but I'm still unable to find a fix. So here's what I'm struggling with: The problem I'm facing involves finding the correct ...

How to create a dynamic height grid using Bootstrap?

Seeking assistance with implementing the layout shown in the image below using bootstrap 4. I am facing an issue with adjusting the height of div 1 and 2 dynamically based on the content in div 3. Can anyone provide some guidance? https://i.sstatic.net/mk ...

How can I prevent the interpolation of "${variable}" in HTML when using AngularJS?

I need to display the string ${date} in a div element: <div>please substitute the ${date} as the tag for the name</div> The displayed content should be: please use the ${date} as the substitute tag to the name. However, the browser interpre ...

What is the best way to restrict the size of a table that is filled with data from a database?

Currently using a combination of React, Node, Express, and Postgres to populate a table with data retrieved from Postgres. The issue arises when the table becomes overly long, prompting the need to display only 5 rows at once while adding a scroll bar for ...

Detecting the click area within a window using JavaScript to automatically close an element

Hello everyone, I am currently working on implementing a JavaScript code that is commonly used to detect click areas for closing an element such as a side navigation or a floating division when the user clicks outside of the element. The functionality wo ...

What is the best way to format each <li> element onto a separate line?

Creating a review section for an item on my website required me to organize the reviews and comments. I utilized the following code structure: <ul> <li> Comment code </li> <li> Comment code </li> </ul> Within the ...

Initiate an Ajax call to pre-fetch video content

I've been attempting to download a video from my own source and display a loading message while it's being downloaded. Once the download is complete, I want to hide the loading icon and have the video ready for playback. My issue lies in gettin ...

Locate and substitute `?php` and `?` in a given string

I am facing a challenge with inserting a large string of valid HTML code into the DOM, as the string also includes PHP code. When Chrome renders the code, it automatically comments out the PHP sections. The PHP code is encapsulated in <?php ... ?>, s ...

Navigating through tabs in a Meteor application: How to maintain the active tab when using the back button

I am working on a multi-page meteor application where each page includes a navigation template. To switch between pages, I am using iron-router. The user's current page is indicated by setting the appropriate navigation link's class to 'ac ...

Having trouble retrieving image files from CSS within a Spring MVC Application

I am facing an issue with my Spring MVC application where I cannot get an image to display in a CSS file accessed from a JSP file. The CSS is working fine, but the image just won't show up. It seems like there might be an issue with how I am addressin ...

Is there a way to eliminate the white border surrounding this input field? (JSFiddle link included)

http://jsfiddle.net/gn3LL/ .error { background-color: red; } <input id="firstname" class="custom error" name="first_name" type="text" placeholder="" class="input-xlarge"> I am trying to remove the small white border around the inside of the inpu ...

Is it possible to extract tooltip text from a website using Python and Selenium, specifically when the text is generated by JavaScript?

Can anyone help me retrieve the tooltip text that appears when I hover over the time indicating how long ago a game was played? You can find my summoner profile here. I have noticed that the tooltip text is not visible in the HTML code and suspect it may ...

Struggling to locate elements with Python selenium

Just starting out with Selenium and attempting to extract data from a particular website: . This site stores information on hotels across the United States. My main objective is to compile this data into a CSV file where each entry would have a 'State ...

Having trouble retrieving the accurate count of buttons with a particular class identifier

I have a task where I need to count the number of buttons within a dynamically created div using JavaScript. The buttons are added from a separate JS file and when I view the code in the browser's inspection tool, everything appears to be correct. How ...