Placing text on top of an image in HTML

I am currently working on creating a responsive grid using HTML and CSS. My goal is to have the text 'Top left' displayed within the image, similar to the example shown at https://www.w3schools.com/howto/howto_css_image_text.asp. However, I am facing difficulties achieving this while maintaining the responsiveness of the grid. Here is the HTML code I am using:

<html>
<head>
<style>
/* Font */
@import url('https://fonts.googleapis.com/css?family=Quicksand:400,700');
  
/* Rest of the CSS code... */

</style>
</head>
<body>
<div class="main">
  <h1>Responsive Card Grid Layout</h1>
  <ul class="cards">
    <li class="cards_item">
      <div class="card">
      <div class="image_wrapper"><img src="https://picsum.photos/500/300/?image=10"><p class="top_left">Top Left</p></div>
        <div class="card_content">
          <h2 class="card_title">Card Grid Layout</h2>
        <p class="card_text">Some text</p>
        <p class="card_text">Some more text</p>
        </div>
      </div>
    </li>
    <li class="cards_item">
      <div class="card">
        <div><img src="https://picsum.photos/500/300/?image=5"></div>
        <div class="card_content">
          <h2 class="card_title">Card Grid Layout</h2>
          <p class="card_text">Demo of pixel perfect pure CSS simple responsive card grid layout</p>
        </div>
      </div>
    </li>

    <!-- More grid items... -->

</ul>
</div>
</body>
</html>

I would appreciate any assistance with this issue. Thank you.

Answer №1

Here is a code snippet that you can try:

