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
278 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 795 views
+1 vote
2 answers 126 views
+1 vote
1 answer 175 views
+5 votes
2 answers 1,217 views
+4 votes
1 answer 114 views
+1 vote
2 answers 212 views
+1 vote
2 answers 114 views
+3 votes
1 answer 3,616 views
+1 vote
2 answers 5,861 views

Not a Member yet?

Ask to Folks Login

My Account
439 Folks are online
3 members and 436 guest online
Your feedback is highly appreciated