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.

https://i.sstatic.net/2FQkD.png

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

Display the images adjacent to the usernames, followed by their respective levels underneath

<div class="players"> {{#each users}} {{#each usersInLobby}} <img src="/img/characters/{{skin}}.png" style="height: 85px; display:inline;"> <p>{{username}}{{level}}</p> {{/each}} {{/each}} ...

What is the best way to incorporate two banners on the Magento homepage alongside my flexslider?

I recently made the switch from using a camera.js slider on my website to a Flexslider. The reason for this change was that I couldn't make the camera.js slider clickable, and I wanted to eliminate the 'Click here' button. However, now that ...

Why should <template> be used in Vuetify?

Exploring the possibilities of Vuetify 2.0 in my current project has led me to dive into the v-stepper component, designed for displaying progress through numbered steps. In the example provided in the playground, I noticed the use of the <template> ...

What is causing the discrepancy in functionality between these two HTML/CSS files with identical code?

In this codepen, you'll find the first example: Subpar Pen: https://codepen.io/anon/pen/jGpxrp Additionally, here is the code for the second example: Excellent Pen: https://codepen.io/anon/pen/QqBmWK?editors=1100 I'm puzzled why the buttons l ...

What is causing my divs not to center with justify-content?

I've been working on centering two divs inside a parent div horizontally. The parent div is set to flex-direction: column so that the child divs stack one below the other, but I want them centered in the middle of the page. Although justify-content: ...

Steps for positioning a play button at the center of all images

index.html - here is the index.html file code containing all the necessary html code <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width ...

Is it possible to nest an array within a value of a Sass object?

I'm currently attempting to incorporate an array within the value of an object. $background-things: ( 'a': ['100vh', 'transparent', 'column'], 'higher': ['70vh', '#000', &ap ...

CSS3 pulsating circular Firefox bug

I am currently working on a CSS3 pulsing circle (animating scale to 1.1). To address a jumpy animation issue in Firefox, I have added a slight rotation. animation: button_pulse 2s infinite ease-out; transform: scale(1.1) rotate(0.1deg); Despite this adju ...

Angular directive button failing to trigger ng-click event

Recently, I started working with Angular and ran into an issue while trying to set up a custom directive. The purpose of this directive is to display a button upon hovering over it, which, when clicked, should trigger a function within the directive. The b ...

How to style a CSS list and center-align it

My website features a <ul> list with submenus structured like this: <div id="cssmenu"> <ul> <li class='active'><a href='index.html'><span>Home</span></a></li> <li class=&ap ...

When attempting to open the HTML file through an Express application, the background image specified by `background-image: url()` may

Upon opening the html file by directly clicking on it, the background-image styled with url() functions correctly. However, when I open this file within an express app using res.sendFile(), the background-image fails to display. Interestingly, all other CS ...

Incorporating a classList.toggle into a snippet of code

button, p, h1, h2, h3, h4, h5, a{ /* Define specific elements to use "fantasy" font */ font-family: Tahoma; } #main_body{ margin: 0px auto; background-color: #dedede; } #top_body{ /* Remove margin for simplicity */ } #t ...

methods for hiding dropdown menu when clicked in html using javascript

I have a web page with a dropdown menu as shown below: <li class="dropdown"> <a class="nav-link" href="#" id="shop_submenu" role="button" data-bs-toggle="dropdown" aria-expanded="false"> Rent ...

Behold the magic of a three-column layout with sticky scroll behavior and an absolute

I am struggling with a layout that involves 3 columns, each with its own independent scroll. The first column contains an absolute element that should overflow on the x-axis, but I am having trouble getting the overflow-x:visible property to work when comb ...

Having an issue with CSS button borders not functioning as expected for some unknown reason

My CSS button has a border added, but it doesn't seem to be showing up. Can anyone provide suggestions on how to fix this issue? Below is the snippet of the code: body { background: grey; } a.button { display: inline-block; -webkit-appearanc ...

StorageLimit: A new condition implemented to avoid saving repetitive values in the localStorage

Is there a way to store the text of an li element as a localStorage value only once when clicked? If it already exists in localStorage, a second click should have no effect other than triggering an alert. I'm currently using an if statement inside a ...

Creating a proper nth-child CSS for multi-level HTML elements involves understanding the structure of the elements

My current project involves a screen where widgets can be dynamically inserted into the DOM. I am looking to adjust the z-index based on the number of times the class decrement-z-index appears. This is the CSS code I have written in an attempt to achieve ...

Can someone please help me convert this jQuery code into vanilla JavaScript?

My Cordova app has an email sending function that utilizes jQuery. During debugging, the ajax function works perfectly in my browser, but once I build the app and test it on my phone, it stops working. I encountered a similar issue before, which was resolv ...

A pair of buttons each displaying a unique div block

I am facing an issue with my jQuery script. I want to be able to click on one of the previewed text associated with a button and then have the other one close automatically. The desired effect is for the text to slide down using slideDown() and disappear ...

Looking for assistance with aligning UL elements in CSS using absolute positioning for a dropdown effect

I'm currently working on implementing a language switching feature on this website. The concept involves using a SPAN element that reveals a dropdown box (based on UL) containing a list of available languages when hovered over. Below is a preview of t ...