How to change the margin value in jquery

WBOY
Release: 2022-06-09 16:35:01
Original
2444 people have browsed it

In jquery, you can use the css() method to change the value of the element margin. This method is used to return or set the css attribute value of the element. The margin attribute is used to set the css attribute of the element's outer margin. The syntax is " $("Specified element").css("margin","Modified margin value");".

How to change the margin value in jquery

The operating environment of this tutorial: windows10 system, jquery3.4.1 version, Dell G3 computer.

How jquery changes the value of margin

margin clears the surrounding (outer border) element area. The margin has no background color and is completely transparent.

margin You can change the top, bottom, left, and right margins of the element individually, or you can change all attributes at once.

How to change the margin value in jquery

Possible values

  • auto Sets the browser margins.

  • length defines a fixed margin (using pixels, pt, em, etc.)

  • % defines a margin using percentages

css() method sets or returns one or more style properties of the selected element.

To set the specified CSS property, please use the following syntax:

css("propertyname","value");
Copy after login

The example is as follows:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="js/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("p").css("margin","0px");
  });
});
</script>
<style>
p
{
background-color:yellow;
}
p.margin
{
margin:100px 50px;
}
</style>
</head>
<body>
<p>这是一个没有指定边距大小的段落。</p>
<p class="margin">这是一个指定边距大小的段落。</p>
<button>设置 p 元素的margin </button>
</body>
</html>
Copy after login

Output result:

How to change the margin value in jquery

Video tutorial recommendation: jQuery video tutorial

The above is the detailed content of How to change the margin value in jquery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template