-3

how can i make my text(txt) centered, im havimg some trouble?

<script>
    
        function myFunction() {
        var txt
        if (confirm("Do you wanna go out with me on Friday 23rd of December?")) {
        txt = "YAAAAAY!"; 
        } else {
        txt = "PLEASE CLICK AGAIN! ";
        }
        document.getElementById("demo").innerHTML = txt;
        }
        
          </script>

1 Answers1

0

If you wan't to make text centered (horizontally) you mainly have 2 options

First one is adding style="text-align: center" to your html tag. It would look something like this <p style="text-align: center">Text here</p>

The second option is gonna be creating a class in your <style> tag

<html>
... <body>
        <p class="text-center">I am centered</p>
        <style>
            .text-center: {
                text-align: center;
            }
        </style>
    </body>
</html>

If you are creating a element in javascript using document.createElement() i suggest using the second solution and combining it with this script

function addClassTo(el, class) {
    el.classList.add('class');
}

// You would use it like this
const element = document.createElement("p");
addClassTo(element, "text-center") 

// For your case if you have the element created
const element = document.getElementById("demo");
addClassTo(demo, "text-center");

I hope this helps