Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background-size and background-position on an img tag

Tags:

html

css

image

I have an image that can be cropped used a JS lib. The lib returns x,y, width and height of the selection.

Initially, the logic was just several uploaded image (in <img>) sit side by side with little edit buttons over them that open a pop-up with the cropping logic. However, now the specification is to have the preview of what was cropped.

The question - is there a way to do it without switching all <img> to <div>s that will have all images as background and can be moved and scaled to show the cropped part? Is there a counterpart to such styling or another possible workaround in which I can keep the <img> tags?

like image 416
Alexey Avatar asked Jan 07 '15 16:01

Alexey


People also ask

What is background image position?

The background-position property sets the starting position of a background image. Tip: By default, a background-image is placed at the top-left corner of an element, and repeated both vertically and horizontally.

Which background property specifies the position of background image?

The background-position CSS property sets the initial position for each background image. The position is relative to the position layer set by background-origin .

How do you resize a background image in HTML?

One of the simplest ways to resize an image in the HTML is using the height and width attributes on the img tag. These values specify the height and width of the image element. The values are set in px i.e. CSS pixels.

What is background image tag?

The <background> attribute in the HTML document is used to specify the background image on a HTML page or a table. You can pass the path of an image as a value of background attribute to set the image of your HTML page or table.


2 Answers

If you don't care about IE you can use object-fit and object-position:

img {
   object-fit: cover;
   object-position: center top;
}
like image 180
Dmitry Grinko Avatar answered Sep 30 '22 17:09

Dmitry Grinko


The best way to achieve this is by using any html element in combination with the css background attribute. Within that attribute you can define margins as sizes to position and resize the background image. In your case that element would have the width and the height given by your crop library. The position of the background image would be x and y (also from the library), but inverted.

It does not matter which element you use, it can be any (See w3.org/TR/CSS2/colors.html), included an img tag as shown in the following example:

<img border="0" style="display:block; width:120px; height:120px; background-color:red; padding:9px; background:url(http://www2.cnrs.fr/sites/communique/image/mona_unvarnish_web_image.jpg) no-repeat; background-position:-100px -40px;" />

Note that if you use the image tag you have to remove the src attribute (e.g. by JavaScript). Otherwise the background image will not be visible.

Nevertheless I would recommend you to use a JavaScript Library / Plugin instead of writing it something by your own. There are a bunch of solutions:

  • JCrop (jQuery Plugin)
  • ImageAreaSelect (jQuery Plugin
  • Uvumitools Crop (Mootools Plugin)
  • JS Cropper UI (Based on scriptacolous and prototype)
like image 36
Guillermo Avatar answered Sep 30 '22 18:09

Guillermo