tags:

views:

184

answers:

9

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

.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?

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.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
<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>
+1  A: 

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.

JT.WK
In this case, it would be better to replace `.body` with `div`.
Aaron Digulla
@JT: The CSS worked when I deleted the period, but it is not recognizing the class name. Also, can I define my own class name?
RPK
yep! you can, however you will need to add reference it with the period (.) and the new class name; ie .myClass{background-color:lightgrey;}
JT.WK
-> this is assuming you have defined the class with: <div class="myClass"></div>
JT.WK
Also try replacing the period with a #
JT.WK
+1  A: 

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.

Johannes Hoff
+3  A: 

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

Aaron Digulla
A: 

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

<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 class is named body.

Shoko
A: 

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 class="body"></div>
Luke Bennett
`lightgrey` is an allowed value: http://www.w3.org/TR/css3-color/#svg-color
Johannes Hoff
Ah thanks - I did a quick check but obviously not thorough enough!
Luke Bennett
+1  A: 

ALL DIVs:

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

DIVs with class "body":

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

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

<link rel="stylesheet" href="../lib/entryformstyle.css" type="text/css"/>
Franz
@Franz: It worked, but it is aligning in center in IE, but not in FireFox?
RPK
Try to use `.div, .div * {` as the selector.
Franz
A: 

tip: for the links inside html use / not \ :)

Ibrahim AbuRajab
A: 

Either use

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

to set background color of all 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.

You can use inline stylesheet as :

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