gpt4 book ai didi

css - 为什么我不能使用 CSS 来限制这个表的大小?

转载 作者:太空宇宙 更新时间:2023-11-04 13:52:24 24 4
gpt4 key购买 nike

我构建了一个简单的网页,其中一个表格是在单击按钮时使用 Javascript 创建的。

我已经为表指定了一个类名,并为此类指定了一些格式化属性,例如:表格布局:固定宽度:650px

另外,我试图强制表格的 td 元素内的文本换行。

尽管如此,表格还是溢出了它的父元素,结果不是我所期望的。自从我在我的网站上创建了另一个以类似方式设置并且工作正常的页面后,我就更加困惑了。

你能告诉我我做错了什么吗?

结果 enter image description here

HTML

<head>
<meta charset="UTF-8" />
<title>Forum</title>
<link rel="stylesheet" type="text/css" href="main.css" />
<link rel="stylesheet" type="text/css" href="stocktails.css" />
<script type="text/javascript" src="forum.js"></script>
</head>
<body>

<nav id="top-menu">
<ul>
<li> <a href="index.xhtml">Home</a> </li>
<li> <a href="charting.xhtml">Charting</a> </li>
<li> <a href="map.xhtml">Map</a> </li>
<li> <a href="financials.xhtml">Financials</a> </li>
<li> <a href="forum.xhtml">Forum</a> </li>
<li> <a href="about.xhtml">About</a> </li>
</ul>
</nav>

<div id="maindiv">
<header>
<hgroup>
<h1> Discussion forum </h1>
</hgroup>
<p> Bounce your investing ideas off the community </p>
</header>

<div>
<p> Key in you post. </p>
<input type="text" id="postText" />
<button type="submit" onclick="tableCreate();">Submit your post</button>
</div>

<article>
<div id="tablePlaceholder"></div>
</article>

<aside class="about">
<header>
<h1> Forum etiquette </h1>
<p> Thou shall not troll </p>
</header>
<section>
<hgroup>
<h1> No promotion </h1>
</hgroup>
<p> Do not use this forum to promote your own business. </p>
</section>
<section>
<hgroup>
<h1> Keep calm and carry on </h1>
</hgroup>
<p> Be courteous. Personal disagreements should be handled through email and not through public posts. </p>
</section>
<section>
<hgroup>
<h1> No hijacking </h1>
</hgroup>
<p> Do not hijack someone else's thread and interrupt a topic of discussion. </p>
</section>
</aside>

<div class="clear"></div>
</div>

<footer>
<p>
This is a footer.
</p>
</footer>

</body>
</html>

CSS

body {
background-color: #fffbef;
}

.story p {
line-height: 1.2;
text-align: justify;
}

h1, h2, h3, h4, h5, h6 {
font-family: sans-serif;
color: #093844;
margin: .25ex 12pt;
}

.clear {
margin: 0;
padding: 0;
clear: both;
}

img.img-right {
display: block;
float: right;
border: none;
padding: 2px;
margin: 3px 6pt 3px 6px;
}

/* navigation menu */

nav#top-menu {
width: 100%;
height: 50px;
//background-color: #fff2c1;
margin: 0;
padding: 0;
}

#top-menu ul {
display: block;
list-style-type: none;
width: 600px;
margin: 0 auto;
padding: 0;
}

#top-menu ul li {
margin: 0;
padding: 0;
}

#top-menu ul li a {
display: block;
float: left;
max-height: 25px;
width: 100px;
margin: 0;
padding: 5px 0;
font-family: sans-serif;
font-size: 20px;
text-align: center;
text-decoration: none;
color: #52736b;
border-bottom: #fffbef solid 2px;
}

#top-menu ul li a:hover { border-bottom: #52736b solid 2px; }

/* header */

header h1 { margin-top: 0; }

header p {
font-family: sans-serif;
font-weight: bold;
font-style: italic;
font-size: 85%;
margin: .25ex 12pt;
color: #093844;
}

/* maindiv */

