Is there a way to achieve a marker-style strike through on text?

I'm attempting to create a strikethrough effect for a marker, just like the one shown in the image below.

In my efforts to achieve this effect, I have tried wrapping the text within two span elements. The inner span has negative margins to reduce the height, while the outer span sets the background with positive margins to compensate. You can see a similar approach in this question.

Unfortunately, my current strategy is not functioning as expected. It seems that using inline elements may be causing issues. I specifically want only the text inside the block to have the strikethrough effect, not the entire block itself.

The relevant CSS code snippet is provided below for reference:

.item .mark {
  background-color: #ffd100;
  margin: 13px 0;
}

.item .mark .text {
  margin: -13px 0;
}

/* other CSS styles */

Additional HTML content for context:

<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,700|Permanent+Marker&display=swap" rel="stylesheet">

<div class="item">
  <h1><span class="mark"><span class="text">Foo</span></span></h1>
  <p>
    [Text content here]
  </p>
  <div class="button"><a href="#">GO</a></div>
</div>

Answer №1

To create a striking background effect, you can utilize the linear-gradient() function in CSS:

.item .mark {
  /* Implementing a linear gradient as the background: */
  background: linear-gradient(
    /* Gradient direction: */
    to bottom,
    /* Initial color: */
    transparent,
    /* Color stops at 40%: */
    transparent 40%,
    /* Abrupt color change at 40%: */
    #ff0 40%,
    /* Continuing with another color until 60%: */
    #ff0 60%,
    /* Another abrupt color change back to transparent: */
    transparent 60%,
    /* Remaining transparent till the end: */
    transparent);
  margin: 13px 0;
  padding: 0 0.5em;
}

.item .mark {
  background: linear-gradient(
    to bottom,
    transparent,
    transparent 40%,
    #ff0 40%,
    #ff0 60%,
    transparent 60%,
    transparent);
  /* unchanged: */
  margin: 13px 0;
  /* Setting padding to extend the 'strike-through' effect: */
  padding: 0 0.5em;
}

/* Additional CSS removed for conciseness */

.item {
  font-family: 'Open Sans', sans-serif;
  font-size: 10pt;
  text-align: center;
  width: 200px;
}

.item h1 {
  font-family: 'Permanent Marker', cursive;
  font-size: 18pt;
  font-weight: normal;
}
<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,700|Permanent+Marker&display=swap" rel="stylesheet">

<div class="item">
  <h1><span class="mark">Foo</span></h1>
  <!-- content omitted -->
</div>

Additional Resources:

Answer №2

To create a bold visual impact, consider utilizing borders.

If you wish to adjust the intensity of the effect, modify the width of

border-bottom: 0.25em solid #ffd100;

.item .mark {
  position: relative;
}
.item .mark::after {
  border-bottom: 0.25em solid #ffd100;
  content: "";
  left: 0;
  margin-top: calc(0.125em / 2 * -1);
  position: absolute;
  right: 0;
  top: 50%;
  z-index: -1000;
}

/* irrelevant CSS */

.item {
  font-family: 'Open Sans', sans-serif;
  font-size: 10pt;
  text-align: center;
  width: 200px;
}

.item h1 {
  font-family: 'Permanent Marker', cursive;
  font-size: 18pt;
  font-weight: normal;
}

.item .button {
  width: 40px;
  height: 40px;
  line-height: 40px;
  margin: auto;
  font-size: 14pt;
  font-weight: bold;
  border: 2px solid black;
  border-radius: 40px;
}

.item .button:hover {
  background-color: #ffd100;
}

.item .button a {
  color: inherit;
  text-decoration: none;
}
<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,700|Permanent+Marker&display=swap" rel="stylesheet">

<div class="item">
  <h1><span class="mark"><span class="text">Foo</span></span></h1>
  <p>
    Jerky pastrami pork belly tongue bresaola. Flank t-bone tri-tip, leberkas
    ribeye salami shoulder ball tip. Capicola frankfurter salami venison
    turducken pig burgdoggen shankle drumstick tail fatback. Turkey pork ham
    ball tip jowl beef rump boudin.
  </p>
  <div class="button"><a href="#">GO</a></div>
</div>

Answer №3

Experience the animated marker strike by clicking on the GO Button

$("#go").click(function() {
  $('.item h1').toggleClass('widthh');
});
.item .mark {
  background-color: #ffd100;
  margin: 13px 0;
}

.item .mark .text {
  margin: -13px 0;
}

.item {
  font-family: 'Open Sans', sans-serif;
  font-size: 10pt;
  text-align: center;
  width: 200px;
}

.item .button {
  width: 40px;
  height: 40px;
  line-height: 40px;
  margin: auto;
  font-size: 14pt;
  font-weight: bold;
  border: 2px solid black;
  border-radius: 40px;
}

.item .button:hover {
  background-color: #ffd100;
}

.item .button a {
  color: inherit;
  text-decoration: none;
}


/* Additional CSS */



.item h1 {
  font-family: 'Permanent Marker', cursive;
  font-size: 18pt;
  font-weight: normal;
  position: relative;
  z-index: 0;
  display: inline-block;
}
.item h1:before {
  content: '';
  position: absolute;
  width: 0;
  left: -10px;
  height: 10px;
  top: 50%;
  background: #ffb300;
  transform: translateY(-35%) rotate(-3deg);
  z-index: -1;
  transform-origin: left;
  transition: .3s all ease;
}

