Designing an interactive region using HTML, CSS, and JavaScript

I recently created this code with some assistance:

When I tried to format the code, it all ended up on one line and looked unreadable. To view the code properly, please visit this link:

http://jsfiddle.net/QFF4x/

<div style="border:1px solid black;" onmouseover="change(this)" onmouseout="restore(this)" ></div>

When the mouse hovers over the black border, it expands downwards. But how can I enlarge the area of the onmouseover effect?

For instance, if the mouse is hovering within 15 pixels below the line, I want the line to expand. How can I achieve that functionality?

*Important: I am restricted to using HTML only and inline coding. I cannot link any JavaScript or CSS files.

Answer №1

Recently, I've received feedback regarding issues with "external files," although this is not relevant. To address this, I will update by stating that everything can actually be contained within a single document: Check out the live demo here (click).

<style>
#outer {
  padding-bottom: 15px;
}

#inner {
  border: 1px solid black;
}

#inner.big {
  border-width: 3px;
}
</style>

<div id="outer">
  <div id="inner"></div>
</div>

<script>

var outer = document.getElementById('outer');

var inner = document.getElementById('inner');

outer.addEventListener('mouseenter', function() {
  inner.className = 'big';
});

outer.addEventListener('mouseleave', function() {
  inner.className = '';
});

</script>

You can achieve the desired effect without JavaScript. Here's an example using only CSS: View the live demo here (click).

  <div class="outer">
    <div class="inner"></div>
  </div>

CSS:

.outer {
  padding-bottom: 15px;
}

.inner {
  border: 1px solid black;
}

.outer:hover > .inner {
  border-width: 3px;
}

In place of JavaScript, I opted for CSS :hover to create the hover effect. By wrapping the element in another container and adjusting padding, you can trigger the effect on the inner element when hovering over the outer container.

If you prefer using JavaScript, it's best to avoid inline js (where JavaScript functions are directly embedded within HTML). Below is a simple example utilizing JavaScript while maintaining style rules in CSS: Experience the live demo here (click).

  <div id="outer">
    <div id="inner"></div>
  </div>

CSS:

#outer {
  padding-bottom: 15px;
}

#inner {
  border: 1px solid black;
}

#inner.big {
  border-width: 3px;
}

JavaScript:

var outer = document.getElementById('outer');

var inner = document.getElementById('inner');

outer.addEventListener('mouseenter', function() {
  inner.className = 'big';
});

outer.addEventListener('mouseleave', function() {
  inner.className = '';
});

To understand why avoiding inline js is recommended, explore these search results for more information: https://www.google.com/search?q=Why+is+inline+js+bad%3F

Answer №2

Check out this unique solution that involves using a wrapping div with padding of -15px. When you hover over the div, it will target the first element child.

<script>
    function change(element) {
        element.firstElementChild.style.border = "3px solid black";
    }
    function restore(element) {
        element.firstElementChild.style.border = "1px solid black";
    }
</script>

<div style="padding-bottom:15px;" onmouseout="restore(this)" onmouseover="change(this)" >

    <div style="border:1px solid black;"  >

    </div>

</div>

This allows you to hover 15px under the border and see the changes in borders.

Here's the fiddle link for reference: http://jsfiddle.net/QFF4x/2/

Answer №3

If you want to enhance the border when hovering, adjust the following:

element.style.border = "3px solid black";

To increase it to 15px, use:

element.style.border = "15px solid black";

Answer №4

To contain the div element within a larger div, you can attach the onclick event to the larger parent div.

<div onmouseover="change(this.childNodes[0])" onmouseout="restore(this.childNodes[0])">
    <div style="border:1px solid black;" ></div>
</div>

Answer №5

this is the answer.

<div style="border:1px solid black;" onmouseover="javascript:this.style.border = '8px solid black';" onmouseout="javascript:this.style.border = '1px solid black';" ></div>

http://jsfiddle.net/5GAUq/

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

Utilizing distinct useState for mapped elements

