To display the image circular even the image is not loaded - html

I am displaying the images in a circular way inside a div tag like this:
My issue is: Suppose an image(ex 2nd image) is not loaded/ present. Even then it should display it in a circular manner. But it is displaying in a square like below image:
I want image tag to be circular even though the image is not present.
Fiddle link

Then also it should display in circular. But it is displaying as square like below image
I'd argue that you probably want to fallback to some other image or some other placeholder. You can achieve it like so:
<img src="https://material.angul.io/assets/img/examples/shiba2.jpg"
class="company"
onerror="this.src='https://via.placeholder.com/70x70'">
Note the onerror. You can attach a handler function to do more complex things like hiding the img element, and showing some other placeholder element.

You can use CSS clip-path to force a circular cut-out, which will then apply to any image that you try to put in (good or broken).
Demo:
.image { width: 60px; height: 60px; }
.clip-circle { clip-path: circle(30px at center); }
<div style="background: #ffddcc">
Broken images: <img src="does-not-exist.png" class="image">
<img src="does-not-exist.png" class="image clip-circle">
</div>
<br />
<div style="background: #ffddcc">
Good images: <img src="https://via.placeholder.com/60x60" class="image">
<img src="https://via.placeholder.com/60x60" class="image clip-circle">
</div>
Not sure if this gives a better display than the other answers... but at least it answers the question :-) Also, browser support for this is not quite universal: see https://caniuse.com/#feat=css-clip-path.

In this scenario you have to use Javascript because you can't know with HTML/CSS if an image is broken
So here is a solution to either hide the image or replace it with another image just like #MrSaints has mentioned
To Hide:
<img src="Error.src" onerror="this.style.display='none'"/>
To Replace:
<img src="Error.src" onerror="this.src='fallback-img.jpg'"/>
Javascript solution for multiple broken images:
document.addEventListener("DOMContentLoaded", function(event) {
document.querySelectorAll('img').forEach(function(img){
img.onerror = function(){this.style.display='none';};
})
});
<img src="error.src">
<img src="error.src">
<img src="error.src">
<img src="error.src">

You can put the images in div and apply the radius on the div by adding overflow:hidden
.img-container{
border-radius: 50%;
height:70pt;
width:70pt;
float: left;
overflow:hidden;
margin:5px;
}
img{
height:70pt;
width:70pt;
}
<div class="img-container"><img src="https://material.angular.io/assets/img/examples/shiba2.jpg" class="company"></div>
<div class="img-container"><img src="https://material.angular.io/assets/img/examples/shiba2.jpg" class="company"></div>

AFAIK we can't make the invalid image tag as circular. But we can do some workarounds by wrapping the img tag in a div and then specifying border-radius: 50%; overflow: hidden to the div and enclose the image within.
.company {
border-radius: 50%;
height: 100%;
width: 100%;
}
div {
border-radius: 50%;
height: 70pt;
width: 70pt;
float: left;
overflow: hidden;
border: 1px solid #555;
}
<div><img src="https://material.angular.io/assets/img/examples/shiba2.jpg" class="company">
</div>
<div>
<img src="https://material.angular.io/assets/img/examples/shib2.jpg" class="company">
</div>
JSFiddle

You can tackle this issue with JS as #MrSaints is showing, but also this can fail as well. I was thinking more as a CSS solution and defaulting this to a color/nothing, but keep the circle shape.
.circle-image {
height:70pt;
width:70pt;
float: left;
border-radius: 50%;
margin: 5px;
background-size: cover;
background-position: center;
background-color: transparent;
background-image: url('https://material.angular.io/assets/img/examples/shiba2.jpg');
box-shadow: 0px 1px 3px rgba(0,0,0,0.3);
box-sizing: border-box;
}
.broken-image {
background-image: url('https://material.angular.io/assets/img/examples/shiba2.jpgssss');
}
<div class="circle-image">
</div>
<div class="circle-image broken-image">
</div>

Related

Div is not affected by css

