Create Line Breaks in Data Description [duplicate] - css

This question already has answers here:
CSS data attribute new line character & pseudo-element content value
(5 answers)
Closed 9 years ago.
I have CSS that is using Data Description to display text on a page.
Code:
CSS
#nav a:after {
text-align:center;
content: attr(data-description);
display: block;
}
HTML
<li><a data-description="The Thinking Man,The Artist, The Philosopher" href="#">The Renaissance Man</a></li>
What I am having trouble with and would I would like to do is to have each part of the sentence on its on line. In other words
The Thinking Man
The Artist
The Philosopher
But when I insert a <br /> it doesn't creat a break it just displays <br /> in the code. How would I create line breaks in a data description using the following code?

You can add a new line by combining \A with white-space:pre-wrap;, for example:
p:after {
content:"Line 1\ALine 2";
white-space:pre-wrap;
}
/* Line 1
* Line 2 */
JSFiddle example.
Unfortunately it doesn't seem you can do this using attr().
<p data-description="Line 1\ALine 2"></p>
p:after {
content:attr(data-description);
white-space:pre-wrap;
}
/* Line 1\ALine 2 */
JSFiddle example.
What you could do to get around this is to use multiple data-* attributes:
<p data-description1="Line 1" data-description2="Line 2"></p>
p:after {
content:attr(data-description1) "\A" attr(data-description2);
white-space:pre-wrap;
}
/* Line 1
* Line 2 */
JSFiddle example.
I don't know if this is a usable solution to your problem, however.

The content attribute won't ever interpret your Html as markup, so you'll need to go about this another way.
One way would be escaping the newline and setting white-space: pre; in your pseudoelement. This value is an abbreviation for 'preserves,' which means the newline will be rendered by the browser.
The Thinking Man,\AThe Artist,\AThe Philosopher
View on JSFiddle
For more on the white-space property, refer to the MDN article on it..
For more on escaping new lines in content, check out the CSS2 specs on strings.

Related

Is there a way to change an elements css based off of its inner text? [duplicate]

This question already has answers here:
Is there a CSS selector for elements containing certain text?
(20 answers)
Closed last year.
I've searched all over the internet for this answer and I'm still clueless, maybe one of you will know. Basically I'm wondering if in a .css file could you theoredically change the css of an object based off of its text
for example:
h1[innerText="some text thats in the h1"] {
/*styles oh what wonderful styles*/
}
yeah you can use JavaScript with if and else statement as if condition satisfies. it will be executed to change inner HTML. Give me 2 mins to attach code. I can only do this by using js. Further properties can be introduce as here first the color was nothing but after that I introduced pink color as background.
x = document.getElementById('text')
y = x.innerHTML
if (y=="her") {
x.innerHTML = "Paragraph changed!";
document.getElementById("p2").style.backgroundColor = "pink";
}else{
console.log('lol')
}
#p2{
width:200px;
height:100px;
padding-left: 10px;
}
<div class="p2" id="p2">
<h1 id='text'>her</h1>
</div>

