Steps for positioning pseudo-elements :before and :after along with the span element

Currently experimenting with pseudo-elements and attempting to insert an image before a span within my custom button.

Initially, I designed a custom button without using the pseudo-element, and everything was centered perfectly.

However, upon adding the pseudo-element :before, I encountered issues with aligning the pseudo-element and span centrally.

Any tips on achieving proper alignment?

Thank you!

.x-box-item {
  border: 1px solid black;
  width: 300px;
  height: 50px;
  position: absolute;
  right: auto;
  left: 0px;
  margin: 0px;
}
.x-btn-wrap {
  width: 100%;
  height: 100%;
  display: table;
}
.x-btn-button {
  text-align: center;
  vertical-align: middle;
  display: table-cell;
  white-space: nowrap;
}
.x-btn-inner {
  display: inline-block;
  vertical-align: middle;
  max-width: 100%;
}
.x-btn-inner:before {
  content: '';
  width: 50px;
  height: 50px;
  background-image: url("https://gradschool.uoregon.edu/sites/gradschool1.uoregon.edu/files/facebook-like-icon-small.png");
  display: inline-block;
  background-size: 50px 50px;
}
<a class="x-box-item" onClick="alert('Hello World!')">
  <span class="x-btn-wrap">
    <span class="x-btn-button">
      <span class="x-btn-inner">
      LIKE
      </span>
  </span>
  </span>
</a>

Answer №1

To properly align it, just add vertical-align: middle; to .x-btn-inner::before.

.x-box-item {
  border: 1px solid black;
  width: 300px;
  height: 50px;
  position: absolute;
  right: auto;
  left: 0px;
  margin: 0px;
}
.x-btn-wrap {
  width: 100%;
  height: 100%;
  display: table;
}
.x-btn-button {
  text-align: center;
  vertical-align: middle;
  display: table-cell;
  white-space: nowrap;
}
.x-btn-inner {
  display: inline-block;
  vertical-align: middle;
  max-width: 100%;
}
.x-btn-inner:before {
  content: '';
  width: 50px;
  height: 50px;
  background-image: url("https://gradschool.uoregon.edu/sites/gradschool1.uoregon.edu/files/facebook-like-icon-small.png");
  display: inline-block;
  background-size: 50px 50px;
  vertical-align: middle;
}
<a class="x-box-item" onClick="alert('Hello World!')">
  <span class="x-btn-wrap">
    <span class="x-btn-button">
      <span class="x-btn-inner">
      LIKE
      </span>
  </span>
  </span>
</a>

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

Attempting to align my range picker horizontally in a single row

After selecting the date range option from my dropdown button, I noticed that the date range options are stacking on top of each other instead of appearing in the same row. Has anyone encountered this issue before? One solution I attempted was adjusting th ...

HTML5 CSS and Rails theme

I am currently utilizing a free HTML5 template found at this URL: Despite my efforts, I am unable to successfully implement the background images from the main.js file (bg01.jpg, bg02.jpg, bg03.jpg). How should I correctly reference these image files wit ...

Prevent a specific item in the navbar from collapsing when toggling the menu

My Bootstrap 4 navbar includes items on both the left and right sides. Take a look at the full version below: https://i.sstatic.net/zqxzI.jpg And this is how it appears when collapsed. https://i.sstatic.net/UmvK0.jpg My goal is to have the Sign Up butt ...

Guide on setting the BackColor of a table cell based on a database value dynamically

In the project I am currently working on, there is a request to change the background color of specific table cells based on their values. In order to achieve this, I am handling the RadGrid_ItemDataBound event and attempting to set the BackColor property ...

Using jQuery to Drag: Creating a draggable effect on a div element

I recently posted a question, but I feel like I need to rephrase it to make it more general. Basically, I have an element that can be moved around and resized using jQuery. I used CSS to attach another div to the right of this element. Initially, when you ...

Searching for a div table using XPATH in Python

Struggling to extract data from a table using Selenium in Python. Any suggestions on how to achieve this? https://i.stack.imgur.com/rPlKR.png <div class="js--property-table-body project-property-table__body"> <span aria-hidden=&quo ...

Is it possible to save a dynamic web page to a file?

I am a beginner C++ programmer diving into the world of web development for the first time. My current challenge involves finding a way to continuously capture and save the HTML content of a constantly updating third-party website onto a static HTML file ...

Creating a centralized form on the webpage

What steps can I take to modify my code to match the structure shown in the image? At present, my code appears as follows: <form id="form" action="/action_page.php"> Login ID:<br> <input type="text" name="id" id="id"> <br ...

Contact form repair completed - Messages successfully delivered

I am facing an issue with the contact form on my HTML landing page. Currently, when you click the 'Submit' button, it redirects to a new PHP page displaying 'Success'. Is there a way to make it so that upon clicking 'Submit' a ...

Instructions on how to make the image within this div expand to full screen in order to cover up the blue background color

I created a div and set its background image, but the image is not covering the full screen. There's some space around it. The blue color is the body color. Please refer to the image here I am very new to web development, so please forgive my silly ...

Steps for creating an offset in Bootstrap 4

Apologies for any confusion due to my not-so-great English skills. I'm currently utilizing the beta version of Bootstrap 4 (the latest, not alpha) and so far it's been great. However, I've noticed that in this new version, the offset class ...

I am unable to activate Wicket's onchange event

I've been trying to automate the population of three dropdown selection elements using a Chrome extension. I'm able to change the value of the first dropdown, but it doesn't trigger the necessary action that populates the second and third dr ...

When activating a bootstrap class button, I must ensure the reply-box remains hidden

let replyButton = document.getElementById('reply-button'); let cardBody = document.getElementById('card-body'); let responseBox = document.getElementById('reply-box'); let cancelButton = document.getElementById('btn btn-d ...

The reveal/conceal feature will not reveal all elements at the outset

I've been working on implementing a show/hide elements filter. However, I'm facing an issue where all elements that are supposed to display initially are hidden instead. Additionally, the 'active' class is not being properly removed or ...

Optimizing Container size for devices with smaller screens

After creating a container with a panel and a panel body, everything looked fine when viewed on desktop. However, upon checking it on my phone, I noticed a large space on the right with nothing there. If you'd like to see an image of the issue, click ...

What is the best way to turn off the annoying pop-up messages that show up for input validation, specifically the one that says "Please complete

Currently using Angular 2, my form has input fields similar to this simplified version: <input class="body-text1" type="text" [(ngModel)]="model.name" name="name" required minlength="1"> <!--more inputs like this --> Although I have implement ...

What are the steps to customize the color of an icon and text when clicked or when hovering over them

I just entered the world of coding and am currently immersed in a project. Right now, I have a side navbar and I'm attempting to change the color of the icon and text when I hover over it or click on a route. For instance, when I click on the Home lin ...

What is the process for adding information to datatables using jQuery?

I have been struggling to make a data table in jQuery function correctly. I am able to append data to the table, but the sorting, pagination, and search features are not working. Even though the data is visible in the table, it shows as 0 records. I am wor ...

Having difficulty formatting text alignment using CSS

Is there a way to maintain the alignment of text in the output of the 'About' section even when the content changes dynamically? I want the new line to appear just below 'Lorem', but currently, it shifts below the colon(:). Since the le ...

Utilizing the "row" and "column" attributes in Angular (Flexbox) results in significant spacing between input fields within the HTML code

I'm working on aligning 2 input fields side by side within the same line. To achieve this, I've been utilizing Flexbox. However, I've observed that when using Flex with both 'row' and 'column', it introduces extra spacing ...