Expanding Images with React-Bootstrap Cards

I need assistance with setting up cards in React-Bootstrap. The Card component is enlarging my images beyond their original sizes. When I attempt to place the image in an img tag outside of the card, it displays at its normal size. Can someone provide guidance on how to prevent the image from stretching? I am using code example from React-Bootstrap's website:

import 'bootstrap/dist/css/bootstrap.min.css';
import {Container, Row, Col, Card, Button} from 'react-bootstrap'
import './App.css';
import logo from "./Assets/Logo.png"
import talkie from "./Assets/Talkie.png"
import rabbit from "./Assets/Rabbit.png"
import shield from "./Assets/Shield.png"

function App() {
  return (
    <>
    
    <Container>
      <Row className="flex-nowrap">
        <Col md={6} sm={6} xs={6} align="left">
        <img  className="nav-logo img-fluid" src={logo}/> 
        </Col>   
        <Col md={6} sm={6} xs={6} className="contactLink" align="right">
          contact
          </Col>
      </Row>
      </Container>
    
      <Container className="px-4" style={{paddingTop: "3%"}}>
        <Row >
          <Col >
      <Card className="cards" style={{ width: '18rem' }}>
  <Card.Img variant="top" src={talkie}   />
  <Card.Body>
    <Card.Title>Card Title</Card.Title>
    <Card.Text>
      Some quick example text to build on the card title and make up the bulk of
      the card's content.
    </Card.Text>
    <Button variant="primary">Go somewhere</Button>
  </Card.Body>
</Card>
</Col>
   </Row>
  </Container>

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

Answer №1

If you want to maintain the original size of the image inside your cards, make sure to include the object-fit: none style property for the Image element. This will prevent the image from resizing.

To learn more about the css property object-fit, visit this link: https://www.w3schools.com/css/css3_object-fit.asp

Here is a snippet of code that can be added to your App.js:

function App() {
  return (
    <>

      <Container>
        <Row className="flex-nowrap">
          <Col md={6} sm={6} xs={6} align="left">
            <img className="nav-logo img-fluid" src={logo} />
          </Col>
          <Col md={6} sm={6} xs={6} className="contactLink" align="right">
            contact
          </Col>
        </Row>
      </Container>

      <Container className="px-4" style={{ paddingTop: "3%", maxHeight: '14rem' }}>
        <Row style={{ maxHeight: '15rem' }} >
          <Col >

            {/* first card */}
            <Card className="cards" style={{ width: '18rem' }}>
              <div style={{ height: '120px', textAlign: 'center' }}>
                <Image src={talkie} style={{ objectFit: 'none', marginTop: '10px' }} alt="talkie" />
              </div>
              <Card.Body>
                <Card.Title>Card Title</Card.Title>
                <Card.Text>
                  Some quick example text to build on the card title and make up the bulk of
                  the card's content.
                </Card.Text>
                <Button variant="primary">Go somewhere</Button>
              </Card.Body>
            </Card>
          </Col>>
          
       {/* Additional cards go here */}
       
        </Row>
      </Container>
    </>
  )
}

The layout should resemble this mockup: https://i.sstatic.net/IPJPh.png

Keep in mind: I also implemented extra styles and wrapped the Image element inside a div to enhance the visuals of the cards.

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

Utilizing AJAX and PHP for seamless communication, retrieve and authenticate HTTPS SSL CERTIFICATE when interacting

Recently, I successfully created a node.js REST server located at . To develop the front-end, I am utilizing a combination of html, javascript, and php. However, when I attempted to implement an SSL certificate on the front-end, a problem arose: An issue ...

pg-promise makes it easy to use named parameters with nested objects

Is it feasible to access nested objects while utilizing named parameters in pg-promise? Take a look at this example: var obj = { name: 'John', address: { postcode: 'abc' } }; db.query('SELECT * FROM users WHER ...

Is it achievable to animate dynamic height using CSS? Open to JS alternatives as well

I am currently utilizing AngularJS, which enables me to utilize ng-show and ng-hide in order to display or hide elements based on a logical condition. My goal is to animate the size changes of the container when the child objects are shown or hidden, creat ...

Issues arising from special characters in my dynamic dropdown menus

Having developed a dynamic dropdown utilizing JS/PHP/MySQL, I have encountered issues with special characters. The purpose of this script is to assist my customers in finding panel meters that match their specific criteria. Our panel meters can handle vary ...

regex for extracting a hyperlink tag from an HTML snippet in a server-side environment

On the server-side, I have an HTML page source stored as a string. My goal is to extract <link rel="icons"... elements from the string and store them in an array. There may be multiple <link rel="icons"... elements with the same starting tag, and I ...

The JQuery .replaceWith method exclusively offers unprocessed HTML content

I'm experiencing a minor issue with my ajax response where it returns raw html and does not execute any scripts, resulting in the jquery ui scripts not being executed. Here are some details: I am currently working with ASP.NET MVC and I need to comm ...

Ways to retrieve the starting position of a span element and then append a value to a hidden field

Link to the Fiddle example: http://jsfiddle.net/socc95bd/1/ JQuery code snippet: var offsets = $('#ss').offset(); var left = offsets.left; $("#hfYN").val(left); alert(left); $(".switch input").on("click", function () { var offsets = $(&apo ...

Choose one checkbox after all checkboxes have been chosen

Seeking assistance with a logical issue involving displaying data fetched in a while loop using PHP, as shown below: if (mysql_num_rows($results) != 0) { // displaying records. while ($row = mysql_fetch_array($results)) { echo &apo ...

gallery showcasing pictures with a prominent main image and accompanying smaller images

I am struggling to display my image gallery the way I envision it. I would like to have a larger main image with the rest of the images displayed on the right side below it. The current code I have is: <div class="gallery"> <a href=" ...

Is there a way to customize the JavaScript click event for specific elements only?

I've been following a tutorial from codrops where each item has a hover event and a click event that triggers an anime.js function. My goal is to prevent certain items (grid cells) from triggering the anime.js function when clicked, while still allow ...

Using .float-right and .float-left within a .row in bootstrap 4 does not achieve the desired result

When using Bootstrap 4, the float property may not work as expected inside a row. I am trying to have the right div appear on the left and the left div appear on the right, but in responsive view I want the right div to be displayed before the left div. ...

Dimensions of table in Internet Explorer and Microsoft Outlook

I am currently working on creating a mailing list and I am facing some challenges in setting the width of a table element in IE/Outlook. Despite trying various solutions that I found in other inquiries, none of them seem to be effective. The code snippet ...

Issue with cross-browser compatibility: jQuery fails to display the first item in the select dropdown

Here is a select box found within a search form: <select name="searchFor" id="searchFor"> <option name="option0" value="Choice">Pick one</option> <option name="option1" value="person">Person</option> <option name="optio ...

Height that is determined in real-time

I am attempting to adjust the height of a div based on the size of the screen. Within this div, there is a calendar that contains multiple lines of content, which can be displayed or hidden based on user preference. The height of the calendar is not fixed, ...

Collaborating with SockJS connectivity

Currently, my Node.js backend is interacting with desktop clients using websockets. The communication from the server side is initiated from a web front-end and everything is functioning properly because I am storing the SockJS Connection instances in an ...

Three.js - implementing a browser alert for updating an element within an Object3D

When replacing an element in Object3D with another element in the same position in the scene, it is necessary to trigger a window alert for the effect to properly refresh. Otherwise, there may be a delay or the old element might still appear along with the ...

Placing text at the bottom of a backdrop image

Looking for a way to position text at the bottom of a background image? Here's how you can do it: <div style="background-image: url('http://www.hdwallpapersinn.com/wp-content/uploads/2013/03/1325120512b60-45927-1.jpg'); height:100px"> ...

Sharing data between a Javascript applet and a webpage: strategies for sending results from the applet back to

When it comes to calling a method in an applet from JavaScript, I am facing a challenge. Both the applet and the JavaScript code are running on the same webpage. I am aware of how to call applet methods from JavaScript and vice versa using techniques like ...

Exploring the functionality of the $.each jQuery iterator. Can someone clarify the distinctions between these two methods?

Having vertices as an array of google.maps.LatLng objects means that they should return latlng points. The first code snippet works perfectly fine, however, I encounter issues when using the second one. // Iterate over the vertices. for (var index =0; ind ...

What is the best way to include an active CSS class in a menu item?

Link to example Can you figure out why the Home button is not turning red in this code snippet? .navbar a:hover, .navbar a:focus, .navbar a:active { color: red !important; } <nav class="navbar navbar-expand-lg"> <div class="collapse navbar ...