CSS crop image to certain dimensions - html

I have looked around a lot for a solution to this but I can't seem to find one.
I have an image that I need to display within a certain set of dimensions. It must be no more than 100% of the width of the container: fine. But when I try to faux crop it to 50% of the container; it is scaled.
An example of the 100% width: http://i.stack.imgur.com/WTisJ.png
And an example of the problem when it is set to only 50% of the container: http://i.stack.imgur.com/J01sF.png
The code:
CSS:
.shopcontent{
margin-top: 120px;
}
.product{
margin: 2px;
display: block;
height: 250px;
width: 300px;
border: 2px solid #7f8c8d;
border-radius: 10px;
overflow: hidden;
}
.prodimg{
max-width: 100%;
max-height: 50%; (The problem line!)
border: 0px solid white;
border-radius: 10px;
}
.prodimgcont{
overflow: hidden;
}
HTML:
<div class="shopcontent">
<div class="product">
<span class="prodimgcont">
<img src="http://u.danmiz.net/xqz" class="prodimg"></img>
</span>
<p>This is a test</p>
</div>
</div>
Thanks for any help: I really have tried to find a way of doing this but nothing seems to work!

If I understand your problem correctly you could achieve the desired cropping effect like so:
HTML
<div class="img_container">
<div class="cropper">
<img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQQWvNeCn17lshW3c9Z4PLXlTZe6GPf2oiNrLZQft1nPgld1WYb" />
</div>
</div>
CSS
.img_container {
width:300px;
height:250px;
}
.img_container .cropper {
width:50%;
height:50%;
overflow:hidden;
}
.img_container .cropper img {
width:200%;
height:200%;
}
You use the .cropper div to set the desired 50% width and add overflow:hidden, then set the child img tag to width:200% (100% of grandparent width)
Fiddle: http://jsfiddle.net/6hjL0pat/3/
EDIT:
Updated fiddle with your use case

