Ways to divide a sentence into separate sections using CSS

In my quest to divide a sentence into three parts using CSS, consider the example:

United States ("US") - Tax reforms

My goal is to assign names to each of these 3 parts.

  • name: United States
  • symbol: US
  • subject: Tax reforms

I am aware that I can use (" for the name, (" and ") for the symbol, and ") - for the subject. However, implementing this seems challenging.

While exploring the 'word-break' property, I have not been able to achieve the desired outcome yet.

Answer №1

Using CSS to split a string into parts is not possible. CSS cannot target individual letters (except for the first letter using the :first-letter pseudo-class). Refer to this question for more details on this topic.

If you need to split a string, JavaScript along with a regular expression can be used. Here's a simple example (remember not to directly parse user input as HTML for security purposes):

const input = 'United States ("US") - Tax reforms';
const regex = /^([\w\s]+)\s\("(\w+)"\)\s-\s([\w ]+)/;
const result = regex.exec(input);

if (result !== null) {
  const resultingObject = {
    name: result[1],
    symbol: result[2],
    subject: result[3]
  };
  console.log(resultingObject);
  document.body.innerHTML = `<p class="name">${resultingObject.name}</p>
    <p class="symbol">${resultingObject.symbol}</p>
    <p class="subject">${resultingObject.subject}</p>`;
}
.name {
  font-weight: bold;
}
.symbol {
  font-style: italic;
}
.subject {
  text-decoration: underline;
}

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

Hidden footer error has been resolved despite background visibility

Greetings! This marks my debut post on this platform. I've encountered a minor setback with the current website project I am working on. As I have implemented a CSS parallax header, I aimed to create a distinctive footer as well. The concept was to ...

Manage several images and set specific width and height to prevent any visual stuttering (Image elements lack defined width and height)

When it comes to using different pictures of varying sizes based on screen breakpoints, what is the most effective approach? For example <picture class="image-wrapper"> <source srcset="images/image-product-desktop.jpg" med ...

Ways to create a fixed top navigation bar that adjusts content similarly to a non-fixed navbar

I'm looking to achieve a fixed navbar at the top that pushes down content when I open the collapse menu, similar to how it functions in static or regular navbars. Something like this example: (https://getbootstrap.com/examples/navbar-static-top/) but ...

Delete the rows in a table's column

I used Bootstrap to create a table. The goal was to have a table with rows that have different background colors for each row. However, I'm facing an issue where there is spacing between the columns when separating the rows. How can I remove this sp ...

Ensure that the status bar remains visible while in full screen mode on Ionic for both android and iOS devices

Greetings! I am currently developing an application with Ionic/Cordova and I have a question regarding enabling the full screen mode while also displaying the status bar. I have already added the following code to my config.xml file: Could someone provide ...

Maintain the appearance of the selected hyperlink

Currently, I'm working with a list of links that are dynamically called using PHP. Whenever these links are clicked, the page refreshes to load the content of the link. However, my goal is to keep the link styled in the same way as when it's hove ...

I'm having trouble getting the height of my div element to render in percentage (%) in the CSS. Can anyone assist me with this issue?

When I try to set the "height" CSS property in percentage (%) for a div element, the HTML doesn't render anything. However, if I set the "height" CSS property using length values such as px or cm, it works perfectly fine. Can anyone provide assistance ...

Is Bootstrap failing to function properly in my Angular project?

During my work on an angular project, I decided to implement bootstrap CSS and js for styling. However, after completing the project, I noticed that the bootstrap was not functioning correctly. I found that when I used the CDN link in the project, the bo ...

Fixed Element Transitioning from Starting Position on Scroll

Check out the JSFiddle example here I'm currently working on a website utilizing Bootstrap 3. I have an image that sticks to the page when scrolling by changing its position to fixed. While this functionality works, the image tends to shift slightly ...

Tips for adjusting text to fit within a container without needing to use overflow:auto

Is there a way to prevent text overflow in a container without using the overflow:auto property? I've come across plugins that automatically reduce the text size, but I'm not keen on that solution. What I want is for long words or URLs to wrap on ...

Add some flair to the html and body elements for a designated React Route

Ensuring that an iframe is rendered at 100% for both width and height can be a challenge. The method outlined in this Stack Overflow post offers a potential solution. body, html {width: 100%; height: 100%; margin: 0; padding: 0} .row-container {display: ...

Adding gaps between hyperlinks in the sidebar navigation using Bootstrap 4

I created a sidebar containing links and I am attempting to add space between each link. Here is the code snippet I have tried: .sidebar-nav>li>a{ padding-bottom: 500px; } Unfortunately, this code is not producing the desired result. Are there any ...

Determine whether the Bootstrap data-style attribute is enabled or disabled

I have a bootstrap checkbox that I would like to trigger an alert dialog when it is switched off. <div class="row"> <link href="css/bootstrap-toggle.min.css" rel="stylesheet"> <script src="javascript/bootstrap-toggle.min.js ...

Modifying email alters the appearance of the input element

Utilizing labels as placeholder text behind input boxes with a transparent background, changing to white on :focus and when input value is > 0, I created a contact form. However, integrating PHP into the form recently caused styling issues. Although I m ...

Creating a custom accordion with pure CSS, no need for Bootstrap or HTML

Struggling to create an accordion using pure CSS Despite numerous attempts, I have been unsuccessful in achieving the desired outcome. ...

Guide on implementing the addClass method for a :before pseudo-element when clicked

A custom feature has been developed that allows for a slideDown effect when the title is clicked, revealing a new section. Initially, the title displays a plus symbol to indicate that it can be expanded. However, upon clicking the title, the intention is t ...

Endlessly tapping through each tab with no direction

I am attempting to click on all unopened tabs (elements) randomly across this page. While the code below usually does the trick, it sometimes doesn't click on all elements. I suspect that there might be an issue with how it loads indexes or some othe ...

Methods for activating touch-like scrolling through the use of mouse grabbing and dragging

Let me show you a simple illustration of the task I'm attempting to accomplish: <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> ...

Tips for effectively showcasing dynamic data retrieved from a database

I need to showcase my dynamic data from a database in a specific format that can accommodate anywhere from one to five variables. https://i.sstatic.net/pPQZE.png https://i.sstatic.net/gSJw6.png How can I effectively display and present the data using HTML ...

Place the collapsible button on the right side of the navigation bar

Looking to position the collapsible button to the right of the navbar in Bootstrap v4, I attempted wrapping the button in: <div class='nav navbar-nav navbar-right'> I also tried adding the float-xs-right class without any luck. A sample ...