javascript - 如何确保在调整窗口大小时 div 不会开始新行?

标签 javascript html css

我正在制作一个简单的井字棋游戏来娱乐。一切工作正常,除了每当我调整窗口大小时,我最右边的 div 都会开始一个新行。为什么会出现这种情况?我几乎尝试了所有的方法。我做了一个根据窗口大小改变div大小的方法。我尝试过 float :左。我尝试过显示:内联和显示:内联 block 。这是我的代码:

            <div id = "pieces">
                <style>
                    .pieces 
                    {
                        width: 100%;
                        height: 100%;
                        float: left;
                        white-space: nowrap;
                        overflow: auto;
                        margin-left: auto;
                        margin-right: auto;
                        
                    }
                </style>
            <div id = "xMark" class = "x">
                X
                
                <script>
                    
                    window.addEventListener("resize", function dimensions() {
                        var textWidth = (window.innerWidth/3);
                        var textHeight = (window.innerHeight/3);
                        document.getElementById("xMark").style.height = textHeight+"px";
                        document.getElementById("xMark").style.width = textWidth+"px";
                    });
                    
                    //document._intervalId = setInterval(dimensions, 1);
                </script>
                <style>
                    .x
                    {
                        
                        
                        height: 33.333333333333vh;
                        display: inline-block;
                        
                        width:  33.333333333333vw;
                        background-color: lightblue;
                        font-size: 30px;
                        border: 5px solid black;
                        text-align: center;
                        vertical-align: middle;
                        overflow: auto;
                        
                        
                        
                    }
                    .x:hover
                    {
                        background-color: blue;
                        vertical-align: middle;
                        text-align: center;
                        cursor: pointer;
                        overflow: auto;
                    }
                </style>
            </div>
            <div id = "hyphonMark" class = "hyphon">
                --
                
                <script>
                    
                    window.addEventListener("resize", function dimensions() {
                        var textWidth = (window.innerWidth/3);
                        var textHeight = (window.innerHeight/3);
                        document.getElementById("hyphonMark").style.height = textHeight+"px";
                        document.getElementById("hyphonMark").style.width = textWidth+"px";
                    });
                    //document._intervalId = setInterval(dimensions, 1);
                    
                </script>
                <style>
                    .hyphon
                    {
                        
                        height: 33.333333333333vh;
                        display: inline-block;
                        width:  33.333333333333vw;
                        background-color: lightblue;
                        font-size: 30px;
                        border: 5px solid black;
                        text-align: center;
                        vertical-align: middle;
                        overflow: auto;
                        
                        
                        
                        
                    }
                    .hyphon:hover
                    {
                        background-color: blue;
                        vertical-align: middle;
                        text-align: center;
                        cursor: pointer;
                    }
                </style>
            </div>
            <div id = "oMark" class = "O">
                O
                
                <script>
                    
                    function dimensions() {
                        var textWidth = (window.innerWidth/3);
                        var textHeight = (window.innerHeight/3);
                        document.getElementById("oMark").style.height = textHeight+"px";
                        document.getElementById("oMark").style.width = textWidth+"px";
                    }
                    document.getElementById("oMark").addEventListener("resize", dimensions);
                    //document._intervalId = setInterval(dimensions, 1);
                    
                </script>
                <style>
                    .O
                    {
                        
                        height: 33.333333333333vh;
                        display: inline-block;
                        
                        width: 32.333333333333vw;
                        background-color: lightblue;
                        font-size: 30px;
                        border: 5px solid black;
                        text-align: center;
                        vertical-align: middle;
                        overflow: auto;
                        
                       
                        
                    }
                    .O:hover
                    {
                        background-color: blue;
                        vertical-align: middle;
                        text-align: center;
                        cursor: pointer;
                    }
                </style>
            </div>
            
            </div>
        </div>

最佳答案

