What is the best way to showcase images along with their captions in a horizontal layout

I am facing an issue with displaying the last four images at the end of the page horizontally. When there is no caption, I can use the display:flex property to achieve this layout, but as soon as I add a caption, the images are displayed vertically instead. I need assistance in fixing this problem specifically with the last four images.

body {
  margin: 0;
  box-sizing: border-box;
}

.header {
  background-color: #696969;
  padding-top: 2rem;
  /* padding-top and margin (in body tag) were used to eliminate the white spaces around the background color */
  padding-bottom: 1rem;
  text-align: center;
}

.profile-image {
  height: 8rem;
  width: 8rem;
  border-radius: 20rem;
  border: 0.3rem solid white;
  transition: transform 1s;
}

.profile-image:hover {
  transform: rotate(360deg);
  transition: transform 1s;
}

.header-links {
  padding-top: 2rem;
}

.header-links a {
  color: white;
  text-decoration: none;
  padding: 1.9rem;
  text-transform: uppercase;
  font-family: 'Bebas Neue', cursive;
  font-size: 2rem;
}

.header-links a:hover {
  color: orange;
}

.name-title {
  color: white;
  font-family: 'Lexend Zetta', sans-serif;
}

.wallpaper {
  width: 100%;
}

main {
  background-color: azure;
  margin-left: 1rem;
}
...
</pre>
<pre><code><section>
  <header>
    <div class="header">
      <figure class="name-title">
        <img class="profile-image" src="https://gamerheadquarters.com/hub/avatar/fallout76tshirt.jpg" alt="profile photo">
        <figcaption>
          <h1>John Johnson</h1>
          <h2>Front End Developer</h2>
        </figcaption>
      </figure>
      <div class="header-links">
        <a href="#about">About</a>
        <a href="#projects">Projects</a>
        <a href="contact">Contact</a>
      </div>
    </div>
  </header>
</section>

<section>
  <img class="wallpaper" src="https://images.pexels.com/photos/691668/pexels-photo-691668.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" alt="mountains">
</section>

<section>
  <main>
    <div class="align-center">
      <h1 id="about" class="about">About</h1>
    </div>
    <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium
      quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Maecenas
      tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales,
      augue velit cursus nunc</p>
    <div class="align-center">
      <h2>Skills</h2>
    </div>
    <div class="align-center-1">
      <ul>
        <li>HTML & CSS</li>
        <li>Javascript</li>
        <li>React</li>
      </ul>
    </div>

    <section>
      <div class="margin-top">
      </div>
      <div class="align-center">
        <h1 id="projects" class="projects">Projects</h1>
      </div>
      <div class="boxes">
        <figure>
          <img class="project1" src="https://www.woodstockvt.com/sites/default/files/styles/hero_x_large/public/media-images/snowmobiletrail.jpg?itok=8mrgv-Vw">
          <figcaption class="center">Winter</figcaption>
          <img class="project2" src="https://www.rd.com/wp-content/uploads/2017/07/01-birth-month-If-You-Were-Born-In-Summer-This-Is-What-We-Know-About-You_644740429-icemanphotos.jpg">
          <figcaption class="center">Summer</figcaption>
          <img class="project3" src="https://wdh01.azureedge.net/-/media/hidden-hearing/uk/shared/images/blog/xsounds-of-spring-banner.jpg,qrev=37A8,ala=en-GB.pagespeed.ic.mC7e7D9h5P.jpg">
          <figcaption class="center">Spring</figcaption>
          <img class="project4" src="https://www.wallpaperup.com/uploads/wallpapers/2016/05/07/945313/0094f0d5744c9910789d20dd3baebf18-700.jpg">
          <figcaption class="center">Fall</figcaption>
        </figure>
      </div>

  </main>

Answer №1

It is considered a good practice to enclose both <img> and <figcaption> within their own <figure> tags. This approach can resolve the issue at hand without the need for any CSS modifications.

Here is the updated HTML code block:

<div class="boxes">
    <figure>
      <img class="project1" src="https://www.woodstockvt.com/sites/default/files/styles/hero_x_large/public/media-images/snowmobiletrail.jpg?itok=8mrgv-Vw">
      <figcaption class="center">Winter</figcaption>
    </figure>
    <figure>
      <img class="project2" src="https://www.rd.com/wp-content/uploads/2017/07/01-birth-month-If-You-Were-Born-In-Summer-This-Is-What-We-Know-About-You_644740429-icemanphotos.jpg">
      <figcaption class="center">Summer</figcaption>
    </figure>
    <figure>
      <img class="project3" src="https://wdh01.azureedge.net/-/media/hidden-hearing/uk/shared/images/blog/xsounds-of-spring-banner.jpg,qrev=37A8,ala=en-GB.pagespeed.ic.mC7e7D9h5P.jpg">
      <figcaption class="center">Spring</figcaption>
    </figure>
    <figure>
      <img class="project4" src="https://www.wallpaperup.com/uploads/wallpapers/2016/05/07/945313/0094f0d5744c9910789d20dd3baebf18-700.jpg">
      <figcaption class="center">Fall</figcaption>
    </figure>
  </div>

Give this solution a try. Hopefully, it will produce the desired output you are looking for.

View the final output screenshot here

Answer №2

To optimize your formatting, consider enclosing each <img> and <figcaption> within a container element (like a <div>) and apply the display: flex; property to the containing parent element (in this case, <figure>).

Answer №3

Referencing MDN - https://developer.mozilla.org/en-US/docs/Web/HTML/Element/figure

To associate a caption with the <figure> element, you can insert a <figcaption> within it (either as the first or last child). The initial <figcaption> discovered within the figure will serve as its caption.

