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:

https://i.sstatic.net/themq.png

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

Incorporate a fontawesome icon into a dynamically created button using ajax

Is it possible to insert fontawesome icons into a button created using this code snippet? $.each(response, function (i, item) { trHTML += '<tr><td>' + item.name + '</td><td>' + "<input type='button&apo ...

Is horizontal spacing automatically added to <img> elements in Bootstrap? And if it is, how can this default setting be changed or overridden?

I'm currently working on the design for a homepage where I want to showcase the same image repeated 4 times in a row, creating a decorative banner effect. Each image is set to occupy 25% of the screen width, which should theoretically fit perfectly si ...

Is the CSS content-visibility property compatible with background images?

Can CSS content-visibility and contain-intrinsic-size also be used with the background-image property, or do they only work with IMG tags? Will either of these properties have any impact on the following code snippet? <span id="image"> #i ...

Is it acceptable to include a checkbox and a hidden input within a label element?

When creating a list of possible products for users to buy, I use the ul tag combined with li. Each product element includes a checkbox that allows users to select the product or not. For products with related information, I want to store this data in a h ...

Learn how to display two rows of div elements within a single div container

In my program, I am using a for loop to display multiple div elements in one row. The first div is showing up correctly, but I am having trouble inserting a new div that looks the same as the first one. How can I achieve this? Thank you in advance. "first ...

Slideshow featuring cards with text overlay

I am working with a bootstrap carousel and trying to place a card with text on top of it. No matter what I try, the card is not appearing as desired. Here is an example of what I want to achieve: https://i.sstatic.net/tjW3w.png Below is the code snippet: ...

What is the best approach for resolving the issue of User having an Unknown field(s) (Id_card_number) specified in the system

I'm facing an issue with my code. Whenever I run a makemigrations command, it returns an error message saying Unknown field(s) (Id_card_number) specified for User. How can I resolve this problem? settings.py INSTALLED_APPS = [ 'django.contr ...

Enhancing jQuery Rating Plugin

Currently, I am working on customizing the jQuery Bar Rating System Plugin. You can view an example of the plugin by visiting this link: . The rating system on my end will resemble Example D. Rather than having the plugin based on user input, my goal is to ...

What is the best way to scale a div proportionally within a bootstrap grid?

Despite spending hours reading numerous online sources, I am still struggling to fully grasp the CSS/HTML box model. Specifically, I am attempting to create sections with a ratio of 3:2, where 3 represents the width and 2 represents the height. https://i.s ...

Utilize CSS for Sticky Headers to Create Full Page Scrollbars for Table Control

Here is a code sandbox showcasing the use of Material UI's Table within a Paper component with a sticky header: https://codesandbox.io/s/2n40y I am looking to have the scrollbars appear at the edges of the browser page, bottom and right, while still ...

Using a MySQL statement within a conditional statement

Looking to modify this code to display different information based on the values retrieved, here is the current code: $sql_doublecheck = mysql_query("SELECT * FROM adminpage WHERE setting='main' AND close_site='1'"); $doublecheck = mys ...

Submit image and transfer it to server using AngularJS and Jersey

I need help with my form that contains the following fields: <div class="form-group"> <label>Username</label> <input type="text" ng-model="username" class="form-control"> </div> <div class="from-group"> < ...

Having a newline between HTML elements can disrupt the structure of an HTML layout

It's common knowledge that in html, a newline between elements is treated as space. However, I find it quite alarming when working on responsive layouts. For instance, take a look at this example where the expected and correct behavior is achieved on ...

Sending information from JavaScript to Python scripts: best practices

Within this HTML file, I have included a script where an ajax request is being made to pass a specific string to a Python function. var message = "hello there!!!" $.ajax({ url: "../SCRIPTS/cond.py", type: 'POST', ...

Issue with radio button not updating when selected

I am encountering a strange issue with a form I am currently developing. The code snippet below showcases the problem: <?php $show = ' checked '; $hide = ' '; /* Some logic to swap the checked here */ ?> <label cl ...

Tips for repairing a horizontal line at the bottom of the <TD> tag and placing text on top

I am working with an HTML table that contains a single TR tag and two TD tags. Each TD has two HR tags creating horizontal lines, along with some text. I am looking for a way to keep the text aligned at the top of the TD and the horizontal line at the bott ...

The hover effect does not function on an SVG element when it is placed within an external file

I've been experimenting with SVG animation. My goal is to change the color of a circle when it's hovered over. <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 437 ...

Tips for accessing the height property of an image

Is there a way to determine if an image lacks the height style property? Exploring CSS in HTML <img id="image" src="images/sports/sports9.png" style="width:100px"> Using jQuery if($('#image').css('height') == 0) { var ima ...

Retrieving data from an external PHP script

I'm facing an issue with retrieving results from a PHP file after submitting a form: index.php (located at ) <form id='loginForm' action='http://domain1.com/mail.php' method='POST'> <input id=&apo ...

Obtain information about a div element while scrolling

Looking to enhance my social feed page by adding a view count feature for each post. The challenge is figuring out how to keep track of views as the user scrolls down the page. Any suggestions? ...