how to insert content before each new line using CSS - css

I have a HTML document that looks like the following, and using only CSS I want to insert an asterisk before each line. For the first line, the following works:
p:before {
content: "*";
}
<p>
test
<br> test2
<br> test3
</p>
but I don't know how to add the asterisk before each subsequent line.
I've tried:
p:before {
content: "*";
}
p>br:after {
content: "*";
}
<p>
test
<br> test2
<br> test3
</p>
but it doesn't seem to work.
How can I accomplish this?
codepen: https://codepen.io/ds241/pen/QWQyPKY

So, you select this div with class quote, extract its innerHTML, and convert it to a string with the toString() method. You then split this string with <br> separater, which results in an array. You then map this array to include a * before the start of each new line word.
Finally, you join this array together using the same <br> separator, and set the innerHTML of your quote to this. The code ends up looking like this.
const quote = document.querySelector(".quote");
let innerHTML = quote.innerHTML;
const t = innerHTML.toString();
let s = t.split("<br>");
s = s.map((i) => {
return `*${i}`;
});
s = s.join("<br>");
quote.innerHTML = s;

Two ways to Make a Line Break with Unicode
First assign the following CSS:
Figure I
p {
white-space: pre;
}
The above applies to both techniques. The first way is to use
in HTML.
Figure II
<p>test1
test2
test3
</p>
The second way is to use ::before, content, and \00000a.
Figure III
p::before {
content: "\00002a test1\00000a \00002a test2\00000a \00002a test3\00000a";
}
p {
white-space: pre
}
.a::before {
content: "\00002a test1\00000a \00002a test2\00000a \00002a test3\00000a";
}
<p class='a'></p>
<p>
*test4
*test5
*test6
</p>

This is because all your content is inside one element. And with the pseudo-selector, you can only target the element, not its content.
let p = document.querySelector('p');
let res = document.querySelector('.res');
res.innerText = p.innerText;
<p>
test
<br> test2
<br> test3
</p>
<div class="res"></div>

You'll need to wrap each of the lines of text. Depending on how you're implementing you can accomplish this in many ways. You could wrap them in individual tags, or tags, or if it's a list consider and tags. This will allow you to apply the CSS to each line.
p:before {
content: "*";
}
p>br:after {
content: "*";
}
<p>test</p>
<p>test2</p>
<p>test3</p>

You are missing one more ‘:’
So the css for this p would be something like:
p::before {
content: "*";
}
p:before {
content: "*";
}

Related

How to target CSS :last-of-type of consecutive elements (not ALL within a parent element)

Maybe this is not possible in CSS3 but I'm trying to automatically insert comma separators when there is multiple consecutive instances of footnotes within a paragraph.
The problem occurs when there is another footnote later on in the paragraph. You'll notice in Example 2 that the comma get's inserted after Footnote 2.
I've tried to switch it around and use first-child but this just ends in a reverse of the same problem.
sup:not(:first-child)::before {
content: ",";}
sup:last-child::before {
content: "";
}
Anyone know of a way to target the last-of-type when it's succeeded by any other content other than another of the same type?
sup:after {
content: ",";
}
sup:last-of-type:after {
content: "";
}
<h2>Example 1</h2>
<p>lorem upsum<sup>1,2</sup></p>
<h2>Example 2</h2>
<p>lorem upsum<sup>1,2</sup> flotsam jetsum<sup>3</sup></p>
You already have the answer: all you need to do is remove ALL your CSS as it's practically doing nothing.
sup:after {
content: ",";
}
sup:last-of-type:after {
content: "";
}
In your html, your code is <sup>1,2</sup> , which means you are already manually inputting the , for multiple values within <sup>.
That particular , within a <sup> with multiple values is not being generated by the above CSS but rather, is there only because you are manually typing it in.
In a <p> with only a single <sup>, the reason why it does not have a , is because of your class sup:last-of-type:after. Since that there is only a single instance of <sup> it is considered the last one too, thus, it adds content of "" to the end of it. Which once again is doing nothing since by default it's empty.
<h2>Example 1</h2>
<p>lorem upsum<sup>1,2</sup></p>
<h2>Example 2</h2>
<p>lorem upsum<sup class="sup-multiple">1,2</sup> flotsam jetsum<sup>3</sup></p>

How to get :before css element value

