CSS- Strategically placing and centering images above specific keywords in (any) HTML content without disrupting the flow of text

My main objective involves dynamically inserting images above text on any given page using a content script. The challenge lies in maintaining the proper alignment of the text after adding the images. To achieve this, I surround the words where the image is injected with a span element that has a specific attribute. Then, my script (the content script of a Chrome extension) wraps that span with another span (the parent in my CSS) that includes the image wrapped within its own span.

The issue at hand pertains to a CSS problem; when injecting the images, the text's lining gets disrupted.

To visualize this more clearly, you can refer to the example at https://jsfiddle.net/wyfumrx6/2/

In the current implementation, I utilize inline-flex and flex-direction:

Here's an HTML example:

<a href="https://www.wikihow.com/Make-a-Good-Cup-of-Tea" style="/* line-height: 57px; *//* height: auto; */font-size: 150%;font-family: Gotham, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;/* height: 55px; *//* display: inline-flex; */">
  <span id="div0" class="aui_top_in_page_parent" tabindex="0" style="">
        <span id="span0" class="aui_top_in_page"><img src="http://www.arasaac.org/repositorio/thumbs/10/50/1/12313.png"
                class="aui_header_images"></span>
  <span aui-symbol="14907" aria-describedby="span0" accesskey="Shift + E">How
        </span>
  </span>
  <span id="div5" class="aui_top_in_page_parent" tabindex="0">
        <span id="span5" class="aui_top_in_page"><img src="http://www.arasaac.org/repositorio/thumbs/10/50/7/7194.png"
                class="aui_header_images"></span>
  <span aui-symbol="17982" aria-describedby="span5" accesskey="Shift + E">to</span></span> <span id="div6" class="aui_top_in_page_parent" tabindex="0"><span id="span6" class="aui_top_in_page"><img src="http://www.arasaac.org/repositorio/thumbs/10/50/3/32751.png"
                class="aui_header_images"></span><span aui-symbol="15410" aria-describedby="span6" accesskey="Shift + E">Make</span></span>
  a Good <span id="div7" class="aui_top_in_page_parent" tabindex="0"><span id="span7" class="aui_top_in_page"><img
                src="http://www.arasaac.org/repositorio/thumbs/10/50/2/2429.png" class="aui_header_images"></span><span aui-symbol="13621 12324 17511" aria-describedby="span7" accesskey="Shift + E">Cup of Tea</span></span>
</a>

Accompanied by the CSS code (also injected):

For the span wrapping the image (inside a span) and the text (also inside its own span):

.aui_top_in_page_parent {
  display: inline-flex;
  flex-direction: column;
  position: relative;
  z-index: 99999999;
  margin: 1%;
}

For the inner span wrapping the image only:

.aui_top_in_page {
  font-size: 14px;
  font-weight: regular;
  overflow: visible;
  padding: 0px 0px 0px;
  color: #fff;
  -webkit-border-radius: 7px;
  -moz-border-radius: 7px;
  border-radius: 7px;
  -webkit-background-clip: padding-box;
  -moz-background-clip: padding;
  background-clip: padding-box;
  text-align: center;
  text-decoration: none;
  z-index: 99999999;
  align-items: center;
}

To see the outcome clearly, visit: https://jsfiddle.net/wyfumrx6/

I strive for the text to remain in a straight line while having the images positioned above the designated words. Moreover, there should be sufficient spacing between the image and the text line above to indicate which word the image corresponds to.

Answer №1

To align the content at the bottom, you can simply use the vertical-align: bottom property when removing the top and bottom 1% margin:

.aui_top_in_page_parent {
  display: inline-flex;
  flex-direction: column;
  position: relative;
  z-index: 99999999;
  margin: 0 1%;             /* update this to only side margins */
  vertical-align: bottom;   /* add this line */
}

.aui_top_in_page {
  font-size: 14px;
  font-weight: regular;
  overflow: visible;
  padding: 0px 0px 0px;
  color: #fff;
  -webkit-border-radius: 7px;
  -moz-border-radius: 7px;
  border-radius: 7px;
  -webkit-background-clip: padding-box;
  -moz-background-clip: padding;
  background-clip: padding-box;
  margin-bottom: 20%;
  text-align: center;
  text-decoration: none;
  z-index: 99999999;
  align-items: center;
}
<a href="https://www.wikihow.com/Make-a-Good-Cup-of-Tea" style="/* line-height: 57px; *//* height: auto; */font-size: 150%;font-family: Gotham, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;/* height: 55px; *//* display: inline-flex; */" class="container">
  <span id="div0" class="aui_top_in_page_parent" tabindex="0" style="
">
    <span id="span0" class="aui_top_in_page"><img src="http://www.arasaac.org/repositorio/thumbs/10/50/1/12313.png" class="aui_header_images"></span>
    <span aui-symbol="14907" aria-describedby="span0" accesskey="Shift + E">How</span>
  </span>
  <span id="div5" class="aui_top_in_page_parent" tabindex="0">
    <span id="span5" class="aui_top_in_page"><img src="http://www.arasaac.org/repositorio/thumbs/10/50/7/7194.png" class="aui_header_images"></span>
    <span aui-symbol="17982" aria-describedby="span5" accesskey="Shift + E">to</span>
  </span>
  <span id="div6" class="aui_top_in_page_parent" tabindex="0">
    <span id="span6" class="aui_top_in_page"><img src="http://www.arasaac.org/repositorio/thumbs/10/50/3/32751.png" class="aui_header_images"></span>
    <span aui-symbol="15410" aria-describedby="span6" accesskey="Shift + E">Make </span>
  </span>
  a Good
  <span id="div7" class="aui_top_in_page_parent" tabindex="0">
    <span id="span7" class="aui_top_in_page"><img src="http://www.arasaac.org/repositorio/thumbs/10/50/2/2429.png" class="aui_header_images"></span>
    <span aui-symbol="13621 12324 17511" aria-describedby="span7" accesskey="Shift + E">Cup of Tea</span>
  </span>
</a>

Updated fiddle

Answer №2

I have discovered the solution to the problem.

I have made a few alterations in the HTML and CSS files as mentioned below.

Added span tag

<span class="aui_top_in_page_parent">a Good</span>

and implemented some modifications in the CSS file.

.aui_top_in_page_parent{
       display: flex;
    flex-direction: column;
    /* position: relative; */
    align-items: center;
    margin: 0;
    justify-content: flex-end;
    margin: 5px;
}
a{
  display: flex;
    flex-direction: row;
}
.aui_top_in_page{

font-size:14px; 
font-weight:regular;
overflow:visible; 
padding:0px 0px 0px;
color:#fff;
-webkit-border-radius:7px;
-moz-border-radius:7px;
border-radius:7px;
-webkit-background-clip:padding-box;
-moz-background-clip:padding;
background-clip:padding-box;
margin-bottom: 0;
text-align:center;
text-decoration:none;
      z-index:99999999;
align-items:center;
}
<a href="https://www.wikihow.com/Make-a-Good-Cup-of-Tea" style="/* line-height: 57px; *//* height: auto; */font-size: 150%;font-family: Gotham, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;/* height: 55px; *//* display: inline-flex; */"><span id="div0"  class="aui_top_in_page_parent" tabindex="0" style="
"><span id="span0" class="aui_top_in_page"><img src="http://www.arasaac.org/repositorio/thumbs/10/50/1/12313.png" class="aui_header_images"></span><span aui-symbol="14907" aria-describedby="span0" accesskey="Shift + E">How</span></span> <span id="div5" class="aui_top_in_page_parent" tabindex="0"><span id="span5" class="aui_top_in_page"><img src="http://www.arasaac.org/repositorio/thumbs/10/50/7/7194.png" class="aui_header_images"></span><span aui-symbol="17982" aria-describedby="span5" accesskey="Shift + E">to</span></span> <span id="div6" class="aui_top_in_page_parent" tabindex="0"><span id="span6" class="aui_top_in_page"><img src="http://www.arasaac.org/repositorio/thumbs/10/50/3/32751.png" class="aui_header_images"></span><span aui-symbol="15410" aria-describedby="span6" accesskey="Shift + E">Make</span></span><span class="aui_top_in_page_parent">a Good</span>  <span id="div7" class="aui_top_in_page_parent" tabindex="0"><span id="span7" class="aui_top_in_page"><img src="http://www.arasaac.org/repositorio/thumbs/10/50/2/2429.png" class="aui_header_images"></span><span aui-symbol="13621 12324 17511" aria-describedby="span7" accesskey="Shift + E">Cup of Tea</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

Uh oh! The dreaded Error [ERR_HTTP_HEADERS_SENT] has struck again in the Node Express MongoDB world. Headers cannot be set after they have

Hey there, newbie in the coding world! I've been diving into a guide on setting up a backend server using Node.js, Express, and MongoDB. You can find the guide here: But I seem to keep running into an error when testing with Postman. Error [ERR_HTTP ...

How to position an image on the left side using AngularJS in an HTML

In the image, you can see rows. This is the code for the view: <div layout="row" layout-align="space-around center" > <product-image-thumbnail layout="row" layout-align="left center" src="vm.callFunction(field)" setter="vm.callFunction(v ...

