Diagonally Positioning HTML Elements

I am new to Javascript and relatively new to HTML. I would like to know which language (pure html or Javascript and html) that I should use, and some algorithm suggestions to do the following:

Place 4 squares on the background, but place them diagonally. Each square is a link that you can click to go to another page. So, how could I arrange the html elements diagonally, as shown in the image below?

http://i54.tinypic.com/2v7uw5u.png

I believe my code would look like this:

<script>
    function positionIt()
    {
        var screenW = // javascript function to get screen width??;
        var screenH = // javascript function to get screen height??;

        var sq1 = document.getElementById( "square1" );
        var sq2 = document.getElementById( "square2" );
        var sq3 = document.getElementById( "square3" );
        var sq4 = document.getElementById( "square4" );

        // What javascript functions set a elements x,y positions?
        // Should I use another way of positioning the square (not by absolute terms)
        sq1.setXPos( screenW / 4 );
        sq1.setYPos( screenH / 4 );

        sq2.setXPos( screenW / 3 );
        sq2.setYPos( screenH / 3 );
    }
</script>

// OR if I use css
.menu { background-color: blue; }
/* Position the square in absolute terms diagonally */
#square1 { x=200; y=200; }
#square2 { x=300; y=300; }
#square3 { x=400; y=400; }
#square4 { x=500; y=500; }

<div class="menu" onload="positionIt()">
    <a id="square1" href="home.html"><img src="square.jpg" /></a>
    <a id="square2" href="home.html"><img src="square.jpg" /></a>
    <a id="square3" href="home.html"><img src="square.jpg" /></a>
    <a id="square4" href="home.html"><img src="square.jpg" /></a>
</div>
+3
source share
2 answers

Live Demo
Live Demo ( left )

, .

HTML:

<div class="menu">
    <a id="square1" href="home.html"><img src="http://dummyimage.com/100/f90/fff" /></a>
    <a id="square2" href="home.html"><img src="http://dummyimage.com/100/f90/fff" /></a>
    <a id="square3" href="home.html"><img src="http://dummyimage.com/100/f90/fff" /></a>
    <a id="square4" href="home.html"><img src="http://dummyimage.com/100/f90/fff" /></a>
</div>

CSS

.menu {
    background: blue;
    width: 800px;
    height: 800px;
    position: relative
}
/* Position the square in absolute terms diagonally */
#square1 { position:absolute; left:200px; top:200px; }
#square2 { position:absolute; left:300px; top:300px; }
#square3 { position:absolute; left:400px; top:400px; }
#square4 { position:absolute; left:500px; top:500px; }

? .: http://css-tricks.com/absolute-positioning-inside-relative-positioning/

+1

.

CSS:

#menu {
    position: relative;
    width: /*enough width for all of the positioned elements*/
    height: /*enough height for all of the position elements*/
}
#squareN {
    top: /*how far from top of #menu*/
    left: /*how far from left of #menu*/
    position: absolute;
    width: /*as required*/
    height: /*as required*/
}
+4

Source: https://habr.com/ru/post/1795123/


All Articles