The background image will expand to fill any available space

I'm currently working on rendering divs with a background image CSS property. The images have fixed sizes, and I want to display them in a grid layout. However, the divs with background images stretch when there is extra space available, which is not the desired outcome. Since I am not an expert in CSS, any assistance would be greatly appreciated. Below is the HTML and CSS code:

.inner-tile {
      display: grid;
      grid-gap: 1em;
      justify-items: center;
      grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
      grid-template-rows: 1fr;
      width: 98%;
      height: 100%;
      padding-top: 1%;
      padding-right: 1%;
      padding-left: 1%;
      /*border: 1px solid red;*/
      font-size: 18px;
    }
    
    .event {
      display: grid;
      grid-gap: 5px;
      grid-template-rows: 1fr;
      grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
      justify-items: center;
      border: 1px solid grey;
      border-radius: 4px;
      min-width: 240px;
      min-height: 200px;
      height: 100%;
      width: 100%;
    }
    
    .event-image {
      background: url('https://placekitten.com/200/200');
      background-repeat: vertical;
      background-size: 100%;
      width: 100%;
      height: 100%;
    }
<div class = "inner-tile">
        <div class = "event">
          <div class="event-image"></div>
          <h3 class = "learn-more">Corporate Event</h3>
        </div>          
        <div class = "event">
          <div class="event-image"></div>
          <h3 class = "learn-more">Weddings</h3>
        </div>          
        <div class = "event">
          <div class="event-image"></div>
          <h3 class = "learn-more">Party</h3>
        </div>                 
      </div>

Answer №1

Give this a shot

.event-picture {
   background-size: cover;
}

Answer №2

Utilizing percentages in the width: and height: properties will allocate that specific percentage of the window space. For instance, setting width: 100%; ensures it occupies the entire available width within the window (100% of the total width).

If you prefer it to occupy a specified number of pixels, simply use width: 100px;, guaranteeing it consumes 100 pixels regardless of other factors.

Answer №3

If you want a fixed width and height for your image, consider using pixels instead of percentage. Here's an example:

.event-image {
  background: url('https://placekitten.com/200/200');
  background-repeat: vertical;
  width: 120px;
  height: 120px;
}

Answer №4

I've encountered a similar problem in the past, but found a simple solution in CSS:

.class img 
{
background-repeat:no-repeat;
background-size:cover;
}

Give this a try - it should resolve the issue!

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

Verify the presence of plain ASCII text