#maindiv {
width: 1250px;
margin: 0 auto;
padding: 10px;
background-color: #eec;
}

/* article */

article {
width: 950px;
float: left;
}

article h1 {
font-size: 110%;
margin-top: 12pt;
padding-top: 3pt;
border-top: 4px solid #52736b;
}

/* aside */

aside.about {
float: right;
width: 238px;
background-color: #b7c4b1;
margin: 3pt 6pt 3pt 6pt;
}

aside h1 { font-size: 110%; }
aside h2 { font-size: 90%; }
aside p {
margin: 0 12pt;
font-family: sans-serif;
font-style: italic;
font-size: 80%;
}

aside section h1 {
padding-top: 3pt;
border-top: 4px solid #4f5856;
}

aside header p {
font-size: 85%;
}

aside section {
padding: 5px 0 5px 0;
}

/* footer */

footer {
background: #4f5856;
clear: all;
}

footer p {
color: #8c8e7e;
font-size: 70%;
font-family: sans-serif;
text-align: center;
margin: 0;
padding: 10px 0;
}


//----------------------------------------------------------------
//--------------------FORUM---------------------------------------
//----------------------------------------------------------------

.forumTable table {
float: left;
padding: 10px;
table-layout: fixed;
width: 650px;
}

.forumTable table th, td {
border: 1px #52736b solid;
word-wrap: break-word
}

.forumTable table caption {
padding-top: 10px;
font-size: 130%;
}

.forumTable tfoot td {
border: none;
}

.forumTable th {
text-align: left;
}

Javascript

function tableCreate(){
var tablePlaceholder = document.getElementById("tablePlaceholder");
var tbl = document.createElement('table');
var br = document.createElement('br');
tbl.className = "forumTable"

for(var i = 0; i < 2; i++){ //rows
var tr = tbl.insertRow();
for(var j = 0; j < 2; j++){ //columns
var td = tr.insertCell();
}
}
//var tf = tbl.createTFoot();
//var tfr = tf.insertRow();
//var tfd = tfr.insertCell();

tbl.getElementsByTagName("tr")[0].getElementsByTagName("td")[0].appendChild(document.createTextNode('Date'));
tbl.getElementsByTagName("tr")[0].getElementsByTagName("td")[1].appendChild(document.createTextNode('Post number'));
tbl.getElementsByTagName("tr")[1].getElementsByTagName("td")[0].appendChild(document.createTextNode('Member'));
tbl.getElementsByTagName("tr")[1].getElementsByTagName("td")[1].appendChild(document.createTextNode('TextTextTextTextTextTextTextTextTextTextTextTextTextTextTextTextTextTextTextTextTextTextTextTextTextTextTextTextTextTextTextTextTextTextTextTextTextTextTextTextTextTextTextTextTextTextText'));

tablePlaceholder.appendChild(br);
tablePlaceholder.appendChild(tbl);

}

最佳答案

你的 CSS 有两个问题:

您使用的选择器不会使您的样式应用于表格。

使用 .forumTable table 将尝试为 table inside 元素设置 forumTable 类的样式。然而,您的 forumTable 类。您应该使用 table.forumTable.forumTable

.forumTable { /*was: .forumTable table*/
float: left;
padding: 10px;
table-layout: fixed;
width: 650px;
}

.forumTable th, td { /*was: .forumTable table th, td*/
border: 1px #52736b solid;
word-wrap: break-word; /*you were also missing this semicolon*/
}

.forumTable caption { /*was: .forumTable table caption*/
padding-top: 10px;
font-size: 130%;
}

此外,您的评论导致 CSS 解析失败;你不能在 CSS 中使用 // 注释。

Here您的代码是否应用了我建议的更改。

还有一件事;您很可能想使用 .forumTable th, .forumTable td 而不是 .forumTable th, td。此更改将设置 .forumTable 内所有 thtd 标签的样式,而不是页面中所有 td 标签的样式,以及 .forumTable 中的所有 th 标签。

关于css - 为什么我不能使用 CSS 来限制这个表的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22129379/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com