Is there a way to stylize the initial words of a brief text block by blending small caps with italics, all while avoiding the use of a span tag?

I have a series of images with numbered captions like "Fig. 1", "Fig. 2", "Fig. 3", followed by a brief description on the same line. Is there a way to programmatically style these strings (the “Fig. #” only) using CSS or Javascript to make them italicized and in small caps? I'd prefer this method over manually adding span tags to each one.

body {
    counter-reset: figcaption;
    counter-increment: 1;
}
figcaption:before {
  counter-increment: figcaption;
  font-variant: small-caps;
  font-style: italic;
  content: "Fig. " counter(figcaption) ". ";
}
#gallery {
    width: 360px;  
    height: 3600px;
    float: left;
    background-color: #F8F1D4;
}

/* Other CSS styling rules go here */

Update

After implementing this solution suggested by guest271314, everything works well. However, I now have three additional questions:

  1. The selected font and font-size are only working for the last caption, Fig. 10. How can I apply this styling to all the captions?

  2. I would like to add a period after the number in "Fig. #."

  3. How can I enclose code and HTML snippets in their own boxes like this?

Answer №1

You have the option to utilize figure, figcaption elements, as well as css properties like counter and counter-increment with a value of 1. You can also make use of the :before pseudo element along with font-variant set to small-caps, font-style set to italic, and set the content to display "Fig" followed by the incremented counter.

body {
  counter-reset: figcaption;
  counter-increment: 1;
}

figcaption:before {
  counter-increment: figcaption;
  font-variant: small-caps;
  font-style: italic;
  content: "Fig. " counter(figcaption) " ";
}
<figure>
  <img src="http://placehold.it/50x50">
  <figcaption>Short description</figcaption>
</figure>
 
<figure>
  <img src="http://placehold.it/50x50">
  <figcaption>Short description</figcaption>
<figure/>

Implementing html in the question:

body {
  counter-reset: figcaption;
  counter-increment: 1;
}
.imageBlockFloatLeftClearLeft > p:nth-child(2):before {
  counter-increment: figcaption;
  font-variant: small-caps;
  font-style: italic;
  font-size: .8em;
  line-height: 18px;
  content: "Fig. " counter(figcaption) ". ";
}
<div class="imageBlockFloatLeftClearLeft">
  <p>
    <img class="img-zoom" src="_images/pg_p_lewis_alex_17981019_ky_christian_war_1197.jpg" class="img-thumbnail" alt="lewis land warrant" width="140" height="143">
  </p>
  <p>Alexander Lewis's warrant #3663 to survey 200 acres of "second rate land" on the west fork of Pond River, dated 19 Oct 1798.</p>
</div>

<div class="imageBlockFloatLeftClearLeft">
  <p>
    <img class="img-zoom" src="_images/pg_p_lewis_alex_17981019_ky_christian_war_1197.jpg" class="img-thumbnail" alt="lewis land warrant" width="140" height="143">
  </p>
  <p>Alexander Lewis's warrant #3663 to survey 200 acres of "second rate land" on the west fork of Pond River, dated 19 Oct 1798.</p>
</div>

An additional suggestion is to add a newline after the text "Fig. 1." within the <p> element using css property white-space set to pre-line and apply styles using the :first-line pseudo-element for the first line of the selected p element.

.imageBlockFloatLeftClearLeft > p:nth-child(2) {
  white-space: pre-line;
}

.imageBlockFloatLeftClearLeft > p:nth-child(2):first-line {
  font-variant: small-caps;
  font-style: italic;
}
<div class="imageBlockFloatLeftClearLeft">
  <p>
    <img class="img-zoom" src="_images/pg_p_lewis_alex_17981019_ky_christian_war_1197.jpg" class="img-thumbnail" alt="lewis land warrant" width="140" height="143">
  </p>
  <p>Fig. 1.
    Alexander Lewis's warrant #3663 to survey 200 acres of "second rate land" on the west fork of Pond River, dated 19 Oct 1798.</p>
</div>

<div class="imageBlockFloatLeftClearLeft">
  <p>
    <img class="img-zoom" src="_images/pg_p_lewis_alex_17981019_ky_christian_war_1197.jpg" class="img-thumbnail" alt="lewis land warrant" width="140" height="143">
  </p>
  <p>Fig. 2.
    Alexander Lewis's warrant #3663 to survey 200 acres of "second rate land" on the west fork of Pond River, dated 19 Oct 1798.</p>
</div>

Answer №2

const imageCaptions = document.querySelectorAll('.imageBlockFloatLeftClearLeft > p:last-child');
imageCaptions.forEach(function(caption) {
    let content = caption.innerHTML; // 'Fig. 4554. Description blah blah 325355 2 4.'
    let matches = content.match(/(Fig.\s\d+.)(.+)/);
    let fig = matches[1]; //'Fig. 4554.''
    let description = matches[2]; // ' Description blah blah 325355 2 4.'
    caption.innerHTML = '<em>' + fig + '</em>' + description;
});

To apply small caps, include this CSS:

.imageBlockFloatLeftClearLeft > p:last-child em {
    font-variant: small-caps;
}

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

