gpt4 book ai didi

html - html/css 初学者 : Why do web pages have so many levels of divs? 为什么不在一个类中定义所有属性?

转载 作者:行者123 更新时间:2023-11-28 05:23:43 25 4
gpt4 key购买 nike

在每个网页中,div 中都有 div,div 中有 div,div 中有 div……永无止境的 div 池。他们都有自己的 ID 或类(class)。为什么会这样。为什么不在一个地方定义所有属性?这种技术有什么意义?

    <div id="body-top">
<div id="body-bot">
<div id="about-box">
<h2><b>About</b> our music</h2>
<img src="images/pic_1.jpg" width="112" height="92" alt="Pic 1" class="left" />
<p>Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
<ul>
<li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. </li>
<li>Cras semper erat ut mi. </li>
<li>Proin lobortis ipsum ac erat. </li>
<li>Morbi nec enim vitae est posuere luctus.</li>
</ul>
</div>
<div id="express-box">
<h2><b>Express</b> Learning</h2>
<img src="images/pic_2.jpg" width="112" height="92" alt="Pic 2" class="left" />
<p>Sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>
<div class="narrow-text">
<p><a href="#">It is a long</a> established fact that a reader will be distracted by the readable content of a page when looking at its layout.</p>
<p><a href="#">The point of using</a> Lorem Ipsum is that it has a more-or-less normal distribution of letters...</p>
</div>
<p class="more"><a href="#"><img src="images/more.gif" width="68" height="14" alt="More" /></a></p>
</div>
<div class="clear"></div>
</div>
</div>
</div>
<div id="foot">
<div id="foot-top">
<div id="foot-bot">
<div id="what-box">
<h2><b>What</b> we offer</h2>
<img src="images/pic_3.jpg" width="86" height="77" alt="Pic 3" class="left" />
<ul>
<li><a href="#">Lorem ipsum</a> dolor sit amet, consectetuer adipiscing elit.</li>
<li>Cras semper erat ut mi. </li>
<li>Proin lobortis ipsum ac erat. </li>
<li>Morbi nec enim vitae est posuere luctus.</li>
</ul>
</div>
<div id="news-box">
<h2><b>News</b> &amp; events</h2>
<img src="images/pic_4.jpg" width="86" height="77" alt="Pic 4" class="left" />
<ul>
<li><a href="#">Nullam</a> scelerisque consequat libero. </li>
<li><a href="#">Vivamus</a> mattis augue ut augue.</li>
</ul>
<p class="more clear"><a href="#"><img src="images/more.gif" width="68" height="14" alt="More" /></a></p>
</div>
<div class="clear"></div>
</div>
</div>

最佳答案

让我们打个比方。

假设您正在设计房屋的布局。

您将房屋的总面积划分为更小的部分(div 的第一层)。这些部分是您的不同房间(您给 div 的类/id)。

<body>
<div class="bedroom room"></div>
<div class="bedroom room"></div>
<div class="kitchen room">
</div>
<div class="washroom room"></div>
</body>

然后假设厨房需要一些元素,它们在厨房中有自己的部分/空间(第二级 div)。冰箱、水槽、炉灶等元素(类/ID)。

        <div class="kitchen room">
<div class="kitchenItem" id="fridge"></div>
<div class="kitchenItem" id="stove"></div>
<div class="kitchenItem" id="sink"></div>
</div>

您可以根据自己的喜好对房子每个部分的元素进行详分割解,类似地,这就是使用 div 设计网页的方式,然后为这些 div 指定类和 ID。

现在假设您的所有房间都具有相同类型的颜色,因此使用 CSS,您可以使用以下方法应用这种通用样式:

.room{
//your common styles here
background-color: white;
}

但是假设现在设计发生了变化,您希望将卧室漆成蓝色而不是白色。因此,您使用第二个类 bedroom 来更改所有卧室的 CSS:

.room.bedroom{
background-color: blue;
}

编辑:

人们使用 div 是因为它们是 HTML 中最通用的元素之一。它们基本上没有给出它们包含的内容的上下文。就像在房子里一样,它们只是表示房子里的空间。而其他元素,如 ph1h2 等,则提供了将包含一些文本的上下文。

关于html - html/css 初学者 : Why do web pages have so many levels of divs? 为什么不在一个类中定义所有属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35242337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com