Scale and center image in each div in this container - html

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

Related

How to center an image horizontally while pushing the bottom (unknown resolution) and keep centered if window is too narrow

Let's say I have an image of unknown resolution. I want to center it horizontally, even if the window is narrower than the picture, and push the bottom of the window to fit the height of this picture.
How can I achieve that with css only? (no javascript)
Obviously the picture will be in an < img > tag since this is the only way to push the bottom. Align it center is easy, the hard part is to keep it centered just like a background-position:center top because when simply centering this < img > tag it will hit the left border of the window instead of overflowing hidden and staying centered.
Thanks!
If you want to do this with the img tag you can use the following two codes:
object-fit:cover;
object-position:50% 50%;
These are pretty much the same as background-size and background-position.
For example how this would be done in code:
html,body {
width: 100%;
height: 100%
}
img {
width: 100%;
max-width: 100%;
max-height: 100%;
object-fit: cover;
object-position: 50% 50%;
float:left;
}
div {
width: 100%;
height: 100%;
background: #222;
display:flex;
color:#fff;
align-items:center;
justify-content:center;
}
<img src="https://www.rafaeldejongh.com/wp-content/uploads/2016/12/GlitchSkull1.jpg" alt="Rafael De Jongh - Glitch Skull"/>
<div>
<p>Other Content</p>
</div>
You can view this as well on JSFiddle
And in case you do want to have a full fluid background image that is set to 100% of the viewports width and height, you could also do this via the following code:
html,body {
width:100%;
height:100%
}
#img {
width:100%;
height:100%;
max-width:100%;
max-height:100%;
background-image:url(https://www.rafaeldejongh.com/wp-content/uploads/2016/12/GlitchSkull1.jpg);
background-size:cover;
background-position:center center;
background-repeat:no-repeat;
float:left;
display:inline-block;
}
#content {
width:100%;
height:100%;
background: #222;
display:flex;
color:#fff;
align-items:center;
justify-content:center;
}
<div id="img"></div>
<div id="content"><p>Other Content</p></div>
Also on JSFiddle
Feel free to ask if you have any further questions!
Voila:
https://jsfiddle.net/gwja6f6z/
HTML:
<div id=main>
<div id=imgwrp>
<img src="https://upload.wikimedia.org/wikipedia/commons/9/94/Ingres%2C_Self-portrait.jpg" border=0>
</div>
</div>
<p>
The picture stays center and does not hit the borders of the window when window is too narrow, and this text is pushed without knowning the height of the picture.
</p>
CSS:
html, body {
width:100%;
height:100%;
text-align:center;
overflow-x:hidden;
}
#main {
position:relative;
left:50%; /* This div is just a 0px width line in middle of window */
width:0;
overflow-x:visible;
text-align:center;
}
#imgwrp {
display:inline-block; /* Image container width is same as image itself */
position:relative;
margin:0 auto 0 auto;
}
#imgwrp img {
display:block;
position:relative;
margin-left:-50%; /* The image is pushed left by 50% its container width */
}

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 a image vertically center

I can't seem to get a image to align vertically in the header. It aligns horizontally but It is a little too far towards the top of the header.
The html:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="home_style.css">
</head>
<body>
<div class="header">
<div class="search_bar">
<input type="text" name="search" placeholder="Search magical instruments, tricks, books and more">
</div>
<div class="user_settings">
<img src="onebit_09.png" width="48px" height="48px">
</div>
</body>
</html>
The css:
html,body{
height:100%;
min-height:100%;
width:100%
min-width:100%;
}
.header{
margin-top:0;
margin-bottom:0;
height:10%;
width:100%;
background-color:#343430;
}
.search_bar input[type="text"]{
position:absolute;
left:20%;
top:4%;
width:27%;
padding:5px;
padding-right:50px;
outline:none;
border: 2px solid #9C4B8F;
border-radius: 5px;
background-color:#FBFBFB;
font-family: Cambria, Cochin, Georgia, serif;
font-size: 16px;
color:grey;
background-image: url('search.png');
background-position: 100% -10px;
background-repeat:no-repeat;
}
.search_bar input[type="text"]:focus{
background-color:#FFFFFF;
border-color:#333333;
}
.user_settings img
{
position:absolute;
top:5%;
left:95%;
height:48px;
width:48px;
margin:-24px 0 0 -24px;
}
The image that I'm trying to position is a little settings cog(user_settings) that is 48px wide and 48px high.
Here is a workaround:
#elementToAlignVertically
{
margin-top:50%;
transform:translate(0px,-50%);
-ms-transform:translate(0px,-50%);
-moz-transform:translate(0px,-50%);
-webkit-transform:translate(0px,-50%);
}
Basicaly you align the element 50% from the top and then use the translate to move it -50% of it's hight so it will center it self.
Probably you'll also have to set the height of the element for it to work or instead of percent use pixels that you want to move the element upwards.
there is a property vertical-align in css which can be valued at varied contents. Check this w3schools tuts.
Vertical-align property
I fixed the problem, I had forgotten to set the padding and margin of the html,body elements to 0. This caused a small white gap at the top of the screen that made it seem like the image was not positioned correctly. Sorry for the inconvenience the corrected code should look like:
html,body{
height:100%;
min-height:100%;
width:100%
min-width:100%;
margin:0;
padding:0;
}
Try using calc for that it will not work in all browser but in some of them will do:
.user_settings img
{
position:absolute;
top:5%;
left:95%;
height:48px;
width:48px;
margin-top: calc(50% - 24px);
margin-top: -webkit-calc(50% - 24px);
margin-top: -moz-calc(50% - 24px);
}
or you can try the margin auto:
.user_settings img
{
position:absolute;
top:0;
bottom:0;
left:95%;
height:48px;
width:48px;
margin-top: auto;
}
Or still my favorite and most reliable is to use some javascript to manipulate the position.
For example jQuery something like:
$(".user_settings img").css('top',$(".user_settings).height()/2-24);
Then make sure you also add a resize watcher like this:
$(window).on('resize', function(){
$(".user_settings img").css('top',$(".user_settings).height()/2-24);
});
In the near future (right now has like the 70% of the browser support) you can do this, a much simpler and elegant solution:
.container img{
width: 100%;
height: auto;
}
#supports(object-fit: cover){
.container img{
height: 100%;
object-fit: cover;
object-position: center center;
}
}

Thumbnail of a Portion of an Image While Maintaining Aspect Ratio

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;
}

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;
}