Separating each word of a title onto its own line using HTML and CSS

Looking for a solution to style dynamic titles with each word on its own line? Check out the desired look here. (Note the black backgrounds for each word)

The challenge is keeping the same style while having the whole title inside one tag without dynamically inserting H1's before and after each word.

Want to change the HTML markup from:

<div class="tagline">
<h1>
    Oh
</h1>
<h1>
    Look
</h1>
<h1>
    A
</h1>
<h1>
    Headline
</h1>
<h1>
    Thanks
</h1>
</div>

to something like this:

<div class="tagline">
    <h1>
        Oh Look A Headline Thanks
    </h1>
</div>

Don't forget to maintain the same style as in the link above. Thanks in advance!

Answer №1

Check it out: http://jsfiddle.net/thirtydot/HksP2/

The design appears flawless in IE9, IE8, recent versions of Firefox, Chrome, Safari, Opera; all on Windows 7. It gracefully degrades in IE7. In Mac's Safari, it's almost impeccable.

This concept originates from a prior response. Quoting my own words from that context:

Remember that adjusting the line-height and padding can be quite challenging to get just right.

line-height: 1.83; seems promising, discovered by selecting something similar to your desire, then experimenting until finding a solution that works well on both Chrome and Firefox (as they render text differently).

HTML:

<div class="tagline">
    <h1><span>
        Oh Look A Headline Thanks
    </span></h1>
</div>

CSS:

.tagline {
    display: inline-block;
    width: 0;
    line-height: 1.83; 
    padding: 1px 0; 
    border-left: 20px solid #000;
}
.tagline h1 {
    font-size: 20px;
    font-weight: normal;
    color: #fff;
    background: #000;
    display: inline;
    padding: 8px 0;
    text-transform: uppercase;
}
.tagline span { 
    position: relative;
    left: -10px;
}

Answer №2

Your best bet to achieve this is by creating a custom JavaScript function that can parse your <h1>oh look ..</h1> content and convert it into multiple separate h1 tags.


update:

A new idea just came to mind: http://jsfiddle.net/2LEmF/10/

In essence, you should move the background color styling to the main div element. Then, set a specific width for your h1 tag that will cause the text to break according to regular text formatting rules, like 10px.

It's uncertain how various browsers will interpret this approach since you are specifying an unusually small size for the H1 element... but it could potentially fulfill your requirements.

Answer №3

Check out this easy way to display each word on a separate line:

https://example.com/unique-link

HTML Code:

<div class="phrase">
    <p>
        Here is a sentence displayed word by word
    </p>
</div>

CSS Styling:

.phrase p {
  display: inline-block;
  word-spacing: 50vw;
}

Answer №4

To achieve a unique design, try setting the width of the h1 element to a value smaller than the length of the shortest word like 10px.

This method yields identical results as shown in your sample (particularly when viewed on Chrome and Firefox).

For a live example, check out this Jsfiddle link.

Answer №6

I'm a little confused about the issue at hand. It appears that you're facing a challenge with the HTML provided in your question and you would like it to be displayed inline?

Have you considered simply adding display:inline; to .tagline ?

http://jsfiddle.net/XmCLd/

Alternatively, is it the reverse situation? Do you have HTML that looks normal but requires line breaks at spaces?

http://jsfiddle.net/GQ44u/

Answer №7

Modify the tagline section to be very slim and change it from inline to block. Then set the h1 to be inline.

.tagline
{
    width: 1px;
    margin:5px;
    display: block;
}
.tagline h1
{
    color:#fff;
    background: #000;
    padding: 4px 10px;
    font-size: 20px;
    line-height: 30px;
    text-transform:uppercase;
    display: inline;
}

Click here for JSFiddle link.

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

Modify the style of a webpage through JavaScript

Need help with calling a JS event based on button presses and changing CSS font styling accordingly for each button? Check out the code snippet below: body { background-image: url("back2.jpg"); background-size: 100% 100%; } ...

After incorporating some movement effects into my menu, suddenly none of the buttons were responding

While working on my website and trying to add a close menu button, I encountered an issue where all the menu buttons stopped functioning. Here is the HTML code snippet: <div id="openMenuButton"> <span style= "font-size:30px;cu ...

Add a layer on top of the background in order to cover the entire screen

After researching various SO questions and answers, as well as examining different JSFiddle and Codepen examples, I have yet to find the solution I am looking for... Here is a sample fiddle of my current issue: https://jsfiddle.net/ts4t13hn/3/ Below is ...

