Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show only the top portion of an image with CSS?

Tags:

css

If an image is taller than 115 px, I'd like to only show the top 115px in a square container. I'm trying this with the following:

http://jsfiddle.net/3mRh9/

But I still see the whole image. How can I always show only the top portion of an image using only CSS?

Code:

<div>
    <div class="floated"> 
        <a class="limited">
            <img src="http://change.gov/page/-/officialportrait.jpg"/>
        </a>
    </div>
</div>


.floated {
    border: 1px solid blue;
    float: left;
}
.limited {
    width: 115px;
    height: 115px;
    overflow: hidden;
    border: 1px solid red;
}
img {
    max-width: 115px;
}
like image 351
SB2055 Avatar asked Nov 02 '13 16:11

SB2055


People also ask

How do I show the top of an image in CSS?

Add CSS. Add a relative div placed in the flow of the page. Set the background image as relative so as the div knows how big it must be. Set the overlay as absolute, which will be relative to the upper-left edge of the first image.

How do I show only a part of an image in CSS?

Wrap the image in a div The markup to set up CSS-only cropping is super basic. All you need to do is wrap the img tag in a div . The pug image is 750 pixels wide and 500 pixels high. Let's make it portrait-oriented by maintaining the 500 pixel height, but chopping the width in half to 375 pixels.

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. e.g. Do note however that this requires that the user have JavaScript enabled in their browser.

How do you bring to top in CSS?

If position: absolute; or position: fixed; - the top property sets the top edge of an element to a unit above/below the top edge of its nearest positioned ancestor. If position: relative; - the top property makes the element's top edge to move above/below its normal position.


2 Answers

http://jsfiddle.net/3mRh9/1/

Add display: block to your .limited style.

.limited {
    width: 115px;
    height: 115px;
    overflow: hidden;
    border: 1px solid red;
    display: block;
}

<a> tags are inline by default, and inline elements cannot have width or height.

Alternatively, you could just wrap with a block-level element, like a div, instead (fiddle):

<div>
    <div class="floated"> 
        <div class="limited">
            <a href="http://example.com">
                <img src="http://change.gov/page/-/officialportrait.jpg"/>
            </a>
        </div>
    </div>
</div>
like image 58
tckmn Avatar answered Sep 28 '22 01:09

tckmn


There is a property that will allow this for showing only part of an image.

It's clip but it only works on absolutely positioned objects.

JSFiddle Demo.

HTML

<div>
    <div class="floated"> 
        <a class="limited">
            <img src="http://change.gov/page/-/officialportrait.jpg"/>
        </a>
    </div>
</div>

CSS

.floated {
    border: 1px solid blue;
    float: left;
}
.limited {
    width: 115px;
    height: 115px;
    display:block;
    border: 1px solid red;
    position:relative;
}

img {
    position:absolute;
    clip:rect(0px,115px,115px,0px);

}

I'm not sure if this is the effect you are after but it is what you asked for.

like image 26
Paulie_D Avatar answered Sep 28 '22 01:09

Paulie_D