Is there a way to partially conceal the H2 text using CSS?

Is there a way to partially hide H2 text using CSS? I have the following code:

HTML:

<div class="twocol-box1 superflex-content-6-12">
    <h2 data-content="My - Text&amp;nbsp;<span style=&quot;float:right;&quot;>May 16, 2022</span>">My - Text&nbsp;<span style="float:right;">May 16, 2022</span></h2>

CSS:

.twocol-box1 h2, .twocol-box2 h2:before {
    content: "My - ";
    position: absolute;
    z-index: 1;
    background-color: white;
    color: white;
}

.twocol-box1 h2, .twocol-box2 h2 {
    position: relative;
    z-index: -1;
}

Updated with Fiddle: https://jsfiddle.net/awm2rs7n/

In the example above, I am trying to hide "My -"

Expected Output: Text May 16, 2022

Your advice is appreciated. Thank you.

Answer №1

Using a clever CSS trick to hide text with background color:

#hideme {
  position: relative;
}

#hideme:before {
  position: absolute;
  width: 50px;
  top: 0;
  bottom: 0;
  z-index: 2;
  background: white;
  content: ""
}
<div class="mytext">
<h2 id="hideme">My - Text<span style="float:right;">May 16, 2022</span></h2>
</div>

Answer №2

Currently, there isn't a straightforward method to achieve this in CSS. However, you can use a workaround by utilizing the :before pseudo-element along with content to hide the desired text. It is recommended to utilize JavaScript to remove the text programmatically for better control.

If the text remains constant, this approach will suffice. Essentially, the solution locates the text and conceals it.

h2 {
  position: relative;
}

h2:before {
  content: "My - ";
  position: absolute;
  background-color: white;
  color: white;
}
<div class="mytext">
  <h2>My - Text<span style="float:right;">May 16, 2022</span></h2>
</div>

Answer №3

One option is to include another span element with a specific class that has a display property set to none.

span.hidden {display: none;}
<div class="mycontent">
<h2 class="partialhide">
<span class="hidden">.My - </span>Content<span style="float:right;">April 27, 2023</span></h2>
 </div>

Answer №4

If you want to hide part of an h2 element, you can use a span to wrap that specific section and then apply a CSS class to it for styling using CSS properties. Below is an example code:

.hide {
  display: none;
}
<div class="mytext">
  <h2 class="hidepartial"><span class="hide">My - </span> Text<span style="float:right;">May 16, 2022</span></h2>
</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

Creating a CSS grid layout with a scrollbar positioned on the left side and content filling from left to right

How can I move the scrollbar to the left while keeping ng-repeat population from left to right? I'm using an ng-repeat to fill a grid that is 3 by X (Width by height). By default, the scroll bar appears on the right side. I want to position it on the ...

The level of transparency linked with text in a blockquote

I've spent hours on this and I'm completely lost. Even though I believe my code is correct, it keeps telling me it's wrong. I don't know where to go from here. The task requires adding a style rule for the blockquote element that chan ...

Leveraging JavaScript for Validating Radio Buttons

I've been following a tutorial guide on this specific website , but I'm encountering some difficulties despite following the exact steps outlined. Can someone provide guidance? Here is what I have done: <html> <script> fu ...

Experiencing Difficulty Retaining Checkbox State Using LocalStorage Upon Page Reload

At the moment, I have a script that automatically clicks on a random checkbox whenever the page loads or refreshes. Through localStorage, I can also view the value of the input assigned to the randomly selected checkbox. However, I'm facing an issue ...

Interactive Dropdown-Buttons dynamically inserted

I have been utilizing a third-party library called Materializecss from this link: My issue arises when attempting to create a Dropdown feature upon clicking a button. It works perfectly fine when I manually place the button in the HTML and click on it, tr ...

The animation for the CSS background image simply refuses to cooperate

My goal is to create a CSS background animation using the Body tag. Below is the code I am currently using: body { animation-name:mymove; animation-duration:5s; animation-direction:alternate; animation-iteration-count:infinite; -webkit-animation-name:mymo ...

The scrollbar remains visible on mobile devices

I'm attempting to remove the scrollbar from all elements in my Vue + Vite application. I do not want to disable scrolling, just hide the scrollbar itself. To achieve this, I have employed the following code snippet. *::-webkit-scrollbar { display: ...

Adsense Responsive Ads by Google Are Not Displaying

Click on this link to see 3 different types of ads - Right, Left, and Header Ads. These ads are supposed to be responsive and have an "Active" status. However, the issue is that the Responsive ads are not displaying properly. After some research, it seems ...

Fixed positioning upon scrolling begins prior to reaching the uppermost point (top div)

Currently, I have implemented a feature where #filter (the white select list in my sidebar) becomes fixed when it reaches the top of the page and stays there while scrolling until it reaches the footer and is released. However, I encountered an issue wit ...

Error message: Python HTML Parsing with UnicodeDecodeError

When utilizing html.parser from the HTMLParser class to extract data from a set of html files, everything works smoothly until an error is triggered by a specific file: UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8a in position 41 ...

Unable to bypass YouTube advertisement

I am currently experimenting with using nodejs puppeteer to test if I can bypass the ad on Youtube. Although this is just for testing purposes, I am facing some challenges with getting it to work as expected. I have implemented a while loop to search for ...

Creating a Form in a Popup with Bootstrap

I have implemented Bootstrap on my website. Check it out at: hubb.tekkkz.com I am facing an issue where, when clicking on the login/register button on the right, the modal pops up. However, the user input elements within the modal are not taking up the ful ...

Combining two forms into one PHP page, but intending to submit only a single form

I'm facing an issue on my page where I have both a sign-in and a sign-up form. Whenever I click on either the sign-in or sign-up button, it always ends up submitting the sign-up form. What am I doing wrong? Here's the PHP code snippet: if($_SE ...

This is an alignment of the background image ('relative') to the right side

I am facing an issue with the header section of my website. The background-image is styled using the following CSS: .header { position: relative; min-height: 436px; background: url("../img/holland-canal.jpg") no-repeat; background-size: co ...

What could be causing the flexbox code to not take effect and the text to refuse wrapping?

I've been utilizing flexbox to style my content, and it worked seamlessly on the first three divs. However, when I attempted to apply the same styling to the fourth one, it didn't quite work out as planned. Despite trying options like flex-wrap t ...

Designing a Dynamic Floating Element that Shifts with Scroll Movement

Hey there everyone!, I am currently working on a project in Wordpress and I was wondering if anyone has experience creating a floating widget that moves along with the page as you scroll. Any suggestions on how to achieve this? Would it involve using Javas ...

Modify the c3 axis labels using CSS

I've been using the c3 package in R, which serves as a wrapper for the C3 javascript charting library created by Masayuki Tanaka. One issue I encountered is that my c3 chart was cutting off the last date on the x-axis. After inspecting it in Chrome I ...

The userscript will keep the window open as long as it remains inactive

Hello everyone, I'm excited to join the StackOverflow community and dive into userscripts for the first time! Putting aside any unnecessary details, I'm encountering a small issue with a script I recently created. (function () { $("#enbut"). ...

The PHP file I'm trying to access isn't receiving the GET request from my PHP script

Basically, I want a button on one php page to trigger the printing of a new php page. This feature is working perfectly. The only issue is that I am trying to ensure the user ID gets passed over to the second page and displayed there. Strangely, it seems t ...

Tips for resolving alignment issues in a chat box:

I am currently working on adjusting the alignment of my chat box app. I have placed 3 div elements with the class .bubble inside a div with the class .chatlines, but they appear to be clustered together instead of aligned properly. It's challenging to ...