How to add a line break within the :after pseudo-element using CSS

I've been struggling to split text inside an :after pseudo-element of a label that receives its content from a data- attribute. I've attempted using word-break, as well as playing around with width, max-width, and position:relative, but nothing seems to be working...

Here's the HTML:

<div class="checkbox">
    <input type="checkbox" name="checkme" id="checkme" />
    <label for="checkme" data-content="Some very lengthy text that needs to be broken into two lines similar to a br tag"></label>
</div>

And here's the CSS:

.checkbox {
    display:block;
    clear:both;
    position:relative;
}

.checkbox:not(:only-child):not(:first-child) {
    margin:10px 0 0 0;
}

.checkbox input {
    display:none;
}

.checkbox label {
    display:inline-block;
    cursor:pointer;
    position:relative;
    width:13px;
    height:13px;
    border:1px solid #383838;
}

.checkbox label:after {
    font-family:Arial;
    font-size:14.5px;
    color:#383838;
    display:inline-block;
    position:absolute;
    left:calc(100% + 7px);
    content:attr(data-content);
    cursor:text;
    white-space:pre;
    word-break:break-all;
    top:50%;
    -webkit-transform:translate(0,-50%);
    -moz-transform:translate(0,-50%);
    transform:translate(0,-50%);
    /* max-width and width not working... */
    max-width:160px !important;
    width:160px !important;
}

.checkbox input:checked ~ label:before {
    display:block;
}

.checkbox input:not(:checked) ~ label:before {
    display:none;
}

.checkbox label:before {
    font-size:16px;
    color:#383838;
    display:block;
    position:absolute;
    top:50%;
    left:50%;
    -webkit-transform:translate(-50%,-50%);
    -moz-transform:translate(-50%,-50%);
    transform:translate(-50%,-50%);
    pointer-events:none;
    margin:0;
    width:auto;
    content:"X";
    font-family:Arial;
}

Can anyone provide guidance on how to fix this issue (or point out what mistakes I might have made)?

Thank you.

Answer №1

Here is a solution to your problem: apply the CSS properties top:170% and white-space:wrap as recommended by @Carol McKay

.checkbox {
    display:block;
    clear:both;
    position:relative;
}

.checkbox:not(:only-child):not(:first-child) {
    margin:10px 0 0 0;
}

.checkbox input {
    display:none;
}

.checkbox label {
    display:inline-block;
    cursor:pointer;
    position:relative;
    width:13px;
    height:13px;
    border:1px solid #383838;
}

.checkbox label:after {
    font-family:Arial;
    font-size:14.5px;
    color:#383838;
    display:inline-block;
    position:absolute;
    left:calc(100% + 7px);
    content:attr(data-content);
    cursor:text;
    white-space:wrap;
    word-break:break-all;
    top:170%;
    -webkit-transform:translate(0,-50%);
    -moz-transform:translate(0,-50%);
    transform:translate(0,-50%);
    /* max-width and width not working... */
    max-width:160px !important;
    width:160px !important;
}

.checkbox input:checked ~ label:before {
    display:block;
}

.checkbox input:not(:checked) ~ label:before {
    display:none;
}

.checkbox label:before {
    font-size:16px;
    color:#383838;
    display:block;
    position:absolute;
    top:50%;
    left:50%;
    -webkit-transform:translate(-50%,-50%);
    -moz-transform:translate(-50%,-50%);
    transform:translate(-50%,-50%);
    pointer-events:none;
    margin:0;
    width:auto;
    content:"X";
    font-family:Arial;
}
<div class="checkbox">
    <input type="checkbox" name="checkme" id="checkme" />
    <label for="checkme" data-content="Some very long text that should be split into two lines like br tag"></label>
</div>

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

Click anywhere on the body to conceal this element

I am currently working on a website where I have implemented a hidden menu that is functioning correctly. However, I want to make it so that when the menu is visible, users can click anywhere on the body to hide it. Unfortunately, my current code is not w ...

Creating a unified border style for both <p> and <ul> tags in HTML5

Is there a way to have both paragraphs and unordered lists contained within the same border? I initially thought I could achieve this by placing the list inside the paragraph tag, but that does not seem to work. Below is a snippet of my external style sh ...

Leveraging the power of the Twitter API to integrate real-time tweets into custom code

Looking for an API that can transform a tweet from Twitter into a string format suitable for integration into a program or website. Any recommendations? ...

