Adjusting the background-image: url settings dynamically in CSS

Imagine you have a URL like this: . Is there a way to use the width and height parameters in the background-image:url property based on the dimensions of another div?


.thumb{
    border:1px solid red;
    display:inline-block;
    height:300px;
    width:300px;
}

.thumbnail{
    background-size:100% 100%;
    background-repeat:no-repeat;
    background-image:url(http://imageurl/image.jpg?w=thumb.width&h=thumb.height)   
}

Answer №1

The standard CSS is unable to achieve this as it is static. To make the CSS dynamic, you would require JavaScript or a CSS preprocessor such as .

Answer №2

If you want to achieve this effect, consider using a real image instead of a background-image and applying the object-fit: cover; property.

.thumb{
  border:1px solid red;
  display:inline-block;
  height:300px;
  width:300px;
}
    
img.thumbnail{
   width: inherit; height: inherit;
   object-fit: cover;
}
<div class="thumb">
  <img src="https://placehold.it/400x600" class="thumbnail">
</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

Change the color of a table cell on two consecutive clicks using jQuery

Hey there! I've got this super simple code that does the job of changing the background color of a selected cell in a table to red. It's pretty awesome and works like a charm. Here's the CSS: .fixture-table td.team.on { background-colo ...

Any suggestions on how to repair this Node.js login interface?

Currently grappling with setting up a Node.js application with a MySQL database to create a basic login functionality. Encountering an issue: Cannot POST /login <body class="hero-image"> <div id="container"> <div ...

Assign a value to a variable based on the button that was clicked using a form

My form consists of 8 buttons, each representing a different color: .settings-theme-buttons{ height: 4vw; width: 4vw; border: none; display: inline-block; border-radius: 100%; cursor: pointer; } #settings-theme-white{ b ...

Issue with displaying Bootstrap modal on top of another modal

Upon clicking the "Click me here" button, a modal pops up and displays a toast message expressing gratitude. The thank you modal should remain open on top of the save modal even after clicking save. However, upon saving, an error message appears: Cannot r ...

Ways to exclusively utilize Javascript on a specific div element

I am looking to implement a script (from JavaScript) within a single DIV on a webpage, rather than affecting the entire page. Is this possible? I suspect it may be a resizing issue, as adjusting the height of the DIV partially works but the width remains u ...

How can we determine if the submit function in HTML5 is returning an error or not?

I have implemented HTML5 validation on my website. <form class="" id="myForm" method="post" action="#" > <input type="text" required /> <input type="submit" /> </form> <div> <input type="button" id="myButton" /></di ...

Is it possible for me to change the text direction from left to right?

Currently, I am facing a challenge involving adding Arabic text to my paragraphs in a right-to-left format. Despite attempting <HTML lang="ara">, and making adjustments through the .css file, I have not been successful. It's important ...

The text box and buttons are not properly aligned

https://i.stack.imgur.com/lxeKw.png I am puzzled by the misalignment of my buttons and text box. <div class="form-group"> <label class="control-label col-md-3 col-sm-3 col-xs-12" for="Pic">Picture/File</label> <div class="col ...

Attempting to disrupt the line for every trio of elements consecutively

I am facing an issue with my code. It's functional, but not completely. ul { columns: 3; -webkit-columns: 3; -moz-columns: 3; } <ul> <li>Some text</li> <li>Some text</li> <li>Some text</li> &l ...

Aligning text with a scalable vector graphic (SVG) illustration

Is there a way to position the text next to the SVG object? The current output is as follows: The desired outcome is to have the text displayed alongside the SVG image: Below is the HTML code snippet: <div class="alert alert-success smallFont" id="i ...

Check if a specific class inside multiple divs with the same class is present using jQuery

I currently have three divs with the class UserDetail. The aim is to hide any UserDetail div that does not contain a div with the class "info". Update from comment: I attempted this solution: var childInfo=$('div.UserDetail').find("div.info"). ...

The constant increase in page header number leading to minor interruptions during page scrolling

On my website, I have a dynamic page header that shows the number of comments a user has scrolled through. You can see this feature in action here: However, there seems to be a slight jitter on the screen every time the comment counter updates... To disp ...

What is the best way to add two different texts (each with a distinct font) and an image into a NatTable cell while also adjusting the margins?

Just a couple of queries: 1) Is it possible to incorporate two different text fonts and an image into a NatTable cell? 2) How can the margins be set up like in the example image below? In different scenarios: a) Through Java 1.6 (without RichTexCellEdi ...

The generation of noisy URLs with hidden inputs is caused by the checkboxes in Thymeleaf Spring forms

Thymeleaf Spring automatically generates hidden inputs for checkboxes in forms, as explained in the documentation at When submitting the form using a GET method, the resulting URL can be quite lengthy and noisy: /search?q=stuff&_filters=on&filter ...

Utilizing Ajax to Create a Form with Invisible Google Recaptcha

Here is my code: function submitForm(token) { $(document).ready(function() { $("#submit").click(function() { var name = $("#name").val(); var email = $("#email").val(); var password = $("#password").val(); var contact = $ ...

Conceal element with transparent overlay without affecting the background

Looking to add animation to a snippet where the login menu rolls out? Consider two methods: adjusting the left position of the login menu or using another div on top to slowly reveal the login menu after moving it. The challenge lies in maintaining trans ...

What is the process for displaying a three.js project?

I've been trying to follow a beginner's tutorial on three.js from this link, but I'm encountering an issue in part 2 where nothing seems to render on my webpage. Despite checking out other guides, I can't seem to solve the problem. I u ...

Switching classes will not alter the current class

Having an issue with using toggleClass in jQuery. Check out the code snippet below: $(".guzik").click(function() { $("#third").toggleClass('.pillar-animation-two'); }); function test(){ document.getElementById(&apos ...

Problem with IE8 - jQuery modifying the navigation HTML when the page is resized

I am facing an issue with responsive navigation not working properly in IE8. The navigation disappears when the window is resized, and I want to prevent this from happening. In my HTML code, I have assigned a class of "lt-ie9" for IE8 compatibility. Coul ...

When using Angular JS, you can easily display a message and refresh the page with just one click

Is there a way to automatically refresh the page after displaying a message or alert indicating "Successful" or the opposite? How can I make this work? I attempted to refresh the code but the message does not appear afterwards. HTML CODE: <div class= ...