I try to set a new text using CSS and pseudo element. How to add a text like Call us now! using CSS if I have a background image as attached file.
Images site is 70 px as height value.
I try to set a new text using CSS and pseudo element. How to add a text like Call us now! using CSS if I have a background image as attached file.
Images site is 70 px as height value.
Create an element, then set the background image via css.
<div class="call">Call Us Now</div>
.call { background-image: url("/img/curve.png"); }
Youâll probably need to play with sizing or coverage but thatâs the basic
idea.
Hi there toplisek,
dispense with the image, CSS is now your friend.
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1">
<title>Untitled document</title>
<!--<link rel="stylesheet" href="screen.css" media="screen">-->
<style media="screen">
body {
background-color: #f0f0f0;
font: normal 1em / 1.6em sans-serif;
}
div {
display: flex;
justify-content: center;
align-items: center;
width: 8.749em;
height: 2.625em;
margin: auto;
border-width: 0.2em 0.2em 0.2em 0;
border-style: solid;
border-color: #25aae2;
border-radius: 0 1.3125em 1.3125em 0;
box-sizing: border-box;
background-color: #fff;
font-size: 1.5em;
color: #25aae2;
}
</style>
</head>
<body>
<div>Call us now</div>
</body>
</html>
coothead
Thank you for the message.
To add your text to the html element using css try this code:
<button class="btn"></button>
.btn {
width: 200px;
height: 70px;
background-image: url("img-sample.jpg");
position: relative;
}
.btn::after {
content: 'Call us now!';
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
Welcome to the forums, @pmdTemplates.
There is a problem with your approach.
Accessibility
Content added using pseudo-elements is not inserted into the DOMâit is only visually displayed. Hence, screen readers wonât be able to access and read the content generated using pseudo-elements. So, it is recommended that you donât use pseudo-elements to insert vital content into a page (such as footer notes that are relevant to the article, for example).
Pseudo-elements are mostly used to insert and style cosmetic content, and should not be relied on to insert content that is relevant to the meaning and completeness of the content on the page.
Also, since the content inserted using pseudo-elements is not inserted into the DOM, this means that you cannot attach any event handlers to it using JavaScript.
Part of my job at work is accessibility related, and I was recommended this screenreader for testing - https://www.nvaccess.org/download/
It actually does pick up pseudo elements. E.g. I had a â+â plus sign in a pseudo element (normally itâs a fontawesome font icon) and it actually read out the standard text, and read out âplusâ at the end. It picked it up.
It ignores font icons though, but regular text like the â+â was picked up. Not saying all screen readers do that but it was a cool little thing we discovered and we were shocked at it happening.
Support is better, however its still at this point a failure:
CSS content Failure Criterion
Not to mention to cootheads example, why use an image when you can use CSS.
I think in this instance, there is no need to use pseudo elements or images.
There are times these things come in useful, but I donât think this is one of them.
I think thatâs the caveat: behaviour differs with different screen reader / browser combinations, and just because something works in one instance, it doesnât necessarily work in another.
Iâm also aware that users of assistive technology are often locked into using older versions because of the prohibitive costs of updating.
Thanks @TechnoBear for your comments but the initial request from @toplisek was to add text to the button using css not html.
And I also recommend to add role=âbuttonâ attribute to the <div>
element or better to wrap 'Call us now! â into the <button>
element as it has built-in keyboard accessibility, so users can navigate between buttons using the âTabâ key and activate their selection using âReturnâ or âEnterâ.
I can see no reference to a button element in
his post.
coothead
Okay and what WE ALL have said is âbad idea use markup / cssâ. If one comes asking for advice and a bad implementation is given, expect it to be called out.
Just because I can tab to your button⌠does not make it accessible. ⌠Whatâs the hover state??? Whats the focus state?? And again⌠css text inject is bad mâkay?
Hello @coothead,
Yes, you are correct he didnât mention a button element, but call to action text like âCall us now!â I used to add into the <button>
or <a href="">
elements in my practice.
I fully agree with you @bwclovis that to add a text to the button with the help of pseudo element is a bad idea. Just thought that it was a kind of a non-trivial task for @toplisek.
âŚit brings this little adage to mindâŚ
âif you find yourself in a hole, stop diggingâ
coothead
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.