htmlとcssの使い方について
解決済
回答 2
投稿
- 評価
- クリップ 0
- VIEW 1,720
HTMLとCSSを使用して表を作成しようとしています。
表の一行目のセルに書かれている文字を白に、セル色をオレンジに変更したいです。
一つのセルごとに文字色/セル色を指定するやり方は出来るのですが、cssで指定した情報をHTMLの指定した範囲内で有効にするやり方がわかりません。現在のコードは以下の通りです。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Home</title>
<link rel="stylesheet"
href="${pageContext.request.contextPath}/resources/app/css/styles.css">
</head>
<body>
<!--<header>
<section class="content-header">
<h1>
<FONT color="white">リモート保守</FONT>
</h1>
</section>
</header>-->
<table>
<div class="line">
<tr>
<th>aaaa</th>
<th>aaaa</th>
<th>aaaa</th>
</tr>
</div>
<tr bgcolor="wheat">
<td>aaaaa</td>
<td>aaaaa</td>
<td>aaaaa</td>
</tr>
<tr bgcolor="lightyellow">
<td>aaaaaaaa</td>
<td>aaaaaaa</td>
<td>aaaaaaa</td>
</tr>
</table>
</body>
</html>
/* @import url(https://fonts.googleapis.com/css?family=Lato:300italic,700italic,300,700); */
body {
padding:50px;
font:14px/1.5 Lato, "Helvetica Neue", Helvetica, Arial, sans-serif;
color:#777;
font-weight:300;
}
h1, h2, h3, h4, h5, h6 {
color:#222;
margin:0 0 20px;
}
p, ul, ol, table, pre, dl {
margin:0 0 20px;
}
h1, h2, h3 {
line-height:1.1;
}
h1 {
font-size:28px;
}
h2 {
color:#393939;
}
h3, h4, h5, h6 {
color:#494949;
}
a {
color:#39c;
font-weight:400;
text-decoration:none;
}
a small {
font-size:11px;
color:#777;
margin-top:-0.6em;
display:block;
}
.wrapper {
width:860px;
margin:0 auto;
}
blockquote {
border-left:1px solid #e5e5e5;
margin:0;
padding:0 0 0 20px;
font-style:italic;
}
code, pre {
font-family:Monaco, Bitstream Vera Sans Mono, Lucida Console, Terminal;
color:#333;
font-size:12px;
}
pre {
padding:8px 15px;
background: #f8f8f8;
border-radius:5px;
border:1px solid #e5e5e5;
overflow-x: auto;
}
table {
width:100%;
border-collapse:collapse;
}
th, td {
text-align:left;
padding:5px 10px;
border-bottom:1px solid #e5e5e5;
}
dt {
color:#444;
font-weight:700;
}
th {
color:#444;
}
img {
max-width:100%;
}
header {
width:270px;
float:left;
position:fixed;
}
header ul {
list-style:none;
height:40px;
padding:0;
background: #eee;
background: -moz-linear-gradient(top, #f8f8f8 0%, #dddddd 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#f8f8f8), color-stop(100%,#dddddd));
background: -webkit-linear-gradient(top, #f8f8f8 0%,#dddddd 100%);
background: -o-linear-gradient(top, #f8f8f8 0%,#dddddd 100%);
background: -ms-linear-gradient(top, #f8f8f8 0%,#dddddd 100%);
background: linear-gradient(top, #f8f8f8 0%,#dddddd 100%);
border-radius:5px;
border:1px solid #d2d2d2;
box-shadow:inset #fff 0 1px 0, inset rgba(0,0,0,0.03) 0 -1px 0;
width:270px;
}
header li {
width:89px;
float:left;
border-right:1px solid #d2d2d2;
height:40px;
}
header ul a {
line-height:1;
font-size:11px;
color:#999;
display:block;
text-align:center;
padding-top:6px;
height:40px;
}
strong {
color:#222;
font-weight:700;
}
header ul li + li {
width:88px;
border-left:1px solid #fff;
}
header ul li + li + li {
border-right:none;
width:89px;
}
header ul a strong {
font-size:14px;
display:block;
color:#222;
}
section {
width:500px;
float:right;
padding-bottom:50px;
}
small {
font-size:11px;
}
hr {
border:0;
background:#e5e5e5;
height:1px;
margin:0 0 20px;
}
footer {
width:270px;
float:left;
position:fixed;
bottom:50px;
}
@media print, screen and (max-width: 960px) {
div.wrapper {
width:auto;
margin:0;
}
header, section, footer {
float:none;
position:static;
width:auto;
}
header {
padding-right:320px;
}
section {
border:1px solid #e5e5e5;
border-width:1px 0;
padding:20px 0;
margin:0 0 20px;
}
header a small {
display:inline;
}
header ul {
position:absolute;
right:50px;
top:52px;
}
}
@media print, screen and (max-width: 720px) {
body {
word-wrap:break-word;
}
header {
padding:0;
}
header ul, header p.view {
position:static;
}
pre, code {
word-wrap:normal;
}
}
@media print, screen and (max-width: 480px) {
body {
padding:15px;
}
header ul {
display:none;
}
}
@media print {
body {
padding:0.4in;
font-size:12pt;
color:#444;
}
}
.line {
bgcolor:gold;
color: White;
}
問題:
HTMLで<div class="line">と指定している部分で下記のWorningが出ております。
1.Multiple annotations found at this line:
- Invalid location of tag (div).
2.- No end tag (</div>).
cssで指定した設定をHTMLの指定範囲で有効にする書き方が分かる方は教えていただけると幸いです。
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
checkベストアンサー
+5
lineクラスに指定してあるbgcolorプロパティはbackground-colorプロパティの誤りでは?必ずしもHTMLの属性名がCSSのプロパティ名と同じではないことに注意してください。
また、tableタグ直下にdivタグを記述することはできません。
Content model:
In this order: optionally a caption element, followed by zero or more colgroup elements, followed optionally by a thead element, followed by either zero or more tbody elements or one or more tr elements, followed optionally by a tfoot element, optionally intermixed with one or more script-supporting elements.
これらを踏まえると、質問者さんのコードは以下のように書けると思います。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Home</title>
<style type="text/css">
/* 文字数制限の関係で省略させていただきます。内容は追記のCSSと変わりませんので、そちらを参考にしてください。 */
</style>
</head>
<body>
<table>
<tr class="line">
<th>aaaa</th>
<th>aaaa</th>
<th>aaaa</th>
</tr>
<tr bgcolor="wheat">
<td>aaaaa</td>
<td>aaaaa</td>
<td>aaaaa</td>
</tr>
<tr bgcolor="lightyellow">
<td>aaaaaaaa</td>
<td>aaaaaaa</td>
<td>aaaaaaa</td>
</tr>
</table>
</body>
</html>
(追記)
HTML部分:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Home</title>
<link rel="stylesheet"
href="${pageContext.request.contextPath}/resources/app/css/styles.css">
</head>
<body>
<table>
<tr class="line">
<th>aaaa</th>
<th>aaaa</th>
<th>aaaa</th>
</tr>
<tr bgcolor="wheat">
<td>aaaaa</td>
<td>aaaaa</td>
<td>aaaaa</td>
</tr>
<tr bgcolor="lightyellow">
<td>aaaaaaaa</td>
<td>aaaaaaa</td>
<td>aaaaaaa</td>
</tr>
</table>
</body>
</html>
CSS部分:
/* @import url(https://fonts.googleapis.com/css?family=Lato:300italic,700italic,300,700); */
body {
padding: 50px;
font: 14px/1.5 Lato, "Helvetica Neue", Helvetica, Arial, sans-serif;
color: #777;
font-weight: 300;
}
h1, h2, h3, h4, h5, h6 {
color: #222;
margin: 0 0 20px;
}
p, ul, ol, table, pre, dl {
margin: 0 0 20px;
}
h1, h2, h3 {
line-height: 1.1;
}
h1 {
font-size: 28px;
}
h2 {
color: #393939;
}
h3, h4, h5, h6 {
color: #494949;
}
a {
color: #39c;
font-weight: 400;
text-decoration: none;
}
a small {
font-size: 11px;
color: #777;
margin-top: -0.6em;
display: block;
}
.wrapper {
width: 860px;
margin: 0 auto;
}
blockquote {
border-left: 1px solid #e5e5e5;
margin: 0;
padding: 0 0 0 20px;
font-style: italic;
}
code, pre {
font-family: Monaco, Bitstream Vera Sans Mono, Lucida Console, Terminal;
color: #333;
font-size: 12px;
}
pre {
padding: 8px 15px;
background: #f8f8f8;
border-radius: 5px;
border: 1px solid #e5e5e5;
overflow-x: auto;
}
table {
width: 100%;
border-collapse: collapse;
}
th, td {
text-align: left;
padding: 5px 10px;
border-bottom: 1px solid #e5e5e5;
}
dt {
color: #444;
font-weight: 700;
}
th {
color: #444;
}
img {
max-width: 100%;
}
header {
width: 270px;
float: left;
position: fixed;
}
header ul {
list-style: none;
height: 40px;
padding: 0;
background: #eee;
background: -moz-linear-gradient(top, #f8f8f8 0%, #dddddd 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #f8f8f8), color-stop(100%, #dddddd));
background: -webkit-linear-gradient(top, #f8f8f8 0%, #dddddd 100%);
background: -o-linear-gradient(top, #f8f8f8 0%, #dddddd 100%);
background: -ms-linear-gradient(top, #f8f8f8 0%, #dddddd 100%);
background: linear-gradient(top, #f8f8f8 0%, #dddddd 100%);
border-radius: 5px;
border: 1px solid #d2d2d2;
box-shadow: inset #fff 0 1px 0, inset rgba(0, 0, 0, 0.03) 0 -1px 0;
width: 270px;
}
header li {
width: 89px;
float: left;
border-right: 1px solid #d2d2d2;
height: 40px;
}
header ul a {
line-height: 1;
font-size: 11px;
color: #999;
display: block;
text-align: center;
padding-top: 6px;
height: 40px;
}
strong {
color: #222;
font-weight: 700;
}
header ul li + li {
width: 88px;
border-left: 1px solid #fff;
}
header ul li + li + li {
border-right: none;
width: 89px;
}
header ul a strong {
font-size: 14px;
display: block;
color: #222;
}
section {
width: 500px;
float: right;
padding-bottom: 50px;
}
small {
font-size: 11px;
}
hr {
border: 0;
background: #e5e5e5;
height: 1px;
margin: 0 0 20px;
}
footer {
width: 270px;
float: left;
position: fixed;
bottom: 50px;
}
@media print, screen and (max-width: 960px) {
div.wrapper {
width: auto;
margin: 0;
}
header, section, footer {
float: none;
position: static;
width: auto;
}
header {
padding-right: 320px;
}
section {
border: 1px solid #e5e5e5;
border-width: 1px 0;
padding: 20px 0;
margin: 0 0 20px;
}
header a small {
display: inline;
}
header ul {
position: absolute;
right: 50px;
top: 52px;
}
}
@media print, screen and (max-width: 720px) {
body {
word-wrap: break-word;
}
header {
padding: 0;
}
header ul, header p.view {
position: static;
}
pre, code {
word-wrap: normal;
}
}
@media print, screen and (max-width: 480px) {
body {
padding: 15px;
}
header ul {
display: none;
}
}
@media print {
body {
padding: 0.4in;
font-size: 12pt;
color: #444;
}
}
.line {
background-color: gold; /* bgcolorではなくbackground-color */
color: White;
}
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
+1
テーブルへのアプローチはブラウザによって多少方言があるので
tbodyやtheadを設定してあげたほうがいいです
(特に今回thタグを使用しているってことはthead?)
なので、theadにcssを設定すればいいですが、その中でもtheadの
先頭行を指定して文字色や背景色を設定するならこんな感じ
<style>
table thead tr:first-child{color:#ffffff;background-Color:#ff8000;"}
</style>
<table>
<thead>
<tr>
<th>aaaa</th>
<th>aaaa</th>
<th>aaaa</th>
</tr>
</thead>
<tbody>
<tr>
<td>aaaaa</td>
<td>aaaaa</td>
<td>aaaaa</td>
</tr>
<tr>
<td>aaaaaaaa</td>
<td>aaaaaaa</td>
<td>aaaaaaa</td>
</tr>
</tbody>
</table>
※thead全体に適用するなら「tr:first-child」はいらないですけどね
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.09%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
2017/02/09 18:59
これはCSSとHTMLを同一のファイルに書くという認識でよろしいでしょうか?
できましたら、別ファイルに書きたいのですが、どうすればよいでしょうか?
2017/02/09 19:07
一応ですが、HTML部分とCSS部分を分けたものを追記させていただきました。
2017/02/10 08:17
本当にありがとうございます。