您需要将 .pieces (类)更改为 #pieces (id),然后您的 CSS 才能工作。

            <div id="pieces">
                <style>
                    #pieces 
                    {
                        width: 100%;
                        height: 100%;
                        float: left;
                        white-space: nowrap;
                        overflow: auto;
                        margin-left: auto;
                        margin-right: auto;
                        
                    }
                </style>
            <div id = "xMark" class = "x">
                X
                
                <script>
                    
                    window.addEventListener("resize", function dimensions() {
                        var textWidth = (window.innerWidth/3);
                        var textHeight = (window.innerHeight/3);
                        document.getElementById("xMark").style.height = textHeight+"px";
                        document.getElementById("xMark").style.width = textWidth+"px";
                    });
                    
                    //document._intervalId = setInterval(dimensions, 1);
                </script>
                <style>
                    .x
                    {
                        
                        
                        height: 33.333333333333vh;
                        display: inline-block;
                        
                        width:  33.333333333333vw;
                        background-color: lightblue;
                        font-size: 30px;
                        border: 5px solid black;
                        text-align: center;
                        vertical-align: middle;
                        overflow: auto;
                        
                        
                        
                    }
                    .x:hover
                    {
                        background-color: blue;
                        vertical-align: middle;
                        text-align: center;
                        cursor: pointer;
                        overflow: auto;
                    }
                </style>
            </div>
            <div id = "hyphonMark" class = "hyphon">
                --
                
                <script>
                    
                    window.addEventListener("resize", function dimensions() {
                        var textWidth = (window.innerWidth/3);
                        var textHeight = (window.innerHeight/3);
                        document.getElementById("hyphonMark").style.height = textHeight+"px";
                        document.getElementById("hyphonMark").style.width = textWidth+"px";
                    });
                    //document._intervalId = setInterval(dimensions, 1);
                    
                </script>
                <style>
                    .hyphon
                    {
                        
                        height: 33.333333333333vh;
                        display: inline-block;
                        width:  33.333333333333vw;
                        background-color: lightblue;
                        font-size: 30px;
                        border: 5px solid black;
                        text-align: center;
                        vertical-align: middle;
                        overflow: auto;
                        
                        
                        
                        
                    }
                    .hyphon:hover
                    {
                        background-color: blue;
                        vertical-align: middle;
                        text-align: center;
                        cursor: pointer;
                    }
                </style>
            </div>
            <div id = "oMark" class = "O">
                O
                
                <script>
                    
                    function dimensions() {
                        var textWidth = (window.innerWidth/3);
                        var textHeight = (window.innerHeight/3);
                        document.getElementById("oMark").style.height = textHeight+"px";
                        document.getElementById("oMark").style.width = textWidth+"px";
                    }
                    document.getElementById("oMark").addEventListener("resize", dimensions);
                    //document._intervalId = setInterval(dimensions, 1);
                    
                </script>
                <style>
                    .O
                    {
                        
                        height: 33.333333333333vh;
                        display: inline-block;
                        
                        width: 32.333333333333vw;
                        background-color: lightblue;
                        font-size: 30px;
                        border: 5px solid black;
                        text-align: center;
                        vertical-align: middle;
                        overflow: auto;
                        
                       
                        
                    }
                    .O:hover
                    {
                        background-color: blue;
                        vertical-align: middle;
                        text-align: center;
                        cursor: pointer;
                    }
                </style>
            </div>
            
            </div>
        </div>

关于javascript - 如何确保在调整窗口大小时 div 不会开始新行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40391789/

相关文章:

javascript - 根据 id 数组字段在 Mongoose 中查找文档

javascript - jQuery ajax 的 URL 无效

jQuery获取容器中加载的图像宽度/高度

javascript - 如何发送带有用户输入地址的 HTML 电子邮件?

javascript - Web 文本字段中的字符集验证

html - 使用 :visited on links doesn't work properly

非空属性的 CSS 属性选择器

javascript - 在 JavaScript for 循环中休眠

javascript - 按钮元素 onclick 无需 JQuery 即可运行 javascript 函数

html - 是否可以访问谷歌浏览器的主题?