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

Guide on how to trigger a pop-up modal to open a new webpage by clicking on a hyperlink

I have a page called one.html that has a link on it <a href="#">Click to open second page<a/> When this link is clicked, I would like for second.html to open in a popup modal. The second page contains a basic table that I want to di ...

How to Properly Adjust the Material UI CircularProgress Size

Is there a way to display the Material UI CircularProgress component at a size that nearly fills the parent div, which has an unknown size? Ideally, I want the size to be around 0.8 * min(parent_height, parent_width). I've experimented with adjusting ...

Compilation of CSS by Webpack resulting in peculiar class identifiers

Currently, I am using webpack for developing a React app with a NodeJS backend. I've encountered an issue with styling the app as webpack seems to be interfering with my CSS, causing changes in class names. For example, in my base.css file, I have d ...

Issues arise when attempting to determine the accurate dimensions of a canvas

Looking at my canvas element: <canvas id='arena'></canvas> This Element is set to fill the entire website window. It's contained within a div Element, both of which are set to 100% size. I attempted running this script: var c ...

Refreshing page using jQuery following an AJAX request

My script is functioning properly, however the use of location.reload(); does not seem to be effective. I am aiming to refresh the page after deselecting the last checkbox. $(".catcf, .coursetype, .yearcf, .manthcf, .venucf").on('click' ...

ng-table Filtering with dropdown selection

Hello, I am currently working on creating a Ng-table with a select dropdown menu for filtering the results. Here is where I am at so far. 1) How can I remove one of the pagination options that appear after applying the filter? I only want to keep one pagi ...

Ways to restrict input text to a specific set of values

When using an input text form, I need to ensure that users only insert values ranging from 1 to 10. However, my attempts to utilize a mask for customization have resulted in allowing values higher than 10. How can I restrict the input to only be allowed b ...

Developing interactive checkboxes for individual rows through React.js

Within a form, I have rows containing two inputs each. Upon clicking "add", a new row is created. For the second row onwards, by clicking "add" a checkbox labeled 1 should be added to indicate dependency on the previous row. In addition, for the third row, ...

Display a hidden div upon loading the page if a certain condition is met

As a beginner in this field, I am struggling to assist a friend with a project. He needs a simple quote form that can generate HTML quotes for his client on a private WordPress page. The form should display a specific div based on the radio button selecti ...

Utilize jQuery to dynamically add a CSS class to a button

When using jQuery to create dynamic buttons, is there a way to add a CSS class to the button during creation without needing an extra line of JavaScript to add the class afterwards? Below is a example code snippet: $.each(data, function (index, value) { ...

Align content to the bottom using Bootstrap 4

Looking for help with aligning content to the middle and bottom on a full-page bootstrap layout? The first page has a background image in the first div, and a darker layer on top in the second div. <div class="h-100 w-100"> <div class="h-100 w-10 ...

Images of varying sizes aligned at the bottom of cards, organized in rows with pure CSS styling

I am working on a layout that involves a container with "cards": images positioned above with related text below. Here's an example: <div class = "cards"> <div class = "card"> <div class = "image-wrap"> <img src= "im ...

Rails assets folder is not directed to the specified directory in the layout file

I have a dilemma in the application layout where I'm referencing assets (js, css, and img) in the public/assets/... directory. For example: <link href='assets/images/meta_icons/apple-touch-icon-144x144.png' rel='apple-touch-icon-pre ...

In PHP, the hidden input type doesn't always provide true concealment within a form if the value being passed

I am attempting to populate a hidden input field with the value of an XML string generated from an array. However, when I set the hidden field value using the XML string, it is being displayed in the HTML output. Surprisingly, if I use plain text as the va ...

What is the best way to eliminate the left margin entirely in CSS?

I am attempting to create an image view that fully covers the window, without any margins. I have tried various solutions such as setting the body margin and padding to 0, but they do not seem to work. body { margin: 0px; padding: 0px; } or *, html { ...

What is the reason behind my button appearing beneath my links in React?

Here is an image showcasing the current header render. The header consists of a HeaderMenu and 3 Links. While the links are functioning properly, the HeaderMenu is causing the links to be positioned below it. The HeaderMenu includes a div that wraps a Butt ...

How can you customize gutter widths for specific rows in Bootstrap 3?

Currently, I am constructing my own website using Bootstrap 3 and have come across a challenge that I would like to address: My goal is to assign distinctive gutter widths to certain rows in my design. The remaining sections of the site will adhere to th ...

Is there a way to display two cards side by side in mobile view?

On a desktop view, 2 cards are displayed in a row. I would like them to also appear in a row on mobile view. I tried using col-sm-4 but it's not working. How can I get the cards to display in a row on mobile view so that I can see both the product pho ...

Steps to create a loading bar that starts loading when an ajax task begins

What is the best way to show a loading bar as an AJAX task begins on a page? Additionally, how can we make sure that hitting the back button will return to what was being viewed before that specific AJAX task? ...

The image appears fuzzy until you hover over it and it transforms into a larger size

Encountering a strange issue with using the zoom property on an image during hover state. The image seems to be blurry before and after the scale transition, but surprisingly sharp during the actual transition. Any tips on how to prevent this blurriness in ...