How can we best prevent images from overflowing their parent <div> in a straightforward and efficient manner?

I seem to be facing an issue with the code snippet provided. The intention is for the image to fit neatly inside the card with some padding around the edges, but it seems to overflow the parent div instead.

Snippet:

<div class="card greeting">
    <h3>Good Morning, admin</h3>
    <p class = "greeting-text">Mornings are the start of new ideas.</p>
    <img src = "<?php echo ASSETS_PATH . 'images/park.png' ?>">
</div>
.card {
  border-radius: var(--border-radius);
  background-color: var(--page-background-color);
  box-shadow: var(--box-shadow);
  cursor: pointer;
  padding: 8px;
  margin: 13px 0;
  border: 2px solid transparent;
  transition: .3s;
  color: var(--font-color);
  object-fit: contain;
}

.greeting{
  background-repeat: no-repeat;
  background-position: right;
  background-size: 55px;
  padding:10px;
}

.greeting img{
  background-repeat: no-repeat;
  background-position: right;
  background-size: 55px;
  padding:10px;
}

.greeting .greeting-text{
    word-wrap: break-word;
    overflow:hidden;
}

Is there a straightforward solution to address this issue? My objective is to ensure that the image fits nicely within the card.

I have attempted utilizing object-fit:contain and other methods, but it appears that either I haven't implemented them correctly or there might be another underlying problem.

Answer №1

Stop overanalyzing, there is no need to set the image as a background image

.welcome img {
  width: 100%;
  height: auto;
}

Answer №2

I found myself in a similar situation and discovered an effective solution by utilizing

<div class="wrapper">

which proved to be very helpful for me. https://www.example.com/wrapper-div Take a look at this informative article.

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

Accessing the parent element within a hover declaration without explicitly naming it

I am facing a challenge with three nested divs, where I want to change the color of the children when the parent is hovered. However, I prefer not to assign a specific class name to the parent element. The issue arises because the children have their own ...

Center column with header and footer flanking each side

Trying to replicate the layout of the Facebook Android app on my page. The app features a 3 column design, with the central column exclusively displaying the header (no footer included, although I require one for my version). This visual representation is ...

Padding causing links to break exclusively in Firefox

As a seasoned front-end developer, I recently encountered an issue that has stumped me. My main navigation bar has extensive right padding to ensure the hover dropdowns have ample space. The problem seems to be in this piece of code: HTML: <div id="h ...

Error occurred while trying to access a file within the server using $_GET method

Struggling to access a URL within my folder, but encountering an error. I have attempted using file_get_contents without success. Warning: include(/home/vol2_3/*****/*****/htdocs/new/td.php?r=8&ids=309834): failed to open stream: No such file or direct ...

Draggable Table Columns with HTML, CSS, and JavaScript

My goal is to create a table in a JSP page that functions similarly to the example shown here. I would like to be able to drag a column of dates within the table and have a form pop up. Can anyone provide guidance on how I can achieve this in JSP? ...

Challenges with Div Height and Border

Hello there! I've run into a height and border issue with a div. Here's the CSS I'm currently working with: .content { background-image: url(../../images/logo-04.png); background-position: left bottom; ...

Three images intersecting one another

I am facing an issue with the transparency of one specific image. The images on the left and center look fine, but the one on the right is not hidden behind the center image. I have tried using overflow: hidden and z-index, but haven't had any success ...

Using jQuery to animate a div element with CSS3

After creating a loader using CSS3 and CSS3 Animations, I am looking to implement the actual loading function with jQuery based on a specified duration. For instance, setting it to load fully in 30 seconds. This is what I currently have: < ...

What could be causing this table to exceed its dimensions by 2 pixels?

Why is the height of this table 102px instead of 100px? Is there another CSS attribute that needs to be set besides: table { border-spacing:0; border-collapse:collapse; } For example, check out this example link. ...

Is your Node.js EventLoop operation stuck in limbo?

This pertains to a Node/Express/MongoDB application, involving an AJAX call Previously, the selector.html() event was updating the html content of the selector, but now it is not. The only modification made was the addition of an editAll() function in ...

Missing arrow icon in Bootstrap 4 navbar menu and sub menu

I am attempting to use the navbar component from Twitter Bootstrap 4 with a nested sub menu, but the arrow in the menu item that has a sub menu is not appearing at all, and I am unsure why. Here is where the arrow appears: https://i.sstatic.net/J6xI4.jpg ...

Using JSON to serialize form data before sending it in HTML

I am in the process of developing a system that requires redirecting a user to a C# application using a form that POSTs data as a serialized JSON string along with the user. The form would be structured like this: <form action="c#methodLocation" metho ...

Setting the background image of a div in my Angular application by utilizing the value returned from an API call

I'm currently working on a movie search application where I need to set an image as the background of a div. The URL for this background image is fetched from an API call. Here is what I have tried so far: <div *ngFor="let movie of movies.results ...

Modifying a single element within a class with Jquery

Is it possible to create a stack of pages on a website using JQuery? I found this image that shows what I'm trying to achieve: image. Instead of applying ID css for each page, I'd like to use JQuery. While researching similar questions, I came ac ...

Wordpress isn't loading CSS as expected (I believe...)

Recently, a wordpress based website I am working on suddenly stopped pulling the CSS files...or at least that's what it seems like. I can't post a screenshot of the wordpress admin dashboard because I don't own the site and my boss might no ...

Tips for sending parameters to a web service URL through a Phonegap HTML page

In my Phone Gap application, I have an HTML page where I need to pass the values like name, contact, etc. to a Webservice URL. However, I am unsure of where to write the code and how to call the webservice URL. Here is a snippet of my HTML code: <div ...

Having difficulty extracting information from an HTML page using Swift

I'm attempting to convert data retrieved from an HTML page into a readable format, but the string urlContent is showing as nil even though the data received from the NSURLSession is not null. This is my approach: var city = "London" var url = NSURL ...

Difficulty with collapsing icon in Bootstrap's navigation bar

I am in the process of building a bootstrap website and facing an issue with the collapsible navigation bar. The nav toggle button is always visible, but I want it to only appear when the nav bar goes to a medium size. How can I achieve this? The code sni ...

Transforming JSON data into HTML displays only the final <li> element

Please take a moment to review the question provided at the end of this post. I am struggling with understanding why only the last li element from the JSON is being rendered. As a newcomer in this field, any help or guidance would be greatly appreciated! ...

unable to expand navbar in Angular 5 project with Bootstrap 4

I am encountering an issue with my navigation bar in a project that uses Angular5 and Bootstrap4. The navigation bar is supposed to expand when the screen size is small, as indicated by the navbar-expand-sm class in Bootstrap4. However, the navbar remains ...