How to fix this code? It does not scale my images - html

I have this piece of code. It doesn't scale my images to this size.
Height and width doesn't work. Why?
in style.css i have this line:
.gallery-popup .image-container img{width: 100%; height: auto; display: block;}
<div class="trace">
<img src="img/content/Imagesall/13347.jpg" height="250" width="250" alt="" />
<div class="tagline">
<div class="content">
<div class="title">TITLE</div>
<div class="description">TEXT</div>
</div>
</div>
</div>
<div class="overflow">
<div class="swiper-container" data-autoplay="0" data-loop="1" data-speed="500" data-center="0" data-slides-per-view="1">
<div class="swiper-w">
<div class="swiper-s">
<div class="image-container">
<img src="img/content/Imagesall/13347.jpg" height="250" width="250" alt="" />
<div class="description">
<div class="title">titletext</div>
<div class="text">texttexttext.</div>
</div>
</div>
</div>

Without any css to look in is this the responsive css for <img> images. Give the img a width and set the height to auto. You do not have to use the height and width in the html, use the css to give a width only
<div class="trace">
<img src="img/content/Imagesall/13347.jpg" alt="" />
.trace img {
width: ...; /* use for example px or % */
max-width: 100%;
height: auto; /* this line to preserve width:height ratio */
border: 0; /* remove default border of img */
}

Related

how to add text above a div dynamically based on div size

I'm trying to display a text above a image. the image is dynamic sometimes its big and sometimes its small.
I need to display text on image, top left but unfortunately for small images it is floated left too much there is a gap.
How to display text above image div based on image width?
<div id="content" class="sidebar__content p-2" style="overflow: hidden; position: relative;">
<div class="bg-E4E4E4 viewer p-2 " style="filter: brightness(100%) contrast(1) saturate(1);" tabindex="0">
<div id="hook" style="transform-origin: 0px 0px 0px; transform: matrix(0.776, 0, 0, 0.776, 0, 0);">
<div class="imageNameStyle">500x500.jpg</div>
<div class="outer-container" id="imgOuterContainer" style="width: 500px; height: 333px;">
<div id="Container" class="inner-container" style="width: 500px; height: 333px;">
<svg id="svgHook" style="pointer-events: none;"></svg>
<canvas width="500" height="333" class="image-layer" id="canvasLayer"></canvas>
</div>
</div>
</div>
</div>
</div>
I need to anchor text div to below one, any help.
It can be achieved using pseudo selector :before
.container{
display: flex;
}
.img::before {
content: "Above text";
position: relative;
}
<div class="container">
<div class="img">
<img src="https://picsum.photos/200">
</div>
<div class="img">
<img src="https://picsum.photos/200">
</div>
<div class="img">
<img src="https://picsum.photos/200">
</div>
</div>

How to set background image for div

I have 3 horizontally aligned images. I set a background image (background image) for the aligned images as below. When I run my code, the background image flows over the images. I want the 3 aligned images to appear on top of the background image. I tried to use box-shadow which couldn't work. I don't want the background image to cross over any of the 3 aligned images. How do I do this please?
HTML
<div class="col-lg-12">
<img src="images/background.png" alt="Change">
<div class="img">
<div class="column-img">
<img src="/images/pic.jpg" alt="" width="426" height="479">
</div>
<div class="column-img">
<img src="/images/pic.jpg" alt="" width="425" height="479">
</div>
<div class="column-img">
<img src="/images/change.jpg" alt="" width="426" height="479">
</div>
</div>
</div>
CSS
//how I align my 3 images horizontally.
.column-img {
float: left;
width: 33.33%;
padding: 5px;
}
Hm, so you want the background.png to appear behind the other images? If you insist on having it within the DOM, you'd need to remove it from the document flow. Something like this:
.column-img {
float: left;
width: 33.33%;
padding: 5px;
}
.background {
position: absolute;
top: 0;
left: 0;
z-index: -1;
}
.background-anchor {
position: relative;
}
<div class="col-lg-12 background-anchor">
<img class="background" src="https://placekitten.com/1300/1300" alt="Change">
<div class="img">
<div class="column-img">
<img src="https://placekitten.com/426/479" alt="" width="426" height="479">
</div>
<div class="column-img">
<img src="https://placekitten.com/425/479" alt="" width="425" height="479">
</div>
<div class="column-img">
<img src="https://placekitten.com/426/479" alt="" width="426" height="479">
</div>
</div>
</div>
css :
.img {
background-image: url('../images/pic.jpg');
}
this is the right way to put a background image in css
get more information from : W3school

Images always in middle of container