First of all, your img tag should be self closing. so replace
<img src="http://u.danmiz.net/xqz" class="prodimg"></img>
with
<img src="http://u.danmiz.net/xqz" class="prodimg" />
To your problem. I'd advice you to give the dimensions to the container (change that spanto div by the way) and then assign your image as a background-image, because it is more useful for scaling images, especially with background-size: cover.
HTML
<div class="shopcontent">
<div class="product">
<div class="prodimgcont"></div>
<p>This is a test</p>
</div>
</div>
CSS
.shopcontent{
margin-top: 120px;
}
.product{
margin: 2px;
display: block;
height: 250px;
width: 300px;
border: 2px solid #7f8c8d;
border-radius: 10px;
overflow: hidden;
}
.prodimgcont{
display: block;
max-width: 100%;
height: 50%;
max-height: 50%;
border: 0px solid white;
border-radius: 10px;
background-image: url(http://u.danmiz.net/xqz);
background-size: cover;
overflow: hidden;
}
I created a JSfiddle to show you how to do it.
This is one way to do it.
Let me know if you absolutely need to use an img tag. There is a solution for that too. In any case: you need to assign the dimensions you want to the container of the image, not the image itself - because the image needs to be cut off.
Please note, that background-size: cover won't work in IE8 and lower, unless you use a polyfill.

Related

How to make image blurred but make edges crisp

Please, don't say anything like OH THERE ARE A LOT OF ANSWERS OUT THERE. I founded a lot of them, but none of them worked. This is HTML:
<img src="images/ONamaImg.png" class="main-page-img"> (not all of the HMTL of course, only pic code)
And here is CSS:
.main-page-img
{
filter: grayscale(100%) blur(10px);
float: right;
width: 550px;
height: 700px;
z-index: -1;
border-radius: 1000px 0px 0px 1000px;
margin-top: -350px;
box-shadow: 0px 0px 30px #777777;
overflow:hidden;
}
Thanks!
First, wrap the image in a container :
<div class="container">
<img src="path/to/image.jpg">
</div>
Then, add these css rules to the container and image:
.container {
overflow: hidden;
height: /*add height*/;
width: /*add width*/;
}
img {
margin: -10px;
}
Note: I didn't add other styles. Make sure to add them.
The basic concept is to wrap the blurred image in the container and clip the blurred edges using negative margins.
I also found a lot of answers that don't seen to solve the problem.
Here is my attempt:
HTML
First I created a container and added an image to use as the crisp border.
<div class="container">
<img class="border-img" src="https://pbs.twimg.com/profile_images/2209676047/gatinho-5755_400x400.jpg" />
<div class="blur-img"></div>
</div>
CSS
Then I use the same image as a background to an epty div inside the container. Within the div backgroud I can scale and adjust the images to make then appear like one.
.container {
width: 450px;
height: 500px;
overflow:hidden;
}
.blur-img {
width: 70%;
height: 70%;
background-image: url("https://pbs.twimg.com/profile_images/2209676047/gatinho-5755_400x400.jpg");
background-position: center;
transform: scale(1.2); /* use scale to zoom image */
filter: blur(10px);
position:relative; /* adjust position */
left:15%;
top:15%;
}
.border-img {
width: 450px;
height: 500px;
position:fixed;
}
Output
And the output looks like this:

Centering an image in div tag while it is responsive

I want to display an image on the webpage. The image is long enough having fixed height. So, when someone checks responsiveness by decreasing screen-size it should remove the extra size from that div(which is happening).
I don't know the particular term. So, I will try to explain. it should be shown from the centre point. If image is "abcdefgh". Assume 'a','b'... all are grid number. The default behaviour when screen size will be relatively half is "abcd", but I want to display "cdef".
I gave overflow: hidden to remove extra image out of div. I tried margin-left, margin-right both auto. But, it is only required when the image is less than div size.
img {
display: block;
margin-left: auto;
margin-right: auto;
}
<!DOCTYPE html>
<html>
<body>
<div style="border:4px solid black; height:200px;overflow:hidden;text-align:center;">
<img src="https://mdbootstrap.com/img/Photos/Slides/img%20(130).jpg" alt="Paris" style="width:100%;height:200px;">
</div>
</body>
</html>
You can add object-fit: cover to 'crop' the image responsively
I used object-fit: cover;, this works.
img {
display: block;
margin-left: auto;
margin-right: auto;
object-fit: cover;
}
<!DOCTYPE html>
<html>
<body>
<div style="border:4px solid black; height:200px;overflow:hidden;text-align:center;">
<img src="https://mdbootstrap.com/img/Photos/Slides/img%20(130).jpg" alt="Paris" style="width:100%;height:200px;">
</div>
</body>
</html>
Only object-fit: cover will not work. It also needs width and height values. For best practice give height value. Please this value should not in percentage(%). And give image width: 100% and height: 100%. It will work.
.parentDiv {
border: 4px solid black;
width: 80%;
height: 250px;
margin: 0 auto;
}
img {
display: block;
object-fit: cover;
width: 100%;
height: 100%;
}
<div class="parentDiv">
<img src="https://mdbootstrap.com/img/Photos/Slides/img%20(130).jpg" alt="Paris"><div>
Please check this link: jsfiddle
You can set fixed size for image an use object-fit: cover in css.
.wrapper {
border: 4px solid black;
height: 200px;
overflow: hidden;
text-align: center;
}
.img {
display: block;
margin-left: auto;
margin-right: auto;
width: 100%;
object-fit: cover;
}
<div class="wrapper">
<img class="img" src="https://mdbootstrap.com/img/Photos/Slides/img%20(130).jpg" alt="Paris">
</div>

Fitting an image and corresponding span to parent div

I have a div of fixed width and height.
I want to put and image and caption to it (using img and figurecaption) such that they both never exceeds the dimensions of the parent.
I tried this :
`
parent->
height: 100px;
width: 100px;
margin: 0;
img->
display: block;
height: 100%;
width: 100%;
figurecaption->
text-align: center
`
When the image is of greater size than the specified height and width, the caption goes outside. How to deal with this. Thanks.
If you are trying to avoid both the image and the caption going outside the parent container, you have several options. The nicest might be to set the image max-height and max-width to 100% and then to overlay the caption on the bottom. If you want to keep them completely separate, you can do something like this:
#container {
height: 400px;
width: 300px;
border: 1px solid #000000;
text-align:center;
}
#image {
max-height: calc(100% - 50px);
max-width: 100%;
}
#caption {
background: #282828;
max-height: 50px;
width: 100%;
text-align: center;
color: #ffffff;
}
<div id="container">
<img id="image" src="http://www.fixedstars.com.au/images/runBack.jpg">
<div id="caption">This is the caption</div>
</div>
This sets the maximum height of the image at100% less the height of the caption. If you prefer for the caption to be stuck to the bottom of the container, even if the image is shorter, st the container to position: relative and give the caption position: absolute; bottom:0; for the container and the caption.
To Fix this issue you have to set width and height to 100% for Image
Below is the complete demo.
Hope this will helpful to you.
<style>
.mydiv {
border-color: red;
border-style: solid;
height: 50px;
width: 80px;
}
.imgStyle {
height: 100%;
width: 100%;
}
</style>
<html>
<div class="mydiv">
<img src="http://www.boltoday.com/wp-content/uploads/2014/10/only-3d-natural-1024x768.jpg" class="imgStyle" />
Your Text Goes Here
</div>
</html>

