Does the 'span' element negatively impact the vertical alignment of text?

While working on my code, I noticed an issue with vertical alignment when using span tags to change the background of the text based on its content. The problem occurs specifically when viewing the code in the Android Chrome browser. You can view the initial version of the code on Code v.1 (JSFiddle) and see the result below:

https://i.sstatic.net/bvnNS.jpg

However, once I removed the span tags, the vertical alignment appeared fine. You can check out the updated version of the code on Code v.2 (JSFiddle) and see the revised result below:

https://i.sstatic.net/4FTqG.jpg

It's important to note that the preview window on JSFiddle may be misleading. When comparing Code v.1 and v.2, you'll notice that the positioning of the text relative to its background differs between the two versions: Code v.1 places the text at the bottom, while in Android Chrome, it appears at the top (as shown in the screenshot).

In addressing this issue, please keep in mind what I am not looking for:

  • Solutions involving JavaScript - these are not suitable for my case.
  • Fine-tuning the padding tag manually - the issue lies within the span tag itself, not in the padding.

Here is Code v.1:

.part_of_speech {
  font-family: Verdana, Geneva, sans-serif;
  color: #fff;
  font-weight: 700;
  border-radius: 0.3em;
  padding: 0.1em 0.2em 0.1em;
  font-size: calc(0.4em + 1.5vw);
}

.noun {
  background-color: #2196f3;
}
// Remaining CSS classes

/* HTML Code Snippet */
<div class="part_of_speech">
  // Span elements here
</div>

Here is Code v.2:

.part_of_speech {
  font-family: Verdana, Geneva, sans-serif;
  color: #fff;
  font-weight: 700;
  border-radius: 0.3em;
  padding: 0.1em 0.2em 0.1em;
  font-size: calc(0.4em + 1.5vw);
  display: inline-block;
  /* NEW */
  background-color: #2196f3;
  /* MIGRATED FROM .partofspeech CLASSES BELOW */
}


/* Deleted partofspeech classes due to removal of <span> tags */
<div class="part_of_speech">NOUN </div>

Answer №1

The resolution turned out to be simpler than expected: there was a clash between the presence of div within each span. By removing the div component from the spans and adding a span selector to the overarching div, the code started functioning correctly as intended:

.part_of_speech {
  font-family: Verdana, Geneva, sans-serif;
  color: #fff;
  font-weight: 700;
  font-size: calc(0.4em + 1.5vw);
}

.part_of_speech > span {
  padding: 0.1em 0.2em 0.1em;
  border-radius: 0.3em;
}

.noun {
  background-color: #2196f3;
}

.adjective {
  background-color: #ff9800;
}

.verb {
  background-color: #4caf50;
}

.adverb {
  background-color: #f94432;
}

.conjunction {
  background-color: #a047f9;
}

<div class="part_of_speech">
  <span class="noun">NOUN</span>
  <span class="adjective">ADJ.</span>
  <span class="verb">VERB</span>
  <span class="adverb">ADV.</span>
  <span class="conjunction">CONJ.</span>
</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

Is it permissible to have empty elements in HTML for accessibility purposes?

Can you help me with a question I have? I was informed that utilizing code like the following is not recommended for creating accessible HTML: <span class="fa-stack fa-lg"> <i class="fa fa-cloud fa-stack-2x"></i> <i class="fa fa-c ...

How can I compare the current page URL with the navigation bar?

Looking to compare the URL of a current page to an element in the navigation bar using jQuery. Started by assigning the current URL to a variable: var currentPage = window.location.href; Then utilized an .each function to loop through each item in the n ...

The ion-range slider does not function properly when trying to display a central label

After customizing the ion range slider to suit my needs, I am looking for some simple functionalities: 1. The label should be centered. 2. When clicking on the label, the handle should move accordingly. 3. There should be a color gap between two valu ...

How can I implement a dynamic sidebar drawer in Bootstrap for my navbar?

Is it possible to achieve a layout similar to this example: I've come across some online examples using different versions of bootstrap, but they tend to alter the css too much, making it harder to grasp the concepts of bootstrap. I am curious if it ...

Retrieve an image using screen resolution parameters [using only HTML]

During a recent interview, the interviewer posed an interesting question regarding 2 images: There is one large resolution image that needs to be displayed on laptops and desktops. There is also a small image that should only be shown on mobile devices. ...

