Search code examples
javascripthtmlcssclassheight

The .style of my class is null in javascript


I have a simple web page where on the top there´s a div with this:

<div class="divmain-login">
    <div class="divmain-sup-login">
        <div class="textos">
            <h1>Manejo de archivos</h1>
            <h2>Gaspar Giménez 2021</h2><h3>v 1.2</h3>
        </div>
    </div>
</div>

I want to have the attribute height of this block (with the class .divmain-sup-login) with this JS code:

function setDivSupHeight(){
const DivSup = document.querySelector(".divmain-sup-login");
console.log(DivSup.getAttribute("style"));

    const divh = DivSup.style.height;
    console.log(divh);
}

    setDivSupHeight();

The console returns this:

null (line 3)

nothing (line 7)

The atrribute style returns null, and this is the CSS:

.divmain-sup-login{
position: sticky;
background-color: #F56D7B;
text-align: center;
width: 100vw;


height: 90px;
    display: flex;
    flex-flow: column wrap;
    justify-content: center;
    align-content: center;
}

I dont know how can i get the height of this block.


Solution

  • Element.getAttribute('style') and Element.style refer to the style="..." attribute of the element - not to anything coming from style rules or final computed style, and in your case that attribute was not used indeed so it's value is null.

    What you actually probably want here is Element.getComputedValue().getProperty(), see https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle