Tips for determining the dimensions of a background image with the background-size property set to contain

CODE EXAMPLE:

<div class="logo-container">
    <span>some text</span>
</div>

STYLING:

.logo-container {
    display: block;
    background: url(img/logo.png) no-repeat center;
    background-size: contain;
    width:100%;
    height:20%;
}

Is there a way to determine the dimensions at which the background image was scaled?

If the background image dimension is different from the size of the .logo-container, how can we find out the current dimensions it is being displayed at?

Answer №1

Fiddle: http://jsfiddle.net/umXk3/

By knowing that the original image size is 780x150, you can calculate its current dimensions based on the size of .logo-container:

var width = $('.logo-container').width();
var height = $('.logo-container').height();

// Calculate dimensions based on whether width is greater than height:
var imgWidth = width > height ? 780/150 * height : width;
var imgHeight = height > width ? 150/780 * width : height;

Answer №2

Consider using this method

let width = $('.brand-container img').width();
let height = $('.brand-container img').height();

Alternatively, you can also retrieve the width and height of the div itself like so:

let width = $('.brand-container').width();

This is because the image serves as the background for the div.

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

Uniformly sized text containers and buttons

Seeking assistance to align a text field and a button in a way that they appear as a combined element. The desired look is shown below (screenshot taken from Chrome Linux): However, when viewed in Firefox Linux, the button is slightly taller by two pixels ...

Using AJAX to send both the complete form data and additional data not included in the form

When it comes to passing the entire form to AJAX, I've got that covered. data: $(this).serialize() But what I'm struggling with is how to pass both the form and values outside of the form. For example: var id = $('[name="formid"]'). ...

Is it possible to duplicate a php/mysql website entirely using php script?

My website built on typo3 does not have any user-generated or dynamic content, so utilizing PHP and a database is deemed unnecessary. I am wondering if it is feasible to clone the entire website using PHP and save each page as an HTML file. I only have ac ...

Tips for concealing content within table cells

I am facing an issue with my form that contains a table. When the user clicks on the radio button labeled "No", I want the content of the subsequent cells in that row to become visible. However, when they click on "Yes", the content should be hidden again. ...

Create an angle-div element that can be expanded or collapsed without relying on

I'm currently experimenting with creating a diagonal div that adjusts its height based on the content within it. While I can extract height values using JavaScript, I am curious if achieving this effect is possible exclusively through CSS. Thus far, ...

"Effortlessly deleting data with AJAX in jQuery: A step-by-step guide

I am having an issue with deleting a comment using ajax in CodeIgniter. When I click the button, the comment gets successfully deleted from the database. However, I encounter an error with the jQuery remove function: Uncaught TypeError: Cannot read proper ...

Unable to get the jQuery click event handler to function properly

I am currently working on an asp.net web application in VS 2013. The application utilizes nested master pages, with the main master page containing the following code snippet: <!DOCTYPE html> <script src="http://code.jquery.com/jquery-lat ...

Trouble detecting browser resize changes

I attempted to create a jQuery function that alerts when the browser is resized, but it doesn't seem to be functioning as expected. http://jsfiddle.net/5m6yG/ function checkBrowserResize(){ var windowHeight = $(window).height(); if ((window ...

Utilize Jasmine's AJAX spy to intercept and inspect the error request

I am encountering an issue while trying to monitor the ajax error request and receiving the following error message. Can someone assist me with this? TypeError: e.error is not a function Code snippet for JS testing : function postSettings() { $ ...

Fade images using jQuery when hovered over

Is there a way to create an interactive image that cycles through multiple images when hovered over, and returns to the original image when the mouse moves away? I'm aiming for a smooth fade effect between images, like transitioning between 3 or 4 dif ...

Error occurred during row deletion in jqGrid causing runtime issue

I am encountering an issue with my jqGrid table, the same one discussed in this problem. I am loading data from the server but storing it locally, therefore I have set the datatype option to 'local'. Additionally, I have configured the delete bu ...

Unable to write to csv file in UTF-8 using PHP fwrite, experiencing issues

Looking for assistance with handling PHP fwrite as CSV file is displaying incorrect characters. The output appears like this: općšpšćšćšp ćpšćpšćp šćpšćpšć This is the PHP code being used fo ...

Modify the custom attribute value assigned to an image

I am facing an issue with a document containing two thumbnail images (front and rear, like a bank check). I have successfully implemented zoom functionality on the front image by displaying a square part of the original large image. However, when I click o ...

Transmit data with a post request from an HTML form to a Node.js application

I'm encountering an issue with a form I created that isn't correctly sending data to my node js app. Interestingly, when using tools like Postman to send requests, everything works fine (the files sent are saved in the desired folder). However, s ...

Limit the length of text using jQuery by specifying the maximum pixel width

Trying to utilize jQuery for a quick function that calculates the pixel width of a string on an HTML page and then shortens the string until it reaches a desired pixel width... Unfortunately, I'm encountering issues with this process as the text is n ...

Upon loading the page, IE11 activates a CSS transition even when a media query that is not applied is present

Encountering a unique situation with IE11 where a panel (DIV) transition is only activated on pageload if a media query matched the highest CSS specificity for the element. The issue does not occur on Chrome, Firefox, or Safari on tablets and phones. The d ...

What is the best way to continuously update a table row based on a database condition

I am working on a table that dynamically adds new rows via AJAX. _group.html.erb <%= content_tag_for(:tr, @group, class: 'group-new-row') do %> <td><%= group.title %></td> <td><%= group.owner_id %>< ...

Is there anyone available to assist me with this contact validation form?

I'm struggling to make this contact form highlight red and display 'not valid' inside the boxes. Despite my efforts, I just can't seem to get it to work! I'm unable to change the existing HTML tags, but I can add to the HTML. I wan ...

How can you retrieve attributes from a specific element within a dropdown menu?

I came across this intriguing HTML code snippet: <select id="selectSomething"> <option id="4" data-name="tomato" data-email="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cfbba0a2aebba08fbbe1aca0a2">[email ...

Dealing with a Bootstrap 4 fixed-top navbar issue: Uncovering the mystery behind the white space

I'm encountering an issue with the navbar that is causing a white space: see screenshot. Here is the HTML code I am using: <div class="header"> <div class="overlay"></div> <nav class="navbar fi ...