Hey there! any question in your mind? Ask It Now!.

Popular Categories





How to center a paragraph of text on a particular word in HTML, CSS, or JavaScript?

0 votes
247 views
asked in Software Testing by AlexStewart (560 points)

I have a passage that is centered on the page, like this:

 _________________________
|                         |
| Once upon a time, there |
|   lived a squirrel who  |
|  lived in the middle of |
|       the forest.       |
|_________________________|

 

 

  • The word who is marked in the HTML with a <div> tag, i.e. <center>Once upon a time, there lived a squirrel <div style="centered">who</div> lived in the middle of the forest.</center>.
  • The HTML appears in an application in which I have little control over editing the HTML itself (the text is already marked with CSS style tags), but can modify the style sheet or add code, such as JavaScript to the header to modify how that style is rendered.

How can I center a passage on a particular word in HTML, CSS, or JavaScript?

1 Answer

0 votes
answered by jazs (1,220 points)
[EDIT] Added fiddle. Cleaned up some code. Updated snippet below. Added comments.
 
[ORIGINAL] I don't believe that this is possible with css. However, I figured a solution could be possible with JavaScript. So, I tried it, and it worked! While this might not be the ultimate solution, consider the following:
 
<div class="jsCenterOnPhrase">
 
The quick brown fox jumped over the lazy dog.
The quick brown fox jumped over the <div class="jsCenteredPhrase">lazy dog.</div>
The quick brown fox jumped over the lazy dog.
The quick brown fox jumped over the lazy dog.
The quick brown fox jumped over the lazy dog.
 
</div>
 
 

Related Questions

+1 vote
1 answer 729 views
+5 votes
2 answers 1,131 views
+4 votes
1 answer 93 views
+1 vote
2 answers 154 views
+1 vote
2 answers 72 views

Not a Member yet?

Ask to Folks Login

My Account
133 Folks are online
0 members and 133 guest online
Your feedback is highly appreciated