I need to verify if the file uploaded is in ascii plain text format. Can you suggest a method? $("#my_file").change(function(){ //alert if file is not ascii plain text }); <input type="file" name="my_file" id="my_file" /> ...

Exploring the power of internal linking with GatsbyJS

I am currently developing a website using gatsbyjs. I have concerns about the crawlability of "onClick" for SEO purposes and I would appreciate some assistance. This is my current code: render={data => ( <div className='feed'> ...

Troubleshooting: Unable to Display Collapsing Navbar in Bootstrap 5 When Toggler is Pressed

My attempt at creating a collapsing navbar isn't working as expected, and I'm struggling to identify the issue. I have set up a collapse toggler that should target #navbar1. However, when I shrink the page, the Navbar collapses, the .navbar-togg ...

Steps for making a grid within a bootstrap 3 modal

I am new to HTML and CSS I'm working on creating a modal content similar to this: https://ibb.co/RvrhmRs Most of the work is done, but my modal currently looks like this: https://ibb.co/1zG0y2t I just need help arranging an icon next to the text. ...

How can jQuery be used to target a specific class based on its inner value?

For instance, within a div of the same class, there are 6 "p" tags - some containing 'member' text and others not. I aim to use jQuery to select all "p" tags with 'member' as their inner text and apply an additional class to them. Here ...

What is the method for adjusting the subheader color on a Material UI card component?

How can I change the subheader text color to white in a Material UI Card with a dark background? Here is my code: const useStyles = makeStyles(theme => ({ menuColor: { backgroundColor: theme.palette.primary.main, color: '#ffffff', ...

Having trouble with a switch statement in Javascript that is unable to find a case for "none."

In my code, I am checking to see if there is a ball in a specific map point and then changing the color of the pixels where the ball is located to match the color of the ball. Here is my code snippet: function UpdateColorInMapPoints(mapPointIndexs) { / ...

What could be the reason for the meta viewport not functioning properly on Android devices with Cordova 5.1.1?

Ever since upgrading my app to cordova 5.1.1, I've noticed that the meta viewport no longer functions properly on Android devices. The app is not displaying correctly as a result. Despite trying various solutions, I am still facing this issue. Is ther ...

Is there a way to retrieve the central anchor point position (x, y) of the user's selection in relation to the document or window?

Is there a way to retrieve the center anchor point position (x, y) of the user's selection relative to the document or window? I have tried using window.getSelection() to get selected nodes, but I am unsure how to obtain their position: See an examp ...

Managing the scrolling direction horizontally with waypoints.js

As I work on creating a custom wizard form with waypoints, I've encountered an interesting issue that has left me puzzled. In my sample CODEPEN, you can see two pages of the wizard process to better understand the problem. Upon clicking the forward ...

Dimensions in Material UI

As I embark on my journey with Material UI components for React, I am facing the challenge of integrating pre-styled components into my project. I have noticed that some components appear too large in my layout due to excessive padding and margins. Curren ...

How to implement a delay for submenu dropdown in Bootstrap 3

I am trying to implement a delay on a submenu that I have created, rather than the entire bootstrap3 dropdown menu. The goal is to allow users to easily move their cursor to the submenu without it closing unexpectedly. Although the jquery code should still ...

Increase in textbox size depending on selected dropdown value

Our concept involves offering three choices: Email #1 pulled from the database Email #2 retrieved from the database Input a new email Should the user select option #3, a textbox will expand at the bottom of the dropdown menu for easy entry of a new emai ...

What is the best way to organize divs in a grid layout that adapts to different screen sizes, similar to the style

Is there a way to align multiple elements of varying heights against the top of a container, similar to what is seen on Wolfram's homepage? I noticed that they used a lot of JavaScript and absolute positioning in their code, but I'm wondering if ...

Unable to navigate a simulated scrollbar

As someone who is new to web development, I am embarking on the journey of building a UI Grid that can effectively display a large amount of data. My goal is to implement a scrollbar that allows for horizontal scrolling across approximately 1,000,000 data ...

When trying to include an external class that extends Pane in a main class in JavaFX, simply adding it may not achieve the

My primary class is as follows: public class Digital_Analog_Clock_Beta_1 extends Application { @Override public void start(Stage primaryStage) { double outerBoxWidth = 500, outerBoxHeight = outerBoxWidth / 2.5; Rectangle outerB ...

Ways to increase the spacing between content boxes using Bootstrap

I currently have a row of three Bootstrap boxes structured like this: <div class="row"> <div class="col-lg-4 pathBoxMain"> <h2>Content</h2> </div> <div class="col-lg-4 pathBoxMain"> <h2>Content</h2> </di ...

What is the best way to conceal a broken image icon without sacrificing all of the stylesheet?

Can someone assist me in understanding and modifying this code? Here is the code snippet: <table> <tr> <td> <img src="" id="img" class="img" style="width:100%;height:200px;background-color:#ccc;border:2p ...

What steps can be taken to resolve the Angular error stating that the property 'bankCode' is not found on type 'User' while attempting to bind it to ng model?

I'm encountering an issue with my application involving fetching values from MongoDB and displaying them in an Angular table. I've created a user class with properties like name and password, but I keep getting errors saying that the property doe ...

Personalizing the share button by changing the icon font to an image

I've been working on incorporating this share button into my website.... Although it functions properly as is, I want to switch out the icon font for an image. I attempted to modify certain CSS properties, but encountered some issues. http://jsfidd ...