当多个div扰乱其对齐时如何对齐div

时间:2016-01-15 13:54:26

标签: javascript html css alignment

我一直试图连续排列几个div。我有8个div彼此相邻,但当我尝试对齐第2行时,我只能装入一个div。我需要帮助

这是我的代码:

<!DOCTYPE html>
    <html lang="en">
    <body bgcolor="H70D4R334">
    <title>Ordinate</title>
    <head>
        <style text/css> 

    #div1{
        float:left;
        width:50px;
        height:50px;
        margin-top:50px;
        margin-right:500px;
        margin-left:400px;
        border:0px solid blue;
    }
    #div2{
        float:left;
        width:50px;
        height:50px;
        margin-top:50px;
        margin-right:50px;
        border:0px solid blue;
    }
    #div3{
        float:left;
        width:50px;
        height:50px;
        margin-top:300px;
        margin-right:5px;
        border:0px solid green;
    }
    #div4{
        float:left;
        width:50px;
        height:50px;
        margin-top:300px;
        margin-left:5px;
        border:0px solid green;
    }
    #div5{
        float:left;
        width:50px;
        height:50px;
        margin-top:300px;
        margin-left:5px;
        border:0px solid green;
    }
    #div6{
        float:left;
        width:50px;
        height:50px;
        margin-top:300px;
        margin-left:5px;
        border:0px solid green;
    }
    #div7{
        float:left;
        width:50px;
        height:50px;
        margin-top:300px;
        margin-left:5px;
        border:0px solid green;
    }
    #div8{
        float:left;
        width:50px;
        height:50px;
        margin-top:300px;
        margin-left:5px;
        border:0px solid green;
    }
    #div10{
        float:left;
        width:50px;
        height:50px;
        margin-top:100px;
        margin-left:400px;
        border:1px solid green;
    }
    #div11{
        float:left;
        width:50px;
        height:50px;
        margin-top:100px;
        margin-left:5px;
        border:1px solid green;
    }
    #div12{
        float:left;
        width:50px;
        height:50px;
        margin-top:100px;
        margin-left:5px;
        border:1px solid green;
    }
    #div13{
        float:left;
        width:50px;
        height:50px;
        margin-top:100px;
        margin-left:5px;
        border:1px solid green;        
    }
    #div14{
        float:left;
        width:50px;
        height:50px;
        margin-top:100px;
        margin-left:5px;
        border:1px solid green;
    }
    #div15{
        float:left;
        width:50px;
        height:50px;
        margin-top:100px;
        margin-left:5px;
        border:1px solid green;
    }
    #div16{
        float:left;
        width:50px;
        height:50px;
        margin-top:100px;
        margin-left:5px;
        border:1px solid green;
    }
    #div17{
        float:left;
        width:50px;
        height:50px;
        margin-top:100px;
        margin-left:5px;
        border:1px solid green;
    }
    #Button{
        float:center;
        width:200px;
        height:50px;
        margin:25px;
    }

    </style>



    <script>
    var dragSrcEl = null;
    var draggSrcID = null;

        function dragStart(ev) {
        draggSrcID = ev.target.id;
        dragSrcEl = ev.target;
        ev.dataTransfer.effectAllowed = 'move';
        ev.dataTransfer.setData('text/html', ev.target.outerHTML);
        }

        function allowDrop(ev) {
            ev.preventDefault();
        }

        function drop(ev) {
            ev.target.appendChild(document.getElementById(draggSrcID));
            ev.preventDefault();
        } 

        function swapDrop(ev) {

          if (ev.stopPropagation) {
            ev.stopPropagation(); // Stops some browsers from redirecting.
          }

          // Don't do anything if dropping the same image we're dragging.
          if (dragSrcEl != ev.target) {
            // Set the source image to the HTML of the image we're dropping on.
            srcEl = document.getElementById(draggSrcID);
            srcEl.outerHTML = ev.target.outerHTML;
            ev.target.outerHTML = ev.dataTransfer.getData('text/html');
           }
            ev.preventDefault();
        }       

    </script>   
    </head>
    <body>
    <center><font size="10">Ordinate</font></center>
        <div id="div10" ondrop="drop(event)" ondragover="allowDrop(event)"> 
        </div>
        <div id="div11" ondrop="drop(event)" ondragover="allowDrop(event)"> 
        </div>
        <div id="div12" ondrop="drop(event)" ondragover="allowDrop(event)"> 
        </div>
        <div id="div13" ondrop="drop(event)" ondragover="allowDrop(event)"> 
        </div>
        <div id="div14" ondrop="drop(event)" ondragover="allowDrop(event)"> 
        </div>
        <div id="div15" ondrop="drop(event)" ondragover="allowDrop(event)"> 
        </div>
        <div id="div16" ondrop="drop(event)" ondragover="allowDrop(event)"> 
        </div>
        <div id="div17" ondrop="drop(event)" ondragover="allowDrop(event)">
        </div> 
        <div id="div1" ondrop="swapDrop(event)" ondragover="allowDrop(event)">
        <img id="D" src="https://placeholdit.imgix.net/~text?txtsize=20&txt=D&w=50&h=50" draggable="true" ondragstart="dragStart(event)" />
        </div>
        <div id="div2" ondrop="swapDrop(event)" ondragover="allowDrop(event)">
        <img id="E" src="https://placeholdit.imgix.net/~text?txtsize=20&txt=E&w=50&h=50" draggable="true" ondragstart="dragStart(event)">
        </div>
        <div id="div3" ondrop="swapDrop(event)" ondragover="allowDrop(event)">
        <img id="I" src="https://placeholdit.imgix.net/~text?txtsize=20&txt=I&w=50&h=50" draggable="true" ondragstart="dragStart(event)">  
        </div>
        <div id="div4" ondrop="swapDrop(event)" ondragover="allowDrop(event)"> 
        <img id="T" src="https://placeholdit.imgix.net/~text?txtsize=20&txt=T&w=50&h=50" draggable="true" ondragstart="dragStart(event)">
        </div>
        <div id="div5" ondrop="swapDrop(event)" ondragover="allowDrop(event)"> 
        <img id="A" src="https://placeholdit.imgix.net/~text?txtsize=20&txt=A&w=50&h=50" draggable="true" ondragstart="dragStart(event)">
        </div>
        <div id="div6" ondrop="swapDrop(event)" ondragover="allowDrop(event)"> 
        <img id="N" src="https://placeholdit.imgix.net/~text?txtsize=20&txt=N&w=50&h=50" draggable="true" ondragstart="dragStart(event)">  
        </div>
        <div id="div7" ondrop="swapDrop(event)" ondragover="allowDrop(event)"> 
        <img id="O" src="https://placeholdit.imgix.net/~text?txtsize=20&txt=O&w=50&h=50" draggable="true" ondragstart="dragStart(event)">
        </div>
        <div id="div8" ondrop="swapDrop(event)" ondragover="allowDrop(event)"> 
        <img id="R" src="https://placeholdit.imgix.net/~text?txtsize=20&txt=R&w=50&h=50" draggable="true" ondragstart="dragStart(event)">  
        </div>
        <center><input id="Button" type="submit" value="Click me to check" onclick="HitEnter()"></center>
        <script>
            word = "ordinate"
            function HitEnter() {
                for (i = 10, str = ""; i <= 17; i++) {
                    if (document.getElementById("div" + i).childNodes.length > 1) {
                        str += document.getElementById("div" + i).childNodes[1].id
                    }
                }
                if (str.toLowerCase() == word) {
                    alert("Good job!!!")
                }
                else {
                alert('You got the word incorrect! Please try again.')

                }
            }
        </script>
    </body>
    </html>

我是css,js和html的初学者。我将不胜感激任何帮助。谢谢!

1 个答案:

答案 0 :(得分:1)

您已使用ID 1

为div设置了以下属性
margin-right: 500px;
margin-left: 400px;
因此,它距离左边界400 px。由于您还设置了边距权限,因此右边的下一个元素设置为500px。这就是你的行破坏的原因。

编辑:

如何修复它:删除所有

margin-right 
从第二行

添加

margin-left: 5px 

使其与另一行相同