Achieve a translucent look for your text with a stylish border

I am attempting to showcase a number on a background image. Currently, my setup looks like this:

body {
  background-image: url("https://i.picsum.photos/id/10/800/800.jpg");
  background-repeat: no-repeat;
  background-position: center;
  width: 400px;
  height: 400px;
}

#age {
  /* 1 pixel black shadow to left, top, right and bottom */
  text-shadow: 2px 0 0 #fff, -2px 0 0 #fff, 0 2px 0 #fff, 0 -2px 0 #fff, 1px 1px #fff, -1px -1px 0 #fff, 1px -1px 0 #fff, -1px 1px 0 #fff;
  font-family: sans;
  color: transparent;
  font-size: 40vw;
}
<h1 id="age">27</h1>

While this setup is close to what I want, the number always appears with a white fill. What I aim for is to have the number displayed prominently, filled with transparency only, and enclosed in a white border. My ultimate goal is to create a counter effect where it visibly counts up to the specified number. However, for now, I simply need help displaying the number without the white fill.

Is there a way to achieve this and show only the white border while letting the background shine through the empty space?

Answer №1

If you're looking for a solution to create outlined text in CSS, one option is to utilize the experimental -webkit-text-stroke property (refer to this helpful response):

#title-container {
  background-image: url("https://images.unsplash.com/photo-1483651646579-612f844a99cc?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260");
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
  width: 500px;
  height: 300px;
  display: flex;
}

#title {
  margin: auto;
  -webkit-text-stroke: 5px black;
  font-family: 'Roboto', sans-serif;
  color: transparent;
  font-size: 50px;
}
<div id="title-container">
  <h1 id="title">Hello World</h1>
</div>

Alternatively, consider using pre-designed fonts that meet your requirements (such as these suggestions).

Answer №2

In the year 2020, a problem arose concerning:

  • text-stroke
  • text-stroke-width
  • text-stroke-color
  • text-fill-color

The issue lies in the fact that even though these properties have been in existence for more than five years, they are still considered experimental features and have not yet been officially included in any specifications by organizations such as W3C or WHAT-WG.

As a result, despite widespread browser support for text-stroke, all browsers (including Firefox) only provide support for text-stroke when it is prefixed with -webkit:

-webkit-text-stroke

For reference: https://caniuse.com/#feat=text-stroke


If using the -webkit- prefix is not an issue for your project, then there may be no cause for concern.

However, if compatibility becomes a concern, here is an alternative method to achieve a visually appealing text-stroke effect by outlining a transparent number within:

<h1 id="age">27</h1>

This can be achieved by applying an SVG image background to the <h1> element.

Here's a demonstration:

body {
  display: flex;
  justify-content: center;
  align-items: flex-start;
}

#age {
  position: relative;
  font-family: sans-serif;
  color: rgba(0, 0, 0, 0);
  background-image: url("https://images.pexels.com/photos/1749/fire-orange-emergency-burning.jpg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940");
}

#age[data-age="27"]::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  background-image: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' viewBox='0 0 400 400'><text x='204' y='320' text-anchor='middle' stroke='rgb(255, 255, 255)' stroke-width='6' fill='rgba(0, 0, 0, 0)' style='font: 320px sans-serif; font-weight: 700;'>27</text></svg>");
}

#age,
#age[data-age="27"]::after {
  width: 400px;
  height: 400px;
  background-repeat: no-repeat;
  background-position: center;
}
<h1 id="age" data-age="27">27</h1>

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

Load form elements using jQuery by triggering an onchange event on a drop down box via ajax

When selecting an option from the dropdown menu, I need to dynamically load form elements based on a statement <select name="type" id="type"> <option value="1">input</option> <option value="2">text</option> </select& ...

How to Center a Bootstrap List on Tablets and Mobile Devices

I've run into a bit of a snag and despite my efforts to research online, I'm unable to find a solution. Currently, I am utilizing bootstrap to develop my website. However, when I resize the page to a mobile or tablet size, my navbar list shifts ...

When using Javascript's textContent on Node, it fails to retrieve a Unicode character

Issue with retrieving Unicode characters using Javascript textContent on Node I am facing a problem where the textContent method does not return a unicode character stored in a Node. How can I efficiently retrieve unicode characters using textContent or a ...

