2017-05-14 4 views
0

Ich habe stundenlang, um dieses Problem zu jagen, hier ist der Code:3 Divs nebeneinander Überlauf einander

<!DOCTYPE html> 
<html> 
<head> 
<style> 
html, body{ 
    height: 100%; 
    max-height: 100%; 
    overflow:hidden; 
} 


.controls{ 
    display: table; 
    height: 10%; 
    margin-top: 1%; 
    width: 100%; 
} 
#w1 { 
    width:25%; 
} 
#can 
    float: left; 
    padding: 0; 
    margin: 0; 
    top: 0; 
} 
#canTwo{ 
    float: left; 
    padding: 0; 
    margin: 0; 
    top: 0; 

} 
textarea { 
    outline: none; 
    -webkit-box-shadow: none; 
    -moz-box-shadow: none; 
    box-shadow: none; 
    font-size: 1.25vw; 
    height: 100%; 
    width: 100%; 
} 
#w2{ 
    width:50%; 
} 
#w3{ 
    width:25%; 
} 

.controlbuttons { 
    display: table-cell; 
    height: 100%; 

} 

</style> 
</head> 
<body> 


<div class="controls"> 
    <div class="controlbuttons" id="w1"><canvas id = "can" width = "0" height = "0"></div> 
    <div class="controlbuttons" id="w2"><textarea rows="3" cols="50"></textarea></div> 
    <div class="controlbuttons" id="w3"><canvas id = "canTwo" width = "0" height = "0"></div> 
</div> 

</div> 
<script> 

document.addEventListener("DOMContentLoaded", function(event) { 
    fitToContainer(); 
}); 
var canvas = document.getElementById("can"), 
    ctx = canvas.getContext('2d'), 
    canvasTwo = document.getElementById("canTwo"), 
    ctxTwo = canvasTwo.getContext('2d'); 
function fitToContainer(){ 

    var control = document.getElementsByClassName("controlbuttons")[0]; 
    var h = control.clientHeight; 
    var w = control.clientWidth; 
    canvas.height = h; 

    canvas.width = w; 

    canvasTwo.height = h; 

    canvasTwo.width = w; 

    ctx.fillStyle = "green"; 
    ctx.fillRect(0, 0, 5000, 5000); 

    ctxTwo.fillStyle = "green"; 
    ctxTwo.fillRect(0, 0, 5000, 5000); 


} 

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

jsfiddle: https://jsfiddle.net/ca3uw837/

Grundsätzlich ich ein Textfeld haben und seine Breite nimmt 50% der Seite und es ist genau in der Mitte, es gibt zwei Leinwände auf seiner Seite, die 25% Breite nehmen. Ich versuche, sie zu bekommen perfekt auszurichten (gleiche Höhe, genau eines neben dem anderen), aber hier ist, wie es auf meinem PC aussieht: enter image description here

Was soll ich tun? benutze flexbox? Ich bin nicht sicher, ob ich weiß, wie ich es erreichen kann, da Leinwände mit ihrer Dimensionierungstechnik sehr schwierig sind. Vielen Dank für Ihre Zeit.

+0

, was die Höhe der Leinwand und dem Textbereich bestimmt? Der "Steuer" -Behälter? –

Antwort

0

Um table-cell Elemente an der Spitze auszurichten, sollten Sie verwenden: vertical-align: top. Beide canvas fehlen height und width Eigenschaft, legen Sie sie auf 100%. In box-sizing: border-box auf das Textfeld:

box-sizing: border-box

Die Breite und Höhe Eigenschaften (und Min/Max-Eigenschaften) enthält Inhalt, Polsterung und Grenze, aber nicht die Marge

So :

textarea { 
    /** ... rest of styles ...*/ 
    margin: 0; 
    box-sizing: border-box; 
    float: left; 
} 
.controlbuttons { vertical-align: top; } /* Align items to the top */ 
.controlbuttons canvas { height: 100%; width: 100%; } 

Demo: (getestet in firefox 53.0.2 & Chrome 56.0.2924.87)

document.addEventListener("DOMContentLoaded", function(event) { 
 
    fitToContainer(); 
 
}); 
 
var canvas = document.getElementById("can"), 
 
    ctx = canvas.getContext('2d'), 
 
    canvasTwo = document.getElementById("canTwo"), 
 
    ctxTwo = canvasTwo.getContext('2d'); 
 

 
function fitToContainer() { 
 

 
    var control = document.getElementsByClassName("controlbuttons")[0]; 
 
    var h = control.clientHeight; 
 
    var w = control.clientWidth; 
 
    canvas.height = h; 
 

 
    canvas.width = w; 
 

 
    canvasTwo.height = h; 
 

 
    canvasTwo.width = w; 
 

 
    ctx.fillStyle = "green"; 
 
    ctx.fillRect(0, 0, 5000, 5000); 
 

 
    ctxTwo.fillStyle = "green"; 
 
    ctxTwo.fillRect(0, 0, 5000, 5000); 
 

 

 
}
html, 
 
