Tips for centering elements in an image caption

I need some help with separating an image and text within an image caption from each other. Currently, they are overlapping and I want them to be distinct. I attempted using inner div elements for the text and image separately, but it caused issues by breaking the image caption. How can I center elements within an image caption?

#gallery-img {
  position: relative;
}
#caption div {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;
  color: #fff;
  background: rgb(0, 0, 0);
  /* fallback color */
  background: rgba(0, 0, 0, 0.7);
  padding: 10px;
  text-align: center;
}
<div id="gallery-img">
  <img src="http://www.umnet.com/pic/diy/screensaver/10/4a206b7a-8ee2.jpg" alt="demo">
  <h2 id="caption"><div><b>Stats:</b> Advanced Mathematics<br/><a href="#">See more</a><img style="width:10%;" src="http://images.clipartpanda.com/circle-clipart-pink-circle-clip-art-at-vector-clip-art-2.png"></div></h2>
</div>

I'm aiming for a design similar to this:

Answer №1

Check out this code snippet!

#gallery-img img {
  float:right;
  width:7%;
}
#gallery-img {
  color: #fff;
  background: rgb(0, 0, 0);
  /* fallback color */
  background: rgba(0, 0, 0, 0.7);
  padding: 10px;
  text-align: center;
}
<div id="gallery-img">
  <img src="http://images.clipartpanda.com/circle-clipart-pink-circle-clip-art-at-vector-clip-art-2.png">
  <div id="caption"><b>Information:</b> Mathematics<br/><a href="#">Find out more</a></div>
</div>

Answer №2

Surely you can use absolute positioning to place the image inside an h2:

#picture-container {
  position: relative;
}
#caption-description div {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;
  color: #fff;
  background: rgb(0, 0, 0);
  /* fallback color */
  background: rgba(0, 0, 0, 0.7);
  padding: 10px;
  text-align: center;
}

#caption-description img{
  position: absolute;
  right: 20px;
  bottom: 10px;
}
<div id="picture-container">
  <img src="http://www.example.com/image.jpg" alt="example">
  <h2 id="caption-description"><div><b>Details:</b> Lorem Ipsum<br/><a href="#">Read more</a><img style="width:10%;" src="http://www.example.com/icon.png"></div></h2>
</div>

Answer №3

To make sure the #gallery-img container contains the image properly, apply float: left; or display: inline-block;. Set display: block; for the img to prevent extra white space and avoid layout issues on the page. Instead of using width: 100%;, consider using right: 0; on the #caption div within the boundaries of #gallery-img. Also, position #caption absolutely to ensure proper margin alignment instead of positioning the div inside it. Does this approach meet your desired outcome?

#gallery-img {
  position: relative;
  float: left;
}
#gallery-img img {
  display: block;
  }
#caption {
  position: absolute;
  left: 0;
  bottom: 0;
  right: 0;
  color: #fff;
  background: rgb(0, 0, 0);
  /* fallback color */
  background: rgba(0, 0, 0, 0.7);
  padding: 10px;
  text-align: center;
}
<div id="gallery-img">
  <img src="http://www.umnet.com/pic/diy/screensaver/10/4a206b7a-8ee2.jpg" alt="demo">
  <h2 id="caption"><div><b>Stats:</b> Advanced Mathematics<br/><a href="#">See more</a><img style="width:10%;" src="http://images.clipartpanda.com/circle-clipart-pink-circle-clip-art-at-vector-clip-art-2.png"></div></h2>
</div>

Answer №4

figure {
  display: flex;
  align-items: flex-start;
}

figcaption {
  text-align: center;
}
img {
  width: 10%;
}
<figure>
  <figcaption><b>Information:</b> Important Details<br><a href="#">Learn more</a></figcaption>
  <img src="http://images.clipartpanda.com/circle-clipart-pink-circle-clip-art-at-vector-clip-art-2.png">
</figure>

Answer №5

Consider utilizing the main image as a background image for a simpler code structure. Position the copy and secondary image in separate divs and float them left and right within another container with margin set to 0 auto for centering. See sample code below.

#gallery-img {
  position: relative;
  background: url('http://www.umnet.com/pic/diy/screensaver/10/4a206b7a-8ee2.jpg');
  width:640px;
  height:640px;
}
#caption {
  position:absolute;
  bottom:0;
  width: 100%;
  color: #fff;
  background: rgb(0, 0, 0);
  padding:15px 0;
  /* fallback color */
  background: rgba(0, 0, 0, 0.7);
  text-align: center;
}
#container {
  width:400px;
  margin:0 auto;
}

#copy {
  float:left;
  width:340px;
}

#image-container {
  float:right;
  width:40px;
}

h2 { margin: 0 }
<div id="gallery-img">
    <div id="caption">
      <div id="container">
        <div id="copy">
          <h2>
          <b>Stats:</b> Advanced Mathematics<br/>
          <a href="#">See more</a>
          </h2>
        </div>
        <div id="image-container">
          <img style="width:100%;" src="http://images.clipartpanda.com/circle-clipart-pink-circle-clip-art-at-vector-clip-art-2.png">
        </div>
      </div>
    </div>
