Add a line break tag (<br>) specifically for devices with a width less than 768 pixels

I have a lengthy subtitle that includes the following:

Self-discovery | Personal development | Health prevention

This is how it appears in HTML:

<h2>Self-discovery <span class="divider"> | </span> Personal development <span class="divider"> | </span> Health prevention</h2>

On screens less than 768px wide, I hide the "|" using this CSS:

@media only screen and (max-width: 769px) {
    .header-description .divider {
        display: none;
    }
}

However, on screens under 1024 pixels, the first two words are on one line and the last word is on another line like this:

Self-discovery Personal development
Health prevention

which doesn't look very visually appealing.

QUESTION:

Is there a way to use a <br> tag instead of the SPAN tag for screens under 1024 pixels?

OR

Can I instruct HTML to put each word on a new line on screens narrower than 768 pixels?

Essentially, I would like each word to appear on a separate line like so:

Self-discovery
Personal development
Health prevention

Answer №1

Instead of using display: none, you can organize them in separate lines by applying display: block and make the separators invisible with visibility: hidden.

Additionally, include font-size: 0 to the divider to eliminate the space it takes up.

Check out the demo below:

@media only screen and (max-width: 769px) {
  .divider {
    display: block;
    visibility: hidden;
    font-size: 0;
  }
}
<h2>Self-Exploration <span class="divider"> | </span> Personal Development <span class="divider"> | </span> Health Prevention</h2>

Answer №2

Consider using 2 spans instead of one:

<h2>Self-Exploration <span class="divider-wide"> | </span><span class="divider-narrow"><br></span> Personal Development <span class="divider-wide"> | </span><span class="divider-narrow"><br></span> Health Prevention</h2>

Adjust your CSS accordingly:

@media only screen and (max-width: 769px) {
    .header-description .divider-wide {
        display: none;
    }
}
@media only screen and (min-width: 770px) {
    .header-description .divider-narrow {
        display: none;
    }
}

There may be a more elegant solution, but this should suffice.

Answer №3

To achieve word wrapping, try setting the parent element's width smaller than the smallest word:

.narrow-parent {
    word-spacing: 10px;
}

This will prompt the words to wrap into new lines.

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

Angular router link circles back to its originator

I've been working on a basic login page that transitions to a homepage once the user successfully logs in. So far, I've focused solely on the HTML structure of the login component without implementing any code in the corresponding TypeScript file ...

What's the best way to customize the dropdown icon in Bootstrap 5?

Is there a way to customize the default icon in the Bootstrap 5 dropdown menu and replace it with a Font Awesome character? I have gone through the documentation, but it didn't provide any helpful information. I also attempted to override the CSS styl ...

position the tooltip within the ample available space of a container in an angular environment

In my editor, users can create a banner and freely drag elements within it. Each element has a tooltip that should appear on hover, positioned on the side of the element with the most space (top, left, bottom, right). The tooltip should never extend outsid ...

Which Web Browsers Support Canvas and HTML5?

Currently working on a project that involves using the HTML5 Canvas element. I am curious about which major browsers (including specific versions) actually support the Canvas tag. I am not interested in hearing about IE. In this tutorial Drawing shapes - M ...

Mirror Image Iframes

Currently, I have been tasked with constructing a side-by-side "frame" using HTML. The idea is that when the content in the iframe on the left is selected, it will appear in the iframe next to it on the same page. Here's some additional context: The ...

Tips for dividing the WordPress header navigation horizontally using separate PHP files

I am trying to customize my WordPress menu navigation by splitting it into two sections: left and right. I have added the menu function in the "functions.php" file to create the menu from the backend, and the frontend modifications will be done in the "hea ...

Using a carousel component in Bootstrap

Just starting out with this, trying to customize Bootstrap to change slides automatically. I followed the documentation at https://getbootstrap.com/docs/4.3/components/carousel/ but for some reason, the slides aren't changing on an interval, even thou ...

The image referenced in the assets folder could not be located within the SCSS

Although I've come across similar questions, none of the solutions provided seem to work for me. I've tried them all, but they just don't seem to do the trick. My goal is quite simple - I just want to set a background image using CSS. Howev ...

Styling a nested scrollbar using JQuery

I am looking to customize two scrollbars using a jquery plugin or JS library. Here is the HTML code: <div id="container"> <div class="fixedHeightContainer"> <div class="fixedHeightContent"> Lorem ipsum dolor sit amet, con ...

Emphasize Expandable Sections based on Search Term

I have been working on developing an HTML page for my company that will showcase a list of contacts in an "experts list". Currently, the list is structured using collapsible DIVs nested within each other. Additionally, the HTML page features a search func ...

ColdFusion encounters an HTML form error, presenting the message: "There is an undefined element in the FORM."

I'm attempting to submit an HTML form that contains checkboxes for each day of the week. Whenever a checkbox is checked, I assign a value of 1 to it. To handle unchecked checkboxes, I set a default value of 0 using a CFPARAM tag in the form action pag ...

How can I activate the mobile zoom feature on kinetic js?

I am currently working on a website using kinetic js + html5 canvas. Everything looks great on the web, but I am facing some issues with the display on mobile devices. Is there a solution to enable the mobile pan and zoom function in kinetic js? Thank you ...

How can I embed an iframe in an Angular 4 application?

I'm facing an issue with my Angular 2 v4 application where I have the following code snippet: <iframe src="https://www.w3schools.com" style="width: 100%; height: 500px"></iframe> However, the iframe does not seem to work. I attempted to ...

The absence of image input was evident in the POST request

Within my Django template, I have an input field linked to a Django ImageField. Once rendered, the HTML code appears as follows: <form action="/accounts/username/" method="post" id="profile_form" enctype="multipart/form-data"> &l ...

I am currently attempting to create a JavaScript function that searches for the <td> elements within an HTML table. However, the function fails to work properly when there are <th></th> tags included

UPDATE: Scratch that, I managed to solve my issue by creating a separate table for the header to contain all of my <th> tags I am working with an HTML table and I would like to add a search bar to filter through the content since it is quite large. ...

There is an issue showing up in the console: $(…).datepicker is not defined as a function

I am new to using HTML with JavaScript. I attempted to implement a datepicker, but unfortunately, it is not working as expected. The error message I am receiving is '$(...).datepicker is not a function' in the console. I am utilizing multiple f ...

Prevent a button on my side bar from being clicked until the user has logged in?

How can I use Django and HTML to disable a button in my tab bar until a user is logged in? This is my sidebar: <div id="mySidebar" class="sidebar"> <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a> <a href ...

Tips for retaining form inputs without the need for a submit event when the page is reloaded or refreshed

I have a form on a page with multiple text inputs In addition to the form, I have implemented Zend pagination to allow users to select results. However, when using Zend paginate, the user's form inputs are lost because it is not submitted. Since th ...

Why isn't a password appearing in the "generated-password" div from the password generator?

The function toIncludeCharacters() appears to be functioning correctly as it returns lowercase letters upon upload, but generatePassword() is currently producing a blank output. After clicking the "Generate Password" button, three blank outputs are return ...

Concealing Nested Div Elements

Can someone help me figure out why my code is not hiding the two divs within my content div? I need assistance checking my code to see what's causing the issue. Thank you! I have made some edits to my code. Currently, both forms appear under the "War ...