I want the first image to stick to the top of the container and the following images should be below it... top:0; lets the image be..200px above the container and if I just say position:relative its always in the middle...;
<div class="container">
<div class="card_left">
<p style="font-size:1.6em; padding: 15px 0;"><b>Title</b></p>
<p style="font-size:1.2em;">Long text</p>
</div>
<div class="card_right">
<img src="../res/images/artikel1bild.PNG"/>
<img src="../res/images/artikel1bild.PNG"/>
</div>
Use display: block so there will be no other images in the same line and margin: auto for centering the image
img {
display: block;
margin: auto;
width: 200px;
}
<div>
<img src="https://www.w3schools.com/css/paris.jpg" />
<img src="https://www.w3schools.com/css/paris.jpg" />
<img src="https://www.w3schools.com/css/paris.jpg" />
</div>
Just add <br /> after each image if you want to stick to HTML:
<div class="card_right">
<img src="../res/images/artikel1bild.PNG"/><br />
<img src="../res/images/artikel1bild.PNG"/><br />
</div>
Or the better way would be to make a separate CSS file and set display: block; for your img tags:
img {
display: block;
}
Example: https://jsfiddle.net/nk8fbr76/
try this
img {
margin: 0, auto;width: 200px;display:inline-block;height:100px;
}
<div class="card_right">
<img src="https://www.w3schools.com/css/img_fjords.jpg"/>
<img src="https://www.w3schools.com/css/img_fjords.jpg"/>
</div>

How to stretch an image to cover full width and height of the div? (Using Flexbox)

