-1

How to assign values or string to p tag through CSS?

I am trying like this, why I am doing like this because I come from Android programming.

HTML file:

<p> </p>

CSS file:

p{
    text:"today"
}

result should= "today" in browser

So please help me.

Minal Chauhan
  • 6,025
  • 8
  • 21
  • 41

3 Answers3

3

Although I can't see a reason why you can't just add the text to the HTML file, I will still answer your question.

There is no way to add text inside of the HTML tag. The only way that you can add text around HTML is through pseudo elements like the following:

p:before{
    content: "today";
    color: black;
}

This is not recommended however, due to the fact that the text won't actually exist in the html and will need to be styled to display properly.

A much better solution would be to use javascript

<script>
     document.getElementById('todayTag').innerHTML = "today";
</script>

The 'todayTag' refers to an ID that will be placed on the p tag.

Niall Walker
  • 155
  • 1
  • 12
  • my one static html file has more css files based on some condition,it should be dynamic ,any way you give some good answer i like it –  Mar 07 '18 at 11:09
1

1)It is not possible in css,

2)Use jQuery or

$("p").html("today");

3)Use JavaScript

document.getElementsByTagName("P")[0].innerHTML = "Today";

note [0] is the index

  • if you use like this p:before{ content: "today"; color: black; } if you have alreday text in

    hi

    then your output is= hi today
    –  Mar 07 '18 at 11:32
0

You cannot do that via css alone, use javascript for that instead.

document.getElementById("demo").innerHTML = "text";
<p id="demo">

</p>
aMJay
  • 2,215
  • 6
  • 22
  • 34
  • my one static html file has more css files based on some condition,it should be dynamic ,any way you give some good answer i like it –  Mar 07 '18 at 11:10