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. |
|_________________________|
I need to adjust the centering such that a specifically marked word is horizontally centered on the page. In this example, the word "who" is centered:
_________________________
| |
| 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?
[EDIT] Added fiddle. Cleaned up some code. Updated snippet below. Added comments.
http://jsfiddle.net/2Hgur/
[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:
Then break it down and rebuild it, perhaps like this:
[ UPDATED FOR REQUIRED NAMING CONVENTION... style="centered" :) ]
The center tag is deprecated and not supported in HTML5, so do not use it. Instead give your
.centered
class some CSS rules like this:However, this will give its own line. If you still want the text around it to wrap, then you'll have to systematically choose your line-breaks – there is no current way to wrap text around a centered node, and
float: center
doesn't exist…shift a particular target word to the best horizontal center that words will allow
You can achieve a rough centering using quite simple JavaScript. All it does is scan along the mark-up, converts the first TextNode it finds to "words" wrapped with
<span>
s, and then trials and errors inserting line breaks until it finds the one that will get the target word nearest to the center.In the fiddle I have highlighted in red the spans that the code deems should have a break after, and green for the target word. In the most cases it does quite well, there are a few occurrences where you may end up with a single word on a line — however, this could be fixed by tweaking the code to your exact use-case.
fiddle
http://jsfiddle.net/s8XgJ/2/
updated fiddle that shows behaviour with a random targeted word:
http://jsfiddle.net/s8XgJ/3/
markup
JavaScript / jQuery
CSS
You'll require this CSS item:
and perhaps the following if your
.centered
item is a<div>
:Then all you need execute is:
problems
To get things precise you'll have to offset something — due to the fact that depending on the length (and placement of) words you cannot always achieve perfect center for paragraph, line and target word all at the same time. The above code still keeps the line centered, at the cost of the target word being offset. You could improve the above to make margin modifications to the line that holds the target span, if you wanted to center the word at the cost of the line.
Currently, if the target word appears in the first say five words, centering is unlikely to be achieved, basically because this method relies on word-wrap and line-breaks. If neither can be used due to the word being at the beginning of the paragraph, then nothing can be done — save for introducing margin, padding or text-indent.
This code relies on being able to read the position of
<span>
elements correctly. Older browsers e.g. IE6 and early versions of Safari/Opera have had problems with this.One further thing to note. This code relies on the browser recalculating it's internal measurements immediately in the same synchronous execution loop. Most modern browsers seem to do this — in most cases — however you may find you need to implement
setTimeout(func, 0)
orsetImmediate
delays in order for this to work on older systems.... and finally
This is a rather bonkers request, what exactly is it for? :)
As other pointed out its not possible to do through html/Css which I agree too, but you can do some sort of centering based on a line(sentence) rather then by a word. Hope this helps you !!
CSS:
HTML :
Fiddle Demo
Update: As pointed by @disule I have removed the deprecated center tag.
The woodcutter method is the
<br/>
tagshtml:
css:
Without word-break you can't be sure that the desired word is centered just as the line it's in.
Here is a different text, the first version the word 'who' centered, but the line not, the seccond the opposite:
As you can see without breaking the words it's entirely based on the text you have and the word you want to center if its possible or not.
Due this there's four solution:
Put only the desired word in that line and then try to be sure that theres exactly as much line before as after. (Only possible if the word is not the first or not the last or it's the only word in the text, and if the word is not roughly in the middle of the text you might end up with really arkward shapes.)
Count the position of the word inside the text and then break the words before and after to match the number of characters before and after the word in the same line.
Implement a word-breaking function in the language of the text, and calculate all the combinations of possible word- and line-breaks to find one (if any) where your desired word is centered.
Hardcode it by yourself, there are things that can be solved much more efficiently by a human than a simple program could (yet). After all we are well bundled supercomputers.