</div>

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

Ways to eliminate unused classes from JQuery UI

We have successfully implemented JQuery UI library for enhancing our interface. However, since we no longer need to support outdated browsers like IE6, classes such as .ui-corner-all, .ui-corner-top, .ui-corner-left, .ui-corner-tl are unnecessary and clu ...

Applying CSS borders with circular corners

Can this unique border style be achieved using only CSS? ...

Can data be transferred from node.js express to front-end HTML without requiring a page refresh?

Currently, I have developed a webpage that showcases all meetings with collapsible accordions. Initially, all the meetings are collapsed. However, when the user clicks on the meeting button (labeled as 'Audio' in the screenshot), a request contai ...

Exploring the application of URL parameters in HTML code

How can I use URL parameters retrieved with Javascript in my HTML? <script> var url_string = window.location.href; //window.location.href var url = new URL(url_string); var c = url.searchParams.get("name"); console.log(c); </script> I am tryi ...

Displaying a banner at the bottom of a React page

I need to display a banner with the text "All items..." on every page. Currently, I have set the position attribute to fixed, but the banner is aligned to the left and I want it to be centered horizontally instead. Below is my code snippet: export const Ba ...

Is there a way to illuminate a complete row by simply hovering over a span within one of the columns?

If I want to change the background-color of a div with classes "row" and "highlightThisRow" when hovering over a span with the class "fromThisSpan", how can I achieve that? In a list, there are multiple rows with several columns. The span in question is l ...

Integrate a button following the first paragraph exclusively when there are two or more paragraphs present

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> jQuery(document).ready(function($) { if ( $('p').length < 1 ) { $('p:last-child').after('<div id="toggle" class="btn"> ...

Setting the z-index of the parent element causes issues with overlapping

When adjusting the z-index property of the parent element, I am experiencing issues with overlapping elements. Below is the HTML and CSS code: .black_gradient{ width:100%; height:100%; background: linear-gradient(0deg,rgb(75, 75, 75) 20%, rgba(75,75 ...

Generate a random word using Typed.js

Can Typed.js generate a random word for output? $(function(){ $(".element").typed({ strings: ["Lorem", "Ipsum", "Dolor"], typeSpeed: 0 }); }); The output should be one of the following words. (Lorem / Ipsum / Dolor) ...

Encountering Issues with Formatting InnerHtml Text using RegEx

Technology: React.js I have been working on a custom function in JavaScript to highlight specific words within a code block. The function seems to be functioning correctly, but the highlighting isn't staying after the function is completed. Any ideas ...

`A mistake occurred while building in the package.json file`

While attempting to run all build processes by using the command npm run build:css, I encountered an error indicated below. Even after running npm cache clean --force, the issue remains unresolved. npm ERR! code ELIFECYCLE npm ERR! errno 1 npm E ...

The mobile functionality of the stylesheet is not functioning properly

Recently, I came across an issue with a stylesheet designed for mobile devices that contains the following code: /* Smartphones (portrait) ----------- */ @media only screen and (max-width : 320px) { /* Styles */ } Surprisingly, this code failed to work ...

The battle between CSS and jQuery: A guide to creating a dynamic zebra-style table without relying on additional plugins

Is there a better way to create a dynamic zebra styling for table rows while still being able to hide certain elements without losing the styling? In this code snippet, I'm using the CSS :nth-of-type(even) to style even rows but running into issues wh ...

The content within the iframe is not displayed

I've set up a dropdown menu with separate iframes for each option. Here's the code I used: $(function(){ $('#klanten-lijst').on('change',function(){ $('#klanten div').hide(); $('.klant-'+t ...

What is the method for accessing HTML tag data within a script?

Looking for a way to extract a specific substring from a given string: var str = `<ul class="errorlist"><li>Enter a valid email address.</li></ul>`; Is there a method to extract the following substring? 'Enter a valid email ...

Converting dynamic content within a div into an interactive link

I am currently working with Longtail's JW Player and facing some difficulties with a basic function. Since I am not familiar with the programming language terminologies, I will describe the issue step by step: There is a JavaScript code that displays ...

Arranging arrows strategically along a line and ensuring they are positioned correctly above all div elements

I'm really struggling with this issue. I have a situation where I need to center a downward pointing arrow within some divs. It should be positioned in the middle, on a brown line, and on top of everything else. The last div should also include the a ...

jQuery Slider Showing Incorrect Images

My jQuery slider is acting strangely. It hides the first image, shows the second one, but then just repeats the cycle by showing the first image again instead of loading the third one. I'm puzzled about what could be causing this issue in my code. Do ...

The nonexistence of the ID is paradoxical, even though it is present

I've been working on a school project that involves a dropdown box with the id "idSelect." However, I'm encountering an issue where it says that idSelect is not defined when I try to assign the value of the dropdown box to a variable. Even after ...

What is preventing the 'p:nth-child(1)' from functioning properly in my HTML code?

Even though I used p:nth-child(1){color: white;} in my CSS style tag, it did not produce the expected result. body { background-color: #162b85; } button, p:nth-child(1) { color: white; } <p class="center">Register your account</p&g ...