Thumbnail of a Portion of an Image While Maintaining Aspect Ratio - html

I'm attempting to create thumbnails out of some images, each of which isn't necessarily the same size at the others.
Here is a Fiddle with my current code. I've read on some other sites, and even on here that I just need to set the width and height of the image class, then apply the overflow:hidden property, but that doesn't seem to be working. It's still changing the aspect ratio of the image. I know I could just simply remove either the height or width property, but I really just want to make a 100x100 crop of the image. I tried clip:rect() but couldn't figure out how to make it work. Ideally, I'd want to crop 100x100 from the center of the full-size image, but using clip, I don't think I can do this if the dimensions of my images aren't all the same.
.thumbnail {
overflow:hidden;
width:100px;
height:100px;
padding:10px;
margin-left:10px;
margin-right:10px;
margin-top:10px;
margin-bottom:10px;
border:10px solid #EEEEEE;
}

using css and html:
First solution:
html:
<div class="imageFrame">
<img src="your_path" alt="thumb" />
</div>
css:
.imageFrame {
overflow:hidden;
width:100px;
height:100px;
padding:10px;
margin-left:10px;
margin-right:10px;
margin-top:10px;
margin-bottom:10px;
border:10px solid #EEEEEE;
position:relative;
}
.imageFrame img{
position: absolute;
top:50%;
left:50%;
margin-left: -50px;
margin-top: -50px;
display: block;
}
Second solution:
here you will have to use some JS to add dynamically the image url path to the <div class="imageFrame".
html:
<div class="imageFrame" style="background-image: url('your_path');"></div>
css:
.imageFrame {
overflow:hidden;
width:100px;
height:100px;
padding:10px;
margin-left:10px;
margin-right:10px;
margin-top:10px;
margin-bottom:10px;
border:10px solid #EEEEEE;
position:relative;
background-repeat: no-repeat;
background-position: center center;
}

Related

Scale and center image in each div in this container

I have this in HTML and i need the pictures inside the containers to be centered and to scale even if i resize the parent div
<div class="art-cont" id="art">
<div class="art-1">
<img src="https://cdn.stocksnap.io/img-thumbs/960w/2SQMYBPQGK.jpg">
</div>
<div class="art-2">
<img src="https://cdn.stocksnap.io/img-thumbs/960w/2SQMYBPQGK.jpg">
</div>
<div class="art-3">
<img src="https://cdn.stocksnap.io/img-thumbs/960w/2SQMYBPQGK.jpg">
</div>
</div>
CSS:
.art-cont{
width:100%;
height:400px;
}
.art-1{
width:70%;
height:50%;
float:left;
position:relative;
overflow:hidden;
border:2px solid #fff;
}
.art-2{
width:30%;
height:50%;
float:left;
position:relative;
overflow:hidden;
border:2px solid #fff;
}
.art-3{
width:100%;
height:50%;
clear:both;
position:relative;
overflow:hidden;
border:2px solid #fff;
}
Here's a fiddle: https://jsfiddle.net/0r7rhe77/
The following rule will do it
.art-cont img {
width: 100%;
height: 100%;
object-fit: cover;
}
as long as you're okay with the browser compatibility of object-fit (roughly 90% of users - most everybody except IE 11 or lower). Also check out the other options for object-fit.
Fiddle
Another option is refactoring your code to display the images using background-image, as all modern browsers support background-size. Remove the <img> elements and instead do:
.art-1, .art-2, .art-3 {
background-image: url(https://cdn.stocksnap.io/img-thumbs/960w/2SQMYBPQGK.jpg);
background-size: cover;
background-position: center center;
}
Fiddle using this approach

Positing search div

I have a problem, I want to set search results be like on picture: http://prntscr.com/62bbce (sry for my bad drawing skills :/)
But i get that: http://prntscr.com/62bbpw
this is my css code:
#search{
margin-left:100px;
width:300px;
border-radius:5px;
}
#search_results{
position:absolute;
background:white;
font-size:17px;
}
You have to set the width of the search results to 300px too if you want both divs to be the same width.
#search_results{
position:absolute;
background:white;
font-size:17px;
width: 300px;
}
You position it the same way as the other div:
#search_results{
border:1px solid grey;
background:white;
font-size:17px;
width: 300px;
margin-left: 100px;
margin-top: 15px
}
Full code: http://codepen.io/anon/pen/raYdLw

text gets longer when zoomed out

