Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7621511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:12:50+00:00 2026-05-31T04:12:50+00:00

Currently, all my images look like this: HTML <img class=photo src=foo.png /> CSS .photo

  • 0

Currently, all my images look like this:

enter image description here

HTML

<img class="photo" src="foo.png" />

CSS

.photo {
   padding: 3px;
   border: 1px solid #000;
   background: #fff;
   width: 64px;
   height: 64px;
   display: block;
}

This expects the aspect ratio of the image to always be 1:1. Now, a new project requirement has come in that the images do not have to be 1:1. Rather, they can be tall rectangles:

enter image description here

In this case, I would like to only reveal the topmost square of the image:

enter image description here

How can this be done with a single <img> tag? I know how to do it with two tags – just wrap the img with a div, apply 3px padding to the div and place the URL as the background-image of the img. But I would like to know how to do this in a cleaner way, without an additional HTML tag.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:12:51+00:00Added an answer on May 31, 2026 at 4:12 am

    Final Update–tested IE8+: This solution uses a refinement of Surreal Dreams’ idea of using outline for getting the border. This allows a cross browser simplicity compared to the linear-gradient method I was previously going for. It works in IE7, except the outline (unsupported).

    Keeping it as an img tag with a src keeps it semantic.

    A margin: 1px is applied to give the outline “space” in the layout, since by nature outline takes up no space. The zero height suppresses the main image, and the padding is used create the desired height for the background image (which is set the same as the main image) to show.

    HTML

    <img class="photo" src="foo.png" style="background-image: url(foo.png)" />
    

    CSS

    .photo {
       padding: 64px 0 0 0;
       border: 3px solid #fff;
       outline: 1px solid #000;
       margin: 1px;
       width: 64px;
       height: 0px;
       display: block;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've currently got all of this mess at the top of my ViewModels which
I'd like to capture a screenshot of what a java applet would currently look
I'm currently having trouble showing images in a table. The images all have different
I'm using jquery to build some html and the final code should look like
With selenium can one run a test which check all images on a current
Currently all our GUI components are written in MFC. Our build environment is: VC
Currently all of my script's settings are located in a PHP file which I
Currently all working with my jquery $.post actions, successful connections, query and json_encode return
I currently do all development work in VB.NET 2008, winforms; but within 18 months
I'm currently getting all the Select elements that exist in a form with the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.