• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Why aren't my buttons aligned?

embers_02

New Coder
please help!
HTML:
<html>
    <head>
        <title>Incremental Adventure</title>
        <style>
            }
            .center {
                text-align: center;
            }
            body {
                font-family: monospace;
                background-color: lightgray;
                text-align:center;
            }
            button {
                font-family:monospace;
                background-color:lightgray;
                border-color:black;
                margin: 5px;
                border-radius:5px;
                padding: 5px;
            }
            
        </style>
    </head>
    
    <body>
        <div class="scoreContainer">
            <p><span id="wood">0</span> wood<br></p>
            <p><span id="woodpersecond">0</span> per second
        </div>
        <button onclick="addToWood(clickingPower)">Gather -  Wood</button><br>
        <button onclick="buyFarm()">Buy lumberjack [<span id="farmcost">150</span>]  <span id="farms">0</span></button>
        <button onclick="buyLumberjack()">Buy Forest [<span id="lumberjackcost">1000</span>] -- <span id="lumberjacks">0</span></button>
        <br>
        
        <button>Buy [insert something here</button>
        <button>Buy [insert something else here]</button>
        
        <script>
            var wood = 0;
            var clickingPower = 10;
            var farmCost = 150;
            var farms = 0;
            var lumberjackCost = 1000;
            var lumberjacks = 0;
                
                function buyFarm() {
                    if (wood >= farmCost) {
                    wood = wood - farmCost;
                    farms = farms + 1;
                    farmCost = Math.round(farmCost * 1.15);
            
                    document.getElementById("wood").innerHTML = wood;
                    document.getElementById("farmcost").innerHTML = farmCost;
                    document.getElementById("farms").innerHTML = farms;
                    updateWoodPerSecond();
                    }
        }
        
                function buyLumberjack() {
                    if (wood >= lumberjackCost) {
                    wood = wood - lumberjackCost;
                    lumberjacks = lumberjacks + 1;
                    lumberjackCost = Math.round(lumberjackCost * 1.15);
            
                    document.getElementById("wood").innerHTML = wood;
                    document.getElementById("lumberjackcost").innerHTML = lumberjackCost;
                    document.getElementById("lumberjacks").innerHTML = lumberjacks;
                    updateWoodPerSecond();
                    }
        }
        
                function addToWood(amount) {
                    wood = wood + amount;
                    document.getElementById("wood").innerHTML = wood;
                }
                
                function updateWoodPerSecond() {
                    woodPerSecond = farms + lumberjacks * 10;
                    document.getElementById("woodpersecond").innerHTML = woodPerSecond;
                }
                
                setInterval(function() {
                    wood = wood + farms;
                    wood = wood + lumberjacks * 10;
                    document.getElementById("wood").innerHTML = wood;
                }, 1000);
        </script>
    </body>
</html>
 

embers_02

New Coder
I fixed the problem by using DIV sections.
Code:
<html>
    <head>
        <title>Incremental Adventure</title>
        <style>
            .sectionLeft {
                text-align:right;
                width:50%;
                float:left;
            }
            .sectionRight {
                text-align:left;
                width:50%;
                float:right;
            }
            .center {
                text-align: center;
            }
            body {
                font-family: monospace;
                background-color: white;
                text-align:center;
            }
            button {
                font-family:monospace;
                background-color:lightgray;
                border-color:black;
                margin: 5px;
                border-radius:5px;
                padding: 5px;
            }
            
        </style>
    </head>
    
    <body>
        <div class="scoreContainer">
            <p><span id="wood">0</span> wood<br></p>
            <p><span id="woodpersecond">0</span> per second
        </div>
        <button onclick="addToWood(clickingPower)">Gather -  Wood</button><br>
        <div class="sectionLeft">
        <button onclick="buyFarm()">Buy lumberjack [<span id="farmcost">150</span>]  <span id="farms">0</span></button>
        <br>
        <button>Buy forest [13000] -- 0</button>
        </div>
        <div class="sectionRight">
        <button onclick="buyLumberjack()">Buy farm [<span id="lumberjackcost">1000</span>] -- <span id="lumberjacks">0</span></button>
        <br>
        <button>Buy Tiaga [17000] -- 0</button>
        </div>
        <script>
            var wood = 0;
            var clickingPower = 10;
            var farmCost = 150;
            var farms = 0;
            var lumberjackCost = 1000;
            var lumberjacks = 0;
                
                function buyFarm() {
                    if (wood >= farmCost) {
                    wood = wood - farmCost;
                    farms = farms + 1;
                    farmCost = Math.round(farmCost * 1.15);
            
                    document.getElementById("wood").innerHTML = wood;
                    document.getElementById("farmcost").innerHTML = farmCost;
                    document.getElementById("farms").innerHTML = farms;
                    updateWoodPerSecond();
                    }
        }
        
                function buyLumberjack() {
                    if (wood >= lumberjackCost) {
                    wood = wood - lumberjackCost;
                    lumberjacks = lumberjacks + 1;
                    lumberjackCost = Math.round(lumberjackCost * 1.15);
            
                    document.getElementById("wood").innerHTML = wood;
                    document.getElementById("lumberjackcost").innerHTML = lumberjackCost;
                    document.getElementById("lumberjacks").innerHTML = lumberjacks;
                    updateWoodPerSecond();
                    }
        }
        
                function addToWood(amount) {
                    wood = wood + amount;
                    document.getElementById("wood").innerHTML = wood;
                }
                
                function updateWoodPerSecond() {
                    woodPerSecond = farms + lumberjacks * 10;
                    document.getElementById("woodpersecond").innerHTML = woodPerSecond;
                }
                
                setInterval(function() {
                    wood = wood + farms;
                    wood = wood + lumberjacks * 10;
                    document.getElementById("wood").innerHTML = wood;
                }, 1000);
        </script>
    </body>
</html>
 

Johna

New Coder
Unfortunately the lengths of the buttons are still off from one another. Can you fix this?
Make the buttons the same length by adding width: 270px;(or any other length) to the button style:
CSS:
.center {
                text-align: center;
            }
            body {
                font-family: monospace;
                background-color: lightgray;
                text-align:center;
            }
            button {
                font-family:monospace;
                background-color:lightgray;
                border-color:black;
                margin: 5px;
                border-radius:5px;
                padding: 5px;
                width: 270px;
            }

That way you wouldn't even have to use DIV elements
 

Top