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;
}
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.
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.
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.
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.
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With