Adjust the dimensions of the traffic signal

I'm facing an issue with my code where the traffic light appears too large and the buttons are positioned too far apart. I attempted adjusting the PX values, but this resulted in the traffic light shapes becoming squares instead of circular and smooth rectangles. Any suggestions or tips would be greatly appreciated! Here is what I am experiencing: https://i.sstatic.net/IMDYQ.png

document.getElementById('stopButton').onclick = stopRed;
document.getElementById('slowButton').onclick = slowYellow;
document.getElementById('goButton').onclick = goGreen;
document.getElementById('Lights').onclick = Lights;
document.getElementById('autoLights').onclick = autoLights;

function stopRed() {
  Lights();
  document.getElementById('stopLight').style.backgroundColor = "red";
}

function slowYellow() {
  Lights();
  document.getElementById('slowLight').style.backgroundColor = "orange";
}

function goGreen() {
  Lights();
  document.getElementById('goLight').style.backgroundColor = "green";
}


function Lights() {
  document.getElementById('stopLight').style.backgroundColor = "black";
  document.getElementById('slowLight').style.backgroundColor = "black";
  document.getElementById('goLight').style.backgroundColor = "black";
}


function lightOne(num) {
  Lights();
  switch (num) {
    case 1:
      stopRed();
      break;
    case 2:
      slowYellow();
      break;
    case 3:
      goGreen();
      break;
    default:
      alert("you made some error");
  }
}

counter = 0;
maxSec = 3;

function timer() {
  setTimeout(function() {

    counter++;
    lightOne(counter);
    if (counter == maxSec) {
      return;
    }
    timer();
  }, 2000);
}

function autoLights() {
  counter = 1;
  lightOne(counter);
  timer();
}
body {
  font-family: sans-serif;
}

#controlPanel {
  float: left;
  padding-top: 30px;
}

.button {
  background-color: gray;
  color: white;
  border-radius: 10px;
  padding: 20px;
  text-align: center;
  margin: 90px 40px;
  cursor: pointer;
}

#traffic-light {
  height: 550px;
  width: 200px;
  float: left;
  background-color: #333;
  border-radius: 40px;
  margin: 30px 0;
  padding: 20px;
}

.bulb {
  height: 150px;
  width: 150px;
  background-color: #111;
  border-radius: 50%;
  margin: 25px auto;
  transition: background 500ms;
}
<div id="controlPanel">
  <h1 id="stopButton" class="button">Stop</h1>
  <h1 id="slowButton" class="button">Slow</h1>
  <h1 id="goButton" class="button">Go</h1>
  <h1 id="Lights" class="button">Clear</h1>
  <h1 id="autoLights" class="button">Auto</h1>
</div>

<div id="traffic-light">
  <div id="stopLight" class="bulb"></div>
  <div id="slowLight" class="bulb"></div>
  <div id="goLight" class="bulb"></div>
</div>

Answer №1

If you want to adjust the size of the lights in your traffic light, you can easily do so by changing the height and width attributes of the .bulb and #traffic-light classes.

For example:

#traffic-light {
   height: 400px;
   width: 150px;
   ...
}

.bulb {
   height: 100px;
   width: 100px;
   ....
}

To modify the spacing between the bulbs, you can experiment with the margin property of the .bulb class until you achieve the desired result.

document.getElementById('stopButton').onclick = stopRed;
document.getElementById('slowButton').onclick = slowYellow;
document.getElementById('goButton').onclick = goGreen;
document.getElementById('Lights').onclick = Lights;
document.getElementById('autoLights').onclick = autoLights;

function stopRed() {
  Lights();
  document.getElementById('stopLight').style.backgroundColor = "red";
}

function slowYellow() {
  Lights();
  document.getElementById('slowLight').style.backgroundColor = "orange";
}

function goGreen() {
  Lights();
  document.getElementById('goLight').style.backgroundColor = "green";
}


function Lights() {
  document.getElementById('stopLight').style.backgroundColor = "black";
  document.getElementById('slowLight').style.backgroundColor = "black";
  document.getElementById('goLight').style.backgroundColor = "black";
}


function lightOne(num) {
  Lights();
  switch (num) {
    case 1:
      stopRed();
      break;
    case 2:
      slowYellow();
      break;
    case 3:
      goGreen();
      break;
    default:
      alert("you made some error");
  }
}

counter = 0;
maxSec = 3;

