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 data manipulation: Determining percentage change within a nested JSON structure

Provided is a JSON file structured like this... data =[ { key: "london", values: [ {day: "2020-01-01", city: "london", value: 10}, {day: "2020-01-02", city: "london", value: 20}, {day: " ...

The content contained within the .each loop within the click event is only executed a single time

While working on coding a menu opening animation, I encountered an issue today. Upon clicking the menu button, the menu opens and the elements inside receive an added class (resulting in a fade-in effect). Clicking the menu button again should close the ...

"Items within mui Grid do not properly align within the Grid container

I'm struggling to fit two large Grid items inside a Grid container. The Grid container needs to be 100% of the screen's height (or parent container) Grid items are large and need to fill the container while remaining scrollable Image #1 shows t ...

What is the best way to divide my value sets using jQuery?

Within a given string such as 28_34/42/34,23_21/67/12,63_5/6/56, I am seeking to split or eliminate sets based on the ID provided. For example, if the ID is 23, then the set 23_21/67/12 should be removed. To achieve this, I am checking the value after the ...

Virtual machines have encountered issues when attempting to utilize setTimeout within the browser with vm.runInNewContext

When running a JS script using the vm module in a browser, the following details are included: vm.runInNewContext(codeToEval, sandboxObject); Although interval methods like setTimeout and setInterval do not work, even when exposed in the sandboxObject cr ...

How can we implement a select-all and deselect-all feature in Vue Element UI for a multi-select with filtering capability?

As a newcomer to VueJs, I am exploring how to create a component that enables multiple selection with a search option and the ability to select all or deselect all options. To achieve this functionality, I am utilizing the vue-element-ui library. You can ...

What is the most effective way to send URL strings to the server-side click handler in a jQuery loop using $.each method?

As I work with URL values in my client-side script, I aim to generate links that can transmit these URL values back to my server-side click handler upon clicking. The server-side code /logclick logs the timestamp and destination of the click for auditing p ...

FadeOut Television static on Canvas after a period of inactivity

Is there a way to smoothly fade out the TV noise after a specific timeout period? I found this code snippet on Stack Overflow that seems to address a similar issue: var canvas = document.getElementById('canvas'), ctx = canvas.getContext( ...

Extract the String data from a JSON file

valeurs_d = ""; for (var i = 0; i < keys.length -1 ; i++) valeurs_d += + event[keys[i]] + ", "; var str5 = ","; var str6 = str5.concat(valeurs_d); var valeurs = str6.sub ...

How to Adjust Bootstrap Interval Dynamically

Here's a neat trick to set the interval of your bootstrap carousel: $('.carousel').carousel({ interval: 4000 }); But what if you want each slide to have a different interval? I decided to add data-interval attributes to each slide lik ...

Extracting a subclass from an array

Currently, I am delving into the world of coding as part of a project I'm working on. Here is an excerpt from my JavaScript code that interacts with Google Maps API: for (i = 0; i < results.length; i++) { console.log("Formatted Address: "+ re ...

Retrieving Table Characteristics from an Online Source

Currently utilizing Python 3.4, Windows 10, and Visual Studio 2015 for my project. I am in the process of developing a program that scrapes phone numbers from websites similar to this one. My approach involves using Beautiful Soup 4 to extract the number ...

Discovering elements in jQuery selected within Selenium

I'm completely new to Selenium. The developers on our website utilized jQuery chosen select for filling drop-downs. I am trying to input specific text and then select the matching text that I entered. Here is what I attempted: [FindsBy(How = How.XPa ...

Ways to extend the default hover duration for the <a> title attribute (tooltip)

Is there a method to extend the length of time that a tool tip appears when using the title attribute in an HTML tag? In Internet Explorer, it seems to vanish after only around 5 seconds. Is there a way to make it last longer? ...

Guide on passing a variable from AJAX response to a JavaScript function when a user clicks

I need to send the value of a variable as a parameter to a javascript function when clicking on a link. The value is obtained from an AJAX response. However, I am encountering an error in the console stating that 'test' is not defined. var test ...

Depending on external software packages

Can you explain the potential risks associated with using third-party packages and npm in a broader sense? If I were to install a third-party package like semantic-ui-react using npm, is there a possibility that I may not be able to use it on my website i ...

Troubleshooting problem with Superfish dropdown menu on Internet Explorer 9

I have encountered an issue with the multi-level drop down menu using superfish plugin. It displays correctly in Chrome, Firefox, and Safari, but in IE9 the menu appears vertically instead of horizontally. Do you have any suggestions for resolving this pr ...

Testing a directive that has templates all contained within a single file with script tags

I'm struggling to include my directive's templates in my Karma unit tests. The templates are all in one file within different script tags. The error message I encounter is: PhantomJS 1.9 (Linux) ERROR SyntaxError: Parse error at /var/www/html ...

Change the color of the navbar when scrolling using bootstrap and jquery

Using Bootstrap to design a navigation bar, I have two main goals: I want the navbar to change color when the page is scrolled down by 20%, and then revert back to its original color when scrolling back to the top. When the collapse featu ...

What is the best way to partition JSON data from an API request in React and display it in various sections within the component

There are 2 JSON objects that contain sales period details based on Month to date and year to date. The data includes information such as Units Sold, Gross Revenue, Year to Date Totals, Month to Date Averages, Expenses, Net Revenues, and Per Unit values. I ...