Expanding image size with CSS

Looking for assistance to fix the squished image in HTML & CSS.

I used the CSS code provided by Figma to style the image in html, but it resulted in a different aspect ratio from the original picture, causing the image to appear squished. Any help is appreciated!

I have tried using max-width, 100vw, and other methods, but none of them seem to work.

.foto_1 {
  position: absolute;
  left: 89px;
  top: 10px; /* 904px; */
}
<div class="foto_1">
  <img src="https://via.placeholder.com/800x1600" alt="" style="width: 551px; height: 590px;">
</div>

Answer №1

The issue with the image appearing squished is due to a mismatch in aspect ratios between the resizing dimensions and the image itself. To resolve this problem, consider the following solutions:

1. Adjusting Width or Height

If you have a specific dimension requirement (e.g. a height of 590px), simply adjust either the width or height property while omitting the other. The browser will automatically resize the image to maintain the correct aspect ratio.

.foto_1 {
  position: absolute;
  left: 89px;
  top: 904px;
}
<div class="foto_1">
  <img src="https://via.placeholder.com/800x1600" alt="" height="590">
</div>

2. Using Object-Fit Property

To ensure that the image fits perfectly within a designated space, you can utilize the object-fit property to customize the display. This allows you to choose whether to crop the image, display it with whitespace, or stretch it to fit the container.

.foto_1 {
  position: absolute;
  left: 89px;
  top: 904px;
}

.foto_1 img{
  object-fit: contain;
}
<div class="foto_1">
  <img src="https://via.placeholder.com/800x1600" alt="" width="551" height="590">
</div>

Answer №2

To achieve your desired size, it is recommended to set a percentage value for the width of the image. The current fixed height and width settings may be causing the unexpected results you are experiencing.

<div class="foto_1"><img src="/ansmet item 1.jpg" alt="" style="width:50%"></div>

Answer №3

To adjust the size of the image, you can specify either the width or height while setting the other value to auto. Here are a couple examples:

<img src="https://via.placeholder.com/800x1600" style="width: 300px; height: auto;"/>

OR

<img src="https://via.placeholder.com/800x1600" style="width: auto; height: 300px;"/>

Answer №4

One effective method I like to use is utilizing background options in CSS. This approach allows for more control over whether you want the image cropped (=cover) or fully visible (=contain). You can even adjust the position, for instance, if the top of your images is a priority.

.foto_1 {
  /* other properties */
  width: 400px;
  height: 150px;
  background:pink; /* just for demo */
  background-size: contain;
  background-position: center;
  background-repeat: no-repeat;
}

.foto_2 {
  /* other properties */
  width: 400px;
  height: 150px;
  background:pink; /* just for demo */
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}
<div class="foto_1" style="background-image: url('https://via.placeholder.com/800x1600');"></div>
<br />
<div class="foto_2" style="background-image: url('https://via.placeholder.com/800x1600');"></div>

The reason why it may not be working for you is because you are explicitly specifying those dimensions. It is simply following your instructions.

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

What is the process for sorting non-integer records in MySql?

I need help sorting MySQL records in descending order to retrieve the most recent entry. Here are the records stored as strings: 1/2017 1/2018 2/2017 2/2018 3/2017 I specifically want to retrieve the value 1/2018. The entries correspond to each year an ...

Technique for updating URL when submitting a form on a webpage

I'm a newcomer to the world of coding and I have a simple issue that needs fixing. I want to create a form field where users can input the last segment of a URL provided to them. After entering this segment, I want the page to refresh automatically a ...

The functionality of scrolling ceases to work once the bootstrap modal is closed

I am utilizing Bootstrap v3.1.1 to showcase a modal popup on one of my web pages. The following code shows how I have implemented it. <!--Start show add student popup here --> <div class="modal fade" id="add-student" tabindex="-1" role="dialog" a ...

The text is exceeding the boundaries of its container, displaying in a single line that extends beyond the page

I am currently working on incorporating text into the project-details section by utilizing a rich text uploading field in Django admin. Despite inputting the text as a paragraph in the Django admin project description, it displays as a single line that ove ...

