The specified CSS background image is not correctly aligned with the dimensions of the selector

I have multiple non-local images that I want to use as background images for classes with a width of 500px and height of 330px. Even though these dimensions are specified in the CSS properties for the class, the

background: url('http://www.example.com/image.jpg');
does not automatically scale to fit this size and instead retains its original remote height.

It's possible that I may be mistaken in thinking that the distinction between remote and local images is causing this issue. It could be that the background image by default does not adjust to the width or height of the selector to which it belongs.

If that is the case, is it feasible to expand the height and width of the background property to match the selector it is applied to, similar to how altering the height/width attributes of the <img> tag affects even remote files?

Below is the relevant CSS + HTML Markup:

#sub_section_1 {
    width: 100%; height: 350px;
    margin-bottom: 20px;
}

#sub_section_1 .text {
    float: left;
    width: 240px; height: 100%;
}

#sub_section_1 .image {
    float: left;
    background: url(../images/sub_image.png) no-repeat;
    width: 500px; height: 330px;
    padding: 10px; margin-right: 10px;
}

.overlay {
    padding: 20px 20px;
    background: url(../images/desc_overlay.png) repeat-x;
    height: 60px; width: 460px;
}


<div id="sub_section_1">
<div class="image">
<div class="acc_image_2"><div class="overlay"><h2>Discount <?php echo $deal2['savings']; ?>% /Your Price - <?php echo $deal2['price']; ?>CHF</h2></div></div>
</div>
<div class="text">
<h2><?php echo $deal2['title']; ?></h2>
<p><?php echo $deal2['description']; ?></p>
<div class="button" align="center"><a href="<?php echo $deal2['link']; ?>">Purchase Deal</a></div>
</div>

If anyone has insights on whether this is feasible, and a reasonably efficient method to achieve it, I would greatly appreciate it. ;)

I prefer to handle this on the client side rather than using an image resizer class, downloading the image locally, and then resizing it with that class.

Answer №1

When it comes to compatibility, the choice you make will depend on your specific needs. CSS3 provides the background-size property, but this may not be supported in older browsers like IE8 and below. In such cases, using a tool like Modernizr can help. Alternatively, you can opt for a more traditional approach by placing an image inside a div element.

<div id="container">
   <div id="background"><img src="...."></div>
   <div id="content">
      Content here
   </div>
</div>

Here's the accompanying CSS:

#container {  
    position: relative; 
}
#background {  
    position: absolute;
    width: 100%;
    height: 100%;
    z-index: 12;
    top: 0;
    left: 0; 
}
#background img {   
    width: 100%;
    height: 100%; 
}
#content {  
    position: absolute;
    z-index: 13;
    top: 0;
    left: 0; 
}

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

Is it possible for browser sync to replace a CSS file hosted on a CDN upon injection?

Trying to solve a dilemma with my Shopify theme. Working on my Shopify theme, I find it frustrating that I have to constantly save my CSS changes, switch tabs, and reload the browser to see updates. While I know about the theme gem, it doesn't provid ...

What is the best way to ensure my arrow text remains properly positioned when using fullpage.js?

Seeking guidance from the web development community. I am currently working on a client's website that utilizes fullpage.js and encountering a persistent issue. On slide1 of the website, I am struggling to display the correct text next to the arrows. ...

Steps for aligning a table in the middle of a webpage

Can someone help me align my table to the center of the webpage? I have tried using align="right" and "center" but neither seem to be working. Any tips on how to position the table correctly? <table style="table-layout:fixed;" align="right" cellspaci ...

Is it possible to adjust the size of a carousel image without compromising its quality?

In the carousel currently displayed on my website, there are three images that are enclosed within the following HTML tag: Despite exploring various online solutions, I have not been able to resolve the issue. Adjusting the height and width settings has o ...

Managing and authenticating HTML checkboxes using Spring MVC controller

Currently working with Spring MVC and designing a user registration page. I want to provide users with the choice to opt-in for email notifications and agree to Terms of Service before signing up. Due to using Thymeleaf as my templating engine, I am facing ...

Utilize PHP to generate a table from data

I successfully created a table using information retrieved from my database, and everything is displaying as intended. However, I am facing an issue where adding a background color applies it to every row in the table. How can I add a background color wit ...

Is it possible to pass image data response from jQuery .ajax into a new AJAX call?

Currently, I am attempting to combine the ImageOptim API with the OCR.space API. Both APIs are exceptional, and I cannot recommend them highly enough! However, a challenge arises as the OCR API only accepts images under 1mb or 2600x2600 px in the free tier ...

unable to view the contents of the template using the directive

I am facing an issue with the Directive, as it is not adding an element to the Template. This directive has been included in the .html file. There are no errors being displayed in the console. directive.js: app.directive('myDirective', function ...

Creating a dual-row HTML table heading

Looking to create a table with a two-row header - the first row containing a search input box and the second row containing column names. Here's what I have so far: <thead> <tr class="bg-primary"> <th colspan="5"> ...

Cursor or caret bleeding through overlay on Internet Explorer

Currently, I am working on a pre-existing website called www.shopthethirdfloor.com. When using Internet Explorer, if you navigate to the products menu, select the search box, and then scroll down so that the search field goes underneath the menu overlay ...

Using SQL to Insert Values into Select/Option

I am attempting to create a select form element with predetermined options. <select name="select1"> <option value="value1">Value 1</option> <option value="value2">Value 2</option> <option value="value3">Value 3 ...

What are some methods for toggling text visibility in PHP?

I am facing confusion regarding the functionality of this code. Here is the snippet in question : //start date to end date <?php if($show5 < $show6) { ?> <a>show content</a> <?php }?> If both 'start da ...

a pair of browser windows displaying a web application built with ASP.NET MVC

We are currently in the process of developing an ASP.NET MVC web application for internal use within our organization. Our users' productivity can greatly benefit from having a larger screen space, which is why we have decided to provide each user wit ...

SVG: spin the entire text

I need assistance rotating a list of words at an angle, specifically tilting only the characters: https://i.sstatic.net/r96Mt.png Here is my code: <svg width="2000" height="130"> <g *ngFor="let fruit of fruits"> <g> ...

Identify modifications in the content within an iframe, where the content is constantly changing due to the use of

Alright, so I have a jQuery mobile enabled page that is being loaded inside an iFrame. Typically, if I want to detect changes in the content of an iFrame, I would use: $('iframe').load(function(){ //content changed! }); However, in this par ...

Creating a table using Jade/Express.js that displays various elements in each row

Looking to create a dynamic table layout using Jade for an array of objects where three objects are displayed on each row. <table> <thead>...</thead> <tbody> <tr> <td>obj0</td> <td>obj1 ...

Shadows on menu buttons transform when clicked using React and CSS

I'm currently working on customizing the styling of a menu using CSS in a project that involves the use of "react-horizontal-scrolling-menu". While I've been successful in styling the menu items with .menu-item:hover & .menu-item:active, I am ...

Guide on connecting MySQL 'id' to an HTML element

I need assistance with setting up a functionality on my website. I have a database containing rows of data, and each row has an 'ID' field in MySQL. I want to connect each ID to a button so that when the button is clicked, a PHP script will run t ...

Is there a light font that cannot be replicated?

I attempted to replicate the font style used in this website's menu, but after implementing it into my WordPress CSS theme, the text still appears bold. Is there a particular technique I can use to achieve the lighter version of the font? I have loade ...

Displaying Empty Boxes using Font Awesome

I was able to show Font Awesome properly before, but now it's not working even though I haven't made any changes. I've attempted various solutions but nothing seems to be fixing the issue. My goal is to display it as a placeholder. Below is ...