function timer() {
  setTimeout(function() {

    counter++;
    lightOne(counter);
    if (counter == maxSec) {
      return;
    }
    timer();
  }, 2000);
}

function autoLights() {
  counter = 1;
  lightOne(counter);
  timer();
}
body {
  font-family: sans-serif;
}

#controlPanel {
  float: left;
  padding-top: 30px;
}

.button {
  background-color: gray;
  color: white;
  border-radius: 10px;
  padding: 20px;
  text-align: center;
  margin: 90px 40px;
  cursor: pointer;
}

#traffic-light {
  height: 400px;
  width: 150px;
  float: left;
  background-color: #333;
  border-radius: 40px;
  margin: 30px 0;
  padding: 20px;
}

.bulb {
  height: 100px;
  width: 100px;
  background-color: #111;
  border-radius: 50%;
  margin: 25px auto;
  transition: background 500ms;
}
<div id="controlPanel">
  <h1 id="stopButton" class="button">Stop</h1>
  <h1 id="slowButton" class="button">Slow</h1>
  <h1 id="goButton" class="button">Go</h1>
  <h1 id="Lights" class="button">Clear</h1>
  <h1 id="autoLights" class="button">Auto</h1>
</div>

<div id="traffic-light">
  <div id="stopLight" class="bulb"></div>
  <div id="slowLight" class="bulb"></div>
  <div id="goLight" class="bulb"></div>
</div>

Answer №2

To adjust the spacing between the buttons, simply decrease the margin-top and margin-bottom values in the CSS rule for those buttons (found at the end of my code snippet).

If you want to reduce the size of the traffic light itself, remove the height attribute (to allow it to adjust based on content), modify the width, and adjust the width and height of the bulbs accordingly.

While I can't be certain if this is exactly what you're looking for, these adjustments should point you in the right direction.

document.getElementById('stopButton').onclick = stopRed;
document.getElementById('slowButton').onclick = slowYellow;
document.getElementById('goButton').onclick = goGreen;
document.getElementById('Lights').onclick = Lights;
document.getElementById('autoLights').onclick = autoLights;

// JavaScript functions for controlling traffic lights
// ...
body {
  font-family: sans-serif;
}

#controlPanel {
  float: left;
  padding-top: 30px;
}

.button {
  background-color: gray;
  color: white;
  border-radius: 10px;
  padding: 20px;
  text-align: center;
  margin: 90px 40px; /* Adjust these values */
  cursor: pointer;
}

#traffic-light {
  width: 150px; /* Adjust width here */
  float: left;
  background-color: #333;
  border-radius: 40px;
  margin: 30px 0;
  padding: 20px;
}

.bulb {
  height: 100px; /* Adjust height here */
  width: 100px; /* Adjust width here */
  background-color: #111;
  border-radius: 50%;
  margin: 25px auto;
  transition: background 500ms;
}

#controlPanel>h1 {
  margin-top: 15px;
  margin-bottom: 15px;
}
<div id="controlPanel">
  <h1 id="stopButton" class="button">Stop</h1>
  <h1 id="slowButton" class="button">Slow</h1>
  <h1 id="goButton" class="button">Go</h1>
  <h1 id="Lights" class="button">Clear</h1>
  <h1 id="autoLights" class="button">Auto</h1>
</div>

<div id="traffic-light">
  <div id="stopLight" class="bulb"></div>
  <div id="slowLight" class="bulb"></div>
  <div id="goLight" class="bulb"></div>
</div>

Answer №3

Ensure that the height to width ratio of #traffic-light is maintained at 2.75.

For instance:

#traffic-light {
  height: 275px;
  width: 100px;
  float: left;
  background-color: #333;
  border-radius: 20px;
  margin: 30px 0;
  padding: 20px;
}

To make the buttons closer together, decrease the margin spacing between them.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Introducing additional fields with variable field names will result in all field names being identical

I am trying to implement a feature where clicking on the add button adds a new field to the page within a hidden div. // Hidden div containing the field <div class="dependent-row" style="visibility: hidden;"> <div class="row" style="border:1p ...

Importing partial peer dependencies in Npm

I have a custom npm package with a peer dependency on element-ui. This package is importing the Pagination component from element-ui: import {Pagination} from 'element-ui';' However, when I import this component in my project, the entire ...

The functionality of Angular material input fields is compromised when the css backface property is applied

I utilized this specific example to create a captivating 3D flip animation. <div class="scene scene--card"> <div class="card"> <div class="card__face card__face--front">front</div> <div class="card__face card__face--ba ...

