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

Why isn't the document.getElementById().value() function functioning properly within my PHP code?

When working on my PHP code, I included the following: <select class="ht__select" onchange="sorting('<?php echo $id ?>')" id="sorting"> <option value="">Default sorting</option> <option value="low_price">Sor ...

What mistakes have I made in this CSS code?

Check out the REQUEST FORM in the image below (with a green background). It is currently aligned at the top, but I would like it to be centered vertically. This is the CSS style being used, .rtitle { background-color: Green; width: 300px; height: 50px; b ...

Is it possible to dynamically group by one column in a dataset without requiring a trigger function?

I've been working on a script that retrieves values from another page and organizes them into a table alphabetically by Name, City, and District. The current script is functioning well, but I now want to enhance it by grouping the values by city as we ...

Tips for transferring data to the next page with JavaScript AJAX

I am working on a webpage that includes an html select element <pre> $query = mysql_query("select * from results"); echo "<select id='date' onchange='showdata()' class='form-control'>"; while ($arr = mysql_fetch_a ...

Tips for creating a "sticky" button in Angular that reveals a menu when clicked

Is there a way to incorporate a feature similar to the "Get help" button on this website: The button, located in the lower right corner, opens a sticky chat menu. I am interested in adding dynamic information to the button (such as the number of tasks a u ...

jQuery - Enhancing User Experience with Dynamic Screen Updates

Is there a way to update the screen height when resizing or zooming the screen? Whenever I zoom the screen, the arrows break. I'm also curious if the method I'm using to display images on the screen is effective. It's supposed to be a paral ...

What is the best way to find an element using either 'className1' or 'className2'?

Within my project, all locators are defined as elements of an enum and follow this format: For example, if it is a CSS locator - DIV_LOCATION("css=div.location-text.text-overflow"). This method parses the string and identifies it as a CSS locator if it be ...

Python Selenium: Cannot Click on Element - Button Tag Not Located

TL,DR: My Selenium Python script seems to be having trouble "clicking" on the necessary buttons. Context: Hello. I am working on automating the process of logging into a website, navigating through dropdown menus, and downloading a spreadsheet. Despite ...

The issue I'm facing with my CSS is that the Doctype is causing some styles to

Hey there! I've been pondering the use of !DOCTYPE html lately. I recently put together a basic webpage that looked great on most browsers except for IE. After doing some digging, it was recommended to me to use !DOCTYPE html. However, this ended up ...

Stack 2 cards inside a DIV vertically using Materialize CSS

My form and table are currently positioned side by side in 2 columns: The layout of this form/table is built using datatable + materialize CSS. I am looking to place the Form and table within a div to ensure that the bottom of both elements are always al ...

Why isn't my easypie number displaying on the inside?

I am currently utilizing the easypie plugin, but I am facing an issue where the number is not displaying inside the pie chart. Despite trying multiple solutions, I haven't been able to resolve this. How can I successfully display the number inside the ...

Split the page in half with a background image on one side and text on the other

Is there a way to split the page vertically, with a background image on one side and text on the other? I've been struggling to achieve this layout - the background image won't stay on the left while the text remains on the right. Can someone off ...

Stop automatic resizing on mobile device after postback

Encountered an issue with a website I'm currently developing. It's not causing any problems, but I want to enhance the user experience. My aim is to maintain the zoom levels of mobile devices after a postback. To provide more context, there is a ...

The Spring Boot application is having trouble retrieving static files

I started a fresh springboot project and organized the directory structure as follows: view image description here The yml configuration is outlined below: server: port: 8783 servlet: context-path: /spring-demo spring: scan : com.example appli ...

Python difficulties in extracting search result links

I need assistance in extracting the search result links from a specific website. Despite using Beautiful Soup with attributes 'a' and 'href', I am unable to retrieve the links to each of the approximately 100 search results on the site ...

Stylish border radius for Bootstrap progress bars

Here is a snippet of code that I am struggling with: .progress-bar{ border-top-right-radius: 40px !important; border-bottom-right-radius: 40px !important; -webkit-box-shadow: none !important; -moz-box-shadow: none !important; box-sha ...

Using just one "nav.php" file for both the homepage and subdirectories

Looking to enhance the efficiency of my websites, I have implemented a PHP file for navigation, footer, sidebar, and other elements. However, this current method involves using two separate files: one for the homepage and another for the remaining pages wi ...

What could be causing the bootstrap card alignment issue when trying to display them side by side

I am currently developing a Flask project where I'm showcasing user information retrieved from the database within bootstrap cards. Utilizing jinja2 to iterate through an object statuses {% extends "base.html" %} {% block content %} {% if ...

Having trouble with the fancybox form

Take a look at this link for information on how to display a login form. Once you click on "Try now", follow these steps: A fancy box should open with fields for name and password. If both fields are left blank and the login button is clicked, an error ...

Execute the JS function during the first instance of window scrolling

Currently, I have implemented a typewriter effect on my website that triggers when the user scrolls to a specific section. However, I want this effect to occur only once. Unfortunately, every time the user scrolls again (in any direction), the function is ...