A div with a responsive background that does not repeat

Is there a way to set the background of this div to act like a regular 100% image and scale accordingly?

This is my CSS:

.videobackground{
margin-top:-5px;
max-width:1140px;
height:348px !important;
background-image:url(imgs/rpp-behind-the-scenes-hero2bg.jpg);
background-position:center; /* IE fix */
-webkit-background-size: contain;
-moz-background-size: contain;
-o-background-size: contain;
background-size: contain;
}

And here's the HTML:

<div class="videobackground"></div>

Everything looks great on desktop, as you can see in this picture:

https://i.sstatic.net/Tb60L.png

However, when I try to scale the page for mobile, things get distorted, like in this picture:

https://i.sstatic.net/t6VY6.png

If anyone has any suggestions on how to solve this issue, please let me know. I've tried multiple approaches without success. The reason I'm using a background div instead of just an image is because I need to overlay a YouTube embed on the left side.

Thank you!

-Adam

Answer №1

Consider including the following code in your CSS:

background-repeat: no-repeat;

This will ensure that the background image is not repeated.

To implement this, adjust your existing CSS to include the line above within the .videobackground class:

.videobackground {
  margin-top: -5px;
  max-width: 1140px;
  height: 348px !important;
  background-image: url(imgs/rpp-behind-the-scenes-hero2bg.jpg);
  background-position: center; /* IE fix */
  -webkit-background-size: contain;
  -moz-background-size: contain;
  -o-background-size: contain;
  background-size: contain;
  background-repeat: no-repeat;
}

If you need further assistance or details on achieving full container backgrounds, check out this resource: https://css-tricks.com/perfect-full-page-background-image/

Additionally, for more concise code, consider using shorthand properties like so:

background: url(imgs/rpp-behind-the-scenes-hero2bg.jpg) no-repeat center center fixed; 

Answer №2

To resolve the issue of conflicting background images in the DIVs above and below your .videobackground div, you need to wrap these 3 DIVs with another DIV and assign the background image to that wrapping DIV (while removing it from the others).

Ensure to include background-repeat: no-repeat, background-size: cover, and

background-position: center center
in your CSS.

Note:

Your HTML code snippet includes:

<div class="bit-1">
  <img src="imgs/rpp-behind-the-scenes-hero1.jpg" width="100%">
</div>
<div class="bit-1">
  <div class="videobackground"></div>
</div>
<div class="bit-1">
  <img src="imgs/rpp-behind-the-scenes-hero3.jpg" width="100%">
</div>

You should update it as follows:

<div id="wrapper">
  <div class="bit-1">
     <!-- contents here, but no image -->
  </div>
  <div class="bit-1">
    <div class="videobackground"></div>
  </div>
  <div class="bit-1">
     <!-- contents here, but no image -->
  </div>
</div>

