Place the logo in the top right corner of the div

Why isn't my logo showing up in the top right corner of my div?

.service-title {
  width: 100%;
  font-size: 24px;
  line-height: 1.1em;
  padding: 1em;
  background-position: right top;
  background: orange;
  background-image: url('http://vectorlogo.biz/wp-content/uploads/2013/01/GOOGLE-VECTORLOGO-BIZ-128x128.png');
  background-repeat: no-repeat;
}
<h1 class="service-title"><a href="/">test</a></h1>

Answer №1

The reason for this issue is that you are overwriting the background-position property with the background shorthand.

Explanation :

The background declaration comprises:

the combination of default values from its individual properties:
background-image: none
background-position: 0% 0%
background-size: auto auto
background-repeat: repeat
background-origin: padding-box
background-style: a shorthand that concatenates its own detailed settings
background-clip: border-box
background-color: transparent

(source : mdn background)

When you use background: orange (after setting background-position in your CSS), it automatically sets background-position: 0% 0%;, thereby overriding your explicit background-position declaration.

Solution :

To prevent this, either place the background-position property after the background shorthand to avoid interference:

.service-title {
    font-size: 24px;
    line-height: 1.1em;
    padding: 1em;
    background:orange;
    background-position: right top;
    background-image:url('http://vectorlogo.biz/wp-content/uploads/2013/01/GOOGLE-VECTORLOGO-BIZ-128x128.png');
    background-repeat:no-repeat;
}
<h1 class="service-title bg-orange icon-logo-clipped"><a href="/">test</a></h1>

Or alternatively, specify background-color instead of using the background shorthand to preserve the background-position directive.

.service-title {
    font-size: 24px;
    line-height: 1.1em;
    padding: 1em;
    background-position: right top;
    background-color:orange;
    background-image:url('http://vectorlogo.biz/wp-content/uploads/2013/01/GOOGLE-VECTORLOGO-BIZ-128x128.png');
    background-repeat:no-repeat;
}
<h1 class="service-title bg-orange icon-logo-clipped"><a href="/">test</a></h1>

Answer №2

Your background: orange; declaration takes precedence over the background-position. This is because the background property encompasses all other background properties, such as color, image, size, and position. For more information on the shorthand background property, please refer to this MDN article.

To resolve this issue, you can either change background to background-color, or place it before background-position

.service-title {
  width: 100%;
  font-size: 24px;
  line-height: 1.1em;
  padding: 1em;
  background-color: orange;
  background-image: url('http://vectorlogo.biz/wp-content/uploads/2013/01/GOOGLE-VECTORLOGO-BIZ-128x128.png');
  background-position: right top;
  background-repeat: no-repeat;
}
<h1 class="service-title"><a href="/">test</a></h1>

Answer №3

Check out this code snippet:

.service-title {
    width: 100%;
    font-size: 24px;
    line-height: 1.1em;
    padding: 1em; 
    background:orange;
    background-image:url('http://vectorlogo.biz/wp-content/uploads/2013/01/GOOGLE-VECTORLOGO-BIZ-128x128.png');
    background-repeat:no-repeat;
    background-position: right top;
}

Here's a demo for reference

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's up with the Twitter Bootstrap parameters?

As a beginner in the world of HTML5 and Twitter Bootstrap, I have been exploring some pre-built examples and now have a few questions... Consider this code snippet from the Fluid Layout Example: <div class="navbar navbar-fixed-top"> <div class ...

JavaScript code that deletes text from a document - Script eradication

I am trying to display the message "Todays Beer Style is: "Beer Style". However, when I add the javascript code, the "Todays Beer Style is:" text disappears. I'm not sure why this is happening. Below is the HTML code for reference. HTML Code <!DO ...

Tips for displaying a horizontal scrollbar on a div within a div that has a hidden scrollbar

To ensure that a horizontal scrollbar is displayed on my "content" div when the screen size is smaller than the specified minimum width, I have set the overflow-x property of the parent "container" to hidden. This prevents the scroll from appearing across ...

A method for determining the quantity of <li> elements within a <ul> container while applying specific conditions

I am currently using document.querySelectorAll('ul > li').length to count the total number of items in my list. However, I am wondering if there is a way to count only those items that meet a specific condition. For example: <ul> < ...