.item h1.widthh:before {
  width: calc(100% + 20px);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,700|Permanent+Marker&display=swap" rel="stylesheet">

<div class="item">
  <h1 class="widthh">Foo</h1>
  <p>
    Jerky pastrami pork belly tongue bresaola
  </p>
  <div class="button"><a href="#" id="go">GO</a></div>
</div>

The pseudo element was only added by me

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 my jQuery code generating a large number of DOM objects?

I am currently developing a hex dumper in JavaScript to analyze the byte data of files provided by users. To properly display a preview of the file's data, I am utilizing methods to escape HTML characters as outlined in the highest-rated answer on thi ...

Achieving distinct CSS margins for mobile and desktop devices in a Django template without the need for multiple {% block content %} statements

Is there a way to dynamically change the margin-left of my main {% block content %} in the base.html template based on whether the viewer is using a mobile or desktop device? This is how my current base.html looks like: <div class="content container-f ...

A guide to accurately accessing form data in Jquery Ajax requests

My beforeSend function is not working properly, as the background color does not change. I suspect it may be due to not correctly referencing the id variable posted from the form. Below is the relevant HTML and JS code: HTML <div id="rec<?php echo ...

Use of number type input on mobile devices in HTML

My goal is to display the up/down arrows alongside an input element with type="number" in Chrome on Android. I've adjusted the CSS to set opacity=1, but unfortunately, they are not appearing. On desktop, however, they show up without any iss ...

Refresh the HTML webpage using AJAX technology

I am trying to implement a simple html page with a single table that updates in the background every 5 seconds. The traditional solution of using <meta http-equiv="refresh" content="5"> is not suitable as it would disrupt the user experience by displ ...

Clicking the delete button in Firebase to remove an item

I am in the process of developing a simple CRUD application and have opted for Firebase as my backend solution. While I have successfully implemented the create and read functionalities, I've hit a roadblock with the delete operation. When attempti ...

What is the best way to extract data from the ajax.done() function?

My question revolves around the function shown below: $.ajax({ url: "../../getposts.php" }).done(function(posts) { var postsjson = $.parseJSON(posts); }); I am wondering how I can access the variable postsjson outside of the .done() function sco ...

Displaying text on hover and showing a text box when a link is clicked using CSS and JavaScript

Hovering over the text will display "Edit," and clicking on it will reveal a text box. This function will be used to update content using ajax. HTML: <table> <thead> <tr> <th> Name </th> <th> Age </th> ...

The process of retrieving keys and values from localStorage in html5

I have stored some important key-value pairs in local storage. Now I need to retrieve both the keys and values, and then display them by appending the values in a list item (li). Currently, my attempt at this looks like: for (var i = 0; i < localStorag ...

Troubleshooting: Ngx-Echarts Animation Issue at Startup

I've been working on integrating ngx echarts into my Angular app, but I'm facing an issue with the animation during the initial load of the chart. Take a look at this Stackblitz example where you can see that the bars render quickly on initial lo ...

Remove the image by clicking on the "X" icon located on the top right corner of the image

One of my tasks involves deleting an image by clicking on the "X" mark located at the top right corner of the image. To achieve this, I referred to this CSS fiddle http://jsfiddle.net/yHNEv/. Sample HTML code: <div class="img-wrap"> <span ng-c ...

Position the div in the center with overflow properties

After returning to web design as a hobby, I've encountered some difficulties. I'm currently trying to center a div that contains other centered divs. However, when resizing the page, I want to ensure that the divs remain centered and their overfl ...

The jQuery hide and show functions lack precision and are too indiscriminate, requiring a more targeted approach

I am designing a unique contact card that captures a user's name and description from an <input> field and appends the input information into a <div> element. This is the flow I am aiming for... User inputs their first and last name alo ...

Tips for making markers act responsively

Recently, I put together a collection of rain radar images that can quickly show whether rain is on the way or not. As you resize the URL window, the two pictures positioned side by side will also shrink. For an illustration, check it out here ... Each p ...

Styling with CSS: Making a div's height fit within another div, including padding

Looking at the image, there seems to be a div with a width of 100% and no defined height. Instead, the height adjusts based on the content, with top and bottom padding in percentages. The issue arises when trying to place navigation buttons on the right si ...

Is there a way to soften the repetitive elements in the background image?

Is there a method to blur the part of a background image that is repeated, such that if it is positioned in the center, the sides of the image appear blurry as well? ...

Move the aircraft across the display in a lively animation

I need help creating an animation where a plane flies across the screen, exits from the left side, and re-enters from the right side in a continuous loop. How can I achieve this effect to make the plane fly endlessly across the screen? Here is my code: ...

What are some methods for embedding HTML content onto a specific section of a webpage in a Node.js + Express application, without displaying it as the

Objective My goal is to insert a portion of HTML into a web page. Issue The page is not displaying properly with the desired styling and layout. I have a specific page that must be written in plain HTML rather than Jade, although I will still be usin ...

Can you please explain what shape-id denotes and provide guidance on locating it within a CSS file?

I have a question that may seem trivial to experienced web developers - what exactly is "shape-id"? For example, in the code <hr shape-id="2">. I'm currently attempting to change the color of the horizontal lines (hr) on my website, but I can& ...

Tips to enhance web page loading speed when parsing JSON data from a URL

While extracting data from a JSON file accessed via URL, I am experiencing long loading times. Surprisingly, the website manages to display the same data only when it's ready, and I aspire to achieve a similar functionality. I have yet to discover a ...