I'm using html and css to make a website.
I made a div and assigned a class to it. I called that class profile_pic. And when I make changes in CSS to that class nothing changes. That div with a class is not being affected by CSS and I don't know why.
If I put the class in an img tag, then the changes will apply. But when I put the class back in that div, changes disappear. Can someone please explain what's happening?
.profile_pic {
width: 50px;
height: 50px;
object-fit: cover;
border-radius: 25px;
display: inline-block;
}
<div class="profile_pic">
<img src="https://via.placeholder.com/100">
</div>
<div>
<img class="profile_pic" src="https://via.placeholder.com/100">
</div>
The changes do apply. You can see this if you inspect the elements with your browser. The reason they don't seem to in the first example is because you aren't constraining the image to the div. If you do that with, for example, overflow: hidden, you then see that it works.
One minor issue is that since object-fit applies to "replaced elements" such as images, and not structural elements like divs, your image position is shifted in the first case. You could remedy this by applying width of 100% to the image.
.profile_pic {
width: 50px;
height: 50px;
object-fit: cover;
border-radius: 25px;
display: inline-block;
overflow: hidden;
}
.profile_pic img {
max-width: 100%;
}
<div class="profile_pic">
<img src="https://via.placeholder.com/100">
</div>
<div>
<img class="profile_pic" src="https://via.placeholder.com/100">
</div>
Showing an alternative approach using clip-path:
.profile_pic {
display: flex;
width: 50px;
height: 50px;
clip-path: circle(25px);
}
<div class="profile_pic">
<img src="https://via.placeholder.com/100">
</div>

How to fit image so that there will be no white spaces beside my image?

I am limited to use only html and css for this project. I try extending the width but that leaves me having horizontal scroll and I don't want to do that. I also try using px as a measurement but that doesn't work either.
<img src="img/gallery.jpg" style="width:100%;height:90%;white-space:nowrap">
https://i.stack.imgur.com/wsxrk.png
use width:100% as shown below:
img{
width:100%
}
You need to use object-fit property on your image. Here is a demo:
.container {
width: 500px;
height: 500px;
border: 1px solid #000;
}
.container img {
width: 100%;
height: 100%;
object-fit: cover;
}
<div class="container">
<img src="https://via.placeholder.com/350x150" alt="">
</div>
There are a few known way of removing white spacing. Here are a the best two, IMO:
Set the font size of the parent to be 0px:
div {
background-color: orange;
margin-bottom: 20px;
}
.nows {
font-size: 0px;
}
<div>
<img src="https://via.placeholder.com/150x150">
<img src="https://via.placeholder.com/150x150">
</div>
<div class="nows">
<img src="https://via.placeholder.com/150x150">
<img src="https://via.placeholder.com/150x150">
</div>
Use comments to mitigate the white space in the code
div {
background: orange;
margin-bottom: 20px;
}
<div>
<img src="https://via.placeholder.com/150x150">
<img src="https://via.placeholder.com/150x150">
</div>
<div><!--
--><img src="https://via.placeholder.com/150x150"><!--
--><img src="https://via.placeholder.com/150x150"><!--
--></div>
body {
margin:0px;
padding:0px;
}
I posted the same problem on other forum and this works. Thanks for the help.

Border-radius does not work

I am trying to make circular borders for profile pictures however I can't get the CSS to work. My CSS and HTML code is shown below
.author-image {
width: 50px;
height: 50px;
border-radius: 50%;
}
<div class="author-info">
<img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=Avatar&w=150&h=150" class="author-image">
</div>
when I try this on jsfiddle I only get a square area instead of a circle. What I'm I missing here?
You don't have an image in the src. In most browsers it would show the "no image found" icon. It worked as soon as I added one.
<div class="author-info">
<img src="http://combiboilersleeds.com/images/image/image-3.jpg" class="author-image">
</div>
.author-image {
border-radius: 50%;
height: 50px;
width: 50px;
}
It is the "border" that covers empty images that is causing the problem.
By giving your image a background color for example (or a valid image path; as you have done), you can see that the border rounding is working ok.
.author-image {
width: 50px;
height: 50px;
border-radius: 50%;
background-color:#555;
}
<div class="author-info">
<img src="" class="author-image">
</div>

Cannot remove borders on images - css