Having trouble accessing the value of a node in JavaScript, as it keeps returning as "null"

Experimenting with converting HTML elements into lists. The objective is to generate a list of all values in a table upon clicking the "page next" buttons on it. Afterward, an alert should display the value of the first item in the list, which corresponds ...

What is a way to increase the boldness of an HTML anchor tag when hovering without using CSS, but by modifying the inline style?

Is it possible to make the following anchor tags appear bolder on hover using inline styles, without utilizing any external or embedded CSS file? <a href="https://en.wikipedia.org/wiki/Internet_of_Things" target="_blank">Internet of things<br& ...

Generating a dynamic triangle within a div while ensuring it fits perfectly with the maximum width

I need help with creating a responsive triangle on a web page that takes three inputs. The challenge is to adjust the size of the triangle so it fits inside a div element while maintaining the aspect ratio of the inputs. For instance, if the inputs are 500 ...

Tips on moving information from one form to another after referencing the original page using Javascript and HTML

Imagine this scenario: A page with three text fields, each with default values. There is also a hyperlink that performs a database lookup and returns parameters to the same page where the hyperlink was clicked. The goal is for the text boxes to be automa ...

Displaying the structure of a MongoDB database using Express and Angular in a tabular format

I am looking to present the data from MongoDB in a table format using HTML along with Node.js, Express.js, and Angular.js. Currently, my approach is as follows: route.js app.get('/superhero', function(req, res) { superhero.superhero_list(r ...

Eliminate JavaScript that blocks rendering:

Currently, I am working on optimizing my website's speed and aiming for a perfect 100/100 score on PageSpeed Insights. The website in question is www.chrispdesign.com. Despite my efforts to relocate the code and make necessary adjustments, I have bee ...

How can you call a function in JavaScript based on a condition?

Is there a way to conditionally truncate text using a function called Truncate only when the offsetHeight of the left div with class demo is greater than the offsetHeight of the right div? If this condition is not met, then simply return the original conte ...

Delay the closure of a window by utilizing a straightforward method when selecting the "X - CLOSE" option with the following code: `<a href="javascript:window.open('', '_self').close();">X - CLOSE</a>`

I need to implement a delay when users click on the link to close a window. The purpose of this delay is to allow time for playing random "signoff" audio files, such as "Thanks!" or "See you next time!". Currently, without the delay, the audio stops abrupt ...

Add a stylish diagonal touch to your header and footer using CSS

I'm exploring the world of CSS and design, aiming to incorporate a stylish diagonal line into the header and footer of my React.js web application using CSS. However, despite trying various solutions, I haven't been successful in achieving the de ...

Tips for obtaining the XPath for an unordered list and a span element

My goal is to find the xpath that will select the seat "12A" from a webpage containing a list of seats. Here's the source code: <div class="seats" style="top: 48px;"> <ul class="row_12 leftOfAisle"> <li><a class="" data-r ...

The Bootstrap nav-link class functions perfectly in Firefox, smoothly guiding users to the appropriate section. However, it seems to be experiencing some issues

I am currently working on customizing a one-page web template and I don't have much experience with Bootstrap. The template can be found at . My issue is that the menu items are not functional in Chrome. When I click on any menu item, nothing happens ...

The Angular 2 view will remain unchanged until the user interacts with a different input box

I am currently working on implementing form validation using Reactive Forms in Angular 2. Here is the scenario: There are two input fields Here are image examples for step 1 and step 2: https://i.stack.imgur.com/nZlkk.png https://i.stack.imgur.com/jNIFj ...

Show a pair of pictures every X hours using Javascript

Trying to create a code that displays different pairs of images each day, but struggling to make it select them randomly. Instead, the code currently just goes through the array one by one. I'm not great with Javascript and haven't found a way to ...

Elevate the placeholder in Angular Material 2 to enhance its height

I want to make material 2 input control larger by adjusting the height property of the input using CSS <input mdInput placeholder="Favorite food" class="search-grid-input"> .search-grid-input { height:30px!important; } As a result, the image o ...

Is it acceptable for a video to autoplay even if it is not connected to the DOM?

Consider the following three scenarios: document.adoptNode, document.importNode, and document.createElement with assigned properties. In all cases, the video autoplay feature is activated even when it's not connected to the DOM. This behavior diffe ...