What is the best way to incorporate CSS into JavaScript code?

Here is the code I am working with:

<script type = "text/javascript">
        function pic1()
        {
            document.getElementById("img").src = "../images/images/1.png";
        }
        function pic2()
        {
            document.getElementById("img").src = "../images/images/2.png";
        }
</script>

This is my html layout:

<span class="background" style="width: 950px; height: 642px; top: 0px; left: 0px">
</span>
<span class="car">
<img src = "../images/images/1.png" id = "img" /> 
</span>
<span class="car2">
<img src = "../images/thumbs/3.png"<input type="button" value="show"onclick="pic2()" />
</span>

I am trying to position the images and thumbs using CSS. The main image should be in the middle of the page, while the thumbnail should be at the bottom.

.car
{
    position: absolute;
    cursor: pointer;
    display: block;
    overflow:hidden;
    margin:0;
    padding: 0;
    width: 638px; 
    height: 274px; 
    top: 200px; 
    left: 150px;
}
.car-colour
{
    position: absolute;
    cursor: pointer;
    display: block;
    overflow:hidden;
    margin:0;
    padding: 0;
    width: 70px; 
    height: 70px; 
    top: 500px; 
    left: 80px;
}

Issue Resolved!

.car
{
    position: absolute;
    cursor: pointer;
    display: block;
    overflow:hidden;
    margin:0;
    padding: 0;
}

<span class="car" style="width: 638px; height: 274px; top: 200px; left: 150px;">
<img src = "../images/images/1.png" id = "img" />
</span>

Answer №1

It is recommended to use a separate CSS file for better organization.

Example of Style.css :

.car
{
    position: absolute;
    cursor: pointer;
    display: block;
    overflow:hidden;
    margin: 0;
    padding: 0;
}

#img
{
    width: 638px;
    height: 274px; 
    top: 200px; 
    left: 150px;
}

Example of Page.html :

<head>
...
<link type="text/css" rel="stylesheet " href="style.css">
...
</head>
<body>
...
<span class="car">
<img src = "../images/images/1.png" id = "img" />
</span>
...
</body>

Answer №2

Span is an inline element within a block level element, and images have their own width and height properties which makes applying borders over the image difficult.

Feel free to experiment with the following URL: [Sample URL][1]

[1]: http://jsfiddle.net/uniqueusername/sampleURL123/

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

JavaScript failing to load following PHP header() redirect

I've set up a page that allows users to sign in by filling out a basic form, which then sends the data to a separate PHP script for validation. After the validation process is complete, the PHP script uses the header() function to redirect the user to ...

"Dynamically generated websites, backend processing, React framework Nextjs, Content Management System WordPress

I'm interested in creating a primarily static site using Next.js, but I also need the ability to provide customers with price estimates based on their specifications. The calculation process needs to be kept private and not exposed to anyone else (oth ...

Vue JS - Troubleshooting Checkbox Validation Error During Form Submission

When a user fills out my registration form, there is a checkbox to confirm acceptance of the terms and conditions. Currently, the validation error for this checkbox appears immediately upon hitting submit, even though the checkbox starts as unchecked. The ...

Adding numbers to a textbox using a JavaScript algorithm

There are two textboxes in this scenario. The first box should always contain 4 digits when you leave it, while the second box should always contain 10 digits. A javascript function needs to be implemented so that when a user leaves one of the textboxes, ...

Converting HTML to plain text - unclear source encoding

I am currently working on a project involving PHP where I extract HTML content from websites, convert them into plain text, and store them in a database. The challenge I am facing is that I do not know the original encoding of the content. What would be t ...

Display only one dropdown menu at a time

Hey there, I'm currently working on a dropdown menu and struggling to figure out how to keep only one item open at a time. I've tried using an array with useState for all my dropdowns but haven't been able to find a solution yet: code co ...

Can you tell me the appropriate type for handling file input events?

Using Vue, I have a simple file input with a change listener that triggers the function below <script setup lang="ts"> function handleSelectedFiles(event: Event) { const fileInputElement = event.target as HTMLInputElement; if (!fileInp ...

Incapable of choosing every radio button within the initial three rows of a table

In my table, each row contains approximately 11 radio buttons and a few other attributes. I am attempting to make the radio buttons in the first three rows non-editable using the provided code. However, it seems that the code is not functioning as intend ...

Utilizing NGRX reducers with a common state object

Looking for a solution with two reducers: export function reducer1(state: State = initialState,: Actions1.Actions1); export function reducer2(state: State = initialState,: Actions2.Actions1); What I want is for both reducers to affect the same state objec ...

Implement a contact form using backend functionality in a ReactJS application

Currently, I am in the process of developing my first website using reactjs. My focus right now is on completing the contact form page, and I have already spent 2 days on it. To handle email functionality, I am utilizing nodemailer with a Gmail account tha ...

Sending data from express js to a different server

Currently, I am faced with a scenario where I need to send a file from Jquery to my Express server and then transfer that request to another server without parsing the file in the Express server. Below are the code snippets I have been using so far: Jquery ...

Converting the Google logo to grayscale for the Google Translate button

Is there a simple way for a non-coder like myself to change the colors of the Google Logo in the Google Translate Button to grey scale instead of its usual bright shades? Below is the embed code I am using: <div id="google_translate_element"></di ...

Utilize JavaScript in conjunction with encfs for enhanced encryption capabilities

I am attempting to access an encfs filesystem using JavaScript, but am struggling to understand the correct approach. I am utilizing the CryptoJS library for this task. The .ecnfs6.xml file contains standard settings and uses the password "123456": < ...

Invoking a function passed via props that utilizes react-router's features

I'm really struggling to grasp this problem. Is there anyone here who could help me out? I have a component where I pass a method called this.fetchContent as props named Filter. This method triggers an action creator that uses axios with Redux to fetc ...

What is the method to define the maximum width for a BootstrapTable while also trimming any data that exceeds this specified width?

In my ASP.NET Web Forms project, I am utilizing Bootstrap 5.2 for a BootstrapTable, which is currently defined as follows: <table class="display table table-bordered table-striped w-100 tables" ...

Utilizing a JavaScript Library in your Scala.js Project: A Step-by-Step Guide

I am currently following a tutorial on setting up dependencies in my Scala.js project. Here is the link to the tutorial: First and foremost, I have organized my project setup as shown below: https://github.com/scala-js/scalajs-cross-compile-example Wi ...

Managing media file transfers using multer and mongodb in a Node.js environment

As I work on developing a blog-style application for a business website, I have successfully implemented a login system and a basic blog structure. My current focus is on enabling users to upload images along with their blog posts. At the moment, I am trou ...

Transforming a string in AngularJS to a "controller as" approach using $parse

How can I modify foo.callbacke to reference the timerController.callbacke method? <div ng-app="timerApp" ng-controller="timerController as foo"> <div ng-repeat="item in [1,2,3,4]"> <div watcher="{'seconds': 'foo.callbacke ...

Javascript - Issue with Ajax causing additional commas in JSON responses

I'm attempting to send a request to a RESTful server using the HTTP module in Node.js. Due to the large response size (64 chunks, approximately 100kb), the HTTP module combines the chunks into a single string response like this: res.setEncoding(& ...

Which CSS properties can I implement to ensure that the text remains legible regardless of the background behind it?

Looking ahead, I am displaying an issue where the colors are almost perfect, but when different percentages are applied, some or all of the text becomes obscured. My goal is to assign the font color based on the background difference. I vaguely remember s ...