The background-image in CSS is not appearing as expected

I need help creating a header with a logo link that changes color when hovered over. To achieve this, I want to use the "background-image" property instead of placing an img tag directly in the HTML. However, I am facing issues getting my logo to display within the div.logo element. I have tried various ways of entering the path, including using a hosting URL for the image, but nothing seems to work. Can someone assist me with this?

HTML

<body>
    <header>
        <div class="logo"></div>
        <nav>
            <a href="#" title="User"><%= image_tag("new-user-icon.png", class: "user-icon") %></a>
            <a href="#" title="About"><%= image_tag("about-icon.png", class: "about-icon") %></a>
            <a href="#" title="Contact"><%= image_tag("contact-icon.png", class: "contact-icon") %></a>
            <a href="#" title="Main Menu" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                <div class="dropdown" >
                    <%=image_tag("new-menu-icon.png", class: "menu-icon") %>
                </a>
            <div class="dropdown-menu dropdown-menu-right" aria-labelledby="dropdownMenuLink">
                <a class="dropdown-item" href="#">Assets</a>
                <a class="dropdown-item" href="#">Debts</a>
                <a class="dropdown-item" href="#">Incomes</a>
                <a class="dropdown-item" href="#">Expenses</a>
                <a class="dropdown-item" href="#">Transfers</a>
                <div class="dropdown-divider"></div>
                <a class="dropdown-item" href="#">Account</a>
                <a class="dropdown-item" href="#">Log Out</a>
            </div>
            </div>
        </nav>
    </header>
    ...
</body>

CSS (Sass)


header {
  display: inline-block;
  background: rgb(125, 185, 232);
  /* Gradient styles */
  border-bottom: 1px solid $universal-background;
  height: 55px;
  width: 100%;
  float: right;
  margin-bottom: 0;
  padding: 0;
}

div.logo {
  float: left;
  height: 55px;
  width: 100px;
  background-color: transparent;
  background-image: url('https://image.ibb.co/nw64yw/logo_white.png');
}

Answer №1

The issue at hand

The main problem you're facing is that the background-image is not adjusting to the dimensions of the container. Due to the large size of your image file, only a small, transparent portion is visible in the top-left corner. Additionally, there are certain styles missing that need to be applied in order to display the background-image correctly.

What needs to be done?

  1. background-position: center ensures the background-image is centered within the container.

  2. background-size: contain specifies the size of the background images, addressing issues with displaying them properly. You can use pixel values or contain / cover. Contain scales the image to fit inside the container, while Cover scales it to cover the whole container. W3C reference

  3. background-repeat: no-repeat ensures that the image is displayed only once.

Code snippet for individual properties:

.logo { 
  background-size: contain;
  background-color: transparent;
  background-repeat: no-repeat;
  background-position: center;
  background-image: url('https://image.ibb.co/nw64yw/logo_white.png');
}

Code snippet using shorthand notation: (W3C reference)

.logo { 
  background-size: contain;
  background-image: transparent url('path_to_image.png') center no-repeat;
}

Check out this live jsFiddle example

I hope this explanation clarifies what caused the issue. If you have any further questions, please don't hesitate to ask.

Answer №2

Here is some CSS code to style a logo:

.logo {
    float: left;
    height: 55px;
    width: 140px;
    background-color: transparent;
    background-image: url(https://image.ibb.co/nw64yw/logo_white.png);
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center;
}

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

What are the alternative ways to deploy a React app on Node.js without relying on Express?

Is there a way to achieve serving html files without using Express in Nodejs? I have experience with serving html files through http/https in a typical website without a frontend framework, but when attempting to do the same with index.html from a React ap ...

Arranging elements within a complex div structure

I have been working on styling this div that displays the products for an e-commerce website. I initially used CSS and a table within it, but it seems like using tables for content is not recommended, so I am trying to find a better solution. However, my a ...

How to customize the appearance of the Facebook login button alongside other login options

I'm having trouble positioning the Facebook login button in my header. It keeps appearing far to the left and below other links :) I'm currently testing it on this page: My goal is to have the button display just to the left of the login link a ...

What is the best way to toggle between tabs using a checkbox that is checked or unchecked?

I have implemented a checkbox with two sets of tabs that are meant to be displayed or hidden based on the checked state of the checkbox. Currently, the checkbox switches tabs when checked, but does not switch back when unchecked. What modifications ca ...

Issue with JSViews visible link and converter not functioning properly

After updating to the latest version of JsViews, I encountered a problem. When using a data-link like "visible{:property}", everything works fine. However, when using a data-link like "visible{convert:property}", it does not work as expected. It appears ...

Prevent using the same image multiple times for various screen resolutions in Bootstrap 4

Greetings! I currently have a functional website where the logo is displayed twice, each with different classes for various resolutions <a class="navbar-brand" href="/"> <img class="d-none d-sm-block" width="279" height="70" src="logo.png"> &l ...

Tips on creating a css selector that targets an element depending on its content

Here is some example HTML code: <tbody> <tr class="cart-item"> <td class="image"> <a href="listing_page/PR1"></a> </td> <th scope="row" class="info"> ... </th> <td class="price"> ...

Creating an HTML Table for Displaying Multidimensional Array Data

Here is a complex array structure I am working with: $finalData = [ '2017' => [ '2017-02' => [ 'mtd' => 317 ], '2017-01' => [ 'mtd' => 101 ...

Tips on causing JavaScript execution to halt until an element has finished rendering

test.view.js timeDBox = new sap.ui.commons.DropdownBox({layoutData: new sap.ui.layout.GridData({linebreak: true}), change: function(oEvent){ oController.getKeyEqChart(); }, }), new sap ...

A pair of div containers arranged in rows

I currently have 9 different div containers, each with unique styling for spacing and padding. My goal is to arrange these div cards into 2 rows with sufficient spacing between them for easy readability. As of now, I have all the div containers stacked in ...

How to limit the input text width of a number type control in Bootstrap to a specific number of digit spaces, N

I need help figuring out how to adjust the width of a number type textbox based on the number of digits. I want to specify that this control should be 6 digits wide, for example. Here is the code snippet: <div class="col-md-1 px-0 py-0 mx-0 my ...

Tips for handling transparent areas during a hover event

Is there a way to make only the rhombus image respond to hover events? I want to exclude the transparent area, as shown in this picture. <img src='http://s30.postimg.org/xpd6gwla9/1_Copy.jpg' id="first"> #first:hover { -moz-box-shadow ...

What is the sequence in which the browser handles CSS?

I have a specific class that is being styled in multiple places on my website. I am curious about the order in which the browser reads and applies these different styles. Can you explain this process to me? Inline Style <div class="yellowtag" sty ...

Displaying a div on the click of an image within a for loop using JavaScript

Currently, my website has a blog page that only showcases each individual blog post's photo and title. This is achieved through the use of a FOR loop (shown below). I am interested in implementing a feature where clicking on a blog post's photo o ...

designing a unique organizational chart layout with HTML and CSS

I'm facing a challenge in creating a customized organizational chart layout using HTML and CSS. Despite my efforts, I am having difficulties achieving the desired structure that includes parent, child, and grandchild nodes connected in a specific way. ...

I am struggling to increase the width of my input bar and center-align it

Below is the CSS class I've created to ensure that all elements remain centered on the user's screen. container: { display: "flex", position: "absolute", top: 0, left: 0, height: "100%&qu ...

Decoding JavaScript content with Python

Currently, I am dissecting this specific portion of HTML <script type="text/javascript"> var spConfig = new Product.Config({"attributes":{"150":{"id":"150","code":"size_shoe","label":"Schuhgr\u00f6\u00dfe","options":[{"id":"494","label ...

choosing a date range

My goal was to determine the number of days between a "fromDate" and a "toDate," with the restriction that the user should not be able to select a date range of more than 90 days. I am utilizing the Dojo framework to create a date calendar. Below is the co ...

Tips for setting up date ranges in Fullcalendar

Currently, I am in the process of developing a fire department shift calendar using fullcalendar. I am faced with the task of altering the CSS for specific dates at irregular intervals that do not correspond to typical days of the week. In this particular ...

Selenium alert: element click blocked: Another element is in the way of receiving the click

I'm currently using Selenium in an attempt to click on the "show more" option under the "about this show" section on the following URL: https://www.bandsintown.com/e/1024477910-hot-8-brass-band-at-the-howlin'-wolf?came_from=253&utm_medium=we ...