I'm making a simple game for my coding course and have been stumped by this strange outline around my images. At first I thought it was Bootstrap 3, but when I plugged the bare bones into a jsfiddle I've got the same outline. Note that this is not the thumbnail border that gets set in thumbnail images. I've thought about overwriting some border # rule but haven't a clue as to what to try.
I've redone the images thinking this might be some artifact of Inkscape, but nope. Any help in either removing the border or making it transparent would be appreciated.
css, note the commented out attempts:
#tommy img {
background: url(http://s32.postimg.org/4fdqh7dxh/tommy200.png);
height: 200px;
width: 200px;
/*
border: transparent !important;
background: transparent;
border-width: 0 !important;
border: none !important;
border: none;
border: 0px;
border-color: #7A45D2 !important; attempt to at least affect the darn thing.
*/
}
and the bit of html:
<div id="tommy" class= "theGroup player-up">
<p>Tommy</p><img>
</div>
the jsfiddle is here: fiddle
The border is coming from you using an img tag without a src specified and the background set as an image. There are two ways you could fix this:
1) Keep setting the image via the background url, but use a different element (probably a div).
HTML:
<div id="tommy" class= "theGroup player-up">
<p>Tommy</p>
<div/>
</div>
CSS:
#tommy div {
background: url(http://s32.postimg.org/4fdqh7dxh/tommy200.png);
height: 200px;
width: 200px;
}
2) Keep using an img tag, but set the image via the src attribute instead of the background.
HTML:
<div id="tommy" class= "theGroup player-up">
<p>Tommy</p>
<img src="http://s32.postimg.org/4fdqh7dxh/tommy200.png"/>
</div>
CSS:
#tommy img {
height: 200px;
width: 200px;
}
HTML:
<div id="tommy" class= "theGroup player-up">
<p>Tommy</p><img src="http://s32.postimg.org/4fdqh7dxh/tommy200.png">
</div>
CSS:
#tommy img {
height: 200px;
width: 200px;
}
Create a transparent gif and save it in your img folder. Then use this code. Works like a charm, gone are the ugly borders.
<div>
<img src="img/transparent.gif" id="tommy" class="theGroup player-up">
<p>Tommy</p>
</div>
#tommy {
background: url(http://s32.postimg.org/4fdqh7dxh/tommy200.png);
height: 200px;
width: 200px;
}

Can multiple imgs form an image background?

Be so kind as to look at this theme here. Look at that block of people's faces in the footer, faded, behind the 'create a nice banner like this with a few simple clicks' text. Can this effect be achieved with multiple img tags?
Now, the way they've done it is simple. They literally have one image of all of those cropped faces which they've set to 20% opacity in photoshop and simply set that as the background image of the div. I want to do the same effect, but with database-derived images, so that approach is useless to me.
So can I do the exact same effect, but through multiple img tags? Something that will work in IE 7,8,9? Something to do with z-indexes, perhaps?
Just use multiple image tags with CSS's opacity to do it. Something like this:
HTML
<img src="..." />
<img src="..." />
...
CSS
img {
opacity: 0.5;
vertical-align: middle;
}
http://jsfiddle.net/6Y5Qg/
You might have to use some tricks for older versions of IE, but all modern browsers will support this just fine.
Stacking divs would be the easiest... here's and example:
<style>
#wrap {}
#imgWrap {
padding: 0;
overflow: hidden;
height: 200px;
width: 500px;
z-index:1;
}
#imgWrap img {
float: left;
}
#container {
color: white;
margin-top: -200px;
width: 500px;
height: 200px;
z-index: 20;
background-color: black}
#container h1 {
margin: 0;
color: white;
padding-top: 50px;
text-align: center;
opacity: 1;
}
</style>
<div id="wrap">
<div id="imgWrap">
<img src="http://mymobileengagement.com/images/social-icons/pinterest/pintrest.png">
<img src="http://mymobileengagement.com/images/social-icons/pinterest/pintrest.png">
<img src="http://mymobileengagement.com/images/social-icons/pinterest/pintrest.png">
<img src="http://mymobileengagement.com/images/social-icons/pinterest/pintrest.png">
<img src="http://mymobileengagement.com/images/social-icons/pinterest/pintrest.png">
<img src="http://mymobileengagement.com/images/social-icons/pinterest/pintrest.png">
<img src="http://mymobileengagement.com/images/social-icons/pinterest/pintrest.png">
<img src="http://mymobileengagement.com/images/social-icons/pinterest/pintrest.png">
<img src="http://mymobileengagement.com/images/social-icons/pinterest/pintrest.png">
</div>
<div id="container">
<h1>TEST</h1>
</div>
</div>