Struggling with aligning images in the center of a square container

Currently revamping my photography website and working on a simple slider to showcase some of my photos. Struggling with aligning the images in divs with padding all around while keeping them centered. I want to replicate the look of my photos on Instagram, where they are centered in squares but maintain their aspect ratio.

You can check out my Codepen here.

Here's an example of what I aim to achieve: https://i.stack.imgur.com/VEOYs.png

SCSS:

.slick-slider {
  .image_container {
    background: red;
    position: relative;
    overflow: hidden;
    height: 256px;
    padding: 20px;

    img {
      position: absolute;
      top: 0; bottom: 0; 
      margin: 0 auto;
      max-height: 300px;
      left: 0; right: 0;
      padding: 15px;
    }
  }
}

HTML:

<div class="slick-slider">
  <div class="image_container">
    <img src="http://cdn.jscotto.com/sites/joescottophotography.com/images/portraits/photo-portraits-1.jpg">
  </div>
  <div class="image_container">
    <img src="http://cdn.jscotto.com/sites/joescottophotography.com/images/portraits/photo-portraits-2.jpg">
  </div>
</div>

Answer №1

Automatically adjusts and fits within the div container

.thumbnail {
      position: relative;
      width: 200px;
      height: 200px;
      overflow: hidden;
    }
    .thumbnail img {
      position: absolute;
      left: 50%;
      top: 50%;
      height: 100%;
      width: auto;
      -webkit-transform: translate(-50%,-50%);
          -ms-transform: translate(-50%,-50%);
              transform: translate(-50%,-50%);
    }
    .thumbnail img.portrait {
      width: 100%;
      height: auto;
    }
<div class="thumbnail">
      <img src="http://cdn.jscotto.com/sites/joescottophotography.com/images/portraits/photo-portraits-5.jpg" alt="Image" />
    </div>
    <div class="thumbnail">
      <img src="http://cdn.jscotto.com/sites/joescottophotography.com/images/portraits/photo-portraits-2.jpg" class="portrait" alt="Image" />
    </div>

Answer №2

After some experimentation, I managed to find the correct CSS code:

.image_container {
  margin: 0 10px;
  position: relative;
  overflow: hidden;
  height: 350px;
  background: rgba($offBlack, 0.05);

  img {
    position: absolute;
    top: 0; 
    bottom: 0; 
    left: 0; 
    right: 0;
    margin: auto;
    max-height: 300px;
    max-width: 310px;
  }
}

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

Balanced Height Panels - Utilizing Flexbox and flex-wrap: wrap

i'm having trouble with a layout design, specifically with getting the columns to be equal height, especially when the text wraps around. Can anyone provide some guidance? https://i.stack.imgur.com/bbDKj.png Within my HTML template, there are multipl ...

What is the best way to retrieve the height of a <DIV> element without factoring in the presence of a horizontal scrollbar with

I have created a unique jQuery plugin that involves utilizing two nested <DIV> elements. The outer div is set with a fixed width and overflow: scroll, while the inner div (which is wider) houses the content for scrolling purposes. Everything is fun ...

PHP code isn't showing any posts

Context: I have implemented a PHP loop to generate a dynamic feed of my portfolio posts on my WordPress website. Issue: The first five posts are being displayed correctly, but the subsequent posts are not. I'm uncertain about the underlying cause and ...

Python Selenium: How to locate elements using xpath in the presence of duplicate elements within the HTML code

Currently, I am utilizing selenium to extract data from a liquor sales website to streamline the process of adding product information to a spreadsheet. My workflow involves logging into the website using selenium and searching for the specific product. Wh ...

One function is failing to execute properly due to multiple if conditions