Text alignment from the lower edge

Imagine having a title inside an image, positioned near the bottom with a negative margin-top. The issue arises when the text orientation expands from top to bottom as more content is added. Is there a way to reverse this, so that the text div grows toward ...

Warning in Next.js: When utilizing conditional rendering, the server HTML is expected to have a corresponding <div> inside another <div>

Although similar questions have been asked on various platforms like Google, none seem to provide answers that align with my specific situation. Essentially, my goal is to have a different search bar displayed in the header based on the page I am currentl ...

Assigning a value to an input field using jQuery

I'm facing an issue while trying to set the value for an input using jQuery. Essentially, when you click on a link, it redirects you to a different page with a form, and I am attempting to set the value of one of the inputs in that form. Unfortunately ...

Using props as classnames in next.js applications

I am currently attempting to create a dynamic header for each page of my app that changes color based on the current page. Here is my approach: <Header className="headerBitcoin"></Header> My goal is to have the same header component ...

I require assistance in understanding how to successfully implement ParseINT with (row.find)

enter image description hereHow To Utilize ParseINT Using row.find(). I Attempted This Code But It Doesn't Work. This is My Code : function update_qty() { var row2 = $(this).parents('.item-row'); var price2 = row2.find(parseInt($ ...

What is the best method to deallocate and remove the background-color property of CSS once it has been defined?

There is a perplexing issue that I am unable to resolve. The "Shop" section on this page has an intriguing translucent blue-green background: This background is set using the ID "#left-area". Interestingly, on another page, which can be found at , the sam ...

Attempting to include a standard VAT rate of 5% on the invoice

/* The code format is correct and I don't see any issues on my end, I hope it works well for you. */ I need assistance in adding a fixed VAT (tax) rate of 5%. The tax amount should be displayed on the row, while the total tax should reflect the sum o ...

Text input field with uneditable text displayed at the end

Click here to view the input field I am looking to create an input field that always displays a "%" at the end of the input. Here is what my react component looks like currently: <StyledBaseInput type="text" ...

What could be causing PHP to incorrectly display form data?

After learning html and css, I decided to venture into php scripting. My goal is to retrieve input from an HTML form using php code that looks like this: form.html : <html> <body> <form action="action.php" method="POST&q ...

How can I connect the box using the Interactive Picture jQuery tool?

When using the Interactive picture jQuery, I have the following code snippet within my document: jQuery(document).ready(function(){ $( "#iPicture6" ).iPicture({ animation: true, animationBg: "bgblack", animationType: "ltr-slide ...

How can I ensure that my navbar-right remains fixed to the right side of the screen and is always visible at the top of the page as I

When the page first loads: After scrolling down: I am attempting to create a fixed navigation bar at the top of the screen, so that it remains visible as the user scrolls down the page. However, I am facing an issue where my navbar brand is positioned to ...

Leverage multiple services within a single AngularJS controller

Is it possible to use multiple services in the same controller? Currently, I am able to achieve this with different services, but it seems to only perform one service. <!DOCTYPE html> <html> <script src="http://ajax.googleapis.com/ajax/libs ...

Headerbar overlapping Div when scrolling

I'm currently trying to create a fixed header bar that allows the content of the page to scroll beneath it rather than displaying above it. This functionality is working on multiple pages, but I'm experiencing issues with my calendar page. When ...

Make sure to keep the <div> element nested within the image

I'm working on creating a photo album that displays images in full screen and allows users to navigate using arrows. Check out my progress here: https://jsfiddle.net/q49wzey6/ Here's the HTML code I'm using: <div class="main"> &l ...

Angular can be used to compare two arrays and display the matching values in a table

Having two arrays of objects, I attempted to compare them and display the matching values in a table. While looping through both arrays and comparing them by Id, I was able to find three matches. However, when trying to display these values in a table, onl ...

Leveraging the Power of SASS Variables Across Your Entire NativeScript Application

Currently, I am in the process of developing an app using NativeScript 6.4.1 along with Angular 8. In order to enhance my styling capabilities, I have decided to incorporate SASS due to its advantageous features like variable names. My designer has suppli ...