DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Languages
  4. Simple Code, Pretty Results: How to Correctly Wrap Text With HTML!

Simple Code, Pretty Results: How to Correctly Wrap Text With HTML!

In this quick post, we go over how developers can easily, and properly, implement text wrapping in their sites so the text is correct on both mobile and desktop browsers.

Marcus Vinnicius user avatar by
Marcus Vinnicius
·
Jan. 08, 19 · Tutorial
Like (5)
Save
Tweet
Share
10.20K Views

Join the DZone community and get the full member experience.

Join For Free

If you work with web development, you already know the character " ", which means “Non-breaking space” and it is used to fill out HTML tag empty spaces. In practice, do you really use it? 

Today, I will show how to wrap text using the   character. 

Let's say you are creating a website with a desktop and mobile version, for example:

Code

<div class="block-form">
  <h2>
    Fill out the form with your personal data and we will contact you! 
    Or you can call us: 
    <span class="phone">0800 3000 9974</span>
  </h2>
  <form>...</form>
</div>

Result  

  


Wow! Great! Apparently, all the code was correct. But, if you observe, the phone number in mobile version is not in the correct format. The mobile phone is wrapping the text, but the best way is to keep the phone number all on the same line. 

Wrong Solution

In this case, our first reaction is to add a tag “<br>”, before the beginning of the number:

<div class="block-form">
  <h2>
    Fill the form with your personal data and we will contact you! 
    Or you can call us: 
    <br><span class="phone">0800 3000 9974</span>
  </h2>
  <form>...</form>

Fantastic! The mobile version looks perfect! But the desktop version…  

In this case, developers identify the problem and create code blocks to solve it. Most of this code will work, but it's unnecessary. 

Correct Solution

The best way to solve this problem is pretty simple. 

Use the powerful character “&nbsp;”. This allows us to inform the browser that blank spaces exist between the blocks of numbers, but it should be altogether in the same line.  Like this:

<div class="block-form">
  <h2>
    Fill the form with your personal data and we will contact you! 
    Or you can call us: 
    <span class="phone">0800&nbsp;3000&nbsp;9974</span>
  </h2>
  <form>...</form>

Yeah! Now it is perfect, in both versions. 


HTML

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Fargate vs. Lambda: The Battle of the Future
  • Introduction to Container Orchestration
  • What Are the Benefits of Java Module With Example
  • Building Microservice in Golang

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: