Div is not affected by css - html

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>

Related

DIsplay: flex takes image height but overflows when text is added

My display: flex container takes my images height but when I add text it overflows the image. Basically the inspector takes the images height but forgets about the added text.
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.book-right-side {
display: flex;
gap: 1rem;
width: 100%;
}
.attraction-card {
width: 100%;
/* height: 100%; */
}
.book-right-side img {
border-radius: 12px;
height: 100%;
width: 100%;
}
<div class="book-right-side">
<div class="attraction-card">
<h4>Beijing City</h4>
<p>Teeest</p>
<img src="https://upload.wikimedia.org/wikipedia/commons/d/dd/Beijing_montage.png" alt="Beijing City" />
</div>
<div class="attraction-card">
<h4>Zhangjiajie Forest</h4>
<p>Teeest</p>
<img src="https://upload.wikimedia.org/wikipedia/commons/c/c4/Badaling_China_Great-Wall-of-China-04.jpg" alt="Zhangjiajie Forest" />
</div>
<div class="attraction-card">
<h4>Great Wall of China</h4>
<p>Teeest</p>
<img src="https://upload.wikimedia.org/wikipedia/commons/c/c4/Badaling_China_Great-Wall-of-China-04.jpg" alt="Great Wall of China" />
</div>
</div>
Error:
and the fiddle: https://jsfiddle.net/yqbcmvox/3/
I think you should take the h4 and p tag in a div, then give that div a class, and make it an absolute, and make the attraction card relative to that absolute, for example:
.text-box {
position: absolute;
top: 0;
left: 0;
}
.attraction-card{
width: 100%;
/* height: 100%; */
position: relative;
}
I think this will fix your problem
.book-right-side {
display: flex;
gap: 1rem;
width: 100%;
overflow: hidden;
}
Surround the <img> tag with a <div> tag.
This will make sure that the image doesn't overload the size of the parent flex div.
Example:
<div>
<img src="https://upload.wikimedia.org/wikipedia/commons/c/c4/Badaling_China_Great-Wall-of-China-04.jpg" alt="Beijing City" />
</div>
Do this for all 3 instances of the <img> tag.
Add a div tag to wrap the image and set the height to 100% {height: 100%}.
I've tested it and it works. I resized the images of the same height, and width and hosted them on a free hosting image online.
<div> <img src="https://i.ibb.co/F4qbx7V/great-wall-21.jpg"> </div>
<div> <img src="https://i.ibb.co/vZTJ9WN/Beijing-montage1.jpg"> </div>
Also, add a height: 100% on your .attraction-card. Without adding the height the image looks stretched (if you notice).
Codepen

To display the image circular even the image is not loaded

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>

<img> height equals to its width inside a div

