Looking for a way to create a CSS grid with non-square random sized images? I need help filling all the space inside a fixed size div. Any suggestions?
Check out my example: (blue represents the fixed size div and red shows the image inside it)
Looking for a way to create a CSS grid with non-square random sized images? I need help filling all the space inside a fixed size div. Any suggestions?
Check out my example: (blue represents the fixed size div and red shows the image inside it)
To achieve a responsive image layout using CSS, you can utilize the background-size: cover; property.
<style>
.img1 {
background-image: url(microsoft-logo.png);
}
.img2 {
background-image: url(google-icon.png);
}
.img3 {
background-image: url(Azend_Loggo.png);
}
.img-cover {
width: 50px;
height: 50px;
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
</style>
<div class="img-cover img1">
</div>
<div class="img-cover img2">
</div>
<div class="img-cover img3">
</div>
To achieve the desired effect, apply the following CSS declaration:
background-size: cover;
background-position: center;
There is a snippet of code that I have used in the past which allows you to specify the desired dimensions ...
<?PHP
function resizeImage($image,$desiredWidth,$desiredHeight) {
$imgInfo = getimagesize("img/photos/".$image);
$width = $imgInfo[0];
$height = $imgInfo[1];
if ($width > $desiredWidth) {
$percentage = ($desiredWidth / $width);
$width = round($width * $percentage);
$height = round($height * $percentage);
}
if ($height > $desiredHeight) {
$percentage = ($desiredHeight / $height);
$width = round($width * $percentage);
$height = round($height * $percentage);
}
$topMargin = ($desiredHeight-$height)/2;
$topMargin .= "px";
return "width=\"$width\" height=\"$height\" style=\"margin-top:$topMargin px;\"";
}
?>
You can then use it like this:
<div id="photo">
<img src="img/photos/<?PHP echo $photoFile; ?>" <?PHP echo imageResize($photoFile,200,150); ?> />
</div>
This method worked well for me and I hope it helps you too.
To style the image tag within a fixed div, consider using the following CSS code. It is important to note that the size of your images should be larger than the size of your div. For example, if your div is 100px in width, the minimum image size should be 100px or larger:
div > img {
width:100%;
overflow:hidden;
}
Since the question has been tagged with jQuery
, I think creating a custom jQuery plugin would be a good solution. Check out this JSFiddle demo for reference.
// Custom jQuery plugin 'containImg'
$.fn.containImg = function(){
this.each(function(){
// Declare variables
var $t = $(this),
$p = $t.parent('div'),
pw = $p.width(),
ph = $p.height(),
// Determine whether to adjust width or height
iw = (pw / ph >= 1) ? 'auto' : '100%',
ih = (iw == 'auto') ? '100%' : 'auto';
// Set dimensions
$t.css({
'width': iw,
'height': ih
});
});
}
// Implementing the plugin
$('img').containImg();
The concept behind this code is to adjust the image dimensions based on its parent div's aspect ratio. If the parent div is horizontally stretched, we set height: 100%
to maintain the aspect ratio, and vice versa for vertical parent divs.
If you have any suggestions for enhancing the code, feel free to share!
To achieve this effect, you can utilize CSS alone, however adding some JavaScript can enhance the functionality. Below is a simple code snippet for your reference.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title></title>
<style type="text/css">
div.image-box{display:block;position:relative;overflow:hidden;width:200px;border:1px solid #ccc;padding:0;height:200px;}
div.image-box img{width:100%;height:auto;margin:0; padding:0;margin-bottom:-4px;position:absolute;top:0;left:0;}
div.image-box.horizontal img{height:100%;width:auto;left:50%;}
div.image-box.vertical img{height:auto;width:100%;top:50%;}
</style>
</head>
<body>
<div class="image-box"><img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcS08-IWtcbvcehgeszlD2nI6s-OAungNCsLNIc-f3QjIOkvOq8abg" alt=""></div>
<div class="image-box"><img src="http://1.bp.blogspot.com/_e-y-Rrz58H4/TUS4qRlRUrI/AAAAAAAABQo/H322eTHRB2s/s1600/Man_Face.jpg" alt=""></div>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.image-box').each(function(){
if($(this).find('img').height() > $(this).find('img').width()){
$(this).addClass('vertical');
$(this).find('img').css('margin-top','-'+$(this).find('img').height()/2+'px');
}else{
$(this).addClass('horizontal');
$(this).find('img').css('margin-left','-'+$(this).find('img').width()/2+'px');
}
});
});
</script>
</body>
</html>
I trust this solution proves useful to you.
I am facing an issue with positioning images within a div to a span. The problem arises as the images are overlapping each other and I am uncertain about how to properly place each image when it is added. Below is the code snippet: The CSS: <style ty ...
Is it possible to check for internet connectivity on a mobile device? Can websockets be utilized to ping a server and determine if the connection is available? I am feeling frustrated as I believed there was a ping function in websockets for client-side u ...
My application features a multiselect dropdown menu that shows a list of countries. While this dropdown functions correctly in Chrome, it displays options differently in IE as shown below: https://i.stack.imgur.com/xhOmV.png I have attempted to adjust th ...
Welcome to my homepage where I showcase 3 movies taken from the store, each with an "add to fav" button. My goal is to have the selected movie appear on the favorites page when clicked. As a newcomer to Vue, any code or documentation suggestions would be h ...
I have integrated a stocks chart from HighStocks with gridster, where each gridster block is draggable. However, the stocks time slider gadget can also be dragged and resized. When I move the slider on top of a gridster widget, the entire widget moves alon ...
<ul class="level0"> <li class="level1" id="cat2441"></li> <li class="level1" id="cat2450"></li> <li class="level1" id="cat2455"></li> </ul> <div class="alles-zwei" id="new-cat2441"></div> <div ...
I'm having trouble creating a responsive dropdown menu. When I click on the dropdown items, nothing happens. Here's the code: <div class="navbar-header"> <button type="button" class="navbar-toggel" data-toggel="collapse" data-target="#m ...
After adjusting my timezone to +14 using a chrome plugin, I noticed that the calendar app is displaying incorrect disabled dates. You can view the issue here. This is the formula I'm currently utilizing to disable dates: disabledDate(time) { re ...
My current code for adding comments is as follows: <div id="2" class="789678"> <div id="dynamic2"> <span class="error" style="display:none">Please Enter Valid Data</span> <span class="success" style="display: ...
As a beginner in the world of coding, my goal is to create a time management website specifically tailored for school use. Despite copying this code multiple times, I have encountered an issue where it does not continue down the page, and I am unsure of th ...
I have developed a web component that includes a method to generate a copyright string: '<p>Copyright © 2020 John Doe<a href="https://www.example.com">. Terms of Use</a></p>' After creating the string, I conver ...
I've been trying to center multiple lines of text in a table cell using the table method, but no matter how many online guides and SO posts I follow, I just can't seem to get it right. What I'm aiming for is to have the text perfectly cente ...
When I make an AJAX call to invoke an action, pass my ViewModel to it, and then attempt to open a new view with a different ViewModel, the redirectToAction and return view methods are not functioning as expected. Other solutions I have come across only inv ...
I recently stumbled upon an interesting observation while coding a video player using HTML5. Surprisingly, I found that my code runs smoothly even without specifying the type attribute (which defines the mime type) in the source element as shown below. Co ...
I am currently developing an online visitor chat software using PHP and MySQL. My goal is to load the page when the submit button is clicked. Submit Button ID: send Visitor ID: vid Chat ID: cid Below is the snippet of code for an Ajax request that I hav ...
Is there a method to prevent the appearance of the white square known as "Focus to toolbar" in TinyMCE? Clarification: Upon pressing Alt+F10 in TinyMCE, a white square is outlined around a button on the toolbar. This allows navigation among toolbar butto ...
Hello there, I am attempting to retrieve all the child values from the parent div .clonedInput when the .send button is clicked. I also want the output to be formatted as shown below and placed in a temporary alert(); variable for code confirmation. //Exa ...
I am having trouble connecting to the MongoDB server using the link provided below. I have double-checked the password and dbName, but it still won't connect. Can someone please assist me with this? const mongoose = require('mongoose'); ...
Currently, I'm working on rotating a mesh by 90 degrees within Three JS. Below is an image illustrating the current position: My goal is to have the selected mesh rotated parallel to the larger mesh. I attempted to rotate the matrix using the follow ...
ETA: Take a look at the JSFiddle to see for yourself, remember to check your console: http://jsfiddle.net/GZNwK/1/ Currently, my goal is to fetch data from a subreddit using the IMGUR API: $.getJSON('http://imgur.com/r/cats.json?callback=?',fun ...