CSS中的条件“宽度大于...”

时间:2019-04-18 05:44:02

标签: css conditional width

可以在 CSS 中设置条件,以检查ul的宽度是否大于X px,并应用不同的样式,或者是否有必要 js

谢谢!

1 个答案:

答案 0 :(得分:0)

以下代码可以为您提供帮助

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css" media="screen">
        body{font-family: helvetica;}
        ul{margin: 0;padding: 0;}
    </style>
</head>
<body>
    <div class="wrapper">
        <ul>
            <li>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo hic, quis quasi consequatur laudantium voluptatibus. Illo soluta quam eum dolore esse inventore accusamus, amet ipsum at numquam, blanditiis enim! Ad!</li>
            <li>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo hic, quis quasi consequatur laudantium voluptatibus. Illo soluta quam eum dolore esse inventore accusamus, amet ipsum at numquam, blanditiis enim! Ad!</li>
            <li>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo hic, quis quasi consequatur laudantium voluptatibus. Illo soluta quam eum dolore esse inventore accusamus, amet ipsum at numquam, blanditiis enim! Ad!</li>
            <li>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo hic, quis quasi consequatur laudantium voluptatibus. Illo soluta quam eum dolore esse inventore accusamus, amet ipsum at numquam, blanditiis enim! Ad!</li>
        </ul>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.js"></script>
    <script>
        var ulwidth = $('ul').width();
        console.log(ulwidth);
        if (ulwidth > 500) {
            alert('Width is greater than 500');
        }
        else {
            alert('Width is less than 500');
        }
    </script>
</body>
</html>

这将帮助您加载页面。