I am working on a 4x3 image gallery with pictures from a database.
All pictures have different sizes.
Using Bootstrap and CodeIgniter I make rows and columns for every image.
Now I want wide pictures to fit the height of the row, center horizontally and hide the horizontal overflow.
I also want that tall pictures fit the width of the column, center vertically and hide the vertical overflow. Just like Facebook.
margin-left: auto;
margin-right: auto; don't center the image.
My index page:
<h1><?=$title?></h1>
<?php $i = 1; foreach ($photos as $photo) {
if ( $i % 3 == 1 ) { ?>
<div class="row gallery-row">
<?php } ?>
<div class="col-md-4">
<img class="gallery-image" src="<?php echo site_url(); ?>assets/images/<?php echo $photo['photo_name'];?>">
</div>
<?php if ( $i % 3 == 0 ) { ?>
</div>
<?php }
$i++;
} ?>
My css:
.row.gallery-row{
height: 25%;
}
.gallery-image{
height : 100%;
overflow:hidden;
display: table-cell;
text-align: center;
}
The closest I get is with the CSS above: The images fit the height of the row and overflow is hidden.
I still want the images to center and if the images are taller then wide they should prioritize fitting the width.
Here is what the page looks like
The bottom left picture has a woman on it but it doesn't show because it isn't centered.
Solution: I changed my css to :
.gallery-image{
height : 100%;
width : 100%;
object-fit: cover;
}
My page now looks like this:
Adding some padding in between the rows will make it look just like facebook.
Maybe adding object-fit in your .gallery-image css is what you want.
Related
I have some code to output images in Wordpress like this...
<div class="col-md-2">
<?php $image = get_field('artist_photo');
if( !empty($image) ):
// thumbnail
$size = 'medium';
$thumb = $image['sizes'][ $size ];
?>
<img src="<?php echo $thumb; ?>" alt="<?php echo $alt; ?>" class="img-responsive" />
</div>
<?php endif; ?>
The problem is, all of the images have different heights (based on their proportions at original upload}.
Is it possible to make them have a uniform height whilst remaining responsive?
Here are two ways to constrain the height of an image in CSS.
First, you can set a maximum height. So something like:
.col-md-2 img {
display: block;
max-height: 300px;
width: auto;
height: auto;
}
Second, you can display the image as a background image and then use the background-size property to constrain it. In your markup display the image using:
<div class="image" style="background: url(<?php echo $thumb; ?>) no-repeat center;" />
And style it like so:
.col-md-2 {
height: 300px;
}
.col-md-2 div.image {
height: 100%;
width: 25%; // for example, assuming you want 4 images per row
background-size: constrain;
}
I'm not sure how this will work in the context of bootstrap, but option 2 might be your only option if you can't specify an explicit height for the image or its parent.
There are more devils in the details and the specifics will depend on your situation, but hopefully this is helpful.
I'm trying to create a full-width responsive portfolio grid. I set the width of the items using calc(), and set the thumbnail image to take up 100% of thumbnail div using:
`img.attachment-portfolio-thumb{
width:100% !important;
height:100% !important;
}`
This works, aside that there is about an extra white 10 pixels below each image, inside each div.portfolio-list, and I can't figure out where it is coming from and how to get rid of it. In "inspect element", when hover over the div.portfolio-list, the div takes up the entire space 100%, so it's something inside the div which is causing the extra space.
What I noticed is that in "inspect element" mode the <a> tag (that the portfolio-thumb thumbnail is in, in the Pods Template), has the following dimensions - and the white extra space if part of it: 315px x 26px (315px is the width of the div.portfolio-list as this browser width).
Link to visual image: The left is on hover, as you can tell the overlay includes the bottom empty 10px. The right bottom you see a white strip.
http://prntscr.com/5yedsp
Below is the code for the grid:
Pods Template:
<div class="portfolio-list">
<div class="overlay">
<p>{#post_title}</p>
{#post_thumbnail.portfolio-thumb}
</div>
PHP:
<section class="portfolio_home_inner">
<section class="portfolio_home">
<?php
echo do_shortcode ('[pods name="portfolio" template="portfolio-list"]');
?>
/ Add Image size for Portfolio List
if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails' );
// additional image sizes
add_image_size('portfolio-thumb', 300, 300, true ); // (cropped)
}
CSS:
div .portfolio-list{
float:left;
width: calc(20%);
overflow: hidden;
position: relative;
display: inline-block;
}
/*Wordpress default selector for new image size*/
img.attachment-portfolio-thumb{
width:100% !important;
height:100% !important;
}
div .portfolio-list p{
display: none;
color:#000;
}
div .overlay:hover:after {
content: ' ';
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
background-color: rgba(234,79,38,.85);
}
Would anyone know what is causing the extra 10px underneath the image?
Thank you!
You image has a margin on it of 32px 0 15px (inspect element with chrome), for the class ".image_pic". Setting the image_pic "margin-bottom : 0;" should sort it out for you, perhaps with the important tag.
I have a little issue here I cant seem to solve.
I am making a website which shows uploaded images on a page (images are uploaded by users and are not the same size). But unfortunately it does not look good at all this way because it show the entire image and if 2 don't fit next to each other on the page the second one gets pushed down to a newline and leaves a huge gap next to image 1. What I want to accomplish is something like this flickr.
How would I be able to accomplish this?
I have all the images inside a DIV name images and all the image DIVS are in the DIV inhoud
HTML and PHP:
<div id="inhoud">
<?php
include('dbc.php');
$sql = "SELECT * FROM tbl_images";
$result = $dbc->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
?>
<div class="image"><img src=<?php echo "uploads/".$row['link'] ?>></img><div class="info"><p><?php echo "By " . $row['user'] ?></p></div></div>
<?php
}
} else {
echo "0 results";
}
$dbc->close();
?>
</div>
CSS:
#inhoud{
width: 100%;
margin-left: auto;
margin-right: auto;
background-color: grey;
padding-top: 100px;
}
.image{
vertical-align: top;
display: inline-block;
}
.image img{
max-width: 100%
}
Well, if you want to do it like Flickr--which keeps a set row height and has variables image widths--then you should be able to do that with CSS.
What you need to do is give the images a set height, and then set width to auto. So, if you want each row to have a height of say 20%:
#inhound {
height:900px; /* if you want to use a percentage make sure its parent has a height */
}
.image img {
height:20%;
width:auto;
}
It's dependent on how exactly you want to display them, of course, but this is the basic principle.
E.g. I have a link
http://img.youtube.com/vi/aOPGepdbfpo/0.jpg
for a youtube video thumbnail:
And I would like to remove the black top and bottom border so I get a picture like this:
Could it be done using PHP function javascript/jQuery or maybe youtube api itself?
YouTube offers images that don't have the 4:3 ratio black strips. To get a 16:9 video thumbnail with no black strips, try one of these:
http://img.youtube.com/vi/<insert-youtube-video-id-here>/mqdefault.jpg
http://img.youtube.com/vi/<insert-youtube-video-id-here>/maxresdefault.jpg
The first one mqdefault comes as a 320x180 pixel image.
The second one maxresdefault comes as a 1500x900 pixel image, so it would need resizing to use as a thumbnail. This is a nice image but it isn't always available. If the video is of a low quality (less than 720p I'd imagine, not exactly sure) then this 'maxresdefault' becomes unavailable. So never rely on it.
Use it as a background image, center it and change height.
http://dabblet.com/gist/4012604
.youtubePreview {
background:url('http://img.youtube.com/vi/aOPGepdbfpo/0.jpg') center no-repeat;
height:204px;
width:480px;
}
If you want it with a flexible width, use this:
HTML
<div class="thumb">
<img src="...">
</div>
CSS
.thumb {
overflow: hidden;
}
.thumb img {
margin: -10% 0;
width: 100%;
}
To remove the black borders from the Youtube thumbnail we need not have to write a seperate code or CSS. Simply use the ytimg.com site, that stands for YouTube image, which fetches the images from YouTube, like thumbnails and icons as required which come from that domain.
Example shown below -
Original Image [With borders]
http://img.youtube.com/vi/JQ7a_8psCn0/hqdefault.jpg
With No borders/Strips
http://img.youtube.com/vi/JQ7a_8psCn0/mqdefault.jpg
OR
http://i.ytimg.com/vi/JQ7a_8psCn0/mqdefault.jpg
This is answer I gave for similar question, but it will solve your problem completely, just cut everything you don't want to be shown from the video with the wrapper div, this is done with html and css.
After searching the net a while for fix of this issue I came up with nothing, I think I have tried everything and nothing solved my problem. Then driven by my logic I just wrapped the iframe of the embedded youtube video in one div set overflow: hidden; to this div and made it's height 2px smaller then the iframe height (on my video there was black border in the bottom).
So the wrapper div is smaller then the iframe and with positioning it over the video you can hide the black borders you don't want.
I think that this is the best solution from everything I have tried so far.
From this example below try embedding just the iframe and you will see small black border at the bottom, and when you wrap the iframe in the div the border is gone, because the div is overlapping the iframe and it's smaller then the video, and it has overflow: hidden so everything that goes out of the div dimensions is hidden.
<div id="video_cont" style="width: 560px;
height: 313px;
overflow: hidden;">
<iframe id="the-video" class="extplayer" width="560" height="315" src="https://www.youtube.com/embed/EErx017kDHQ?&autoplay=0&rel=0&fs=0&showinfo=0&controls=0&hd=1&wmode=transparent&version=2;yt:quality=high;" frameborder="0" allowfullscreen></iframe>
</div>
In my case the border was with about 2px height so I made the wrapper div 2px smaller in height to hide the border, in your scenario if the border is on the top of the video or on the sides and/or with different dimensions, you have to make different dimensions for the wrapper div and position it good so it overlaps the video where the borders are and with overflow:hidden; the borders are hidden.
Hope this will help.
How youtube do it. There's lot of parameter in the image url.
https://i.ytimg.com/vi/XkOpbLBzPsY/hqdefault.jpg?custom=true&w=196&h=110&stc=true&jpg444=true&jpgq=90&sp=68&sigh=Gv-oyTIgA39e7UG01pZ2RiGbwSo
I'm not an expert, i was looking for a solution to remove the black bars of youtube video thumbnails, found a few solutions but didn't worked for me. Started experimenting with the solutions i found and came up with this.
https://jsfiddle.net/1fL2ubwy/
.row, .col, [class*="col-"] {
margin-left: 0;
margin-right: 0;
padding-left: 0;
padding-right: 0;
}
.row {
background: #f8f9fa;
margin-top: 20px;
}
.col {
border: solid 1px #6c757d;
padding: 10px;
}
.yt-thumb {
width: 100%;
height: 74%;
overflow: hidden;
}
.yt-thumb > img {
margin: -10% 0;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap-theme.min.css">
<div class="container">
<div class="row">
<div class="col-md-6 col-sm-6 col-6 card vd-block mb-3">
<a class="yt-thumb" href="https://www.youtube.com/embed/wb49-oV0F78" data-rel="lightcase">
<img class="aligncenter" src="https://img.youtube.com/vi/wb49-oV0F78/hqdefault.jpg" alt="thumbnail" width="100%" height="auto">
</a>
</div>
<div class="col-md-6 col-sm-6 col-6 card vd-block mb-3">
<a href="https://www.youtube.com/embed/wb49-oV0F78" data-rel="lightcase">
<img class="aligncenter" src="https://img.youtube.com/vi/wb49-oV0F78/hqdefault.jpg" alt="thumbnail" width="100%" height="auto">
</a>
</div>
</div>
</div>
I needed a responsive way of doing it, so let's consider this code is running on the window.addEventListener("resize");
We basically want to convert the 4:3 ratio to 16:9
<div id="video-item">
<img src="https://i.ytimg.com/vi/{videoId}/hqdefault.jpg" />
</div>
const videoItem = document.getElementById("video-item");
const img = videoItem.querySelector("img");
resize()
{
img.style.top = `${-(img.offsetHeight - (img.offsetWidth * 9 / 16)) / 2}px`;
videoItem.style.height = `${9 / 16 * videoItem.offsetWidth}px`;
}
#video-item
{
position: relative;
overflow: hidden;
}
#video-item img
{
width: 100%;
position: absolute;
}
BTW, you can also fallback to the calculated height for the image (in case the image wasn't fully loaded)
img.style.top = `${-((img.offsetHeight || (3 / 4 * img.offsetWidth)) - (img.offsetWidth * 9 / 16)) / 2}px`;
I want to center an image in an area, without resizing... I am using HTML.
Example:
I have an image <img src='img1.png' width='64' height='64'> - the image is actually 64x64. It displays perfectly.
Now, I have another image <img src='img2.png' width='64' height='64'> however, the image is not as big as it should be, its 32x32 - what happens here is it resizes the image to 64x64 and makes it look like $%^&.
How do I make images smaller then the desired width and height centered in the 'img' area without any resizing what so ever?
What you will need is something like this:
<div class="box">
<img src="whatever size image you'd like" />
</div>
And for the styling (in an external stylesheet, natch) you'd apply:
/* Image centering */
div.box {
border: 1px black solid;
height: 64px;
width: 64px;
background: #444;
display: table-cell;
vertical-align: middle;
}
.box img {
display:block;
margin: 0px auto;
}
This works for images with dimensions <= 64x64px, and is easily modifiable to work with larger pics. The key elements here are
set dimensions on the div
display as a table-cell (allows vertical align)
vertical align (aligns on the Y-axis w/out weird hacks)
display:block on the img element
margin: auto centers the image laterally
Solution without IE-unfriendly display:table-cell:
<!DOCTYPE html>
<style>
div {
line-height:64px; /* that's the secret sauce */
text-align:center;
width:64px; height:64px;
}
img {vertical-align:middle}
</style>
<div><img …></div>
You could try putting the image inside a DIV that is 64x64 and not specifying the image dimensions. Then you could style the div so its contents are centered and any overflow is hidden.
You can dynamically get an image size using the getimagesize() php function:
<?php
$size = getimagesize('imgX.png');
$height = $size[1];
$width = $size[0];
?>
<div style="text-align: center">
<img src="imgX.png" width="<?php print($width) ?>" height="<?php print($height) ?>" />
</div>
I've had to do something similar with 36x36 images. Users were able to upload any size but the thumbnails were only to show the center 36 square pixels.
Markup:
<li><div><span></span>
<img src="_media/objects/jessica-bowman.png" alt="Jessica Bowman" /></div>
<p>Jessica Bowman</p>
</li>
The span was just there to get rounded corners on the image, it's not necessarily needed.
CSS:
ul.recent-list li div {
position: relative;
float: left;
width: 36px;
height: 36px;
overflow: hidden;
}
ul.recent-list li div span {
position: relative;
z-index: 100;
display: block;
width: 36px;
height: 36px;
background: url("../_media/icons/icon-overlay.png") top left no-repeat;
}
ul.recent-list li div img {
position: relative;
top: -36px;
z-index: 0;
float: left;
}
JavaScript:
$(window).load(function() {
$("ul.recent-list div img").each(function() {
var moveX = ($(this).width() / 2 * -1) + 18;
var moveY = ($(this).height() / 2) * -1 - 18; // 18 is 1/2 the default offset of 36px defined in CSS
$(this).css({'top' : moveY, 'left' : moveX});
});
});
The solution is a simple bit of CSS + HMTL
<img src="transparentpixel.gif"
width="64"
height="64"
style="
background-image:url('path/to/image.jpg');
background-repeat:no-repeat;
background-position:center center;
" />
the transparentpixel.gif is a simple 1x1px transparent gif image
An img tag with width and height attributes is saying "stretch or shrink the image to this size regardless of its actual size". use something like:
<div style="text-align:center;">
<img src="x.jpg">
</div>
and no i don't know why text-align would work, but it appears to in my experience.
Use CSS to render the image using background:
<div style="background: url(img1.png) no-repeat center center; height: 64px; width: 64px;"></div>
This will show the image in the center, without scaling it.