What is the method for rearranging the order of Bootstrap 5 columns within nested elements without using hidden duplicates?

This snippet of code is designed to show blocks in different ways on mobile and desktop devices. <div class="col-12 col-sm-6 order-sm-1">TITLE</div> <div class="col-12 col-sm-6 order-sm-0">IMAGE</div> <div cla ...

What could be causing the cell containing my image to not resize along with the image?

I'm attempting to create a table with 4 cells, each containing an image. My goal is to use a test image for now and set the size of the image in percentage so that it resizes with the window. However, I am facing an issue where when I specify the imag ...

Why are all the values in my list appearing as undefined after parsing with JSON?

Just starting out with this project. I'm attempting to print a list, where the names come from a button. However, all the names are showing up as undefined. I suspect it has something to do with local storage, but I can't pinpoint the issue. < ...

Dynamically switch between showing more and showing less content on each div

Whenever the specific show more button is clicked, the content should be displayed without causing the entire component to re-render. I have tried using a useState, but each time the button is clicked, the whole component re-renders, resulting in long load ...

Tips for preventing breaks in typography

I have implemented a material ui Typography component, but it's causing a line break even though there is enough space. Here is the code snippet: <Box flexDirection="row"> <Typography> Gender: <RadioGroup row ...

The CSS hover effect on a <td> element is malfunctioning and not working as expected

My goal is to create a dropdown list embedded within each td cell of a table, similar to this setup: <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> ...

Issues with Alignment of Navbar Elements in Bootstrap 3

When I test my website locally, the elements are aligned correctly as expected. However, when I view it on my live webpage, the elements are all left-aligned. I've attempted a solution from this source, but the issue persists. What confuses me is why ...

Is it best to stick with a static page size, or

While I typically design my webpages dynamically by determining the screen size and creating divs accordingly, I'm curious about the advantages of using a 'static' sizing approach (such as using pixels). Any insights on this contrasting meth ...

Error: Attempting to access the 'main' property of an undefined object - React dilemma

For my React webpage, I decided to create a theme using createMuiTheme from material-ui, and here is the palette I defined: palette: { primary: { main: "#333333" }, secondary: { main: "#727171" }, background: { paper: " ...

How can I assign an attribute to an element that has been dynamically generated?

Upon receiving a response from a $.post, I found that it contains various elements including <a> and <img>. To prevent the <a> element from performing its default actions when clicked, and to disable the draggable option for both the < ...

The controller consistently receives a null value from JSON for only a specific field

Hello everyone! Prior to asking my question, I came across the following posts: questions/11344035 - questions/15939944 - questions/9412449 - questions/9162359 - questions/1551263. Unfortunately, none of them were able to solve my issue... :( This is my ...

In what way can you reach an unfamiliar form within a controller?

I am working with multiple dynamically generated forms, each associated with a different model. In my controller, I need to iterate through all the errors within the forms. I assign form names based on the models. <form name="{{myForm}}" novalidate> ...

A fixed header for tables that shifts along with horizontal scrolling

Seeking assistance with a persistent issue - I have a table with a fixed header on vertical scroll (see CSS and Javascript below), but the header fails to move horizontally when I scroll. window.onscroll = functi ...

Stable footer slides in Bootstrap

Currently, I am in the process of developing an application using Angular JS, Bootstrap, and Jquery. Everything seemed to be going smoothly until I encountered an issue with the fixed footer on mobile browsers. Whenever I click on an input box, the footer ...

What is the best method for finding and observing the Javascript code being utilized on a webpage?

Is there a way to find and access specific Javascript used on a webpage? Imagine browsing through a webpage's source code and stumbling upon an element like this: <img border="0" alt="" onmouseout="hidetrail();" onmouseover="showtrail('imag ...

"Can I align a div in the center using inline-block with an

Despite my efforts, I still can't figure this out. My apologies for the duplication. I attempted to use text-align: center, margin: 0 auto;, and display: flex;, but none of them worked as expected in the end. I apologize for the messy formatting. ...

Is it possible for a Simplemodal popup to appear only once per user session

I'm completely new to javascript and jQuery. Recently, I've started using SimpleModal basic from SimpleModal to show a popup upon visitors landing on my website. Everything seems to be working perfectly, but there's one issue - the popup kee ...