CSS - using numbers to create dynamic background images

I am looking to create a design where the background images in CSS display increasing numbers per article, like this example:

This idea is similar to another post on Stack Overflow discussing using text as background images for CSS. You can find it here: Text as background images for CSS ( Text as background images for CSS ) which includes a JS FIDDLE with relevant code snippets.

var $divs = $('.randbg'),
  alpha = 'abcdefghijklmnopqrstuvwxyz';

$divs.each(function ea() {
  var letter = alpha[Math.floor(Math.random() * alpha.length)].toUpperCase();

  $(this)
    .attr('data-content', letter)
    .html('<img src="http://placehold.it/75x75&number=' + letter + '"/>');
})
.randbg {
  width: 7px;
  height: 75px;
}

.randbg:before {
  content: attr(data-content);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="randbg">What happens</div>
<div class="randbg">Why not</div>
<div class="randbg"></div>
<div class="randbg"></div>

Below is the code I am currently working with:

<?php $i2=0; 
foreach ($items2 as $item2) {
    if ($i2 < 4) {

        printf('<div class="right-now-box">');
        printf('<img src="%s" class="width-100">',$item2['image2']);
        printf('<a target="_blank" class="left-sidebar-news-text" href="%s">%s</a>', $item2['link2'], $item2['title2']);
        printf('</div>');

        $i2++;
     }
 }
 ?>

Answer №1

By utilizing CSS counters along with the pseudo-element ::before:

body {
  counter-reset: myCounter;         /* initiate a counter named myCounter */
}


.article {
  border: 1px solid black;
  position: relative;
}

.article::before {                  /* add a ::before element for each .article */
  counter-increment: myCounter;     /* increment the myCounter counter */
  content: counter(myCounter);      /* display the value of counter in this ::before element */
  
  font-size: 5em;
  color: #00ffff;
  position: absolute;
  right: 0;
  bottom: 0;
  z-index: -1;
}
<div class="article">
  Lorem Lorem Lorem Lorem Lorem Lorem Lorem ... (repeated text)
</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

Is it better to define the SVG `viewbox` once in a `symbol`, or each time an SVG is `used`?

Based on my research, some tutorials suggest that when inserting an SVG <symbol> with <use>, the viewBox property only needs to be defined once - in the <symbol> tag. While this method seems to work fine, it results in the contents of th ...

Turn off logging functionality in a Node.JS environment

I am looking to turn off logging for specific environments in my Node.JS application using Express. Would the following method be considered the most optimal way to disable logging within Node.JS applications? if(process.env.NODE_ENV == "production") { ...

Special html effects for scrolling

Recently, I've been noticing a trend of websites incorporating new and innovative scrolling effects. One great example is: As you scroll down the page, the initial section stays fixed in place (z-index of 1?) while the content scrolls over it seamles ...

What's up with the Chrome Dev Tools Error Log?

I am having an issue with debugging JavaScript files using breakpoints in Chrome Dev Tools. Whenever an error occurs, a red error message flashes on the screen so quickly that I can't read what it says. Is there a way to change this setting or access ...

Set iframe scroll to match the height of the user's screen resolution

Can the height of an iframe scroll be adjusted to match the size of the screen (browser)? Essentially replacing the default browser scroll with the iframe scroll. Here's what I'm looking to achieve: Currently, my browser scroll is disabled: < ...

Enhance your website with an AJAX form integrated with a powerful Website

I need to monitor conversions on a button that is activated by AJAX using visualwebsiteoptimizer's Custom Conversion Goal Code, which consists of generated Javascript code. If I include this code in a PHP file triggered by AJAX, will it execute the j ...

Empty the password field

<input name="e_password" id="e_password" type="password" autocomplete="off" /> The password field above is causing some trouble as it gets automatically filled in Mozilla, but not in Chrome and IE11. This seems to be retaining values from previous a ...

Angular's two-way binding feature does not seem to be updating the value for date

In my Angular - Dotnetcore 2.0 form, users are required to update their demographic information including their birth date. Initially, I was using the following code snippet to display the birthdate: <input id="dateOfBirth" type="date" class="form-cont ...

Incorporating a loading animation with an AJAX call

I've come across various solutions to this issue, one of the most enlightening being a response on Stack Overflow suggesting to "display it as soon as you initiate the AJAX call" and then "hide the image once the request is complete." But how would I ...

Node.js exporting fails due to an undefined variable

When attempting to export variables in Node.js, I keep receiving an undefined error. I'm not sure what the issue is, can anyone provide assistance or suggestions? Here is a snippet from index.js: var test = 10; module.exports.test = test; And here i ...

Avoid having a pre tag enclosed long line exceed its container's width

Within the <pre> </pre> tags, I have a lengthy single line of text that needs formatting. Is there a method to automatically wrap the text or must I manually insert <br> tags within the text? ...

Having difficulty accessing the value of a table td element in HTML using a jQuery selector

When creating a table, I utilize ng-repeat to generate table rows. Whenever the dropdown changes, a function is triggered which applies certain conditions. Based on these conditions, an object is added to an array that is bound to a scope variable. Here i ...

Both if and else statements are carrying out code in JavaScript/jQuery

The second if statement is functioning correctly, but the first one always triggers the else statement and never stands alone. This jQuery function is enclosed within another function that is invoked under the "$(document).ready" command. I resorted to u ...

Create a new array by dynamically generating a key while comparing two existing arrays

One of the features in my app involves retrieving data from an API and storing it in $scope.newz. The previous user activity is loaded from LocalStorage as bookmarkData. I am facing a challenge with comparing the contentId values in arrays $scope.newz an ...

Issue encountered: Unable to load resource - ajax file upload failed due to net::ERR_SSL_BAD_RECORD_MAC_ALERT error

I've implemented an AJAX form file upload using jQuery. After testing my code across multiple computers and browsers, I've encountered an issue on one Windows 8 machine running Chrome where the upload functionality fails with the following error ...

Use CSS to apply hover effects to multiple sections of text at the same time

Is there a way in CSS to apply the hover state to one part of HTML text when another part is hovered over, both sharing the same class? I have a block of text in HTML that is broken down into individual words, each word linked to a specific CSS class. It ...

iScroll not working in conjunction with jQuery Mobile

I am looking to integrate iScroll into my jQuery Mobile pages in order to enable horizontal scrolling of a large table. With the recent release of the first beta of iScroll 5, I am eager to utilize this version which includes a dedicated horizontal scrolli ...

What is the process for transferring input field values to PHP?

I am working on setting up a database and login form, and I need to create a PHP script to validate the login credentials. What are the different types of data access and how can they be utilized? Specifically, how do I retrieve user input from elements ...

Unable to activate animation within a nested ngRepeat loop

I'm struggling to understand how to initiate animations within a nested ngRepeat using Angular. The CSS class ".test" is supposed to be animated. However, when I apply ".test" on the inner ngRepeat, it doesn't seem to work (Plunker): <div ng ...

Title: "Custom Captions for Buttons on Telerik MVC Scheduler Form"

How do I modify the label of the Save button in my custom Telerik Scheduler form? I want to change it to "Book" instead of "Save". These buttons are generated dynamically when the "onAdd" event is triggered. Below is the code for my Scheduler: @(Html.Ke ...