Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply a CSS property using JavaScript?

I have the following structure of the code. If the height of UL exceeds 270 pixels, then I want to add the CSS property overflow-y:scroll; or else as it is.

<ul class="tabs">
    <li>Content</li>
    <li>Content</li>
    <li>Content</li>
</ul>
like image 785
Swati Avatar asked Feb 12 '26 18:02

Swati


2 Answers

The following code uses jQuery to get the height and compare it. Note that it probably needs to be visible at the point where you call this to have a valid height.

$(function() {
    $(".tabs").each( function() {
        var $this = $(this);
        if ($this.height() > 270) {
            $this.css( 'overflow-y', 'scroll' );
        }
    });
});

Note this will work on all elements with the class tabs, not just the first one.

like image 130
tvanfosson Avatar answered Feb 14 '26 09:02

tvanfosson


if($(".tabs").height() > 270) {
    $(".tabs").css("overflow-y", "scroll");
}

Assuming you're using jQuery.

like image 40
Makram Saleh Avatar answered Feb 14 '26 08:02

Makram Saleh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!