First of all, I'm not really good with CSS but I'm trying to make the <img> height equals the width of it using only CSS.
I'm also using bootstrap as shown below, so the width of each column is responsive.
#import 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css';
.album .album_photo .photo_link img {
width: 100%;
}
<div class="album">
<div class="col-xs-3">
<div class="album_photo">
<a href="#" class="photo_link">
<img src="someurl" />
</a>
</div>
</div>
<div class="col-xs-3">
<div class="album_photo">
<a href="#" class="photo_link">
<img src="someurl" />
</a>
</div>
</div>
</div>
This is how it looks like right now:
and this is what I'm trying to achieve:
Take a look at this pen, you'll know how to do that using padding-bottom trick:
Code pen
.album_photo {
position: relative;
padding-bottom: 100%;
overflow: hidden;
}
img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
Consider using image as background in conjunction with background-size: cover.
I like this method. It makes the content of the column (in this case .album_photo) position: relative, sets the inner wrapper of the element ('.photo_link img') position: absolute; with a height of 100%. To keep the shape of the column, you use a pseudo-element that has a padding-top: 100%. The reason why this works is because percentage based padding is always relative to the width of the element. Thus with a padding of 100%, it will always be just as tall as it is wide. You can use the same method to create ratio based container sizes too (e.g. 3:1 ratio for slideshows having absolutely positioned slides). It's a neat trick.
#import url(https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css);
.album_photo {
position: relative;
overflow: hidden;
}
.photo_link img {
position: absolute;
top: 0;
left: 0;
}
.album_photo:after {
content: '';
display:inline-block;
vertical-align: top;
width: 100%;
height: 0;
padding-top: 100%;
}
<div class="container">
<div class="album">
<div class="col-xs-3">
<div class="album_photo">
<img src="//placehold.it/300x200" />
</div>
</div>
<div class="col-xs-3">
<div class="album_photo">
<img src="//placehold.it/300x200" />
</div>
</div>
<div class="col-xs-3">
<div class="album_photo">
<img src="//placehold.it/300x200" />
</div>
</div>
<div class="col-xs-3">
<div class="album_photo">
<img src="//placehold.it/300x200" />
</div>
</div>
</div>
</div>
try this
img{
aspect-ratio:1;
}
https://developer.mozilla.org/en-US/docs/Web/CSS/aspect-ratio
You can scale the images in any way, by simply applying a width & height. For example, You can say
.full-width-height { width: 100%; height: 100%; }
You can also use min-width, max-width, min-height and max-height.
However, you will run into aspect ratio issues with this.
You have two options that will keep aspect ratios in check using CSS and
.auto-width { width: auto; height: xxx; }
.auto-height { width: xxx; height: auto; }
Bootstrap provides a responsive class you can use as well. The class is img-responsive which you can read about here. This class is often used with center-block helper class.
you want your "album_photo" class to have a width and height of 100%, because those will fill the space in the parent element which has a class of "col-xs-3"
CSS:
.album_photo {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
set margin and padding to 0 and you will see that the img fits nicely in the parent element.

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

Block that simultes images {width: 100%; height : auto} behaviour

I want to create the following layout :
Is a stripe of a variable number of images that have various widths and heights, that are:
proportional
scaled at the same height;
and the sum of their widths are equal to the parent width.
***It's kind of complicated to express myself;
I was wondering if it's possible for a block to simulate the img neat proportion behavior when you set a width to a percentage and it calculates the height of it automagically.
I've made up a diagram that maybe explain better what I want to achieve :
I want for the image to have collectively 100% width of the parent element, scaled with at the same height without loosing their proportion.
I've tried various implementations trying to figure out a way in which I can translate compute a percentage height in css that fills all the width for a block, just how the image behaves when there are {width: 100%; height : auto} properties.
So here is what I've got so far :
Strike #1, tried a simple solution
Problem: container height must be predefined.
.container {
width : 100%;
border: 1px solid black;
height: 50px; /* I would like to say here auto */
}
.image-wrapper {
white-space: nowrap;
height: 100%;
max-width: 100%;
border: 1px dashed gray;
}
.image {
height: 100%;
width: auto;
}
<div class="container">
<div class="image-wrapper">
<img class="image" src="http://placehold.it/100x200" />
<img class="image" src="http://placehold.it/300x200" />
<img class="image" src="http://placehold.it/800x400" />
<img class="image" src="http://placehold.it/10x80" />
<img class="image" src="http://placehold.it/800x400" />
</div>
</div>
Strike #2, display: table anyone ?
Problem: Don't even need to mention it, images are cropped the container size doesn't follow its parent size .
.container-wrapper {
width: 40px;
height: 50px;
}
.container {
width : 100%;
border: 1px solid black;
display: table;
height: 100%;
}
.image-wrapper {
display: table-row;
height: 100%;
border: 1px dashed gray;
}
.item {
display: table-cell;
border: 1px solid blue;
width: 100%;
height: auto;
}
.image {
height: 100%;
width: auto;
}
<div class="container-wrapper">
<div class="container">
<div class="image-wrapper">
<div class="item">
<img class="image" src="http://placehold.it/100x200" />
</div>
<div class="item">
<img class="image" src="http://placehold.it/300x200" />
</div>
<div class="item">
<img class="image" src="http://placehold.it/800x400" />
</div>
<div class="item">
<img class="image" src="http://placehold.it/10x80" />
</div>
<div class="item">
<img class="image" src="http://placehold.it/800x400" />
</div>
</div>
</div>
</div>
***I must say that I am looking for a HTML/CSS solution without the involvement of JavaScript code.
Do you have a clue on how can I approach this ?
So a trick I just came up with is to use the automagic scaling of an image to scale the containing filmstrip div, but hide it with opacity (in a real example, I'd use a transparent .png as well). This sets the height of the filmstrip relative to its width. If you want your filmstrip to be 5:4 or 16:9 or whatever, just change the proportions of the .magic image.
The container inside is then set to be absolutely positioned so it inherits the size of the .magic image.
The images themselves are set to take up the full height of the filmstrip, and are given different widths. The actual image is set with background-image which uses background-size: cover and background-position: center to fill the div.
.filmstrip {
width: 100%;
position: relative;
/* just to make it easier to see what's going on */
border: 1px solid red;
}
.magic {
width: 100%;
height: auto;
display: block;
/* we don't actually want to see this, we're just using it for it's ratio */
opacity: 0;
}
.contents {
position: absolute;
top: 0; bottom: 0;
left: 0; right: 0;
}
.contents .image {
height: 100%;
background-size: cover;
background-position: center;
float: left;
margin-right: 2%;
/* just to make it easier to see what's going on */
border: 1px solid blue;
box-sizing: border-box;
}
.contents .wide {
width: 30%;
}
.contents .narrow {
width: 10%
}
<div class="filmstrip">
<img class="magic" src="http://placehold.it/400x100" />
<div class="contents">
<div class="wide image" style="background-image: url('http://placehold.it/300x100');"></div>
<div class="narrow image" style="background-image: url('http://placehold.it/300x100');"></div>
<div class="wide image" style="background-image: url('http://placehold.it/300x100');"></div>
</div>
</div>
Browser support should be: Chrome 3+, Firefox 3.6+, IE 9+, Opera 10+, Safari 4.1+ which is basically because of the use of background-cover.
Have a look at my stackoverflow 33117027 answer in which I made suggestions about creating a filmstrip. It has a reference to an eleborate Codepen example. You can easily strip/add what you need...