seeking a solution to simulate a variety of web browsers

Seeking a solution to preview my website on various browsers including IE7 and above, as well as different versions of Firefox. Although I am new to CSS, I previously tried installing software on my PC to assist with this task, which unfortunately caused ...

What is the best way to transform a JSON data-storing object into an array within Angular?

I am currently working on developing a machine learning model using tensorflow.js, but I have encountered a roadblock. The issue I am facing is that I have stored my JSON content in a local object, but for it to be usable in a machine learning model, I ne ...

Testing Ajax code encounters error

Currently, I am running a code test with Jasmine and setting up a mock object for the ajax method. spyOn($,'ajax').and.callFake(function(e){ console.log("is hitting"); }) In order to test the code snippet below: $.ajax({ url: Ap ...

CSS Padding Hover Transition solution for Eliminating Flickering Text issue

Is it possible to achieve this? I'm attempting to animate the padding around a centered text link. When the text link is clicked, the padding changes from 20% to 100%, which works correctly. The text is horizontally and vertically centered using CSS ...

Creating image gallery with navigation arrows in JS/jQuery to loop through array of thumbnails

I am relatively new to working with arrays, and I have been exploring ways to use them in controlling the positions of thumbnails when navigating through a series of images using next or previous buttons. My goal is to make the thumbs loop seamlessly whene ...

Conceal the div by clicking outside of it

Is there a way to conceal the hidden div with the "hidden" class? I'd like for it to slide out when the user clicks outside of the hidden div. HTML <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.c ...

The font-family CSS properties inheritance is not functioning as I had anticipated

I'm currently working on a webpage where I want to add a list of links that resemble tabs. While this style is functioning correctly for the main pages, I'm having trouble implementing it for a new section. The existing list is located within: ...

Dynamic text input and selection menu with AJAX (PHP and JavaScript)

As a student who is new to Javascript and PHP, I am trying to create a login page for my website that can verify user input in the database using AJAX. For example, when a user enters their username and password, the system should automatically check if t ...

Several SVG Components failing to function properly or displaying differently than anticipated

I've encountered a puzzling issue that I just can't seem to solve. Here's the scenario: I'm working on a NextJS App and have 5 different SVGs. To utilize them, I created individual Icon components for each: import React from 'reac ...

Making an HTTP request within a forEach function in Angular2

Encountering an issue while using the forEach function with HTTP requests. The _watchlistElements variable contains the following data: [{"xid":"DP_049908","name":"t10"},{"xid":"DP_928829","name":"t13"},{"xid":"DP_588690","name":"t14"},{"xid":"DP_891890" ...

I am facing difficulties displaying Arabic language characters on my HTML code

I created a website and included the following meta tag in the head section: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> However, all Arabic text appears distorted as shown in the image. Interestingly, when I manually cha ...

Issues with implementing KoGrid within the Durandal JS framework

How do I properly bind a koGrid in my Durandal JS view page? The code provided below is not functioning as expected. View (HTML) <div id="functiontable" class="form-actions"> <div style="height: 200px" data-bind="koGrid: ...

Issue with Socket.io connection event failing to trigger

Having recently delved into the world of socket.io, I followed the provided documentation and watched several tutorials on YouTube to set up a basic functionality. My goal was to see "New socket connection" logged in the console when I visit the index page ...

Mastering card sliding and spacing in React NativeLearn how to effortlessly slide cards horizontally in your React

My aim with the following code snippet is to achieve two objectives: Apply a margin of 20 units to each card Display all four cards in a single row, allowing users to swipe horizontally Unfortunately, I have not been successful in achieving either of th ...

How can one locate a particular element/style/class in the dev tools DOM while debugging in MUI v5?

Exploring the DOM and pinpointing the specific component file and style block in MUI v4 is a straightforward process: Locating a particular element/style class in MUI v4 However, in MUI v5, this functionality is no longer available, making it challenging ...

Tips for hiding the overflow scrollbar on Microsoft Chrome when there is no content to scroll

Looking for a solution to hide scroll bars in Microsoft Chrome, but only when there is no content to scroll? The current div and styles always show the horizontal and vertical scroll bars, even when the height exceeds the content. <div style="backgroun ...

JavaScript method for altering the values of variables

Having a small issue with my JavaScript function. Let me tell you what's going on: var intervalId = setInterval(function() { var value = parseInt($('#my_id').text(), 10); if(value > 0) { clearInterval(intervalId); console.log ...

Center flex items along the vertical axis

IMAGE <div className="d-flex align-items-center"> <img src={member.user_profile_image} width="41" height="41" alt="" ...

Manipulate SVG elements by dragging them along the boundary of the path

Looking to achieve a specific functionality where I can drag and drop an element along the edges of a drawn path, rather than inside the path itself. To clarify, the goal is to move the red marked element on the black line bordering the path, allowing move ...

Set the Vue 3 Select-Option to automatically select the first option as the default choice

I am attempting to set the first select option as the default, so it shows up immediately when the page loads. I initially thought I could use something simple like index === 0 with v-bind:selected since it is a boolean attribute to select the first option ...