CSS, Responsive Design, Image not resizing

I have a header image that stopped resizing after I placed it in a CSS wrapper. I specified the width of the wrapper, b/c I want the navigation to the right to be directly above the white space area below - flushed to the right.
How can I set the width of the wrapper in which I place an image but maintain the responsiveness (image responds to resizing)?
HERE's the URL; http://www.insidemarketblog.com/
Here's the code:
HTML
<div id="header">
<div class="wrap">
<span class="menu_control">≡ Menu</span>
<ul class="nav"><li class="page_item page-item-35">ABOUT US</li></ul>
<h1 id="site_title"><a href="http://www.insidemarketblog.com"><img id="thesis_logo_image" src="http://www.insidemarketblog.com/wp-content/uploads/2014/03/logo_header1.png" alt="Inside Market Strategy" width="400" height="87" title="click to go home" />
</a></h1>
</div>
</div>
CSS:
.wrap {
width: 1000px;
margin: 0 auto;
border: 1px solid #000;
position: relative;
}
You need to make the image flex too:
img {
display: block;
width: 100%;
height: auto;
}
I believe you have two issues here:
1st, Your .wrap is 1000px in width. please make it 100%
2nd, your img css should have a max-width: 400px; and width:100%
This way you will have your img exactly the way you want it.
Here is the css:
.wrap {
width: 100%;
margin: 0 auto;
border: 1px solid #000;
position: relative;
}
img {
width: 100%;
max-width: 400px;
height: auto;
}
See it in Action: http://jsfiddle.net/salota8550/59LKP/

Align DIVs horizontally within a scrolling div

