how to fit an image in footer - css

i want to put an image in footer (jquery mobile),
how to make that image fit to width of footer (different width of smartphone)
Here is my code :
<div data-role="footer" data-position="fixed">
<div class="adsspace"><img id="myads" src="ads.gif"></div>
</div>
Here is my css example
#media only screen and (max-device-width:767px){
.adsspace{
width:767px;
height:50px;
}
.adsspace div, .adsspace img {
position:relative;
max-width: 767px;
max-height: 50px;
}
}
my ads.gif has width and height : 1200 * 180 px
thank you

You may try with:
.adsspace {
width:100vw;
}
It will grant the image width always adapt to the browser's width. However, you must take into account that the image height will increase as well. If you need to work with fixed dimensions, you can try with the object-fit rule, like that:
#myads {
width:1200px;
height:50px;
object-fit: fill;
}
And then you'll need to apply it as well in the media queries, specifying the new dimensions.
In case the original dimensions of the image file don't match exactly with the footer proportions, then the image will appear distorted. In that case object-fit: cover could be more suitable:
#myads {
width:1200px;
height:50px;
object-fit: cover;
}
I hope it helps!

#media only screen and (max-device-width:767px){
.adsspace{
width:100%;
height:100%;
}
.adsspace div, .adsspace img {
position:relative;
}
}
<div data-role="footer" data-position="fixed">
<div class="adsspace"><img id="myads" src="http://1.bp.blogspot.com/-GQ4s4jpyCOE/T4Zi23RtGFI/AAAAAAAAAr8/tdnhfESXJRM/s320/google+giant+face.png"></div>
</div>

Related

Make div have image height while image is loading. (avoid repaint)

Browser firstly is loading div with height 0,
and only after makes height equals image height.
Here are the screen shots : https://puu.sh/vR0Gp/10233ce94d.png
I want to make height as image height from the beginning to avoid repaints.
Here is the page: http://a4004cc1.ngrok.io/banner1.html
html of the banner:
<div class="home-top-box">
<div class="banner">
<img src="mobile-main.jpg" width="750" height="500">
</div>
</div>
css of the banner:
.home-top-box .banner{
position:relative;
height:auto;
width:100%;
display: inline-block;
}
.home-top-box .banner img{
width:100%;
}
Tried changing height to 100%, using min-height - those still didn't solve the problem.
Try changing this so that the parent has an inner padding that matches the image aspect ratio. http://i.imgur.com/2viiD35.png http://i.imgur.com/7k8uszJ.png
.home-top-box .banner {
position: relative;
height: 0;
/* width: 100%; */
/* display: inline-block; */
padding-bottom: 66.6%; /* (500 / 750) * 100 = 66.6% */
}
If you know the image aspect ratio, then you could recalculate your height using jQuery:
$.ready(function(){
$("div.banner").height($("div.banner").width()/750*500);
});
You shold take in account some padding, margins and borders, or make them zero if possible.

Crop image to size of <div>

I have a little problem with some of the images on my website. I have a 280x310 div-container, but all of my images are bigger than this container. I'd like to maintain the aspect ratio of the images, and fit them to the full height of the div, cropping any extra parts of the image on the left and right (keeping the image centered). Is this possible in css? Thanks for your help.
Add this to your css code
.imgfit { width: 250px; height:500px; object-fit: cover}
Object-fit will do the job for you.
Place the image as a background image for the div and use background-size: contain (to see all of the image) or background-size: cover (to scale and crop)
Check out Set size on background image with CSS?
This can be achieved with a little bit of jQuery:
By setting overflow:hidden to the div containing the image, and height:100% to the image, it will be resized to fill the height of the div, without distortion. Then we use jQuery to center the image with position:absolute.
(http://jsfiddle.net/kK4ZV/)
HTML:
<div class="container">
<img src="http://placehold.it/350x150">
</div>
<div class="container2">
<img src="http://placehold.it/350x150" class="imageid">
</div>
<div class="container2">
<img src="http://placehold.it/600x300" class="imageid">
</div>
CSS:
.container {
border:1px solid red;
width: 280px;
height:310px;
}
.container2 {
border:1px solid blue;
width: 280px;
height:310px;
overflow: hidden;
position:relative;
}
.container2 img {
height:100%;
}
.js-fix {
position:absolute;
top:0;
left:50%;
}
jQuery:
$(".imageid").each(function(){
var hWide = ($(this).width())/2; //half the image's width
hWide = '-' + hWide + 'px';
$(this).addClass("js-fix").css({
"margin-left" : hWide,
});
});
(jQuery code borrowed from here)
position: fixed;
x-overflow: hidden;
max-width: 100%
You can do this using html:
<img src='image.jpg' alt='Image' width='240' height='310'/>
You can view more of this at http://www.w3schools.com/tags/att_img_height.asp
Also, using css you can make a class or ID for the image itself:
img.sized{
height:310px;
width:210px;
}
Then use the class in your img tag:
<img src='image.jpg' class='sized'/>
You can read more about CSS sizing on http://www.w3schools.com/css/css_dimension.asp

Crop an image to square using percentages and max widths

Working a responsive site, so I cannot use set widths.
I need pictures to all crop to square. I cannot define the exact measurements because it also needs to have max-width:100% in order to make it a responsive image which adjusts it's sized relative to the container (which is relative to the width of the browser).
I've seen a lot of solutions that suggest using background-image but this not possible, it must be an img tag. It also must work in IE8.
Any ideas?
I currently have:
.views-field-field-photo img {
width: 100%;
height: auto;
}
<div class="field-content">
<img src="imagehere" >
</div>
using padding-bottom along with positioning and overflow:hidden you can create a responsive square container:
.field-content{
width:80%;
padding-bottom:80%;
margin:1em auto;
overflow:hidden;
position:relative;
background:#000
}
.field-content img {
position:absolute;
width:auto;
min-width:100%;
min-height:100%;
}
DEMO
jQuery DEMO center images when scaling
I tidied up some of the js allowing multiple images to be scaled and put 4 images on a simple grid
You can do something like overflow:hidden
I've made a square of 100px you can define your own size.
HTML
<div id="frame">
<img src="your image"/>
</div>
CSS
#frame{
width:100px;
height:100px;
overflow:hidden;
}
#frame img{
width:auto;
height:100%;
min-width:100px;
min-height:100px;
}