The functionality of the onclick and onserverclick attributes seem to be malfunctioning when

I am in the process of creating a basic login screen using the code provided below: <form id="login"> <div class="formHeader"> <h1>Login</h1> </div> <div class="formDiv"> <input type="text" placeholder="Email" na ...

Adjusting specific sections of a container in real-time

Fiddle: https://jsfiddle.net/1b81gv7q/ Sorry for the slightly cryptic title; I couldn't come up with a better way to phrase it. Imagine this scenario: there's a container with content that needs to be dynamically replaced. If I wanted to repla ...

Need to tick only specific items from the list in Django checkbox?

I am dealing with another checkbox question where I have items in a list, each with a corresponding checkbox. My goal is to tick all the items that are a subset of certain defined items. This specified subset of items can be found using items = client.stor ...

What is the best way to determine if a character is valid for use in HTML rendering?

There are certain characters, like ordinal 22 or 8, that do not appear correctly in HTML (when using Chrome for example when copying and pasting them into this 'Ask question' editor; assuming utf-8 encoding). How can I identify which characters a ...

Can dynamically loaded scripts from the <head> section slow down the loading of a webpage?

Will the full page be blocked while waiting for a dynamically inserted script tag to download and load, if it is done from within a <script> in the <head>? ...

Displaying content conditionally - reveal a div based on user selection

I have a dropdown menu and need to determine whether to display a specific div using the value selected via v-if. <select class="custom-select custom-select-sm" id="lang"> <option value="1">English</option> <option value="2">Sv ...

Submitting a form in a Chrome packaged app

Exploring Chrome packaged apps for the first time and facing a common issue that involves two questions. Attempting to create a basic form to submit data to a server using Ajax and POST. However, running into the restriction of not being able to use inlin ...

<!--[if IE]> does not function properly on any web browser

Hello, I am experiencing a perplexing issue and despite searching extensively on Google, I have been unable to find a solution! I am trying to change the body background color to red only in Internet Explorer, but it is not working as expected. Here is t ...

Load Form with Json Data from LocalStorage Key-ID to Auto-Populate Fields

After successfully setting the ID in localStorage, I now need to retrieve and display only that specific record from my JSON data. The content of my localStorage is: This information is obtained from my JSON data after a button click. The challenge is us ...

How can you identify dynamically created elements within an AngularJS directive?

I have a directive where I need to target specific DOM elements, some of which are dynamically generated in an ng-repeat loop. If I try to select them directly, I only get the static elements. However, if I delay the selection by, let's say, 500ms, I ...

Attempting to dynamically populate a dropdown menu in HTML using the ng-repeat directive from AngularJS

I am facing an issue while trying to populate my select dropdown in HTML using ng-repeat. Upon inspecting the browser, I noticed that my ng-repeat directive seems to be commented out. Here is my HTML code: <select ng-model="transportcompanies" ng-opti ...

Toggle the navigation bar

Visit our webpage at: You can also check out the code on Codepen: https://codepen.io/mitchellbarron10/pen/EqZjQj This project involves JavaScript embedded in html code. The task at hand is to ensure that the navbar (located on the top right corner of th ...

Bizarrely rapid transitions from standard to hovering appearance in CSS

After creating a custom styled button with a hover effect, I noticed an issue where if you happen to hover over a specific spot on the button, it quickly switches between its normal and hover states. https://i.sstatic.net/uPq1F.gif This is my code: but ...

Adjust the CSS of a dynamically generated jQuery checkbox button in real-time

I am working on a project where I am creating a series of jQuery checkboxes dynamically within a loop. Here is how I am doing it: var checkbox = $('<input>').attr({type: 'checkbox', id: checkbox_id); panel.append(checkbox); panel ...

Email Template not rendering properly across various platforms

Dealing with a frustrating issue here. I created a template using Foundation's E-mail features and it looks perfect in Chrome's Device mode as well as in MailChimp. However, when testing it in Litmus and sending it to my own email, things start t ...

Steady Navigation Bar in Javascript without Bouncing

While experimenting with a fixed navigation bar, I've encountered an issue where the content below jumps up on the page when the navigation bar "fixes" itself to the top of the screen. You can check out the JSFiddle I've been working on for refer ...

Tutorial on wrapping HTML content in a CDATA tag using jQuery

Is there a way to enclose some HTML in an element using a CDATA tag to "sterilize" it, so to speak? I've attempted to do so using the following code: $(this).html( "<![CDATA[" + $(this).html() + "]]>" ); However, this approach simply encodes ...