Showing a cropped section of an image inside a div container

Is it possible to display a specific portion of an image within a div that is 300*300px in size while maintaining responsiveness, using only HTML and CSS?

The div always occupies 33% of the page width.

Here is an example of my code:

HTML

<div class="crop">
    <img src="path/to/img">
</div>

CSS

position: relative;
overflow: hidden;
min-height: 300px;

Answer №1

Yes, it's actually quite simple:

.picture {
display: block;
position: relative;
overflow: hidden;
width: 300px;
height: 300px;
}

.picture img {
position: relative;
left: -100px;
top: -100px;
}

<div class="picture">
    <img src="http://img05.deviantart.net/c135/i/2011/134/7/0/bridge_by_kzonedd-d3gcetc.jpg" alt="photo of old wooden bridge" />
</div>

Just remember that the minus 100px is only for demonstration purposes. Feel free to experiment with different values to see how positioning works.

Hope this helps. Wishing you all the best and enjoy exploring!

Answer №2

To address this issue, one possible solution is to utilize a background-image and adjust its position using the background-position property:

.crop{
  position: relative;
  overflow: hidden;
  min-height: 300px;
  width: 300px;
  background-image: url(https://cdn.pixabay.com/photo/2015/07/06/13/58/arlberg-pass-833326_960_720.jpg);
  background-size: contain;
}
<div class="crop">
</div>

Answer №3

Indeed, utilizing flexbox can achieve that design

Below is the code with explanations

div{
width:300px;
height:300px;
overflow:hidden;
position:relative;
left:50%;
transform:translateX(-50%);
display:flex;
justify-content:center;
align-items:center;
}
div img{
flex:0;
width:auto;
min-height:100%;}
<div>
  <img src ="http://www.cooperindustries.com/content/dam/public/safety/notification/products/Mass%20Notification%20Systems/Spotlight/MNS_WideArea_Spotlight3.jpg">
</div>

You can use flex-start, flex-end, and center in justify-content to align horizontally.

For align-items, you can adjust vertically using the same values as justify-content.

Since the image is landscape, I set min-height to 100% and width to auto. Adjust accordingly for portrait or square images.

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

JavaScript PIP video feature

Currently, I am utilizing the requestPictureInPicture function to display the HTML video element in a popup window. However, I have encountered an issue where the size is restricted to approximately 920 x 540 when in Picture-in-Picture mode. I am wonderin ...

Validation issue with Reactive Forms not functioning as expected

My latest project involves a user signup component that I created from scratch import { Component } from '@angular/core'; import {UserManagementService} from '../user-management.service'; import {User} from "../user"; import {FormBuild ...

Unveil a portal to the identical webpage

My apologies for my English skills... Could anyone advise me on how to display an image? Hello, I am looking for a way to have a small window open on my site when a user clicks on the zoom icon, where I can describe a product. Any guidance on the HTML co ...

Image loading in NextJS is limited to only Firefox browsers

Images are loading only in Firefox and not anywhere else. Here is the import statement: import gradient from '../public/mountains/gradient.svg' And here is the Image component: <div id='gradient' className={`${styles.bgimage} ${sty ...

Link embedded within a list item

How can I make the hyperlink element fill up the width and height of the li element inside? <ul> <li><a href="#" style="display: block; width: 100%; height: 100%;">this link to fill up the li element width and height</a><l ...

Effortlessly create a seamless transition in background color opacity once the base image has finished

I've set up a div with a sleek black background. Upon page load, I trigger an API request for an image, which is then displayed in a secondary div positioned behind the main one. After this, I aim to smoothly transition the overlaying div's opaci ...

Setting a constant width for text characters across all devices using CSS and HTML

I am looking to display text in my textarea with the same width in pixels across all devices. Essentially, I want to set the character width in pixels so that it appears consistently on any device (Desktop/Mobile). <html> <head> <styl ...

storing a value in the browser's local storage

I am in the process of creating a new game that includes a high score feature. The idea is that when the current score surpasses the existing one stored locally, it will be replaced: localStorage.setItem('highScore', highScore); var HighScore = ...

How can I ensure the text remains centered within a div, even when there are buttons aligned to the left or

I have a CSS class called .text-align-center that I used to center text. .text-align-center { text-align:center; } However, I noticed that when there is a left or right arrow icon within the <div>, the text does not appear perfectly centered. Is ...

Alter the button's color seamlessly while staying on the same page

I have implemented a feature that allows me to change the category of photos without having to leave the page and it works perfectly. My next goal is to create a button system where the pre-defined category of a photo is indicated by a button with a green ...

Matching CSS attribute values with another attribute

Can CSS be used to target elements based on the value of another attribute? For instance: <div data-attr1="abc" data-attr2="def"></div> <div data-attr1="abc" data-attr2="abc"></div> I am looking for something like this (although i ...

Styling each individual page in R Shiny with CSS

I've created an R shiny application and am in the process of adding styling to it. I've incorporated some HTML code and want to customize elements like the background color using CSS. After doing some research online, I learned that I should use ...

Ways to access every iframe on a webpage

I am facing a challenge with a highly intricate HTML page. <html> <head></head> <body> <iframe> A </iframe> <table> <div> <span> <div> <iframe ...

Attempting to achieve the simultaneous display of an image overlay and image zoom when hovering over the mouse

I'm struggling to display an image overlay and image zoom simultaneously when hovering the mouse. After adding the image zoom effect, the overlay disappears. It seems like one transition is overriding the other. Whenever the zoom works, the overlay do ...

Extract information from the table using $_POST

I possess a datatable within a form. Inside that table, there is a column designated for setting a price. Upon submitting the form on the same page, the $_POST data fails to populate. Consequently, I am in need of inserting it into the MySQL db post-submit ...

Modifying object properties in JavaScript

Looking to update a boolean attribute for an object in JavaScript (in this case, the object is part of fullPage.js library). I am interested in turning off the navigation attribute by setting it to false, and ideally would like to do this from a separate f ...

problem | JQuery slide causing automatic page scroll to top

Here is the HTML for my slide: <div class="slide_container"> <div class="one_slide"> <a href="#"><img alt="slide_img" class="slide_img img-responsive" src="images/slide2.jpg"></a> </div> ...

How to design a bell curve using CSS styling

Exploring CSS shape design, I am aiming to create a classic bell shape reminiscent of the holiday season. While the top and bottom balls are not essential to my design, here is the basic outline I'm striving for: This is what I have been able to achi ...

Incorporating Bootstrap's Inline Design Elements

I am having trouble aligning a series of elements horizontally using bootstrap as shown in the example image below: Whenever I add the "form-control" class to the input element, it gets pushed down below the other elements. I have experimented with differ ...

Designing a dynamic button that expands on hover to catch the eye

My instructor has provided us with this code snippet: <a class="btn btn-tertiary" href="#"> <span class="circle"> <i class="arrow-right"> </i> </span>Create profile </a> .btn.bt ...