The visibility of the button background created with :before will only be apparent when a content property is specified

There's an unusual issue I've encountered... I have a simple button, nothing out of the ordinary, just a basic button like this

<button class="btn btn--error">
    Button Text
</button> 

Now, I want to add an image next to my button text using CSS instead of placing an img tag within the button. To achieve this, I'm utilizing the :before pseudo element, and here is my SCSS code:

.btn {
  font-family: 'Roboto', sans-serif;
  text-align: center;
  text-transform: capitalize;
  text-decoration: none;
  outline: 0;
  margin-bottom: 2px;
  padding: 0;

  &:before {
    background: url("/assets/images/icons/svg/ic_add_24px.svg") no-repeat top left;
    background-size: contain;
    content: '';
    width: 18px;
    height: 18px;
  }
 }

The image location is correct, but for some reason, it doesn't display in the browser...

https://i.stack.imgur.com/R2Goj.png

However, by adding some content to the content property within the :before pseudo element as shown below:

&:before {
    background: url("/assets/images/icons/svg/ic_add_24px.svg") no-repeat top left;
    background-size: contain;
    content: '___';
    width: 18px;
    height: 18px;
  }

The background image now appears!

https://i.stack.imgur.com/MZFcu.png

If you have any suggestions on how to display the background image without inserting text into the content property, your help would be greatly appreciated.

Answer №1

Click here for the solution

By adding inline-block to pseudoelement, we can change their default inline behavior. Pseudoelements have a width and height of 0 by default if they have no content.

.btn {
  font-family: 'Roboto', sans-serif;
  text-align: center;
  text-transform: capitalize;
  text-decoration: none;
  outline: 0;
  margin-bottom: 2px;
  padding: 30px;

  &:before {
    background: url("https://cdn4.iconfinder.com/data/icons/new-google-logo-2015/400/new-google-favicon-512.png") no-repeat top left;
    background-size: contain;
    content: '';
    width: 18px;
    height: 18px;
    display: inline-block;
  }
 }

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 the parameter retrieval method correct or erroneous?

I am looking to retrieve data by clicking a link. Here are the links available: <a class="note" id="' . $data["p_id"] . '" value="1" href="javascript:void(0)">+1</a> <a class="note" id="' . $data["p_id"] . '" value="-1" ...

Difficulty Clicking Links with CSS 3D Transformation

I'm currently working on implementing a CSS 3D transform effect on my website. However, I am encountering some issues with clicking the links once the card has been flipped over. Question Why is this happening? How can I resolve this so that I can ...

Controlling the back DIV while maintaining overlapping layers

I successfully positioned my right hand content on top of the leaflet map in the background using CSS properties like position and z-index. However, I am looking for a way to make the overlapping div not only transparent but also non-interactive while stil ...

A guide on utilizing Xpath to reference a tag within a class

Trying to obtain the website link by using xpath and selenium with a reference to the class pv-contact-info__contact-type ci-websites. [Here is the html snippet being referenced][1] sel = Selector(text=driver.page_source) website = sel.xpath("//*[@class= ...

PHP: Dynamically update div content upon submission

I am attempting to update the "refresh" div after clicking the Submit button and also at regular intervals of 5 seconds. Despite looking through various resources, I have not been able to find a solution that meets my requirements. <script src="h ...

JavaScript function unable to access static file for image

I need assistance with dynamically displaying an image (a checkmark or "X") based on a variable. When I insert the image using a script directly in the HTML file, it functions correctly. However, when attempting to set the image from within the createEasyD ...

Divide the PHP code into separate files

Can someone help me with a coding dilemma I have? I couldn't find any information on Google or this site about it. A friend of mine who has experience in website development and runs his own business uses an interesting system that is new to me. He ...

Is there a potential bug in Chrome with the submit button appearing "depressed" when focused?

Why do submit buttons act differently in Chrome? <input type='text' placeholder='Dummy Input'/> <input type='submit'/> In Chrome, the active 'depressed' state of the submit button will only occur if the ...

What is the process to discover, upload, and implement a custom font?

In my quest to locate and incorporate a font named neontubes, or if not available, a font called freestyle script, I have learned that I must obtain an src which is then included in my style.css file and subsequently designated in the styling rule like s ...

Next.js encountered an issue: React.Children.only function was expecting to receive only one React element child, but received more

I have created a component named Nav located in the components directory. Here is the code for it: import Link from 'next/link'; const Nav = () => { return( <div> <Link href="/"> <a> Home </a> ...

Making all elements the same height in rows with w3.css

Struggling to achieve equal height for the elements in my rows. Any suggestions on how to make this work? Thank you and cheers. <div class="w3-row"> <div class="w3-container w3-quarter w3-green text-center matrix-element"> < ...

Why does this CSS ticker only display the first four posts?

Hello, I'm interested in adding a News Ticker to my Rails application. I came across this CSS ticker: I'm facing an issue where it only displays the first four posts. I believe the problem might not be with my data retrieval using the .each loop ...

Embed the Facebook Share Button using iFrames

I currently have a website that showcases news articles in a list format using PHP and MySQL. You can check it out here: My goal is to incorporate a Facebook share button/link on each article so that users can easily share individual posts on their own Fa ...

Align an image in the center vertically within a left-floated div with a minimum height

I am currently attempting to vertically align an image within a div that is positioned to the left, and has a minimum height of 150 pixels. The issue I am facing is that none of my attempts seem to be successful. I have tried various methods, including ut ...

Display the field names from a MySQL table on a web page

Is there a way to show all column names from a MySQL table on a webpage if only the table name is known? ...

Enhance the appearance of the popover by incorporating an arrow-like element for dismissing the

Here is some code I want to modify: https://i.stack.imgur.com/0C61Y.png I would like to add an arrow element at the top, similar to this: https://i.stack.imgur.com/pDT3s.png This will give it a popup-like appearance. Below is the CSS styling for the co ...

Is there a way to apply padding to a div element that has a display property set to table-cell?

I am trying to achieve a design where I have an image and a div containing vertically centered text. The div should have padding around it along with a border, similar to this example: Currently, this is the closest I have come to replicating the desired ...

What is the reason behind the modification in flex item size when align-items property is applied?

When creating the responsive design, I utilized flexbox. Surprisingly, without changing the size of each flex-item, applying align-items:center caused the size of the first flex item (PICTURE 2) to change when displayed in a vertical view. On the other han ...

Modifying an HTML attribute dynamically in D3 by evaluating its existing value

I'm facing a seemingly simple task, but I can't quite crack it. My web page showcases multiple bar graphs, and I'm aiming to create a button that reveals or conceals certain bars. Specifically, when toggled, I want half of the bars to vanish ...

SQL Query: Change text color based on the result number retrieved

After executing my SQL SELECT statement, I want to display every record in a div using a While statement. I would like the divs to alternate in color, for example: Result 1 = white div Result 2 = grey div Result 3 = white div Result 4 = grey div. I am un ...