I am struggling to find a solution on how to pass specific useState states to respective mapped elements. export const Polska = () => { const [riverVisible, setRiverVisible] = useState(false) const [mountainVisible, setMountainVisible] = useState(fa ...

Transforming varied JavaScript objects into a serial form

In my application, there is a concept of an interface along with multiple objects that implement this interface in various ways. These objects are created using different factory methods, with the potential for more factories to be added as the application ...

Using setInterval to update the content of a Text Area continuously

I am currently working on a script that involves extracting a string from a textarea, breaking it down into an array using the delimiter "=====\n", and then displaying each element of the array in the textarea every 250ms. However, I have noticed that ...

JavaScript function not executing

Within a panel in an UpdatePanel, there is both a dropdown list and a file upload control. The goal is to enable the Upload button when a value is selected from the dropdown and a file is uploaded. This functionality is achieved through a JavaScript functi ...

Unraveling the Mystery of Passing Props in React.js

Currently taking an online course to learn React, I encountered a unique scenario where one property is attached to another property in this manner: this.props.property01(this.props.property02) The tutor briefly touched on this code line, leaving me quit ...

Reactjs implemented with Material UI and redux form framework, featuring a password toggle functionality without relying on hooks

Currently, I am working on a react project where I have developed a form framework that wraps Material-UI around Redux Form. If you want to check out the sandbox for this project, you can find it here: https://codesandbox.io/s/romantic-pasteur-nmw92 For ...

Styling text on a HTML webpage

Is there a way to center text in the caption and add a link on the far-right of the caption? Let me know if this is possible. The format for the caption should be: Centered Text Right-justified link ...

Setting a uniform width for all columns in a table using ReactJS

When working with variable amounts of data displayed as columns in a table, I struggled to maintain fixed widths for each column. Despite my efforts, the columns would stretch based on available space rather than obeying the specified widths. For example, ...

Uncover the valuable information within a string using regex in JavaScript

I am looking for a solution to extract key values from a string that looks like this: <!-- Name:Peter Smith --><!-- Age:23 --> My goal is to create a standard function that can extract any value needed. The function call would be something like: var ...

eliminate the common elements between two arrays in typescript/javascript

I have two lists of objects, each containing two fields: let users1 = [{ name: 'barney', uuid: 'uuid3'}, { name: 'barney', uuid: 'uuid1'}, { name: 'barney', uuid: 'uuid2 ...

Transfer information from a Json file to the Parse.com backend using the Rest API

Good evening everyone, I'm facing a challenge in loading data from a .json file into the Parse.com backend. While I successfully managed to manually add json data using the terminal on my Mac by following the example provided by Parse.com: curl -X P ...

Learn how to dynamically import external modules or plugins using the import() functionality of TypeScript 2.4 within the production script generated by Angular CLI

Utilizing the typescript 2.4 [import()][1] feature has proven to be effective for dynamically loading modules. Testing this functionality has shown positive results, especially when importing modules and components located within the same directory. Howev ...

How can I determine the width of a Bootstrap Column on a smartphone?

Currently, I am utilizing Photoshop to design a sequence of Wireframes for a forthcoming eCommerce website. I have a solid grasp on Bootstrap functioning based on a '12 column' structure, where the width of each column varies depending on the vi ...

Invoke a function within an HTML element inserted using the html() method

Looking for help with a JavaScript function: function toggle_concessions(concessions) { var text = "<table>"+ "<tr><td class='concession-name'>gfhgfbfghfd</td><td class='op-encours&a ...

Reversing the Jquery checkbox functionality

Seeking some quick assistance here. I'm puzzled as to why this function seems to be working in reverse. Whenever I try to check the checkboxes, they all get unchecked and vice versa. Everything was functioning smoothly until I introduced the .click() ...

The connections of directives

In my Angular application, I am encountering an issue while trying to enhance the functionality of a third-party directive with my own custom directive. The problem lies in the order of instantiation of these directives. The intended usage of the directiv ...

The element does not recognize the property 'width' since it is not defined in the type of 'GlobalEventHandlers'

I'm trying to determine the size of an image using JavaScript, but I encountered a TypeScript error: const img = new Image(); img.onload = function() { alert(this.width + 'x' + this.height); } img.src = 'http://www.google.com/intl/en_ ...

Ways to substitute numerous instances of a string in javascript

I have experience in developing websites using reactjs. I usually implement restAPI's with java and work with liferay CMS. In one of my projects, I created a shortcode for accordion functionality like this: ('[accordion][acc-header]Heading 1[/ac ...

Exploring Angular 4: Embracing the Power of Observables

I am currently working on a project that involves loading and selecting clients (not users, but more like customers). However, I have encountered an issue where I am unable to subscribe to the Observables being loaded in my component. Despite trying vario ...

ajax response includes both a single variable as well as an array variable

My comment form requests newtimestamp and newcomment variables. The newtimestamp is a single variable, while the newcomment is an array generated from a foreach loop. A snippet from ajaxrequest.php: foreach ($array2 as $print2) { $var1 = $print2['va ...