I have this minor stupid point I have to cover with automated test, and its driving me crazy, I am not able to get the value of :before css element, the code is really simple as the test also, but I still need some help on it. Here is the code I have.
.text-currency-positive::before {
content: "+ ";
}
<div class="amount">
<span class="text-currency text-currency-positive text-monospace text-nowrap">
::before
100,00 €
</span>
</div>
Ok here we go:
<h1 class="element">The value of my pseudo element is: </h1> // see the class
Then we add the pseudo element:
.element:before {
content: '+NEW';
}
Now the JS:
var content = window.getComputedStyle(
document.querySelector('.element'), ':before' // target the classes pseude
).getPropertyValue('content'); // here we can get the computed styles/values
This is not enough, as this would return a string ..."+". So we do this:
var makeVar = content.replace(/"/g, ''); // replace the quotes with nothing
var firstLetter = makeVar.charAt(0); // here we get the value of first char after we have replaced the quotes
Display it:
var h1 = document.querySelector(".element");
h1.innerHTML += " content is: " + firstLetter;
If you do not replace the quotes with the regex, youll get "+" returned, but maybe that is what you want.
Cheers, link:
https://codepen.io/damianocel/pen/ooJqem

Capitalize after a point in css

Having enough people writing in upper case, I inserted the syntax text-transform: lowercase; or the text to be written in lower case and syntax ::first-letter for a capital is created after the beginning of each sentence after the point.
text-transform: lowercase; works fine but for ::first-letter he created me a capital letter at the beginning of the sentence but not after!
Is it possible to create CSS capitalized after a point?
Keep all data into a variable and split it with the point you want. Then display all array inside paragraph. This might be working.
var str = "What ever you want to do. Please do it here.";
var res = str.split(".");
then use for loop and getElementbyId to replace the content
Try this:
str = 'ABC. DEF. XYZ';
str2 = str.toLowerCase();
str3 = str2.replace(/\. /g, '.</span> <span class = caps>')
$('#output').html('<span class = caps>' + str3)
.caps {
display: inline-block;
}
.caps::first-letter {
text-transform: uppercase;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="output"></div>
Convert the entire string to lower case; then replace the . with span elements; apply CSS rules to the span elements so that they are block level, inline and first letter capitalised); and just to tidy up, add an opening <span> before the replacement string to match the closing tag at the end of the first sentence.
<html>
<head>
<style>
p::first-letter {
font-weight: bold;
color: red;
}
</style>
<script>
function myFunction() {
var str = document.getElementById('data').innerHTML;
var res = str.split(".");
var data = "";
for(i=0; i<(res.length-1); i++){
var data = data + "<p>"+res[i]+".</p>";
}
document.getElementById("data").innerHTML = data;
}
</script>
</head>
<body onload="myFunction()">
<div id="data">What ever you want to do. Please do it here.</div>
</body>
</html>
This will automatically change the data onload.
Have more questions leave me a message in grandamour

Change one character only using CSS content

I inherited code that layers up a font heading - multiple divs draw the font - like this:
<div class = 'stage'>
<div class = 'layer'></div>
<div class = 'layer'></div>
<div class = 'layer'></div>
<div class = 'layer'></div>
</div>
The text itself is defined in the css under "layer.after" as "content: "xyz!"".
My aim is to style the "!" in "XYZ" in a different font... if the content was in the HTML section I could just add a span.
But here, my text is defined in Content in css.... How can I style the last letter differently than the rest in this type of setup, or add a span to the css, or even a short script to change the last letter (!) to a different font? I've tried last letter selector to no avail.
Using pseudoclasses on pseudoelements is not allowed. Therefore what you want is not possible without changing existing code.
Is there some actual text in HTML? If not you can use ::before for your text and ::after for "!" - JSFiddle
CSS
.layer::before {
content: 'xyz';
color: blue;
font-weight: bold;
}
.layer::after {
content: '!';
color: red;
font-weight: bold;
}
You'll need to use some script if changing the markup is not an option.
If you have jQuery available, try something like this:
$(function() {
$(".stage .layer").each(function() {
var content = $(this).html();
content = content.substr(0, content.length - 1)
+ "<span>"
+ content.substr(-1)
+ "</span>";
$(this).html(content);
});
})
See http://codepen.io/ondrakoupil/pen/VLBoXR for live example.

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';
}

Resources