可以使用CSS的flex布局来实现点击文字弹出自适应居中的div弹窗。以下是一个示例代码:

<!DOCTYPE html>
<html>
<head>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        .overlay {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            z-index: 1;
        }

        .popup {
            background-color: white;
            padding: 20px;
            border-radius: 5px;
        }

        .popup-close {
            text-align: right;
        }

        .popup-content {
            margin-top: 10px;
        }

        .show {
            display: flex;
        }
    </style>
</head>
<body>
    <p>点击文字弹出div弹窗:</p>
    <p><a href="#" onclick="showPopup()">点击弹窗</a></p>

    <div class="overlay" id="overlay">
        <div class="popup">
            <div class="popup-close">
                <a href="#" onclick="hidePopup()">关闭</a>
            </div>
            <div class="popup-content">
                这是一个弹窗
            </div>
        </div>
    </div>

    <script>
        function showPopup() {
            document.getElementById('overlay').classList.add('show');
        }

        function hidePopup() {
            document.getElementById('overlay').classList.remove('show');
        }
    </script>
</body>
</html>

以上代码中,通过flex布局的justify-content: centeralign-items: center属性来实现div居中。点击弹窗时,通过JavaScript代码来添加和移除CSS类名show来控制弹窗的显示和隐藏。

请写一个html点击文字弹出div弹窗的代码要求不管宽度和高度有多大都是自适应居中的代码

原文地址: http://www.cveoy.top/t/topic/i7nh 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录