In your CSS, remove the background-image from .videobackground and add it to `#wrapper' like so:

#wrapper {
  background: url(imgs/rpp-behind-the-scenes-hero2bg.jpg) center center no-repeat;
  background-size: cover;
}

By making these changes, all three DIVs will share a common background image, resolving the previous conflict among different images.

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

Table with a dynamic image background that seamlessly adjusts to full width while maintaining its original aspect ratio

I am currently facing an issue with a table that has a background image. I want the background image to span 100% of the width of the div container and automatically adjust its height while maintaining its aspect ratio. #pitch{ background-color: d9ffd9 ...

Retrieve data from json files

After retrieving this object from my controller, I have a name, a list of beans, and a list of operations: { "name": "Charge", "beans": [ ], "operations": [ { "name": "getSize", "returnType": "java.lang.Integer", "descriptio ...

Troubleshooting the dark mode toggle feature on WordPress after converting from Webflow: JavaScript unable to locate CSS variables

Having an issue with my WordPress site and custom JavaScript. The code is loaded into the site using this script tag on line 16 in the head-section: <script tr-color-vars="text,background,button-background,button-text,background-50,nav-menu-link,to ...

Tips for adding page numbers to a PDF created from HTML using wkhtmltopdf

Our response: We currently utilize wkhtmltopdf for generating PDFs from a specified html template. A brief overview: We incorporate Sulu CMF in constructing our backend, which is built on Symfony2. The KnpSnappy Bundle acts as a Symfony wrapper for wkht ...

Ways to decrease font size and insert a line break within a constrained input space?

I am facing an issue where the input box remains static with old numbers when it overflows, and newly typed numbers do not appear at all. <!DOCTYPE html> <html> <body> <input type="text" id="output-area" placehold ...

What is the best way to align this button in the center of my page horizontally?

I'm working with SharePoint and trying to center this element horizontally on the page within the limitations of the platform at my job. I'm using a Script Editor within a Web Part, and have experimented with different CSS properties like line-he ...

What is the best way to add margin or padding to an absolutely positioned element once it has finished loading?

My website layout is structured as follows: <div id="header"> <div id="elementAbsolute"> <div id="headerElement1"> <div id="headerElement2"> </div> <div id="content"> etc etc The issue I am facing is that the ...

The jQuery scrollTo plugin fails to consider the "over" argument

Both of these commands will move the content to the same spot (the left edge of the designated element) $("#gallery").stop().scrollTo(thisP, 400, {offset:{top:0, left:-$(window).width()/2}}, {over:0.5}); $("#gallery").stop().scrollTo(thisP, 400, {offset: ...

How can I link two separate webpages upon submitting a form with a single click?

Here is a snippet of my code: <form action="register.php" method="post"> <input type="text" name="uname"> <input type="submit" > </form> Within the register.php file, there are codes for connecting to a database. I am looking ...

Ways to incorporate a border line between a pair of icons within a bootstrap card

Incorporating a border line within the card-header is my current goal. My approach involves positioning two icons - one on the left side and the other on the right. Illustrative example below: https://i.sstatic.net/BgxVu.png Utilizing HMTL and CSS to ac ...

What is the best way to assign a value to the param tag of an applet using JavaScript variables

This is my external javaScript file named myJs.js function search(){ var hint = document.getElementById("sChoice").value; var item = document.getElementById("sKey").value; document.getElementById("c").value=hint; document.getElementById ...

Angular-fontawesome icons are experiencing issues with their background color not scaling properly

Currently utilizing angular-fontawesome, I am seeking to alter the background color of a font-awesome fa-icon <fa-icon class="vue-icon" [icon]="faVue" ></fa-icon> To change the color, I modified the CSS using ...

The curly braces in AngularJS are failing to display the values on the HTML page

After trying various articles and solutions to different questions, I am still unable to resolve my issue. I am working on a blank ionic project and it is running smoothly in my browser using ionic serve without any errors. However, instead of displaying ...

What is the best way to display a web application on IE8 when compatibility mode is enabled, even though the application itself does not support compatibility mode?

Although I've looked for answers on stackoverflow and other sources, none of them seem to work for me. My application doesn't support Internet Explorer compatibility mode, but the client workstations have it enabled by default. I've tried v ...

Creating visually appealing transition effects like sliding in an autocomplete feature can enhance the user experience and

I have successfully implemented autocomplete functionality, but now I am looking to add a transition effect (slide down) to the suggested menu. After researching online, I found a helpful tutorial on this topic: Based on the information I gathered, I tri ...

Tips for keeping my background image from shrinking when I resize the window?

When I resize the webpage window, my background image shrinks in size. Currently, it is not repeating (which is a step forward), but now I need it to cover the entire screen to avoid showing white space around the background when the window gets smaller. ...

What is the best way to center the image on a page?

How can I center an image on the screen with a caption? <div id="team-area"> <div class="container"> <div class="row"> <div class="col-12"> <h3 class="main-title">Our Team</h3> < ...

Show an Unordered List in a Horizontal Orientation

I'm having trouble getting my Subnav list to display horizontally. Can anyone offer a solution to this issue? http://jsfiddle.net/nategines/9bued/10/ ...

What obstacles may hinder the loading of CSS with PHP variables?

Visit this website for agriculture updates: I have utilized PHP to generate specific color schemes and styles. It seems to function correctly on all tested desktop laptops, however, it fails to load the CSS files (only loads HTML) on a Galaxy Note runnin ...

What is the process for modifying the text in a browser's URL bar using JavaScript?

Is it possible to use JavaScript to set some text in the web browser's URL box when a button on an HTML page is clicked? For example, setting the text to "www.google.com". ...