css代码body>*是什么意思

今天学习前端的时候,发现body>*这样一个选择器,详细代码如下。我大概知道它的作用,但是不太确定,想从网上搜个现成的答案,百度了一下居然没找到,只好按照自己的理解写一下。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>子代选择器</title>
    <style type="text/css">
        body>*{ width:980px; height:auto; margin:0 auto; margin-top:10px;
            border:1px solid #000; padding:5px;}
        header{ height:50px;}
        section{ height:300px;}
        footer{ height:30px;}
        section>*{ height:100%; border:1px solid #000; float:left;}
        aside{ width:250px;}
        article{ width:700px; margin-left:10px;}
    </style>
</head>
<body>
<header>头部</header>
<nav>导航</nav>
<section>
    <aside>侧边栏</aside>
    <article>文章</article>
</section>
<footer>页脚</footer>
</body>
</html>

""是通配符,包括所有元素。
"body>
"是指选择所有父级元素是 body的子元素,通俗点说就是只包括body的儿子这一代,但不包括孙子辈。在本例中,"body>*"包括的第一级子元素有header、nav、section、footer,不包括aside、article。

">"子代选择器

">"选择器是css3新增的选择器,用于选择元素的第一级子元素。
案例见下面代码:

<style>
div>p   /*指div的第一级子元素p*/
{
	background-color:#ccc;
}
</style>
<div>
<p>I live in Duckburg.</p> <!--背景色是#ccc-->
</div>
<div>
<span><p>I will not be styled.</p></span> 
<!--有span元素,这里p不是div的第一代子元素,所以背景色不是#ccc-->
</div>

关于这个子代选择器,大家可以参照菜鸟教程,网址如下:https://www.runoob.com/cssref/sel-element-gt.html
同样的道理,section>*是指section的所有第一代子元素,本例中包括aside、article两个元素。

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是补全后的代码: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>testD3-3-drawingDivBar</title> <script type="text/javascript" src="resource/d3.v5.min.js"></script> <style type="text/css"> #d3 { width: 150px; height: 200px; background-color: #ccc; display: flex; justify-content: flex-end; } .bar { width: 25px; margin-left: 10px; background-color: blue; } </style> </head> <body> <div id="d3"></div> <script type="text/javascript"> // 选择id为d3的div进行绘图 const svg = d3.select("#d3") .append("svg") .attr("width", "100%") .attr("height", "100%"); // 层数量为5。层间距为10px。层宽度为25px。层高度为175px,效果形状是阶梯状 const data = [175, 150, 125, 100, 75]; svg.selectAll(".bar") .data(data) .enter().append("rect") .attr("class", "bar") .attr("x", (d, i) => i * 35) .attr("y", d => 200 - d) .attr("width", 25) .attr("height", d => d); </script> </body> </html> ``` 解释: 1. 样式中设置 `display: flex;` 和 `justify-content: flex-end;` 是为了让矩形从右往左排列。 2. `x` 属性设置为 `(d, i) => i * 35`,其中 `i` 表示当前数据的索引,也就是第几个矩形,`35` 是一个矩形的宽度和层间距的和。 3. `y` 属性设置为 `d => 200 - d`,其中 `200` 是 `div` 的高度,`d` 是数据数组中的元素值,表示当前矩形的高度。这样设置可以让矩形从下往上排列,并且顶部对齐。 4. `width` 属性设置为 `25`,与样式中的层宽度相同。 5. `height` 属性设置为 `d => d`,与数据中的元素值相同,表示当前矩形的高度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值