<head>
  <style>
  /* Font */
  @import url('https://fonts.googleapis.com/css?family=Quicksand:400,700');
  /* Design */
  *,
  *::before,
  *::after {
  box-sizing: border-box;
  }
  html {
  background-color: #ecf9ff;
  }
  body {
  color: #272727;
  font-family: 'Quicksand', serif;
  font-style: normal;
  font-weight: 400;
  letter-spacing: 0;
  padding: 1rem;
  }
  .main{
  max-width: 1200px;
  margin: 0 auto;
  }
  h1 {
  font-size: 24px;
  font-weight: 400;
  text-align: center;
  }
  img {
  height: auto;
  max-width: 100%;
  vertical-align: middle;
  }
  .btn {
  color: #ffffff;
  padding: 0.8rem;
  font-size: 14px;
  text-transform: uppercase;
  border-radius: 4px;
  font-weight: 400;
  display: block;
  width: 100%;
  cursor: pointer;
  border: 1px solid rgba(255, 255, 255, 0.2);
  background: transparent;
  }
  .btn:hover {
  background-color: rgba(255, 255, 255, 0.12);
  }
  .cards {
  display: flex;
  flex-wrap: wrap;
  list-style: none;
  margin: 0;
  padding: 0;
  }
  .cards_item {
  display: flex;
  padding: 1rem;
  }
  @media (min-width: 40rem) {
  .cards_item {
  width: 50%;
  }
  }
  @media (min-width: 56rem) {
  .cards_item {
  width: 33.3333%;
  }
  }
  .card {
  background-color: white;
  border-radius: 0.25rem;
  box-shadow: 0 20px 40px -14px rgba(0, 0, 0, 0.25);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
  }
  .card_content {
  padding: 1rem;
  background: linear-gradient(to bottom left, #EF8D9C 40%, #FFC39E 100%);
  }
  .card_title {
  color: #ffffff;
  font-size: 1.1rem;
  font-weight: 700;
  letter-spacing: 1px;
  text-transform: capitalize;
  margin: 0px;
  }
  .card_text {
  color: #ffffff;
  font-size: 0.875rem;
  line-height: 1.5;
  margin-bottom: 1.25rem;
  font-weight: 400;
  }
  .made_by{
  font-weight: 400;
  font-size: 13px;
  margin-top: 35px;
  text-align: center;
  }
  .top-left {
  position: absolute;
  top: 0px;
  left: 0px;
  margin: 0px;
  padding: 20px;
  color: #fff;
  }
  </style>
</head>
<body>
  <div class="main">
    <h1>Responsive Card Grid Layout</h1>
    <ul class="cards">
      <li class="cards_item">
        <div class="card">
          <div </div>
          <div><img src="https://picsum.photos/500/300/?image=10"><p class="top-left">Top Left</p></div>
          <div class="card_content">
            <h2 class="card_title">Card Grid Layout</h2>
            <p class="card_text">Some text</p>
            <p class="card_text">Some more text</p>
          </div>
        </div>
      </li>
      <li class="cards_item">
        <div class="card">
          <div><img src="https://picsum.photos/500/300/?image=5"></div>
          <div class="card_content">
            <h2 class="card_title">Card Grid Layout</h2>
            <p class="card_text">Demo of pixel perfect pure CSS simple responsive card grid layout</p>
          </div>
        </div>
      </li>
      <li class="cards_item">
        <div class="card">
          <div><img src="https://picsum.photos/500/300/?image=11"></div>
          <div class="card_content">
            <h2 class="card_title">Card Grid Layout</h2>
            <p class="card_text">Demo of pixel perfect pure CSS simple responsive card grid layout</p>
          </div>
        </div>
      </li>
      <li class="cards_item">
        <div class="card">
          <div><img src="https://picsum.photos/500/300/?image=14"></div>
          <div class="card_content">
            <h2 class="card_title">Card Grid Layout</h2>
            <p class="card_text">Demo of pixel perfect pure CSS simple responsive card grid layout</p>
            <button class="btn card_btn">Read More</button>
          </div>
        </div>
      </li>
    </ul>
  </div>
</body>
</html>

Answer №2

To achieve the desired effect, ensure that the image wrapper has its position set to relative. Next, set the class top-left to position:absolute; left:0px; top:0px.

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

Tips for overlaying text onto a MaterialUI icon

https://i.stack.imgur.com/cFr5Y.pngI am currently working on a project that requires me to overlay a number on top of an icon. Specifically, I am utilizing the MaterialUI ModeComment icon and attempting to display text over it. Despite trying the following ...

Tips for creating consistent spacing between elements using Tailwind CSS version 3

I am currently utilizing Tailwind CSS in conjunction with next.js for my project. I have a total of 8 images, each varying in size, and my goal is to display 4 images per row on medium and large devices, while displaying 2 images per row on small devices. ...

Issue with grid breakpoints for medium-sized columns and grid rows not functioning as expected

I'm working on building a grid layout from scratch in Vue.js and have created my own component for it. In my gridCol.vue component, I have defined several props that are passed in App.vue. I'm struggling to set up breakpoints for different screen ...

Blurring images with a combination of jQuery Backstretch and Blurjs

I have a background image displayed using Backstretch and want to apply Blur.js to blur elements on top of it. However, when I try to use Blur.js on the backstretched image, it doesn't work. I believe this is happening because Blur.js uses the CSS pro ...

The conditional statement within an AngularJS object is reliant on the selected option within an HTML dropdown menu

I am looking to apply an if condition in an object to capture specific values from two HTML select options. The Angular framework will then post these values to the database based on the user's selection. HTML <tr> <td>En ...

What is the best method to restore a table cell to its original width?

Here's a quick overview of the issue I'm facing: http://jsfiddle.net/k2Pqw/4/ Once I adjust the red td width, I'm struggling to revert it back to its original size. Setting it at 25% doesn't dynamically adjust like the other tds, it r ...

Tips for positioning three child divs horizontally within a parent div with flexible dimensions and arranging each one separately

Is there a way to stack three divs horizontally within a fluid-width container div, with each child div scaling proportionally when the browser is resized for responsive design? The left-most div should be on the left, the middle div centered, and the righ ...

Using image paths in CSS for styling purposes

As I develop an Asp.net website, I've encountered a surprising issue. Here is my folder structure: - Styles - master.css - Images -webPages -scripts When trying to access an image in the Images folder from the CSS file, I receive a ...

Automating Check Box Selection with Xpath in VBA using Selenium

Having trouble identifying the correct checkbox to tick. I'm struggling with the xpath. <div class="custom-control custom-checkbox" style="display: inline-block;"> <input name="PriceRealization" class="custom-control-input" id="check-7012949 ...

CSS properties are ineffective in scaling the image strip

I have a large image strip measuring 130560 x 1080. My goal is to load it into an image tag and use the parent div as a viewport, showing only one section of the image at a time. However, I'm encountering a problem when specifying the height and widt ...

Implementing onMouseEnter and onMouseLeave functionalities for music player

Currently facing a challenge while developing a music player app. I am trying to implement a feature where: Upon hovering over a song, a "play" button should appear in place of the song number. The currently playing song should display a "pause" ...

What is the best way to incorporate Font Awesome icons into a UTF-16 encoded HTML document?

Trying to incorporate Font Awesome icons in UTF-16 encoded HTML pages can be a bit tricky. Following these steps should help you achieve the desired results: <head> <meta charset="UTF-8"> <link rel="stylesheet" href="https://use.fontawe ...

What is the proper method to deactivate a hyperlink in Angular 2?

I'm currently developing a web application using Angular2, and I find myself in need of displaying, but making it non-clickable, an <a> element within my HTML. Can anyone guide me on the correct approach to achieve this? Update: Please take no ...

Tips for customizing the appearance of the <select> <option> element with jQuery or CSS

What are some ways to style the <option> HTML tag, using jQuery or CSS? Any suggestions on styling the <option> tag? ...

Guide on showcasing an icon adjacent to the input fields once they have been validated successfully with the help of jquery plugins

I am struggling with the code below which is supposed to validate the SubmitForm when the button is clicked. It successfully changes the textboxes to red and displays an error message with a symbol if validation fails. However, I am wondering how I can sho ...

Shifting an image outside of the container in a Bootstrap 4 card header

I am attempting to create a design similar to this: https://i.sstatic.net/OR4Em.png I am currently using Bootstrap 4 cards, but I am struggling to figure out the best way to move the icon/image outside of the Cards header. I have experimented with differe ...

Printing HTML to a VueJS page is simple and efficient

I have a situation where one of my attributes in a property contains an HTML string. Instead of rendering the HTML as expected, when I output it within my template, the browser displays the raw HTML code with tags intact. It doesn't interpret it as a ...

Is it possible for the shadow of a pseudo element to remain lingering behind its corresponding element?

I'm experiencing an issue where the shadow of the pseudo element :before appears in front of its associated element. Check out this JSFiddle link for reference <div class="rectangle"> <p>Lorem Ipsum</p> <a href="#" style ...

Tips for disabling the Enter key event specifically (without the combination of Shift+Enter) in a contenteditable field

I'm trying to incorporate the contenteditable attribute in my code, but I've run into an issue. I would like for pressing (shift + enter) to move to the next line (Note: I only want shift + enter to trigger this action), and when I press the ent ...

Customize Joomla content in a component by using CSS within a template that I personally designed

Is there a way to customize the text content of a component in Joomla by editing the body text? The CSS properties for #content are adjusting padding, margin, border, width, height, and background but not affecting font attributes... Inside index.php < ...