Starting letter and word in jekyll articles and pages

How can I customize the appearance of the first letter and word in my posts and pages on a Jekyll blog? I want to achieve a design similar to this:

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

To accomplish this, I have used the following CSS style along with span tags:

:not(.post-excerpt) > .initial-word {
  color: #166079;
  font-variant: small-caps;
  font-weight: bold;
}

:not(.post-excerpt) > .initial-word .initial-letter {
  float: left;
  font-size: 3.15em;
  line-height: 0.5;
  margin: 0.225em 0.159em 0 0;
  color: #166079;
  font-weight: normal;
  text-transform: uppercase;
}
<p>
  <span class="initial-word"><span class="initial-letter">L</span>orem</span> ipsum dolor sit amet
</p>

If I have a Jekyll post with introductory text like this:

Lorem ipsum dolor sit amet

# Main title of the post

Lorem ipsum dolor sit amet

The content of the post available through the liquid code {{ content }} will look something like this:

<p>Lorem ipsum dolor sit amet</p>

<h1>Main title of the post</h1>

<p>Lorem ipsum dolor sit amet</p>

I want to enhance the post content by adding span tags with specific rules:

  • The initial tags should only be applied to the introduction; they should not appear if the content doesn't start with a p tag, and they should be limited to the first paragraph.
  • The

    <span class="initial-word">
    tag can encapsulate more than one word in the post:

    <span class="initial-word">Lorem ipsum</span> dolor sit amet
    
    # Main title of the post
    
    Lorem ipsum dolor sit amet
    

In cases where multiple words are encapsulated in the

<span class="initial-word">
tag, only the
<span class="initial-letter">
tag should be added.

I currently have complex code for posts:

{% assign content_start=content | slice: 0, 3 %}
{% assign tokens=content | split: '<span class="initial-word">' %}
{% assign count=tokens | size %}
{% if content_start == "<p>" or count > 1 %}
  {% if count > 1 %}
    {% assign tokens=tokens[1] | split: "</span>" %}
    {% assign initial_word=tokens[0] %}
  {% else %}
    {% assign initial_word=content | remove_first: "<p>" | truncatewords: 1, "" %}
  {% endif %}
  {% assign initial_letter=initial_word | slice: 0 %}
  {% assign span_letter=initial_letter | prepend: '<span class="initial-letter">' | append: '</span>' %}
  {% assign span_word=initial_word | replace_first: initial_letter, "" | prepend: span_letter %}
  {% unless count > 1 %}
    {% assign span_word=span_word | prepend: '<span class="initial-word">' | append: '</span>' %}
  {% endunless %}
  {% assign content=content | replace_first: initial_word, span_word %}
{% endif %}

And a similar version for pages. Is there a simpler way to achieve this without such complicated code?

Answer №1

This may not be a comprehensive solution and there could be some misinterpretation on my part, but regarding the initial letter query, my recommendation would be to utilize the :first-letter selector rather than enclosing it within a span.

If you're looking to target the first paragraph specifically, perhaps using p:first-of-type could serve as a helpful tool?

Answer №2

Upon reviewing @IanDevlin's comment, I decided to modify the explicit marking of the first word by following the example set by the excerpt separator:

Lorem ipsum<!--first-word--> dolor sit amet

# Primary heading of the article

Lorem ipsum dolor sit amet

I utilized the ::first-letter selector in order to adjust the liquid code to identify the initial p tag that met my criteria (essentially, focusing on paragraphs that begin the content):

{% assign content_start=content | slice: 0, 3 %}
{% if content_start == '<p>' %}
  {% if content contains '<!--first-word-->' %}
    {% assign content=content | replace_first: '<!--first-word-->', '</span>' %}
  {% else %}
    {% assign first_word=content | remove_first: '<p>' | truncatewords: 1, '' %}
    {% assign span_word=first_word | append: '</span>' %}
    {% assign content=content | replace_first: first_word, span_word %}
  {% endif %}
  {% assign content=content | replace_first: '<p>', '<p class="first-p"><span class="first-word">' %}
{% endif %}

This results in the HTML code below, along with updated CSS styling:

:not(.post-excerpt) > .first-word {
  color: #166079;
  font-variant: small-caps;
  font-weight: bold;
}

:not(.post-excerpt) > .first-p::first-letter {
  float: left;
  font-size: 3.15em;
  line-height: 0.5;
  margin: 0.159em 0.159em 0 0;
  color: #166079;
  font-weight: normal;
  text-transform: uppercase;
}
<p class="first-p">
  <span class="first-word">Lorem ipsum</span> dolor sit amet
</p>

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

Troublesome situation arising from CSS floats overlapping div elements

It's strange how this issue always occurs when using floats. This is what my div (samle) looks like: <div class="main> <div class="inn_div">&nbsp</div> </div> Here is my stylesheet: .main{ width:250px; border:1px ...

Tips for passing variables through onclick to JavaScript file

My task involves querying a database and implementing a filter based on country selection from a map. After writing the JavaScript and PHP code, everything seems to work fine except for one issue - passing a name with an onclick function to initiate the qu ...

