There is a horizontal list consisting of images and captions under them. I want to make the title dependent on the width of the image.
Got the width of all the pictures. Now all the headers come the same width.
Js:
$(".project-img img").one('load', function() { var width = $(this).width(); $('.projects h1').css('width', width); console.log(width); }) .each(function() { if (this.complete) $(this).trigger('load'); });
HTML:
<div class="projects"> <div> <a href="#someproject1" title="someproject1"> <span class="project-img"> <img src="./img/project1.jpg" alt="someproject1" title="someproject1" /> </span> <h1>Название проекта1</h1> </a> </div> <div> <a href="#someproject2" title="someproject2"> <span class="project-img"> <img src="./img/project2.jpg" alt="someproject2" title="someproject2" /> </span> <h1>Название проекта2</h1> </a> </div> <div> <a href="#someproject3" title="someproject3"> <span class="project-img"> <img src="./img/project3.jpg" alt="someproject3" title="someproject3" /> </span> <h1>Название проекта3</h1> </a> </div> </div>
h1
inside the list. - Pro markwidth
is added, and that the latter is the essence of the problem. - Pro mark