Discover the ultimate guide to harmonize IE 9 with the ingenious Bootstrap Multiselect plugin developed by davidstutz

I've come across an amazing plug-in developed by David Stutz that allows for a Bootstrap and jQuery multi-select options list. Here are some resources: Check out the source code on Github Find documentation and examples here This plug-in works fla ...

What is the method in HTML to resize an image in just one direction?

Imagine this scenario: I need to fit a 10x60 image into a 15x15 container. The goal is to stretch the image width proportionally (resulting in a 15x90 image), but without stretching the height beyond the width, which would cut off the bottom of the image. ...

Is there a way to change a class on an anchor element without disrupting its functionality?

The code snippet provided includes an anchor tag with classes for "window" and "less" applied. Is there a way to eliminate the anchor tag while still keeping the classes intact in the code, ensuring that the functionality remains unchanged? ...

Creating an array of logos in ReactJS with the help of TailwindCSS

After seeing multiple implementations of this feature, I find myself struggling to search for a solution. I can only access the HTML and CSS through inspecting elements, wondering if others are facing the same issue as me. Typically, we aim to implement t ...

An error is thrown when using less.js

Every time I attempt to add less.js, I encounter an XmlHttpRequest Exception 101. Including the .less file is done using this method: <link rel="stylesheet/less" type="text/css" href="anything.less" /> This issue only arises when I upload the th ...

Explore an illustration of a website with a hover effect

As I delve into the world of web development, I have taken up the challenge of replicating another website's layout to hone my skills. However, there is one aspect of this site that has me stumped =/ <div class="a"> <span>Teste</span&g ...

the background image shivering with movement

While experimenting with some animations, I encountered a small issue: When trying to animate a div that has an image as its background, the image appears to shake, especially when expanding and shrinking the div from the center. Is there a way to prevent ...

Adjust the image's placement and reduce its size as the screen size decreases, without using any media queries

Essentially, I encountered an issue where I had a table row with 2 cells - the left cell containing text and the right one containing an image. As the screen size decreased, I needed the image to move below the text. After some investigation, I delved into ...

Responsive grid made with HTML and CSS to create dynamic DIVs

Currently, I am working on a project that requires a responsive page layout using divs. The layout should resemble a grid, with 3 columns that reorganize into 2 columns on smaller screens. Here is the basic HTML structure I have so far: <div id="main" ...

Place an image at the center with a height set to 100% within a div that has a fixed height and

Apologies for asking about this topic again, but I have been unable to find a solution where the image fills 100% of the height. If you'd like to see the issue in action, here's a link to the jsfiddle: http://jsfiddle.net/BBQvd/3/ I'm just ...

Issue encountered: The differ cannot recognize the provided object '[object Object]', which is of type 'object'. NgFor is limited to binding Iterables only

I have been following a tutorial on Ionic created by Paul Halliday, focusing on a shopping list project that utilizes Firebase and Angular. However, I am encountering an error every time I try to run the application: Error: Uncaught (in promise): Error: ...

What is the best way to make a line widget that has rounded edges at both the beginning and end?

I've been scouring the internet for a solution to this problem, but so far I haven't had any luck. My goal is to design a horizontal line with dots at each end, similar to this example. Does anyone know how I can achieve this using CSS? I atte ...

Struggle of UL vs. LI Designs

Currently, I am tackling a web development project that entails a CSS file known as Style.css. Inside this file, a styling series is outlined for UL and LI elements, which have been implemented across various parts and sections of the project. The setup lo ...

Creating a foundational design with Bootstrap 5, featuring a sidebar that scrolls seamlessly and a stunning fixed

I'm having some trouble figuring out the layout for a webapp I want to develop. LAYOUT IMAGE What I envision is a sidebar menu on the "S" section that can be scrolled independently, and a large image on the "I" section that changes based on th ...

issues with padding in the main content section

When I try to search for something that isn't on the page, a strange margin or padding issue occurs. I have thoroughly reviewed the code but can't seem to pinpoint the problem. The unwanted 30pxish margin after the footer should not be present wi ...

Adjust the appearance of an element based on user selection from a dropdown menu under certain circumstances

I am working on creating a dropdown menu using a JavaScript array. My goal is to display a specific span element when certain options are selected. For instance, I want the span to be shown only when options "a" and "c" are selected, but not when option " ...