HTML5: Implementing shared web workers for efficient communication across multiple connections

As far as I know, one of the main advantages of HTML5's shared web workers is their ability to handle multiple connections within a single separate thread of execution. I'm curious if anyone has managed to achieve multiple connections working as ...

Restful Spinner

app.config(function(RestangularProvider) { RestangularProvider.addRequestInterceptor(function(element) { console.log("Request initiated"); return element; }); RestangularProvider.addResponseInterceptor(function(data) { ...

Mastering the ng-submit directive for AngularJS

Having an issue with my form that submits a location to Google's Geocoder and updates the map with the lat/long. When using ng-click on the icon, it requires double clicking to work properly. And when using ng-submit on the form, it appends to the URL ...

How can I display a list of relative urls in an android TextView?

I have a situation in my application where I am displaying a block of HTML content in a TextView, which has been extracted and parsed from a webpage. This HTML includes numerous links, with some being absolute and others relative. My goal is to allow the u ...

Using Variables in JavaScriptExecutor with Selenium and C#

Currently in my Selenium tests, I am utilizing JavaScriptExecutor as shown below: IJavaScriptExecutor js = driver as IJavaScriptExecutor; string Pixels = "600"; js.ExecuteScript("arguments[0].style='transform: translateX(600px);'&q ...

The update feature seems to be malfunctioning within the MEAN Stack environment, specifically with node.js and angular js

Looking for some assistance as a beginner in the mean stack. I'm trying to update a record using the update function, but it's not working as expected. I need to update a specific object based on its ID, however, I'm encountering issues wit ...

Disabling hover effects in Chart.js: A step-by-step guide

Is there a way to disable hover effects, hover options, and hover links on a chart using chart.js? Here is the code I have for setting up a pie chart: HTML.. <div id="canvas-holder" style="width:90%;"> <canvas id="chart-area" /> </div ...

Ensure that bulleted lists and numbered lists are aligned to the left side

Check out this website where the ordered and unordered lists are not aligned correctly. The ideal alignment would have the bullets (or numbers) left aligned. For instance, the number "1." should be aligned to the left on the same line as the heading "Per ...

Updating values within an ng-repeat loop by using the ng-change directive to increment or decrement

Below is an example code snippet that demonstrates a checkbox feature: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Welcome to LearnKode - A code learning platform</title> ...

Using PHP to create custom HTML email templates and sending them out via

I have successfully used phpmailer to send normal pre-defined emails. Now, I am looking to enhance the email format by connecting my text editor with a PHP script to send HTML emails. The text editor is a template from CKEditor. https://i.stack.imgur.com/v ...

Utilizing Vue JS to showcase a pop-up block when hovering over a specific image

There are four images displayed, and when you hover over each one, different content appears. For example, hovering over the first image reveals a green block, while hovering over the second image reveals a blue block. However, the current logic in place i ...

Adjust the height of images to be consistent

I'm currently working on creating a grid layout with 4 images in a row using DaisyUI card component. However, I've run into an issue where there is white space at the bottom of some images due to varying image heights. Is there a solution that wo ...

Is there a way to dynamically adjust the font size to perfectly fit the content within a div?

There is a division element containing text: <div style="white-space:nowrap;overflow:none;width:50px;"> With some text in it </div> Is there a way to adjust the font size of the text within the division so that all of the content is display ...

Trouble arises when attempting to add an image to a spherical object

I've searched through various threads, Googled extensively, watched YouTube tutorials.... But I can't seem to figure out how to apply a texture to my Sphere. When I run this code, all I see is a white Sphere without any texture showing up. Can so ...

How can I stop and hover over time in AngularJs Interval?

Within my UI, I have a time element that is continuously updated using AngularJS Interval. Even the milliseconds are constantly running. Is it possible to implement a feature where the time pauses when hovering over it? Any assistance would be greatly appr ...

Encountering 404 Error in Production with NextJS Dynamic Routes

I'm currently working on a next.js project that includes a dynamic routes page. Rather than fetching projects, I simply import data from a local JSON file. While this setup works well during development, I encounter a 404 error for non-existent pages ...

How to make browsers refresh cached images

.button { background: url(../Images/button.png); } Issue: To improve performance, static content on the website is cached with expiration headers. However, when an image changes, users must manually refresh their browser cache (Ctrl+F5 in IE). ...

Sometimes Google Maps API doesn't load properly on my page until I hit the refresh button

Occasionally, I encounter a problem where my Google Map fails to load when the webpage is first opened, resulting in a blank map. However, upon refreshing the page, the map loads correctly. The error message appearing in the Chrome console reads as follow ...