HTML CSS里面为啥不能用 *margin:0; padding:0;?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HTML CSS里面为啥不能用 *margin:0; padding:0;?相关的知识,希望对你有一定的参考价值。

星号代表 全部 就是全部样式的 外边距 和 内边距都为0; 意思就是写CSS之前把所有浏览器的默认样式统一了
然后在写CSS样式就OK了 这些样式叫重置文件
html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, font, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td border: 0 none; font-family: inherit; font-size: 100%; font-style: inherit; font-weight: inherit; margin: 0; outline: 0 none; padding: 0;
:focus outline: 0 none;
table border-collapse: collapse; border-spacing: 0;
caption, th, td font-weight: normal; text-align: left;
a img, iframe border: medium none;
ol, ul list-style: none outside none;
input, textarea, select, button font-family: inherit; font-size: 100%;
body font-family: "宋体"; font-size: 12px;
参考技术A

这里说明一下:*(星号)代表通配符表示了所有的标签。这样的写法是可以的,但是会造成所有标签的外部边距和内部边距都变0了。例如<h1>标签显示在页面上就变得没有间距了。所以一般是不建议这样写。你可以指定一些标签来清除它们的边距:

div,ul,limargin:0px;padding:0px

参考技术B 你要明白这个样式的意义,不是不能用,只是会有兼容性问题,不建议,现在主流的都是这样写:
body, div, dl, dt, dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,textarea,p,blockquote,th,td,select,img margin: 0; padding:0; /* [disabled]padding:0; */本回答被提问者采纳
参考技术C *这个通配符在网页加载时会比较慢。所以一般不推荐或者是禁止使用的。 参考技术D 不是不能用,是避免用。
* 把所有元素都选择了一次,效率慢(不过那点时间一般也感觉不出来)

为啥HTML中table内部的内外边距不可用?

为什么table内部的内外边距不可用?

table标签,默认的display方法是:table
tr标签,默认的display方法是:table-row
td标签,默认的display方法是:table-cell

padding可以用于td标签(margin不能用),但是margin、padding都不能用于tr标签
除非你更改其display为block~(会破坏表格)
参考技术A table的内边距是使用属性cellpadding,如下:
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<table cellpadding="10" border="1" style="border:1px solid red;border-collapse:collapse;">
<tr>
<td>111</td>
<td>222</td>
</tr>
<tr>
<td>333</td>
<td>444</td>
</tr>
</table>
</body>
</html>
参考技术B 可以在td里添加一个div,设置div的边距。

以上是关于HTML CSS里面为啥不能用 *margin:0; padding:0;?的主要内容,如果未能解决你的问题,请参考以下文章

css已设置width100% 为啥宽度没有占满浏览器?

为啥 CSS 中的“margin: 0 auto”+“display:block”命令使元素居中? [复制]

在网页制作中margin是啥意思,为啥后面还要跟个0?

ie8下面margin:0 auto;不能居中的解决方案,ie8兼容性代码

在css中,为啥h3在div中不位于顶端显示

为啥VSCode里面HTML没有CSS和JS提示了