body { 
 
    height: 100%; 
 
    max-height: 100%; 
 
    overflow: hidden; 
 
} 
 

 
.controls { 
 
    display: table; 
 
    height: 10%; 
 
    margin-top: 1%; 
 
    width: 100%; 
 
} 
 

 
#w1 { 
 
    width: 25%; 
 
} 
 

 
textarea { 
 
    outline: none; 
 
    -webkit-box-shadow: none; 
 
    -moz-box-shadow: none; 
 
    box-shadow: none; 
 
    font-size: 1.25vw; 
 
    height: 100%; 
 
    width: 100%; 
 
    margin: 0; 
 
    box-sizing: border-box; 
 
    float: left; 
 
} 
 
#w2 { 
 
    width: 50%; 
 
} 
 
#w3 { 
 
    width: 25%; 
 
} 
 
.controlbuttons { 
 
    display: table-cell; 
 
    height: 100%; 
 
    vertical-align: top; 
 
    
 
} 
 
.controlbuttons canvas { 
 
    float: left; 
 
    padding: 0; 
 
    margin: 0; 
 
    top: 0; 
 
    height: 100%; 
 
    width: 100%; 
 
}
<!DOCTYPE html> 
 
<html> 
 

 

 
<body> 
 

 

 
    <div class="controls"> 
 
    <div class="controlbuttons" id="w1"> 
 
     <canvas id="can" width="0" height="0"></canvas> 
 
    </div> 
 
\t <div class="controlbuttons" id="w2"> 
 
     <textarea rows="3" cols="50"></textarea> 
 
    </div> 
 
    <div class="controlbuttons" id="w3"> 
 
     <canvas id = "canTwo" width = "0" height = "0"></canvas> 
 
    </div> 
 
    </div> 
 

 
</body> 
 
</html>

+0

Können Sie näher erläutern, was das Problem war und was die Lösung war? – RunningFromShia

+0

@RunningFromShia hat meine Antwort aktualisiert. –

1

Anwenden flexbox zu .controls die untergeordneten Elemente auszurichten. Wenden Sie außerdem box-sizing: border-box auf textbox an, da die Standardpolsterung mit der 100% Höhe des Textfelds hinzugefügt wird. border-box wird die Polsterung einschließlich der Höhe machen.

document.addEventListener("DOMContentLoaded", function(event) { 
 
    fitToContainer(); 
 
}); 
 
var canvas = document.getElementById("can"), 
 
    ctx = canvas.getContext('2d'), 
 
    canvasTwo = document.getElementById("canTwo"), 
 
    ctxTwo = canvasTwo.getContext('2d'); 
 

 
function fitToContainer() { 
 

 
    var control = document.getElementsByClassName("controlbuttons")[0]; 
 
    var h = control.clientHeight; 
 
    var w = control.clientWidth; 
 
    canvas.height = h; 
 

 
    canvas.width = w; 
 

 
    canvasTwo.height = h; 
 

 
    canvasTwo.width = w; 
 

 
    ctx.fillStyle = "green"; 
 
    ctx.fillRect(0, 0, 5000, 5000); 
 

 
    ctxTwo.fillStyle = "green"; 
 
    ctxTwo.fillRect(0, 0, 5000, 5000); 
 

 

 
}
html, 
 
body { 
 
    height: 100%; 
 
    max-height: 100%; 
 
    overflow: hidden; 
 
} 
 

 
.controls { 
 
    display: flex; 
 
    height: 10%; 
 
    margin-top: 1%; 
 
    width: 100%; 
 
} 
 

 
#w1 { 
 
    width: 25%; 
 
} 
 

 
#can float: left; 
 
padding: 0; 
 
margin: 0; 
 
top: 0; 
 

 
} 
 
#canTwo { 
 
    float: left; 
 
    padding: 0; 
 
    margin: 0; 
 
    top: 0; 
 
} 
 
textarea { 
 
    outline: none; 
 
    -webkit-box-shadow: none; 
 
    -moz-box-shadow: none; 
 
    box-shadow: none; 
 
    font-size: 1.25vw; 
 
    height: 100%; 
 
    width: 100%; 
 
    box-sizing: border-box; 
 
} 
 
#w2 { 
 
    width: 50%; 
 
} 
 
#w3 { 
 
    width: 25%; 
 
} 
 
.controlbuttons { 
 
    height: 100%; 
 
}
<div class="controls"> 
 
    <div class="controlbuttons" id="w1"><canvas id="can" width="0" height="0"></div> 
 
    <div class="controlbuttons" id="w2"><textarea rows="3" cols="50"></textarea></div> 
 
    <div class="controlbuttons" id="w3"><canvas id = "canTwo" width = "0" height = "0"></div> 
 
</div>

+0

Können Sie näher erläutern, was das Problem war und was die Lösung war? – RunningFromShia

+0

Die Textbox hat standardmäßig eine Auffüllung, die ihre effektive Größe größer als die 100% macht; Die Einstellung der Rahmenbox stellt sicher, dass die Polsterung auf 100% beschränkt ist: Die Flexbox wurde verwendet, um die Elemente auszurichten. Ich habe Tabelleneigenschaften entfernt, da Tabellen für nichts anderes als tabellarische Daten verwendet werden sollten, speziell NICHT für Styling und Positionierung –