Take a look at my JavaScript code below: function showLater1() { if ((vidos.currentTime >= 30) && (vidos.currentTime <= 34)) { lay.style.opacity = "1"; content.style.opacity = "0"; controls.style.opacity = "0"; ...

Is there a way to restrict form choices depending on the previous entry, while also keeping all fields, including file selections, filled out upon submission?

Currently, I have a form set up as follows: <form name="estimate" action="live_preview.php" method="post"enctype="multipart/form-data"> <label for="file">Upload File 1:</label> <input type="file" name="file" id="file"/> ...

CSS Troubleshoot: Unable to Change Background of Current Menu Page Item

I've been attempting to add a small graphic using CSS to indicate the current page the viewer is on, but for some reason, it's not highlighting properly. Below is my code: HTML: <ul class="side-nav"> <a href="http://www.cieloazulsant ...

Display the text area when the "Yes" radio button is clicked, while it remains hidden either by default or when the "No" radio button is selected

I am currently working on an html code and I am looking for a way to use java script in order to create a text area box only when the "Yes" radio button is selected. This text area should be hidden by default or when selecting "NO". <table class="tab ...

Toggle textboxes using jQuery depending on the radio button choice

I'm trying to make specific textboxes appear when a particular radio button is clicked on my form, but I want them to remain hidden otherwise. Here's an example of what I've implemented: HTML: Radio buttons: <p>Show textboxes<inpu ...

Showing database table entry that corresponds to WordPress post ID

Seeking guidance on a project. I have created a website for a volleyball club using Wordpress and am collecting player profile data through a Google form/spreadsheet, which I then save as a .csv file and import into a custom table named 'profiles&apos ...

Tips for accurately placing the iOS audio player in the footer section

In my web page, I have created a simple footer that shows the currently playing track title and includes an HTML audio player. The layout looks great on desktop browsers and Android devices, but on iOS, the player is positioned far below the footer. To ad ...

The issue with the min attribute for number inputs not functioning properly when using decimal

When inputting the value 3 in the field and clicking on submit in the HTML snippet below, Chrome displays an error message stating: Please enter a valid value. The two nearest valid values are 2.0001 and 3.0001. This is confusing because I have set the mi ...

Display a circular div containing information; upon hovering over it, reveal all details

Looking to create a layout with three circular divs side by side where the bottom of each circle displays the beginning of an information text. Upon hovering, the full text should be revealed: My initial approach involved setting the CSS properties for a ...

Enhance user experience with AngularJS Bootstrap autocomplete feature by automatically filling input box when hovering over dropdown options

I am currently implementing the AngularJs Bootstrap typeahead functionality. Typically, when we enter text that matches an option, the dropdown list appears. https://i.stack.imgur.com/395Ec.png What I aim for is to automatically fill the textbox with the ...

Refine information by using checkboxes with the help of jQuery, MySQL,

How do I use PHP and jQuery to filter content with checkboxes? I'm getting a warning about an undefined index when I try to post the form via jQuery. Check out my JavaScript file: function filterContent(){ var all = $(".all").val(); $('input.al ...

Having issues with a Retina iPhone interacting with a website built using responsive Twitter Bootstrap

I'm currently utilizing Twitter Bootstrap alongside responsive CSS for a web page that is optimized to run in a UIWebView. My goal is to ensure the consistency of the page's appearance across all iPhones, with an exception for images. These imag ...

Tips on including variables within quotation marks in JavaScript

I'm currently working on a JavaScript project using Pug. My goal is to use Express and MongoDB to develop a CRUD application that generates an edit button for each post, triggering a specific event when clicked. However, instead of the desired resul ...

Looking to Identify a Click Within a Complicated Component and Retrieve the Component's ID

Currently, I am working with React for development and have a need to capture clicks at the topmost parent level for performance reasons. const clickHandler = (e) => { console.log("clickHandler", e.target) --> I want to identify the child ...

What causes the entire page to disappear when the screen is adjusted to mobile width?

I'm facing an issue with my ReactJS screen. Everything works perfectly until I resize the screen to mobile width (sm of Material UI) and then everything turns into a WhiteScreen. I've been trying to debug this but can't seem to find the root ...

Count the start and end rows of a table within the <tfoot> using pdfHTML/iText 7

Currently, I am working on a project where I am developing a document generator that utilizes pdfHTML 3.0.3 and iText 7.1.14. The document includes a table displaying 'items'. It's worth noting that these item rows will likely span across mu ...