How do apply a CSS pseudo element to elements matching multiple classes? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I've had a lot success of applying pseudo elements to single class, but when I only want it to work when a parent class (we'll say .foo) also applies, the content is not applied before the other class specified (in this case, 'bar):
.foo .bar:before {
content: "URL: ";
}
Is there some way to achieve this?
Update
Actual example:
.metajelo-ui_resourceMDSource:before {
content: " Resource Metadata Source ";
font-weight: bold;
}
/* FIXME: this is not working for some reason: */
.metajelo-ui_resourceMDSource.metajelo-ui_url:before {
content: "URL: ";
}
The idea is that :before should be triggered on any url-bearing element that is also a resourceMDSource-bearing element (taking the .metajelo-ui_ prefix into account).
As we can see, there is a ::before for the .metajelo-ui_resourceMDSource, which corresponds to the first style posted above, however, the highlighted span with the url class has no ::before shown, and there is no "URL: " being rendered on the page.
In case this isn't descriptive enough, here is a live demo: https://labordynamicsinstitute.github.io/metajelo-ui/ (click add item for Supplementary Products) - will be happy to fill in more details later.
If you apply the class to the same element...
HTML
<div class="foo bar"></div>
Then your CSS should look like this...
.foo.bar:before { [rules here] }
If the class is on any ancestor like this...
<div class="foo">
<div class="bar"></div>
</div>
Then your CSS should look like this (space between classes)...
.foo .bar:before { [rules here] }
Just remove the space between .foo and .bar to select multiple classes (element is .foo also is .bar).
Here's the code:
.foo.bar:before {
content: "URL: ";
}
<p class="bar alo">bar without foo</p>
<p class="bar foo">bar with foo</p>

Using <a> + :after to make a better css ruby layout but alignment doesn't work

I've tried to reuse the ruby layout from the learnJapanese reddit (example) which works with <a href='#reading' title='hiragana'>japanese kanji</a> and then does display the hiragana before the title via css for #reading :before.
In my case I want to display the reading below the word (using css :after) but I am not sure how I could align it with the rest of the text:
"onetwo continue" should be on the same line.
Here is an example of what I want this to look like:
jsfiddle is here: http://jsfiddle.net/eex8gx6g/
Using position:relative for the subtitle and a margin-bottom to accommodate for the subsequent lines of text:
a[href$="/reading"], a[href$="#reading"] {
...
position:relative;
margin-bottom:1em;
...
}
...
a[href$="/reading"]:after, a[href$="#reading"]:after {
...
position:absolute;
top:1.4em;
left:0em;
...
}
http://jsfiddle.net/eex8gx6g/5/

CSS How to add new pseudo content

.newclass {
content: "\00A9";
}
In the above code, a copyright icon shows up. I have a question and a requirement.
Question - Where is this icon come from? any image from my pc, internet or some other way.
Requirement - If I have to introduce a new code, and associate a new icon for that code, how to do it?
It's an unicode escape sequence,
here you can find some examples:
http://css-tricks.com/snippets/html/glyphs/
these works with pseudo elements and are unicode characters.
http://codepen.io/anon/pen/EFAtk
there a list here : http://unicode-table.com/en/
HTML test
©
<p> #
http://unicode-table.com/en/</p>
CSS test
:before {
color:red;
}
body:before {
content: "\00A9";
}
p:before {
content:'\0040';
}

Add line break to ::after or ::before pseudo-element content

I do not have access to the HTML or PHP for a page and can only edit via CSS. I've been doing modifications on a site and adding text via the ::after or ::before pseudo-elements and have found that escape Unicode should be used for things such as a space before or after the added content.
How do I add multiple lines in the content property?
In example the HTML break line element is only to visualize what I would like to achieve:
#headerAgentInfoDetailsPhone::after {
content: 'Office: XXXXX <br /> Mobile: YYYYY ';
}
The content property states:
Authors may include newlines in the generated content by writing the "\A" escape sequence in one of the strings after the 'content' property. This inserted line break is still subject to the 'white-space' property. See "Strings" and "Characters and case" for more information on the "\A" escape sequence.
So you can use:
#headerAgentInfoDetailsPhone:after {
content:"Office: XXXXX \A Mobile: YYYYY ";
white-space: pre; /* or pre-wrap */
}
http://jsfiddle.net/XkNxs/
When escaping arbitrary strings, however, it's advisable to use \00000a instead of \A, because any number or [a-f] character followed by the new line may give unpredictable results:
function addTextToStyle(id, text) {
return `#${id}::after { content: "${text.replace(/"/g, '\\"').replace(/\n/g, '\\00000a')} }"`;
}
Nice article explaining the basics (does not cover line breaks, however).
A Whole Bunch of Amazing Stuff Pseudo Elements Can Do
If you need to have two inline elements where one breaks into the next line within another element, you can accomplish this by adding a pseudo-element :after with content:'\A' and white-space: pre
HTML
<h3>
<span class="label">This is the main label</span>
<span class="secondary-label">secondary label</span>
</h3>
CSS
.label:after {
content: '\A';
white-space: pre;
}
I had to have new lines in a tooltip. I had to add this CSS on my :after :
.tooltip:after {
width: 500px;
white-space: pre;
word-wrap: break-word;
}
The word-wrap seems necessary.
In addition, the \A didn't work in the middle of the text to display, to force a new line.
worked. I was then able to get such a tooltip :
You may try this
#headerAgentInfoDetailsPhone
{
white-space:pre
}
#headerAgentInfoDetailsPhone:after {
content:"Office: XXXXX \A Mobile: YYYYY ";
}
Js Fiddle
For people who will going to look for 'How to change dynamically content on pseudo element adding new line sign" here's answer
Html chars like
will not work appending them to html using JavaScript because those characters are changed on document render
Instead you need to find unicode representation of this characters which are U+000D and U+000A so we can do something like
var el = document.querySelector('div');
var string = el.getAttribute('text').replace(/, /, '\u000D\u000A');
el.setAttribute('text', string);
div:before{
content: attr(text);
white-space: pre;
}
<div text='I want to break it in javascript, after comma sign'></div>
Hope this save someones time, good luck :)
Add line break to ::after or ::before pseudo-element content
.yourclass:before {
content: 'text here first \A text here second';
white-space: pre;
}
Found this question here that seems to ask the same thing: Newline character sequence in CSS 'content' property?
Looks like you can use \A or \00000a to achieve a newline
<p>Break sentence after the comma,<span class="mbr"> </span>in case of mobile version.</p>
<p>Break sentence after the comma,<span class="dbr"> </span>in case of desktop version.</p>
The .mbr and .dbr classes can simulate line-break behavior using CSS display:table. Useful if you want to replace real <br />.
Check out this demo Codepen: https://codepen.io/Marko36/pen/RBweYY,
and this post on responsive site use: Responsive line-breaks: simulate <br /> at given breakpoints.

Resources