I want to adjust the size of the image to cover up the entire div. It would be very helpful if you guys can suggest anything in CSS rather than using javascript for it.
Here is a sample of what i have right now
.third{
width:30%;
float:left;
margin:8px;
}
.second{
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-flex-flow: row wrap;
-ms-flex-flow: row wrap;
flex-flow: row wrap;
}
img{
width:100%;
object-fit:fill;
}
<div class="first">
<div class="second">
<div class="third">
<img src="https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg">
</div>
<div class="third">
<img src="http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg">
</div>
<div class="third">
<img src="https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg">
</div>
<div class="third">
<img src="https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg">
</div>
<div class="third">
<img src="http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg">
</div>
<div class="third">
<img src="http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg">
</div>
</div>
</div>
Here is the link to the codepen for more details: http://codepen.io/gruler05/pen/vGyObo
The first, third and the fourth images are smaller than the rest. Can anyone please tell me how can I stretch those image to the size of their container div so that it looks like all the images are of the same size. Thank you in advance. P.S: I tried object-fit but it didn't work.
Using background-size:cover; in your CSS rules for the element is probably the easiest way to get full image coverage. This might cut off some of the content, though, depending on if you center the background image in the div or how you resize it.
background-size:contain; might be more useful, but I'm a huge fan of the cover size property.
Update
If you want to avoid background-image then you could try an img tag set to 100% width and/or height, but this will warp your image and cause other issues, and you'd potentially have to perform some JS calculations to prevent this.
Why the aversion to using a background image?
Here is a few ways, depending on if you want stretch or clipped images.
In sample 1 and 2, the trick I used is to keep the img as visibility: hidden to not have to set a fixed height on the third div.
Also, using the inline style for the image url, you can easily make that work with angular without messing with the external CSS.
Sample 1 - stretched
.third {
flex: 1 0 calc(33% - 16px);
margin: 8px;
position: relative;
background-size: 100% 100%;
}
.second{
display: flex;
flex-flow: row wrap;
}
img {
visibility: hidden;
width: 100%;
height: auto;
}
<div class="first">
<div class="second">
<div class="third" style="background-image: url( https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg)">
<img src="https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg">
</div>
<div class="third" style="background-image: url(http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg)">
<img src="http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg">
</div>
<div class="third" style="background-image: url( https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg)">
<img src="https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg">
</div>
<div class="third" style="background-image: url( https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg)">
<img src="https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg">
</div>
<div class="third" style="background-image: url(http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg)">
<img src="http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg">
</div>
<div class="third" style="background-image: url(http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg)">
<img src="http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg">
</div>
</div>
</div>
Sample 2 - clipped, center aligned
.third {
flex: 1 0 calc(33% - 16px);
margin: 8px;
position: relative;
background-size: cover;
background-position: center;
}
.second{
display: flex;
flex-flow: row wrap;
}
img {
visibility: hidden;
width: 100%;
height: auto;
}
<div class="first">
<div class="second">
<div class="third" style="background-image: url( https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg)">
<img src="https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg">
</div>
<div class="third" style="background-image: url(http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg)">
<img src="http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg">
</div>
<div class="third" style="background-image: url( https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg)">
<img src="https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg">
</div>
<div class="third" style="background-image: url( https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg)">
<img src="https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg">
</div>
<div class="third" style="background-image: url(http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg)">
<img src="http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg">
</div>
<div class="third" style="background-image: url(http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg)">
<img src="http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg">
</div>
</div>
</div>
Sample 3 - clipped, center aligned, fixed height (removed the img as well)
.third {
flex: 1 0 calc(33% - 16px);
margin: 8px;
position: relative;
background-size: cover;
background-position: center;
height: 150px;
}
.second{
display: flex;
flex-flow: row wrap;
}
img {
visibility: hidden;
width: 100%;
height: auto;
}
<div class="first">
<div class="second">
<div class="third" style="background-image: url( https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg)">
</div>
<div class="third" style="background-image: url(http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg)">
</div>
<div class="third" style="background-image: url( https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg)">
</div>
<div class="third" style="background-image: url( https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg)">
</div>
<div class="third" style="background-image: url(http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg)">
</div>
<div class="third" style="background-image: url(http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg)">
</div>
</div>
</div>
Try adjusting your .third, it has a default padding that you can remove by adding the below CSS. You can adjust the height to however much you need. This way you can avoid background-image, but background-image would still be most efficient.
.third {
width: 33.33%;
height: 100px;
float: left;
margin:0;
padding:0;
}
You can do this, but it will lose the image's aspect ratio.
HTML: Add a div with class="row" and put 3 images inside each
<div class="first">
<div class="second">
<div class="row">
<div class="third">
<img src="https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg">
</div>
<div class="third">
<img src="http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg">
</div>
<div class="third">
<img src="https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg">
</div>
</div>
<div class="row">
<div class="third">
<img src="https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg">
</div>
<div class="third">
<img src="http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg">
</div>
<div class="third">
<img src="http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg">
</div>
</div>
</div>
</div>
CSS: add the following code to give the .row a fixed height and set the image height to 95%
.row{
height:150px; //whatever height you want
}
.third img{
height:95%;
}
First delete all the <img> tags
then use this css
.third{
width:290px;
height:180px;
float:left;
margin:8px;
background-image:url("https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg");
background-size:cover;
}
Although flexbox is a great approach, The most easiest approach to do it by using tables, and inserting the images as data.
Here is the code
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Images</title>
</head>
<body>
<table>
<tr>
<td> <img src="https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg" width="200" height="200"></td>
<td> <img src="http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg" width="200" height="200"> </td>
<td> <img src="https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg" width="200" height="200"> </td>
</tr>
<tr>
<td> <img src="https://i.ytimg.com/vi/nT8pKpXQSJY/maxresdefault.jpg" width="200" height="200"> </td>
<td> <img src="http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg" width="200" height="200"> </td>
<td> <img src="http://i1.wp.com/proyectart.es/wp-content/uploads/2015/10/One-Punch-Man.jpg" width="200" height="200"> </td>
</tr>
</table>
</body>
</html>

stretch image over div

I have a gallery and the thumbnails are set to have a width of 100%. however some images do not fill the full height of this div.
How do I get it so that the images stretch over the full width and height of the div? (can't be changed to background images)
<div class="gallery-grid">
<div class="section group">
<div class="col span_1_of_6">
<img src="images/flowers.jpg" alt="" />
</div>
<div class="col span_1_of_6">
<img src="images/back.jpg" alt="" />
</div>
<div class="col span_1_of_6">
<img src="images/flowers.jpg" alt="" />
</div>
<div class="col span_1_of_6">
<img src="images/back.jpg" alt="" />
</div>
<div class="col span_1_of_6">
<img src="images/flowers.jpg" alt="" />
</div>
<div class="col span_1_of_6">
<img src="images/back.jpg" alt="" />
</div>
</div>
<!--style-->
.gallery-grid img{
width:100%;
}
It works for me. You must have some other css affecting it. See here: http://codepen.io/anon/pen/JGmRBW
.gallery-grid img{
width:100%;
}
Try this:
gallery-grid .section .col a{
display: block;
}
.gallery-grid .section .col a img {
width: 100%;
height: 100%;
}
You should consider that a is an inline element, so to put an image propely in it, you may need to set its display property correctly.
In action
a{
display: block;
width: 100px;
height: 100px;
}
img{
width: 100%;
height: 100%;
}
<p>The parent anchor has 100x100 dimensions:</p>
<p>The child image has 100x50 dimensions:</p>
<img src="https://placehold.it/100x50">