Choosing the Right Size for Your Background Images

Struggling to adjust my body image so that it resizes based on the browser window. I have everything laid out on the page, but I want it to always fit the entire page without requiring scrolling. I attempted to use some javascript to solve the issue, but it wasn't successful so I removed it. However, I have left it in for your reference.

// function set_body_height() {
//     $('body').height($("window").height());
//   }
//   $("window").ready(function() {
//     $("window").bind('resize', set_body_height);
//   });
html{
  background-color: black;
}
button {
  font-size: 30px;
  background-color:red;
  text-decoration: none;
  border-radius: 10px;
  color: green;
  position: absolute;
  top: 80%;
   left: 0;
}
h1 {
  position: absolute;
  top: 10%;
   margin: 0 auto;
   font-size: 5vw;
}

img {
background: url(http://images.buzzillions.com/images_products/07/02/iron-horse-maverick-elite-mountain-bike-performance-exclusive_13526_100.jpg);
    background-repeat: no-repeat;
    /*background-position: center center;
    background-attachment:fixed;*/

width: 100%;
  position: absolute;
  top: 0;
  left: 0;


  /*max-height: 99%;
  max-width: 99%;
  object-fit: contain;
  background-repeat: no-repeat;
  background-size: cover;
  position: relative;*/
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>MATT'S Running Game</title>
  <link rel="stylesheet" href="styleIndex.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  <script src="app.js"></script>

</head>
<body>
 <img src="http://www.freevector.com/uploads/vector/preview/16619/FreeVector-Angry-Birds-Characters-1.jpg" />
<h1>Matt's Angry Bird Rip-Off Game</h1>
  <button><a href="game.html" style="text-decoration:none">Start</a></button>

</body>
</html>

Answer №1

It is not advisable to use the background-image property directly on the img tag as it will not produce the desired effect. Instead, apply it to the body tag. By doing this, you can utilize the background-size: cover; rule, which ensures that your background remains responsive without causing scrollbars.

body {
  background-image: url(http://www.freevector.com/uploads/vector/preview/16619/FreeVector-Angry-Birds-Characters-1.jpg);
  background-repeat: no-repeat;
  background-size: cover;
}
button {
  font-size: 30px;
  background-color: red;
  text-decoration: none;
  border-radius: 10px;
  color: green;
  position: absolute;
  top: 80%;
  left: 0;
}
h1 {
  position: absolute;
  top: 10%;
  margin: 0 auto;
  font-size: 5vw;
}
<h1>Matt's Angry Bird Rip-Off Game</h1>
<button>
  <a href="game.html" style="text-decoration:none">Start</a>
</button>

Answer №2

To achieve this, utilize the properties background-size and overflow-y.

// function set_body_height() {
//     $('body').height($("window").height());
//   }
//   $("window").ready(function() {
//     $("window").bind('resize', set_body_height);
//   });
html {
  background-color: black;
}
body {
  overflow-y: hidden;
}
button {
  font-size: 30px;
  background-color: red;
  text-decoration: none;
  border-radius: 10px;
  color: green;
  position: absolute;
  top: 80%;
  left: 0;
}
h1 {
  position: absolute;
  top: 10%;
  margin: 0 auto;
  font-size: 5vw;
}
img {
  background: url(http://images.buzzillions.com/images_products/07/02/iron-horse-maverick-elite-mountain-bike-performance-exclusive_13526_100.jpg);
  background-repeat: no-repeat;
  background-position: center center background-size: cover;
  width: 100%;
  position: absolute;
  top: 0;
  left: 0;
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>MATT'S Running Game</title>
  <link rel="stylesheet" href="styleIndex.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  <script src="app.js"></script>

</head>

<body>
  <img src="http://www.freevector.com/uploads/vector/preview/16619/FreeVector-Angry-Birds-Characters-1.jpg" />
  <h1>Matt's Angry Bird Rip-Off Game</h1>
  <button><a href="game.html" style="text-decoration:none">Start</a>
  </button>

</body>

</html>

Answer №3

Does this example meet your requirements?

/* Custom styles below */

html{
  background-color: black;
}
button {
  font-size: 30px;
  background-color: red;
  text-decoration: none;
  border-radius: 10px;
  color: green;
  position: absolute;
  top: 80%;
   left: 0;
}
h1 {
  position: absolute;
  top: 10%;
   margin: 0 auto;
   font-size: 5vw;
}

img {
background: url(http://images.buzzillions.com/images_products/07/02/iron-horse-maverick-elite-mountain-bike-performance-exclusive_13526_100.jpg);
    background-repeat: no-repeat;
    /*background-position: center center;
    background-attachment:fixed;*/
  height:100%;
  width: 100%;
  position: absolute;
  top: 0;
  left: 0;
  background-size:cover;


  /*max-height: 99%;
  max-width: 99%;
  object-fit: contain;
  background-repeat: no-repeat;
  background-size: cover;
  position: relative;*/
}

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

Updating a web page in Node.js with Express when new data is added to the database

I've managed to set up messaging on my website, but I want the recipient to see messages in real-time without needing to refresh the page. It would be neat if a notification or badge appeared for the user when they receive a new message. As a beginn ...

Tips on aligning a button next to an image

I need help adjusting my code to display a button next to an image on my webpage. I want it to look like an image button in a loop where users can download, but I am struggling with CSS. Can anyone please modify my code to meet my requirements? Thank you i ...

Create a dropdown menu option that changes based on the number of items that have been

I need a selection option that displays values 1 to 5. Depending on the selected value, it should generate select options equal to the number selected. For example, if I choose 3, then it should generate 3 select options. Is it possible to loop the ajax in ...

Ways to dynamically emphasize text within ngFor loop

Within my ngFor loop, I have a set of rows. <div *ngFor="let block of data;"> <div class="class-row"> <div class="left">A Label:</div> <div class="right">{{block.key1}}</div> </div> <div class="clas ...

What is the method to display the width of a drop-down menu when using overflow:hidden?

Are there any solutions available for displaying a dropdown menu while using overflow in bxslider? The div currently has overflow:hidden to properly display the carousel. Any ideas on how to resolve this issue with z? .member{ position: absolute; ...

The alignment of the ml-auto class is not properly positioning the navbar links to the right

I've been using the reactstrap library and following this guide: In the example provided, the <nav className='ml-auto' navbar> is pushing the <NavItem> to the right. However, in my implementation (which is very similar to the ex ...

.htaccess file is causing js and css files to not load

I followed an MVC tutorial by howcode on YouTube, but I encountered an issue where my CSS and JS files were not loading due to the htaccess configuration. .htaccess file: RewriteEngine On RewriteRule ^([^/]+)/? index.php?url=$1 [L,QSA] I attempted vario ...

Exploring the Interplay of Classic ASP and AJAX Variables References

When the page loads, I check for an empty session variable. If it is empty, I trigger an AJAX function to include a hidden login form with ASP script that becomes visible through JavaScript. This part of the process works smoothly. Upon submitting the for ...

Encountering issues when using array.map with null entries in a react application

Struggling to iterate over the location array and map it? Despite several attempts, handling the null object within the array seems challenging. What am I missing here? While using a for loop resolves the issue, the map function is proving to be a roadbloc ...

What is the purpose of exporting the metadata variable in the Layout.js file in Next.js?

I've recently started using next.js and I'm a bit confused about why the metadata variable is exported from the Layout file without being imported anywhere. import "./globals.css"; export const metadata = { title: "NextJS App&qu ...

What is the best way to construct a template string to display the contents of an object?

Here is an array of objects: var students = [ { name : "Mike", track: "track-a", points : 40, }, { name : "james", track: "track-a", points : 61, }, ] students.forEach(myFunction); function myFunction (item, index) ...

What is the best method for adding lengthy text into a mySQL database with PHP?

My current setup involves using a basic HTML form and PHP to insert strings into a MySQL Database. This method works well for shorter strings, but I've encountered issues with longer strings. Interestingly, when using phpmyadmin, I can successfully i ...

What is the best method for displaying the toggle button for Drawer/Sidebar above the main content and appbar?

After reviewing Vatsal's suggestion, it appears that moving the toggle button above the drawer is the solution to our main issue. By taking advantage of the open state of the drawer, we can adjust the left property of the button in the toggleButton cl ...

Creating a universal property class for all components in a VueJS project: step-by-step guide

In my VueJS project, I am utilizing Vuetify and have noticed that several components display text in uppercase. To override this, I need to add the class="text-none" property to each component individually. Is there a way to apply this attribute ...

Struggling to generate a navigation bar in Angular, however, it is not appearing on the localhost screen

I need help with displaying a navigation bar using Angular. I have added the necessary code in nav.component.html, but the navbar is not appearing on my localhost. Could you please take a look at my code and let me know if there are any errors? Below is t ...

Retrieving Data Attribute From Form Using Python Flask

Is there a way to retrieve data from a POST request in Flask that is not the standard text field value? I am looking for a solution that does not involve using JavaScript, but rather only Python. Let's consider a simplified example: I need to extrac ...

Tips for modifying a JSON array post-encoding

Is there a way to update a JSON array that has already been encoded as JSON? I am interested in modifying the following JSON array : {"Status":"OK","Message":"API Call worked","Result":[{"interval":"2014-11-30", "leads":0,"name":"CarEnquiry","status":"NE ...

Visualizing Data with HTML and CSS Chart Designs

I'm seeking assistance with creating an organization tree in a specific layout. https://i.sstatic.net/z9LRc.jpg I am having trouble replicating the tree structure shown in the image. I need help generating a similar tree structure where multiple le ...

Creating a Typescript Interface for Custom Tooltips in Recharts

Although I am still new to Typescript, I am attempting to customize the Tooltip content for my Recharts chart in a React app using Typescript. The @types/recharts package has already been installed as part of the devDependencies. However, upon defining th ...

Angular implementation of a filter dropdown with subcategories

One of the challenges I am facing involves two dropdowns that need to be populated with data retrieved from the server. The first dropdown displays a list of categories, while the second one shows all the corresponding subcategories. Here is an example of ...