The height of the iframe with the id 'iframe' is not functioning as expected

I am trying to set the iFrame's height to 80% and have an advertisement take up the remaining 20%. Code <!DOCTYPE html> <html lang="en"> <head> </head> <body> <iframe id="iframe" src="xxxxxx" style="border: ...

The view has no access to $scope but the controller does

I need to display the iso code using a $scope when selecting a country from a list. The list of countries is fetched through a $http call in a factory and then stored in a scope. ///////// get countries //////////////// tableFactory.getCountries().then(f ...

Current page with animated CSS3 menus

I found a cool menu example on this webpage: http://tympanus.net/Tutorials/CreativeCSS3AnimationMenus/index10.html. I want to modify it so that the current page's menu item has a different color from the others. For example, when I'm on the home ...

Having trouble retrieving the content within an HTML tag using jQuery?

I'm facing some challenges when it comes to extracting the content of an html tag using the Chrome console. I've been trying different methods for about half an hour, but nothing seems to work. That's why I need some help :) The code I want ...

Submit data in the manner of a curl request

Instead of using a curl command to push a file to a web server, I am attempting to create a simple webpage where I can select the file and submit it. Here is the HTML and JavaScript code I have written for this purpose: <html> <body> <i ...

Expand Elements to occupy entire width

My CSS skills are still in the early stages. I haven't delved too deeply into it. I have a query regarding how to achieve full width for an element. I've included my code below, but the basic approach I tried using 'width: 100%' didn&a ...

Can using .wrap( ) negate the styling effects of the wrapper?

I am struggling with centering a button on my webpage. Currently, I have the following code for creating the button: var button = ($('<button>', { "id": "jspsych-free-sort-done-btn", "class": "jspsych-free-sort", ...

How can I open a new window, redirect the current one, and bring focus to the new window using JavaScript?

Trying to troubleshoot a problem I'm having with the following setup: - Using SAP Portal, I am launching an HTML page containing this code. - The goal is for the HTML page to open a new window. - Once the new window opens, the original HTML page ...

Homepage experiencing issues with Jquery slider functionality

I have been attempting to incorporate a slider on my magentogo store, but unfortunately, after following all the code from the demo example of the slider along with the necessary CSS, the slider refuses to work. The CSS is configured to display none, as th ...

Generate a custom map by utilizing JavaScript and HTML with data sourced from a database

Looking for guidance on creating a process map similar to this one using javascript, html, or java with data from a database. Any tips or suggestions would be appreciated. https://i.sstatic.net/tYbjJ.jpg Thank you in advance. ...

Unable to incorporate additional functions while using directives

http://example.com Is there a way to incorporate an addChild(tree) function into this code in order to expand the data array? I have made changes to the template as shown below: '<p>{{ family.name }}{{test }}</p>'+ '<ul>& ...

Issue with jQuery AJAX call: When submitting an HTML form, control is not being returned to the calling

I am facing an issue with my HTML form where it is loaded correctly into the DOM through a jQuery ajax call. The problem arises when I submit the form data to a PHP module using another jQuery ajax call. Even though the network traffic shows that the form ...

Having trouble aligning material-ui GridList in the center

I am currently working with a GridList in order to display Cards. The styling for these components is set up as shown below: card.js const useStyles = makeStyles({ card: { maxWidth: 240, margin: 10 }, media: { heigh ...

Unselect all options in Angular's multiple selection feature

Objective: My goal is to ensure that when I invoke the myMethod() function, all options are unselected. Current Issue: Currently, calling myMethod() will only deselect the last option, leaving the others selected if they were previously selected. Possibl ...

Showcasing Portfolio Work in a User-Friendly Mobile Design

Currently revamping my portfolio website and looking for ways to optimize the display of my personal projects. I have a card-like interface in place that works well on desktop but only shows one project at a time on mobile devices. Seeking solutions to imp ...

The functionality of the jQuery program is compromised when used on the Google Chrome browser

I just recently started diving into the world of Jquery, attempting to learn it from scratch. However, I have encountered an issue while trying to make my Jquery program run smoothly: Let's take a look at the CSS first: p { opacity: 0; } Now on ...

Showcase three elements next to each other on the same row (Photo Gallery, Description, and Contact Form)

I am attempting to showcase three divs in a single line while working on a Subtheme for the Drupal 8 Bootstrap Theme. These particular divs are fields nested within a view known as viewproducts. The divs include: Juicebox Photo Gallery; ...