I'm working on a photography website. One of the things we're trying to aim for is a 'film strip' type of display for the images, as opposed to the usual thumbnail or 'tabulated' formation.
It works with tables. No problemo. The only thing that makes me not want to use a table is the fact that I'm not showing data, there's no need for columns and rows.
Another thing that is a slight spanner in the gears is the fact that I'm putting the images as backgrounds of divs. This is for basic 'copy protection', and also so I can overlay items over the photo on hover of the div.
The way I've got it coded at the moment is:
container [
[image]
[image]
[image]
[image]
]
I've drawn a skitch to help out with the visualisation of this..
As soon as the width of the container is met, the image-divs are dropping to the next line.
The CSS for the Divs is as follows:
.gallery_block_image_p {
width: 354px;
height: 532px;
display: inline-block;
margin: 0px;
padding: 0px;
margin-left: 10px;
float: left;
background-repeat: no-repeat;
}
and for the container...
#gallery {
border: 0px solid black;
position: relative;
top: 99px;
/* width: 8000px; */ /* When this is uncommented it works, with a huge amount of space to the right */
height: 532px;
z-index: 99;
}
and last but not least, the HTML used for the image divs...
<div id="gallery_1_0_img" class="gallery_block_image_p" style="background-image: url(gallery_img/ith/adamd_20101021_137.jpg);"></div>
if you remove "float:left;" from the gallery block style and add "white-space:nowrap" to the container then it should work.
Edit: I think something like this is what you're looking for
<div style="width: 800px; overflow-x:auto; white-space: nowrap;">
<div style="width: 300px; height: 100px; background-color: #f00; display: inline-block;"></div>
<div style="width: 300px; height: 100px; background-color: #0f0; display: inline-block;"></div>
<div style="width: 300px; height: 100px; background-color: #00f; display: inline-block;"></div>
<div style="width: 300px; height: 100px; background-color: #ff0; display: inline-block;"></div>
</div>
Try specifying the width of 800 and adding an overflow declaration:
#gallery {
border: 0px solid black;
position: relative;
top: 99px;
width: 800px;
height: 532px;
z-index: 99;
overflow:auto;
}
try using the overflow property for the container. so something like this:
#gallery {
overflow-x: scroll;
overflow-y: hidden;
}
here are some examples http://www.brunildo.org/test/Overflowxy2.html
I think you might need to define the width of your gallery! see fiddle
I have added the view to hold it all, but like you seemed to find there was no way of forcing a line, might be able to do something with positioning.
Alternatively declare the width at the top of the page with the server side logic instead of the javascript on the fiddle
Not tested, but could you use the
white-space:nowrap;
css property to stop the divs from wrapping when you specify the width?
I have done some thing very similar with a site and was challenged by this as the user would be adding / removing divs on his own. My solution for this was to use jQuery to count each item/div within the container and set the width of the container based on items within the container.
jQuery:
$('.gallery-item').each(function(scroll){ n = n+310; });
$('#gallery').css( "width", n);
});
I came up with a bit of a hacky solution, the only downside of which, you need to know the width of the scrolling gallery. I'm sure that's pretty easy to predetermine or calculate. Below is the code and here is an online demo.
Some cheeky jQuery will allow you to calculate it all on the fly if results are dynamic.
<style type="text/css">
#gallery {
border: 0px solid black;
position: relative;
width:500px;
height: 450px;
overflow:scroll;
overflow-y:hidden;
z-index: 99;
}
.gallery_block_image_p {
width: 354px;
height: 400px;
margin: 0 0 0 10px;
padding: 0;
background-repeat: no-repeat;
display:inline-block;
}
#stretch{
width:1850px;
}
</style>
<div id="gallery">
<div id="stretch">
<div id="gallery_1_0_img" class="gallery_block_image_p" style="background-image: url(http://blogs.westword.com/demver/kitten.JPG);"></div>
<div id="gallery_1_0_img" class="gallery_block_image_p" style="background-image: url(http://blogs.westword.com/demver/kitten.JPG);"></div>
<div id="gallery_2_0_img" class="gallery_block_image_p" style="background-image: url(http://blogs.westword.com/demver/kitten.JPG);"></div>
<div id="gallery_3_0_img" class="gallery_block_image_p" style="background-image: url(http://blogs.westword.com/demver/kitten.JPG);"></div>
<div id="gallery_4_0_img" class="gallery_block_image_p" style="background-image: url(http://blogs.westword.com/demver/kitten.JPG);"></div>
</div>
</div>