开发者

Is it possible to inline a class definition of CSS inside an xhtml file?

开发者 https://www.devze.com 2023-04-05 16:55 出处:网络
Is it possible to inline a class definition of CSS inside an xhtml file? I mean, to put someting like:

Is it possible to inline a class definition of CSS inside an xhtml file?

I mean, to put someting like:

p.first{ color: blue; }
p.second{ color: red; }

Inside my 开发者_如何学Cpage, not in a separate CSS file.


I think you're trying to put your CSS in the HTML page, not inline.

You can put CSS in an HTML page (usually in the head) by surrounding it in style tags:

<style type="text/css">
    p.first{ color: blue; }
    p.second{ color: red; }
</style>


Sure, here's an example. However, it is best practice to keep your styles in a separate css file.

<html>
    <head>
        <title>Classes</title>
        <link rel="stylesheet" type="text/css" href="css/styles.css"/>    
        <style type="text/css">
         img {
             padding:10px;
             margin:5px;
             border:1px solid #d5d5d5;
          }
          div.thumb {
             float:left;
          }
          div.caption {
             padding-left:5px;
             font-size:10px;
          }
       </style>
     </head>
    <body>
        <div>your page code etc..</div>
    </body>
</html>


You can also put css inside the p tag.

<html>
<body>
<p class="first" style="color:blue;"></p>
<p class="second" style="color:red;"></p>                                               
</body>
</html> 


The nice thing about CSS is it works in any file not just an HTML,XML file. You just need to define the syle block like this anywhere in the page

<style type="text/css">
    <all my styles goes here>
</style>

In HTML and HTML/XHTML, the standard is, you will put this block in the head section. If it is other type of file for example .aspx, or .php, the block still works, even it is not in head block.

Example

<?php
 /* mytest.php file */
<style>
   <my styles>
</style>

?>

the same is true for ASPX file.

You can also define inline CSS which means CSS goes right in the element tag. The syntax is

<p style="<all my styles>"> My paragraph contain inline CSS</p>


Yes, you can insert CSS styles in the HTML file. For example:

<p>...</p>
<style type="text/css">
p.first { ... }
</style>
<div>...</div>

As you'll find in the literature, it's not considered a good practice though.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号