This indicates that there must be only one <figcaption> per <figure>. As a result, each <img> and <figcaption> should reside in separate <figure> elements.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unexpected behavior in Internet Explorer with ChartJS version 2.6.0

When using ChartJS v2.6 to create a pieChart within a Bootstrap Grid, everything appears correctly in Chrome and Firefox, but not in Internet Explorer. In IE, the chart is unintentionally stretched on larger screens. Bootstrap should handle the sizing, but ...

Working with double quotes within variable in JavaScript

I am currently working on dynamically creating HTML tags using JavaScript. Please take a look at the code snippet below: function getPhotosSuccess(data) { if (data.d.results.length > 0) { var response = data.d.results; var innerht ...

Struggling with transitioning from table layout to CSS, specifically encountering difficulty in aligning all elements properly

Back in the "good old days," we used to simply put everything into td cells and it would all line up perfectly. Now, with CSS, things are a bit more complicated. I'm struggling with how to achieve a specific layout. What I want is to have text on the ...

What is the best way to modify the appearance of the button?

I'm attempting to change the appearance of buttons at the top of a webpage to be square and blue. I have jQuery code for this purpose, but it doesn't seem to be functioning correctly. Here is the snippet of code I am using: $(document).ready(fu ...

Accumulation of style from various directives in AngularJS on a single element

Currently, I am working on developing a component framework and find myself in need of a method to apply styles from multiple directives to an element in a way that they can accumulate. The priority of the styles should be determined by the directive creat ...

Popup modal is obstructed by carousel indicator

I'm having trouble with the following issue: The 3 dots represent my carousel indicator. My problem is that the carousel indicator is blocking my pop-up modal image. I've tried adjusting the z-index of the modal without success. Here is the code ...

AngularJS - Sending configuration values to a directive

I'm trying to figure out how to pass parameters (values and functions) to an Angular directive. It seems like there should be a way to do this in Angular, but I haven't been able to locate the necessary information. Perhaps I'm not using th ...

Conflicting behavior between jQuery focus and blur functions and retrieving the 'variable' parameter via the $_GET method

There is a simple focus/blur functionality here. The default value shown in the 'Name of Venue' input field changes when the user clicks on it (focus) and then clicks away(blur). If there is no text entered, the default value reappears. Input fi ...

The hamburger menu is malfunctioning and spilling over

// App.js import './App.css'; function App() { return ( <div className='navbar'> <label className="hamburger-menu"> <input type="checkbox" /> </label> <div class ...

Is there a way for me to insert a variable into the src attribute of my img tag like this: `<img alt="Avatar" src=`https://graph.facebook.com/${snAvatarSnuid}/picture`>`

I need assistance with passing a variable called snAvatarSnuid within the img src tag, specifically after facebook.com/ and before /picture as shown below: <img alt="Avatar" src=`https://graph.facebook.com/${snAvatarSnuid}/picture`> Note: 1) The ht ...

Utilizing and incorporating distinct html elements on a separate webpage

I am in the process of developing a dynamic newspaper-style website where articles will be regularly updated and replaced on the Home Page. Each article featured on the Home Page will serve as a link to a separate page for full details. These article links ...

Javascript Steps to trigger a function when selecting the Stay on Page option in Google Chrome

If you're testing my code in the Chrome Browser, hitting refresh will prompt you with 2 options. Leave This Page Stay on This Page By clicking on the Stay on this page button, it should trigger my custom function displayMsg(). Can anyone pr ...

The material UI styled component is not appearing as expected

I'm having trouble getting the MUI styled() utility to apply styles to <MyComponent>Styled div</MyComponent> in my index.jsx file. Any ideas why? import Button from '@mui/material/Button' import Grid from '@mui/mater ...

Looking for a search box that functions like Google's search feature

I am attempting to create a text box similar to the Google search text box... So far, I have implemented a feature where, upon entering a character, words starting with that character are displayed in a div using AJAX. This functionality is working well, ...

Set HTML form elements to be shown in 'display only' mode, without any interactivity

In the process of developing a dynamic form builder, I am allowing users to add various blocks of content like buttons, text inputs, paragraphs of text, and images to a page. They can later publish this as a simple HTML form for their use. When it comes t ...

Tips for disabling default browser input validation in React

https://i.stack.imgur.com/834LB.png Is there a way to remove the message "Please fill out this field" while still keeping the "required" attribute intact? I have my own validation system in place, so I need to use the "required" attribute to determine whe ...

Enable the ability to scroll and click to navigate an overlapping Div element

A customer has a website with a dark teal design and is not pleased with the appearance of the scroll bar, as it disrupts the overall style. The client requested that I find a solution without using third-party libraries, and one that they can easily under ...

Issue encountered while attempting to retrieve data from a local json file due to Cross-Origin Resource Sharing

I'm attempting to fetch and display the contents of a JSON file on a webpage, but I'm encountering this error message: Access to XMLHttpRequest at 'file:///C:/Users/bobal/Documents/htmlTry/myData.json' from origin 'null' has ...

Binary stream (byte array) compatible video player for seamless playback

Is there a JavaScript/jQuery/HTML5 video player that can play a video without needing a physical file? I have a video file stored in a database (varbinary(MAX)) and I am looking for a player that can play the video using the byte array source. Has anyone ...

Validating Color Properties with CSS 3.0

When using Visual Studio 2012 (Ultimate), a red squiggly may appear indicating that rgba(255, 255, 255, 0.4) is not a valid color property value However, according to the W3C, it is considered a valid value. Could this be a bug in Visual Studio, or am ...