The hyperlink for making phone calls appears twice on an Android phone

When you click on the number, it will be displayed twice in the phone screen like this... 12345678910,12345678910 ...instead of just once... 12345678910 Take a look at the code snippet below: {#if order.salesRep} <div class="info"> ...

Using jQuery, make an element fade out only when its parent div is clicked

JavaScipt $(".card-container").on('click', function() { $(this).fadeOut("fast"); }); CSS <div class="card-container"> <div class="card-wrap"> <div class="card-content"> Lorem ipsum </di ...

Utilizing pure JavaScript to dynamically fetch HTML and JavaScript content via AJAX, unfortunately, results in the JavaScript

I am trying to load an HTML file using AJAX and then execute a script. Here is the content of the HTML file I want to load: <div class="panel panel-body"> <h4>Personal Data</h4> <hr /> <span data-bind="editable: firs ...

What is the best method to create a gap between the header and table body in Angular Material version 13?

Is there a way to create space (margin) between the header and body in an Angular Material table? I initially attempted to use a solution found here, but it was unsuccessful. Check out the demo here: https://stackblitz.com/edit/angular-ivy-anphrw?file=sr ...

Steps for stopping the sass --watch function on a specific folder

Successfully got my SASS up and running with the help of fantastic documentation available everywhere. The guide I followed was http://sass-lang.com/guide. sass --watch myfolder/css Running a specific command worked like a charm, enabling every change in ...

Ways to format a C# string outcome showcased in HTML (ASP.NET)

I'm facing an issue with formatting the output (string) retrieved from a C# database connected to ASP.NET. I want to style the row["Grens"] differently from row["Sanctie"], for instance. The result is displayed in a basic div: <div id="resultaat" ...

Determine element height in Angular 5 by clicking on it

Currently, I am experimenting with determining the height of an element that I click on within a sidebar. The purpose is to display submenus accordingly. Below is the code snippet that I am working on: <li (click)="newsExpanded = !newsExpanded; getMarg ...

Is it possible to alter the background behind each word in a text using only CSS?

I have a question about styling quotes by highlighting each word in the text with a background color. The issue I'm facing is that when using a solid background, there are no gaps between the colors. How can I achieve a result similar to this, but wit ...

Challenges with layout and adaptability

Hello, I am looking for some assistance in making my HTML responsive and with positioning the items on my screen. Below is a picture of how I envision it to look: Desired Look: https://i.sstatic.net/84m3H.png I have been experiencing issues with display ...

Display a form inside a div when a button is clicked using jQuery

Currently, I am using Cloud 9 to develop a website. However, I have hit a roadblock when it comes to displaying and hiding my form. The issue lies with a fixed sidebar that includes a "Contact" button. Once this button is clicked, it should toggle the visi ...

Create a table that excludes the content of the initial cell

I am seeking to update my table design. The desired look can be found here. Currently, my table looks like this: current table. The key difference I want to achieve is the removal of the first cell (positioned at 0;0) in the desired table design. Additio ...

Calculate the total sum of selected values in a multiple select dropdown using jQuery

Is there a way to calculate the sum of selected items in a multiple selection dropdown menu? For instance, if I select "X12SO" and "X13SO", their values should add up to 30. let total = 0; $("select[name='myselect[]'] option").each(function(){ ...

Leverage Angular variables within HTML tags

Below is the code in my controller.js file: $scope.animatt = 900; and here is the corresponding html code: <div id="properties" data-{{animatt}}="left:100%;top:10%;"> <h2>all numeric properties</h2> </div> I am t ...

Retrieving random HTML content with an AJAX request

Solving this technical issue has been quite a challenge for me. Running on Ubuntu 14.04 with Apache and PHP5, I initially suspected my NetBeans IDE to be the culprit, but then I directly ran it from the web root /var/www/html. PHP is functional as I'v ...

"Master the art of creating a curved circular arrow using a combination of HTML, SVG, D3.js

I am looking to design an SVG circle with a fading stroke that blends into the background and features an arrow at one end. The goal is for the ring to fade out completely right above the arrow, similar to the visual reference provided in the image.view ex ...

The functionality of PHP's array_key_exists is compromised when dealing with a non-multi-dimensional array

In my possession, I hold an array containing the names of all countries across the globe: $countries = array( "GB" => "United Kingdom", "US" => "United States", "AF" => "Afghanistan", "AL" => "Albania", "DZ" => "Algeria", "AS" = ...

The function driver.find_element is unable to locate an element using the "class_name" attribute

My attempt at using driver.find_element, with the "class_name" method to locate and click on a button for expanding rooms on this website resulted in an error message: raise exception_class(message, screen, stacktrace) selenium.common.exceptions.N ...

Ensure that clicking on various links will result in them opening in a new pop-up window consistently

I am facing an issue with my HTML page where I have Four Links that are supposed to open in separate new windows, each displaying unique content. For instance: Link1 should open in Window 1, Link2 in Window 2, and so on... The problem I'm encounter ...