Html 定义 Div 标签的样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1706302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:15:50  来源:igfitidea点击:

Define style of Div tag

htmlcss

提问by RKh

I want to define the style of Div based on a CSS file. The CSS code I have written is:

我想根据 CSS 文件定义 Div 的样式。我写的 CSS 代码是:

.body
{
    text-align: center;
    padding: 1px;
    margin: 1px;
    color: black;
    background-color:lightgrey;
}

I want the background of each Div section to be light-grey. What's wrong with the above code?

我希望每个 Div 部分的背景都是浅灰色。上面的代码有什么问题?

Edited:

编辑:

This is my HTML code. I changed the Div class as suggested below, but it is not working. Please also check whether the Link tag contains path correctly or not. The CSS file is located in a Lib folder, up-one-level.

这是我的 HTML 代码。我按照下面的建议更改了 Div 类,但它不起作用。另请检查链接标签是否正确包含路径。CSS 文件位于上一级的 Lib 文件夹中。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <title>Add/Update Political Party</title>
        <link rel="stylesheet" href=".\Lib\entryformstyle.css" type="text/css"/>
    </head>
    <body>
        <div id="sectionEntryForm" class="div">
            <table id="tblEntryForm" cols="2">
                <tr>
                    <td colspan="2" align="center">Add/Update Political Party</td>
                </tr>
                <tr>
                    <td>Party full name:</td>
                    <td><input id="inPartyFullName" name="inPartyFullName" accept="text/plain" maxlength="80" class="inputBoxStyle"></td>
                </tr>
                <tr>
                    <td>Party short name (initials):</td>
                    <td><input id="inPartyShortName" name="inPartyShortName" accept="text/plain" maxlength="80" class="inputBoxStyle"></td>
                </tr>
            </table>
        </div>
    </body>
</html>

回答by Aaron Digulla

Nothing, you just have to use <div class="body">to make the DIV pick it up.

没什么,您只需要使用<div class="body">使 DIV 拾取它即可。

回答by eozzy

ALLDIVs:

所有DIV:

div
{
    text-align: center;
    padding: 1px;
    margin: 1px;
    color: black;
    background-color:lightgrey;
}

DIVs with class "body":

带有“body”类的 DIV:

div.body
{
    text-align: center;
    padding: 1px;
    margin: 1px;
    color: black;
    background-color:lightgrey;
}

回答by Franz

If your CSS file is up one level, you have to include it like this:

如果你的 CSS 文件上升一级,你必须像这样包含它:

<link rel="stylesheet" href="../lib/entryformstyle.css" type="text/css"/>

回答by SpaghettiMonster

try deleting the period (.) before 'body'

尝试删除“正文”之前的句点 (.)

edit: it is also probably worth having a quick read of this post

编辑:也可能值得快速阅读这篇文章

it explains the difference between "." and '#' when defining styles.

它解释了“。”之间的区别。和 '#' 定义样式时。

回答by Johannes Hoff

This only works on the class "body", that is

这仅适用于“身体”类,即

 <div class="body">

If you want it to work on any divs, the selector (".body" in your sample code) should be div.

如果您希望它适用于任何 div,则选择器(.body示例代码中的“ ”)应该是div.

回答by Tola Odejayi

For this style to work, your div needs to be written like this:

为了让这种风格起作用,你的 div 需要这样写:

<div class="body">Your text here</div>

In other words, the .in front of the body in your css tells the browser to apply the style in the braces to an element whose classis named body.

换句话说,. 在 css 中的 body 前面告诉浏览器将大括号中的样式应用于名为body的元素。

回答by Luke Bennett

Try changing:

尝试改变:

background-color:lightgrey;

to

background-color:silver;

or

或者

background-color:#CCC;

This is assuming that your div has the body class applied:

这是假设您的 div 应用了 body 类:

<div class="body"></div>

回答by Himadri

Either use

要么使用

div
{
    text-align: center;
    padding: 1px;
    margin: 1px;
    color: black;
    background-color:lightgrey;/*#CCC*/
}

to set background color of all div

设置所有div的背景颜色

Or use

或使用

div.body
{
    text-align: center;
    padding: 1px;
    margin: 1px;
    color: black;
    background-color:lightgrey;
}

and set <div class="body"></div>for each div.

<div class="body"></div>为每个 div设置。

You can use inline stylesheet as :

您可以将内联样式表用作:

<div style="background-color:lightgrey;"></div>