Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I put CSS and HTML code in the same file?

Tags:

html

css

I'm a profane in CSS, I don't know anything about it. I need to put HTML code and the CSS formatting for it in the same string object for an iPhone program.

I have the HTML code and the CSS code, but I don't know how to mix them together. Could you help me?

The HTML code:

<html>
<head>
    <link type="text/css" href="./exemple.css" rel="stylesheet" media="all" />
</head>
<body>
    <p>
    <span class="title">La super bonne</span>
    <span class="author">proposée par Jérém</span>
    </p>
</body>
</html>

The CSS styles:

.title {
    color: blue;
    text-decoration: bold;
    text-size: 1em;
}

.author {
    color: gray;
}
like image 668
Nielsou Hacken-Bergen Avatar asked Aug 24 '11 15:08

Nielsou Hacken-Bergen


People also ask

Do you write HTML and CSS in same file?

CSS and HTML are best kept separate so that the CSS is uniformly accessible to all pages on a site, not just the document it is included in. Integrating CSS into webpages is an inefficient way to style a site. There are three ways to incorporate CSS: External file.

Can you add HTML to CSS?

There are three different ways to link CSS to HTML based on three different types of CSS styles: Inline – uses the style attribute inside an HTML element. Internal – written in the <head> section of an HTML file. External – links an HTML document to an external CSS file.

Should CSS be in a different file than HTML?

Keep it separate. HTML for centent, CSS for style, JavaScript for logic.


3 Answers

<html>
<head>
    <style type="text/css">
    .title {
        color: blue;
        text-decoration: bold;
        text-size: 1em;
    }

    .author {
        color: gray;
    }
    </style>
</head>
<body>
    <p>
    <span class="title">La super bonne</span>
    <span class="author">proposée par Jérém</span>
    </p>
</body>
</html>

On a side note, it would have been much easier to just do this.

like image 64
Prisoner Avatar answered Oct 14 '22 21:10

Prisoner


You can include CSS styles in an html document with <style></style> tags.

Example:

<style>
  .myClass { background: #f00; }

  .myOtherClass { font-size: 12px; }
</style>
like image 39
Mark Avatar answered Oct 14 '22 21:10

Mark


Is this what you're looking for? You place you CSS between style tags in the HTML document header. I'm guessing for iPhone it's webkit so it should work.

<html>
<head>
    <style type="text/css">
    .title { color: blue; text-decoration: bold; text-size: 1em; }
    .author { color: gray; }
    </style>
</head>
<body>
    <p>
    <span class="title">La super bonne</span>
    <span class="author">proposée par Jérém</span>
    </p>
</body>
</html>
like image 3
Phil Avatar answered Oct 14 '22 22:10

Phil