two pictures align exactly the width of containing element

I have two banner images, each of them has the same height but different width. Each one of them is nested in <a> tag (to make the images open a link) and the <a> tags are nested in <div> tag.
My problem is, I need these two images to sit next to each other and automatically adjust to the width of the <div> tag so that they fill exactly 100% of the <div> width, keeping the ratio of the individual image widths the same. The div tag is fluid (it resizes with the size of the screen) and I'd like these two images to be automatically adjusting so that they always fill exactly 100% of the div width. How do i do this using css.
Here is my html:
<div class='banner'>
<a class='mainBanner' href='Help.php?title=Help'><img src='banner1.png' alt='mainBanner' /></a>
<a class='openAccount' href='Profile.php?title=Registration'><img src='banner2.png' alt='openAccount' /></a>
As long as your images are not dynamic (ie. you know the widths in advance), you could do it like this:
<div class='banner'>
<a class='mainBanner' href='#'><img src='http://placekitten.com/200/200' alt='mainBanner' /></a>
<a class='openAccount' href='#'><img src='http://placekitten.com/300/200' alt='openAccount' /></a>
</div>
.banner {
display: table;
width: 100%;
}
.banner a {
display: table-cell;
}
.banner a:first-child {
width: 40%; /* this image is 200px wide */
}
.banner a:last-child {
width: 60%; /* this image is 300px wide */
}
.banner a img {
width: 100%;
height: auto;
}
The combined widths of the sample images is 500px, so your percentages are 200 / 500 = .4 or 40% for the first and 300 / 500 = .6 or 60% for the second.
I would use percentage and float the images like so DEMO http://jsfiddle.net/kevinPHPkevin/6H4cV/
.clear {
clear:both;
width:100%;
}
.banner {
width:100%;
background:#000;
}
.mainBanner img{
width:70%;
background:#ff0;
display:block;
float:left;
}
.openAccount img {
width:30%;
background:#ccc;
display:block;
}
You'll have to use (or simulate) tables! :D
I didn't touch much of your html. I removed the white-space between the 2 <a> and I enclosed it in another div (with the class derp).
CSS :
div.derp {display:table; border:1px solid green;width:100%}
div.banner {background:light-blue;border:1px solid blue;display:table-row}
a {display:table-cell;border:1px solid red;}
img {display:block;width:100%}
And I made a demo too!
I would use:
.banner {
display: table;
width: 100%;
}
.banner a {
display: table-cell;
}
.banner a img {
width: 100%;
height: auto;
}
this way the two image width can have always different widths, as long as they have the same hight.
demonstration - resize result display to see the effect.
Hope it helped you!
height the same, different widths?
ok, so if you now two widths of this images too just make the percent and float left. For example if one image is 200px wide and another 300px wide just give 40% and 60% for both a (don't forget to add them also display block or even display inline block without float left) and give width 100% for both img's. if you don't know the width, you need to use javascript unfortunately.
very pseudocode because I am really tired.
a1.width = img1.width/(img1.width+img2.width)*100+%
a2.width = img2.width/(img1.width+img2.width)*100+%
and of course a - display:block and img's width:100%.
or something like this if you would like I can write it tomorrow but I am not sure is this answer you are looking for, so I am not doing it now.

CSS: navigation bar to expand to the whole page height

Im not too great at CSS but hopefully someone on here can help. I have the following mockup. (i have stripped out my content to make it easy to view)
<body>
<div id="container">
<div id="header"></div>
<div id="body">
<div id="navBar"></div>
<div id="mainContent"></div>
</div>
<div id="footer"></div>
</div>
</body>
my CSS is as follows:
html,
body {
margin:0;
padding:0;
height:100%;
}
#container {
min-height:100%;
position:relative;
}
#header {
background:#ff0;
padding:10px;
}
#body {
padding:10px;
padding-bottom:60px; /* Height of the footer */
}
#footer {
position:absolute;
bottom:0;
width:100%;
height:60px; /* Height of the footer */
background:#6cf;
}
now im unsure as to how to get the "navBar" to be the page height. I've tried adding height: 100% but that doesnt work.
Thanks,
Matt
Giving an element height: 100% will give it a height equal to that of its containing element, which in your case is #body. Since body in your example is only as big as it needs to be to hold its content, #navBar will be 100% of that height.
To fix this, you can make #container and #body height:100% to make them as tall as tho body tag, which takes up the whole page:
#container {
height:100%
}
#body{
height:100%;
}
In the interest of completeness, you could also set the top and bottom of #navBar:
position: absolute;
top: 20px;
bottom: 60px; /* height of footer */
To understand the difference, play around with This JS Fiddle. Mess around with the height and top, bottom, position properties to see how your changes affect the layout; just don't use both positioning methods at once!
Your issue appears to be that each parent DIV all the way up to the BODY tag must explicitely have a height of 100% for #navBar to have 100% height. This means you would also have to set the height of #body to 100% as well, since it is the parent container of #navBar.
Have a look at this site - I assume you want a two column layout - this site will show you how to do what you want. Hope it helps.

Resources