What is the best way to scale an image to perfectly fit its container using CSS?

After starting the Freecodecamp course, I encountered a roadblock while working on one of the projects. Here is the current status of my project: https://codepen.io/otapadar/full/JjRaoex (I am aiming to replicate this website: https://codepen.io/freeCodeCamp/full/zNBOYG). The issue lies within the second section where I have images arranged in a grid format. My goal is to resize these images so that each covers around 80-90% of the parent element, similar to the original site.

Upon inspection of my website, you'll notice that the images are not resizing properly. To address this, I attempted using the object-fit: cover property on my images but unfortunately, it did not resolve the issue.

I would greatly appreciate any assistance in resolving this matter. Thank you in advance!

HTML:

<script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>

<nav id="navbar">
  <ul>
    <li><a class="nav-link" href="#welcome-section">About</a></li>
    <li><a class="nav-link" href="#projects">Work</a></li>
    <li><a class="nav-link" href="#contact">Contact</a></li>
  </ul>
</nav>

<main>
  <section id="welcome-section">
    <h1>Hey, I am Brad</h1>
    <p>Web Developer</p>
  </section>
  <section id="projects">
    <h2 class="projects-header">My Projects</h2>
    <container class="project-grid">
      <a class="project-tile" target="_blank">
        <img class="project-image" src="https://raw.githubusercontent.com/freeCodeCamp/cdn/master/build/testable-projects-fcc/images/tribute.jpg">
        <p class="project-title">Tribute Page</p>
      </a>
      <a class="project-tile" target="_blank">
        <img class="project-image" src="https://raw.githubusercontent.com/freeCodeCamp/cdn/master/build/testable-projects-fcc/images/random-quote-machine.png">
        <p class="project-title"><Random Quote Machine/p>;
      </a>
     And so on...
    </container>
    <a class="button" href="https://codepen.io/otapadar" target="_blank">Show All</a>
  </section>
  <section id="contact">
    <h1>Let's Work Together</h1>
    <p>How do you take your coffee?</p>
    <container class="social-icons">
      <a class="profile-link" target="_blank"></a>
      <a class="profile-link" target="_blank"></a>
      <a class="profile-link" target="_blank"></a>
      <a class="profile-link" target="_blank"></a>
      <a class="profile-link" target="_blank"></a>
    </container>
  </section>
</main>

CSS

@import url('https://fonts.googleapis.com/css2?family=Raleway&display=swap');

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  font-family: 'Raleway';
  scroll-behavior: smooth;
}
Rest of the CSS code goes here...

Answer №1

The issue with your CSS can be found in the following line: height: calc(100%-6rem);. Make sure to include spaces around the operator like this: height: calc(100% - 6rem);

Answer №2

It's advisable to include white space before and after minis in this manner

height:calc(100% - 6rem)

Answer №3

Consider adding a max-width of 100% to the project-image class in your CSS file. You can also specify a custom width or height for the img element.

    .project-image{
      height: 300px;
      width: 600px;
      max-width: 100%;
      object-fit: cover;
}

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

Transfer selected option with various values using JavaScript from one list to another

Currently, I am experimenting with creating a Dual List Box for forms that involve multiple selections. This setup includes two lists: one containing options to choose from and the other displaying the selected options. My approach involves using vue2 alon ...

FancyBox - Is there a "Never show this message again" option available?

I implemented FancyBox on my website to display important information to users. However, I would like to enhance the user experience by adding a button that allows them to set a cookie and prevent the message from popping up for about a month. Since I&apo ...

What is the best way to incorporate a button in my code that automatically triggers changes at regular intervals?

Is there a way to automatically change the color of my working traffic light in JavaScript on a timed basis, rather than relying solely on button clicks? Here is the current code I am using: <!DOCTYPE html> <html> <head> <style> # ...

Tips for showcasing an HTML Element from a different website

Suppose I have the identifier of a class, is there a way to extract the HTML content associated with that class and convert it into a string? I am not necessarily looking for specific code, but any helpful resources would be greatly appreciated. I am unsu ...

Eliminate unnecessary spacing from the sticky container