Tips for repairing damaged HTML in React employ are:- Identify the issues

I've encountered a situation where I have HTML stored as a string. After subsetting the code, I end up with something like this: <div>loremlalal..<p>dsdM</p> - that's all How can I efficiently parse this HTML to get the correct ...

Learn the process of effortlessly transferring user information to a MongoDB database

Using socket.io, I am broadcasting geolocation data (lat, lng) and updating the marker icon on a map every time a user connects to the app. When a user from Japan connects, their position is shared with me and vice versa. The issue arises when I only want ...

Dropdown for state selection within WooCommerce for specific countries

I am encountering a challenge in configuring a form with default WooCommerce country and states selection dropdowns. Essentially, my goal is to present the Country selection first, followed by the State selection based on the chosen country. For instance, ...

Sending MVC3 model information as JSON to a JavaScript block

My challenge is to correctly pass my MVC3 model into a script block on the client side. This is the approach I'm taking in my Razor view: <script type="text/javascript"> var items = @( Json.Encode(Model) ); </script> The "Model" in t ...

Transform input data from JSON format to FormURL Encoded data by utilizing the transformRequest method

I've been attempting to convert JSON data into formURL encoded data, but I'm encountering issues. Here's my HTTP post code snippet: $http.post(API_ENDPOINT.login, credentials, { transformRequest: transformRequestAsFormPost }) This is ...

Update destination upon click

I'm new to JavaScript and attempting to modify a redirect link using checkboxes that will modify the URL, followed by a button that will use the updated URL. However, I'm facing some challenges with my code and could use some guidance. Here is t ...

Leveraging JavaScript classes within Angular2

Incorporating a native JavaScript library into my Angular 2 project has been my latest endeavor. After building and bundling the JS file, I found myself in need of just one specific class called 'fhir' from this bundle. My approach involved creat ...

What's the best way to display the spinner while making an axios request?

When I put the spinner on a component did mount method with a setTimeOut method inside, it shows. However, I want the spinner to display only when there is no content on the page. After the axios request finishes loading, the page content should be shown i ...

Should I consolidate all my .js files into one file - worth considering?

On my site Pure Words, I currently have multiple scripts. Should I consolidate them into a single file or maintain them as separate entities? ...

Concealing elements such as buttons, icons, and text dynamically with AngularJS depending on screen resolution

I have mastered responsive html using Bootstrap, but now I need to dynamically remove elements based on screen resolution. Let's compare browser view versus mobile view: https://i.sstatic.net/q53aa.jpg Depending on the resolution, certain buttons s ...

Utilize the float left and float right properties within a div container with a width of 100% to ensure compatibility

I need to align two divs within a container-div in IE7. The first div should float to the left, and the second to the right. Currently, both divs are floating to the left according to my CSS configuration. Here is the CSS code I'm using: .container ...

Using ng-options in Angular to make four distinct selections: a guide

JS: angular .module('app', []) function MainCtrl() { var ctrl = this; ctrl.selectionList = [ { id: 1, name: 'apple'}, { id: 2, name: 'banana'}, { id: 3, name: 'grapes'}, { ...

The text/font-weight of the header button is mysteriously shifting without warning

While creating a header for my webpage, I encountered an issue where the text family was changing when the dropdown menu was launched in Safari 8. Curiously, this behavior did not occur when using Chrome to launch the jQuery function. Even after attempting ...

What exactly comprises an HTTP Parameter Pollution attack within the context of Node.js and Express.js?

I came across this information on https://www.npmjs.com/package/hpp According to the source, "Express populates http request parameters with the same name in an array. An attacker can manipulate request parameters to exploit this vulnerability." I am cur ...

Instructions for showing a particular image on a different page upon clicking a button with JavaScript

I have created a fictional ecommerce website as a personal project. The home page features image links to various shoe products, each with its own product page containing an image, name, and placeholder description. When users click the "Buy Now" button on ...

Continuously making calls in React using the fetch method

Having an issue with fetching data from a JSON file, as I noticed in the network tab that the call is being made continuously. How can I resolve this problem? Check out the Network Tab Image for reference. class Banner extends Component { constructo ...

Is it possible to load textures in Three.js and Cordova without relying on a server or HTTP?

I am currently working on developing a Cordova game using Three.js with the goal of making it playable offline. However, I have encountered an issue where Three.js requires texture files to be served via HTTP, making it challenging to achieve offline funct ...