Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Css Displaying only center part of image

Tags:

jquery

css

Is it possible to display only center part of of image. For eg an image height =300 px and width=520px using CSS or JQUERY css would be better if possible else i can use Jquery, Googling didnt helped me

is it possible that i can show only 200 * 130 form the center using css for eg

 -----------------------
|                       |
|       --------        |
|      |Need this|      |
|      | Part    |      |
|       ---------       |
|_______________________|
like image 331
William Octavia Avatar asked Aug 01 '11 05:08

William Octavia


People also ask

How do I show the center part of an image in CSS?

To center an image, we have to set the value of margin-left and margin-right to auto and make it a block element by using the display: block; property. If the image is in the div element, then we can use the text-align: center; property for aligning the image to center in the div.

How do I hide part of an image in CSS?

You can use the max-height property to specify the maximum height of the image, and then use overflow: hidden; to hide anything else.

How do I center an image horizontally in CSS?

To horizontally center a block element (like <div>), use margin: auto; Setting the width of the element will prevent it from stretching out to the edges of its container.

How do I center a div in CSS?

You can do this by setting the display property to "flex." Then define the align-items and justify-content property to “center.” This will tell the browser to center the flex item (the div within the div) vertically and horizontally.


3 Answers

You can set the image as the bg of an element, and then something like this:

        div {
            background: #eee url(http://www.google.co.cr/logos/classicplus.png) center center;
            width: 100px;
            height:100px;
            border: 1px solid red;
            text-indent: -9999px;
        }
<img src="http://www.google.co.cr/logos/classicplus.png">
<div>some text that won't be seen</div>

And end up with something like http://jsfiddle.net/9nBRe/

like image 194
leopic Avatar answered Oct 26 '22 11:10

leopic


set following CSS :

.background{

background-position: -200px -130px;
background-image:url("image_name.png");
width:200px; height:130px;

}

like image 22
Tushar Ahirrao Avatar answered Oct 26 '22 13:10

Tushar Ahirrao


If the image needs to be an <img> element, you can do it using an overlay image with a transparent "hole" punched in the center.

See http://jsfiddle.net/jkwSe/1/.

It's hard to see, but the image on top is white, but has a transparent center, like a rectangular donut. I left a little of the image on the right so you can see the effect.

like image 24
g_thom Avatar answered Oct 26 '22 13:10

g_thom