英文:
Moving style property from div element
问题
我有以下具有样式的div:
<div class="icon red" style="flex: 0.8">
如果我想将样式从div属性中移除,这是否是正确的方式:
<div class="iconred">
<style>
div.iconred {
flex: 0.8;
}
</style>
</div>
英文:
I have the following div with style:
<div class="icon red" style="flex: 0.8">
If I want to move the style from the div attribute , is this the correct way:
<div class="iconred">
<style>
div.iconred {
flex: 0.8;
}
</style>
</div>
答案1
得分: 2
<div class="icon red" style="flex: 0.8">
这个div 具有两个不同的类: icon 和 red。
在CSS中,您使用了 div.iconred,表示一个具有单个类 iconred 的元素。
因此,您实际上应该使用 div.icon.red,因为它会查找同时具有 icon 和 red 两个类的div元素。
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<head>
<style>
div.icon.red {
flex: 0.8
}
</style>
</head>
<body>
<div class="icon red"></div>
</body>
<!-- end snippet -->
<style> 标签必须始终放置在head元素内。由于scope属性已被弃用,因此不再有效将其放置在body内。
英文:
<div class="icon red" style="flex: 0.8">
That div has two different classes: icon and red.
In CSS you used div.iconred which indicates an element with a single class, the class iconred.
So you actually should use div.icon.red as it then looks for a div element with both the classes icon and red.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<head>
<style>
div.icon.red {
flex: 0.8
}
</style>
</head>
<body>
<div class="icon red"></div>
</body>
<!-- end snippet -->
The <style> tag has to be always placed within the head element. With the deprecation of the scope attribute, it is no longer valid to place it within the body.
答案2
得分: 1
不,这是不正确的,如果您想为一个元素添加样式,您有以下选项:
内联样式,就像您首先所做的那样,
内部样式,在HTML页面的<head>部分中定义,使用<style>元素。
外部样式,在HTML页面的<head>部分添加一个链接
在您的情况下,您想使用内部样式,因此您需要将您的style元素移到<head>部分。
正确的方式:
<head>
<style>
div.iconred {
flex: 0.8;
}
</style>
</head>
<body>
<div class="iconred">
</div>
</body>
在文档中阅读更多内容。
英文:
No, this is not correct, if you want to add style to an element, you have options:
Inline style like how you do first,
Internal style defined in the <head> section of an HTML page, within a <style> element.
External style add a link to it in the <head> section of HTML page
In your case you want to use Internal style so you have to move your style element to the <head> section.
The correct way:
<head>
<style>
div.iconred {
flex: 0.8;
}
</style>
</head>
<body>
<div class="iconred">
</div>
</body>
Read more in documentation
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论