I have a div in which there is an image and text everything is working fine but when it is zoomed out the text gets longer and jumps out of the div currently i am using overflow-x:hidden; to hide it but why does this happen? This is my code http://jsfiddle.net/z19n1jjg/
<div id="test"><img src="http://placekitten.com/300/301"/>
<p>content</p>
</div>
CSS
#test{
border:1px solid black;
width:300px;
height:320px;
overflow-x:auto;
}
#test img{
float:left;
width:150px;
height:150px;
}
You are using pixels. They will change on different display sizes.
If you want to code relatively, use em's instead of pixels.
View updated fiddle: http://jsfiddle.net/z19n1jjg/1/
#test{
border:1px solid black;
width:18em;
height:22em;
padding:1em;
}
#test img{
float:left;
width:5em;
height:5em;
margin:0 .5em 0 .5em;
}
Is this what you were looking for?

CSS Blocks same height with different image heights

I'm having some trouble styling my news articles. This is preview of what I'd like to have:
On the left you always have an image (width is always the same, height isn't). On the right you have some information and a button on bottom aligned with the image.
<div id="newsItemImage">
<img src="" alt="" />
</div>
<div id="newsItemOther">
<p></p>
<button></button>
</div>
Float left on both of the divs. But the height of the two div's isn't the same. How can I make them equal?
This is what I have now:
.newsItemPic
{
width:333px;
border:1px solid black;
float:left;
height:100%;
}
.newsItemOther{
width:860px;
border:1px solid red;
float:left;
height:100%;
}
They are next to each other but the right content is not the same height as the image. So the image that's supposed to be under comes up under the content.
JSFIDDLE: http://jsfiddle.net/ZhD9Z/
Fiddle
as image is not responsive and it has 200px absolute width, i created one container width:500px;
then righttext must contain button itself but button must be aligned width image bottom, so righttext height equals with image height and button positioned at bottom:0
.eachNewsBox
{
padding:10px;
width:500px;
background-color:gray;
display:block;
float:left;
margin-top:20px;
}
.imgbox
{
display:block;
float:left;
height:100%;
position: relative;
}
.imgbox img
{
max-width:200px;
border:1px solid #000;
float: left;
}
.button
{
width:100px;
height:20px;
line-height:20px;
background-color:#FFF;
text-align:center;
margin-bottom:0px;
color:#000;
position:absolute;
bottom:0;
}
.rightText
{
float:right;
font-size:10px;
max-width:242px;
padding-left:10px;
color:#FFF;
height: 100%;
left:210px;
}

aligning an element, inside an element

Looking at my attached image, I am trying to get the darker rectangle shape, containing an image (with red border), to be aligned at the bottom center of the lighter grey square.
I have tried setting the dark rectangle to position:absolute with a 0px bottom but then I lost my center alignment using margin:0 auto. I've also tried just using a vertical-align:bottom but still won't play ball!!!
What is the CORRECT way of doing this? One thing to bare in mind is that where I have used the sizes 170 x 105, these are actually unknown as they are dynamically produced elements, size is variable.
This is my code so far:
.item_preview { width:220px; height:200px; text-align:center; position:relative; background:#EEE; }
.item_preview_img_wrap { width:170px; height:105px; margin:0 auto; background:#CCC; vertical-align:bottom; }
.item_preview_img_wrap img { margin:0 auto; border:solid 5px #FF0000; }
<div class="gallery_item">
<div class="item_preview">
<div class="item_preview_img_wrap">
<img src="asf.jpg">
</div>
</div>
<div class="item_options">
options
</div>
<div class="item_info_header">
<div class="date">Date</div>
<div class="item">ID</div>
<div class="clear"></div>
</div>
<div class="item_info_main">
<p class="caption">Caption here</p>
<p class="subject"><strong>People:<strong> People here</p>
</div>
</div>
If you want to display image at bottom of light grey box then use CSS below:
.item_preview { width:220px; height:200px; text-align:center; position:relative; background:#EEE; }
.item_preview_img_wrap { width:170px; height:105px; margin:0 auto; background:#CCC; }
.item_preview_img_wrap img { border:solid 5px #FF0000; position: absolute; left: 50%; bottom: 0px margin-left: -halfofimagewidth }
Note: -halfofimagewidth is half of size of your image for example if your image is 100px and border is 5px then it should be -55px
If you want to display image at bottom of dak grey box at center then use CSS below:
.item_preview { width:220px; height:200px; text-align:center; background:#EEE; }
.item_preview_img_wrap { width:170px; height:105px; margin:0 auto; background:#CCC; position: relative; }
.item_preview_img_wrap img { border:solid 5px #FF0000; position: absolute; left: 50%; bottom: 0px margin-left: -halfofimagewidth }
Let me know if you still find any issue
Is the width of the inside box always going to be relative to the outer box? If so you could use a percentage for your left value like so...
http://jsfiddle.net/hcharge/zYprr/
Write like this:
.item_preview_img_wrap {
width:170px;
height:105px;
position:absolute;
left:50%;
margin-left:-85px;
bottom:0;
background:#CCC;
}