Trying to implement a sticky menu in an angular 14 project using angular material 14 has been quite challenging for me. Initially, I attempted to use the 'fixed' position, but encountered issues where the menu would consistently return to the to ...

Customize the appearance of all QProgressBars using Qt stylesheets in your C++ program

Struggling to figure out how to dynamically apply a stylesheet to multiple or all widgets in Qt Creator 4.1 (Qt 5.7) using C++. For instance, if I have 3 progress bar widgets, currently I am setting the same stylesheet for each one individually with CSS: ...

Tips for displaying an HTML page using JavaScript

In my project, I am working with an .html file (File X) that needs to immediately open another .html file (File Y) based on a certain value. Could someone provide guidance on the best way to achieve this using JavaScript? Additionally, I would like the pa ...

Using HTML within a Material-UI Accordion component

I am attempting to display HTML content within an Accordion component. import {Accordion, AccordionDetails, AccordionSummary, Typography} from '@material-ui/core'; import { Badge } from 'reactstrap'; ... const buildAccordion = (f, i) ...

AngularJs fails to apply style to dynamic content in IE8 when using HTML5

Currently, I am in the process of developing a website with AngularJs and utilizing numerous JS scripts to address compatibility issues with Internet Explorer. I have organized the data in JSON format, and each page is designed to fetch and display differ ...

Trouble with downloading a file from an HTML hyperlink

When I attempt to download a file from my file folder using the absolute path <a href='N:\myName\test.xlsx'>download</a>, the file opens directly instead of downloading. However, if I use the relative path <a href=&apos ...

How to Dynamically Adjust the Color of a KendoPanel During Execution

I am currently utilizing KendoPenel to showcase some information and I need to enlarge a specific panel and alter its color during the runtime if any issues are detected within the data. For expanding and selecting the panel, I have implemented the code b ...

Chrome page loading with smooth color transitions

Setting global colors on links and transitions on their color: a { color: blue; transition: color 300ms linear; } Later in the code, there's more specific styling for links in the navigation: nav a { color: red; } nav a:hover { color: black; } ...

What is the best way to activate the cube portfolio using custom jquery?

Is there a way to activate the Cube portfolio filter using custom JQuery? see image here For instance: In my Cube portfolio slider, I have over 20 projects featuring different technologies. When I slide the div, I want the filter to be activated based on ...

Create personalized styles for each item within a stack with specific spacing using the @mui library

Is there a way to change both the background color and spacing area when hovering over each item in my list? https://i.stack.imgur.com/87TST.png <Stack spacing={4} divider={<Divider variant={`fullWidth`} orientation={`horizontal`} flexItem/>}> ...

Mobile devices consistently experiencing issues with JavaScript generated elements overlapping

I'm currently in the process of creating a quiz based on a tutorial I found at https://www.sitepoint.com/simple-javascript-quiz/. While I followed the tutorial closely and integrated Bootstrap, I am encountering an issue specifically with mobile devic ...

Executing SendKeys on a div element with the attribute coleditable="true" in Selenium and C#

I am facing a challenge with an element that I am unable to input text into. <div class="floatstart gridcell grideditablefield" colname="Items" coltype="string" coleditable="true" val="" style="widt ...

Exploring optimal practices for CSS components and responsive design integration

If I were to develop a reusable component like a table that will be used across various pages, the content and number of columns may differ from one page to another, but the overall design should remain consistent. .my-table { background-color: ... et ...

Having difficulty in getting a hyperlink to open in a new tab with _blank attribute

I'm currently working with this code for my link. <a href="https://dribbble.com/jamesfrewin"><img class="socialicon" src="images/icons/dribbble_dark.png" onmouseover="this.src='images/icons/dribbble_pink.png'" onmouseout="this.src= ...

Showing local storage on a webpage using Jquery

I have encountered an issue where I can successfully add and remove items from local storage, but the added item does not display on my HTML page. The expected behavior is that when an item is added to local storage, it should be visible on a favorites pag ...

Different tab designs with a variety of color schemes

I am looking to implement various color combinations for different tab selections using the background-color property. For instance: About: red .nav-tabs, dark red for .active, Services: yellow .nav-tabs, dark yellow for .active, .... .... and so fort ...