2017-03-29 124 views
2

我見過類似的話題,但不適合我的具體情況。我需要在表格中嵌套一個表格,它將佔用幾個單元格,但是當我嘗試嵌套表格時,它會同時刪除周圍的表格。我包含了一張關於應該如何看待我的代碼的圖片,我的代碼是目前的樣子。有人可以解釋這是如何完成的?非常感激。如何在表格中嵌套表格?

/* 
 
    CSS for Lounge Project 
 
    Filename: styles.css 
 
    
 
    Author: Justus Self 
 
    Date:  3/21/17 
 
    HTML5 and CSS3 Illustrated Unit I, Lounge Independent Project 
 
*/ 
 

 
/*Reset Styles*/ 
 
html, body{ 
 
    font-size: 14px; 
 
    width: 100%; 
 
    } 
 
a, body, footer, header, h1, h2, h3, img, li, nav, p, ul, table, tbody, td, tfoot, th, thead, tr{ 
 
    border: 0; 
 
    padding: 0; 
 
    margin: 0; 
 
} 
 
img { 
 
    max-width: 100%; 
 
    height: auto; 
 
    width: auto; 
 
} 
 

 
/*Drink font colors*/ 
 

 
h2.green { 
 
    color: green; 
 
} 
 
h2.blue { 
 
    color: blue; 
 
} 
 
h2.purple { 
 
    color: purple; 
 
} 
 
h2.red { 
 
    color: red; 
 
} 
 
h2.yellow { 
 
    color: gold; 
 
} 
 

 

 
/*center and border image*/ 
 

 
img.smlounge { 
 
    display: block; 
 
    margin-left: auto; 
 
    margin-right: auto; 
 
    border: 3px solid; 
 
    border-color: red; 
 
} 
 

 

 
/*Table styles*/ 
 

 
td, th { 
 
    border: 1px solid black; 
 
    font-size: 1.3em; 
 
    margin-left: auto; 
 
    margin-right: auto; 
 
    text-align: center; 
 
    padding: 0 1em ; 
 
} 
 
table { 
 
    border-collapse: collapse; 
 
    margin: 0 auto; 
 
} 
 
td[colspan="2"] { 
 
    width: 20%; 
 
} 
 
.drink-name { 
 
    text-align: left; 
 
}
<!DOCTYPE html> 
 
<html lang="en"> 
 

 
<head> 
 
    <meta charset="utf-8" /> 
 
    <title>Head First Lounge Elixirs</title> 
 

 
    <!-- 
 
    Elixir page for Lounge Project 
 
    Filename: elixir.html 
 
    
 
    Author: Justus Self 
 
    Date:  3/21/17 
 
    HTML5 and CSS3 Illustrated Unit I, Lounge Independent Project 
 
    --> 
 

 
    <link type="text/css" rel="stylesheet" href="../lounge.css" /> 
 
</head> 
 

 
<body> 
 
    <h1>Our Elixirs</h1> 
 

 
    <h2 class="green">Green Tea Cooler</h2> 
 
    <p> 
 
    <img src="../images/green.jpg" alt="Green Tea Cooler" /> Chock full of vitamins and minerals, this elixir combines the healthful benefits of green tea with a twist of chamomile blossoms and ginger root. 
 
    </p> 
 
    <hr/> 
 
    <h2 class="blue">Raspberry Ice Concentration</h2> 
 
    <p> 
 
    <img src="../images/lightblue.jpg" alt="Rasberry Ice Concentration" /> Combining raspberry juice with lemon grass, citrus peel and rosehips, this icy drink will make your mind feel clear and crisp. 
 
    </p> 
 
    <hr/> 
 
    <h2 class="purple">Blueberry Bliss Elixir</h2> 
 
    <p> 
 
    <img src="../images/blue.jpg" alt="Blueberry Bliss Elixir" /> Blueberries and cherry essence mixed into a base of elderflower herb tea will put you in a relaxed state of bliss in no time. 
 
    </p> 
 
    <hr/> 
 
    <h2 class="red">Cranberry Antioxidant Blast</h2> 
 
    <p> 
 
    <img src="../images/red.jpg" alt="Cranberry Antioxidant Blast" /> Wake up to the flavors of cranberry and hibiscus in this vitamin C rich elixir. 
 
    </p> 
 
    <hr/> 
 
    <h2 class="yellow"><a name="Yellow">Lemon Breeze</a></h2> 
 
    <p><img src="../images/yellow.gif" alt="Lemon Breeze Drink" /> The ultimate healthy drink, this elixir combines herbal botanicals, minerals, and vitamins with a twist of lemon into a smooth citrus wonder that will keep your immune system going all day 
 
    and all night. 
 
    </p> 
 
    <hr/> 
 
    <br/> 
 
    <table> 
 
    <thead> 
 
     <tr> 
 
     <th colspan="6">Our Drink Prices</th> 
 
     </tr> 
 
     <tr> 
 
     <th colspan="2">Drink</th> 
 
     <th colspan="2">Size</th> 
 
     <th colspan="2">Price</th> 
 
     </tr> 
 
    </thead> 
 
    <tbody> 
 
     <tr> 
 
     <td rowspan="2" colspan="2" class="drink-name">Green Tea Cooler</td> 
 
     <td colspan="2">16 oz.</td> 
 
     <td colspan="2">$3.75</td> 
 
     </tr> 
 
     <tr> 
 
     <td colspan="2">24 oz.</td> 
 
     <td colspan="2">$4.75</td> 
 
     </tr> 
 
     <tr> 
 
     <td rowspan="2" colspan="2" class="drink-name">Raspberry Ice Concentration</td> 
 
     <td colspan="2">16 oz.</td> 
 
     <td colspan="2">$3.75</td> 
 
     </tr> 
 
     <tr> 
 
     <td colspan="2">24 oz.</td> 
 
     <td colspan="2">$4.75</td> 
 
     </tr> 
 
     <tr> 
 
     <td colspan="2" class="drink-name">Cranberry Antioxidant Blast</td> 
 
     <td colspan="2">20 oz.</td> 
 
     <td colspan="2">$4.75</td> 
 
     </tr> 
 
     <tr> 
 
     <td rowspan="2" colspan="2" class="drink-name">Lemon Breeze</td> 
 
     <td>Iced</td> 
 
     <td>16 oz.</td> 
 
     <td>$3.75</td> 
 
     </tr> 
 
     <tr> 
 
     <td>Frozen</td> 
 
     <td>20 oz.</td> 
 
     <td>$4.75</td> 
 
     </tr> 
 
    </tbody> 
 
    </table> 
 
    <p><a href="../lounge.html">Back to the Lounge</a></p> 
 

 
    <footer> 
 
    &#169;2016, Head First Online Lounge<br /> All trademarks and registered trademarks appearing on this site are the property of their respective owners. 
 
    </footer> 
 
</body> 
 

 
</html>

How the table should look

+0

我不明白你的桌子看起來和照片一樣嗎? – garek007

+0

在圖像中,您是否看到靠近桌子底部的一些單元格的雙邊框?這是嵌套在原始表格中的表格。我必須複製這個項目。 – SkryBlackfall

回答

0

你可以做類似下面並改變CSS根據自己的需要。

<!DOCTYPE html> 
 
<html lang="en"> 
 

 
<head> 
 
    <meta charset="utf-8" /> 
 
    <title>Head First Lounge Elixirs</title> 
 

 
    <!-- 
 
    Elixir page for Lounge Project 
 
    Filename: elixir.html 
 
    
 
    Author: Justus Self 
 
    Date:  3/21/17 
 
    HTML5 and CSS3 Illustrated Unit I, Lounge Independent Project 
 
    --> 
 

 
    <link type="text/css" rel="stylesheet" href="../lounge.css" /> 
 
</head> 
 

 
<body> 
 
    <h1>Our Elixirs</h1> 
 

 
    <h2 class="green">Green Tea Cooler</h2> 
 
    <p> 
 
    <img src="../images/green.jpg" alt="Green Tea Cooler" /> Chock full of vitamins and minerals, this elixir combines the healthful benefits of green tea with a twist of chamomile blossoms and ginger root. 
 
    </p> 
 
    <hr/> 
 
    <h2 class="blue">Raspberry Ice Concentration</h2> 
 
    <p> 
 
    <img src="../images/lightblue.jpg" alt="Rasberry Ice Concentration" /> Combining raspberry juice with lemon grass, citrus peel and rosehips, this icy drink will make your mind feel clear and crisp. 
 
    </p> 
 
    <hr/> 
 
    <h2 class="purple">Blueberry Bliss Elixir</h2> 
 
    <p> 
 
    <img src="../images/blue.jpg" alt="Blueberry Bliss Elixir" /> Blueberries and cherry essence mixed into a base of elderflower herb tea will put you in a relaxed state of bliss in no time. 
 
    </p> 
 
    <hr/> 
 
    <h2 class="red">Cranberry Antioxidant Blast</h2> 
 
    <p> 
 
    <img src="../images/red.jpg" alt="Cranberry Antioxidant Blast" /> Wake up to the flavors of cranberry and hibiscus in this vitamin C rich elixir. 
 
    </p> 
 
    <hr/> 
 
    <h2 class="yellow"><a name="Yellow">Lemon Breeze</a></h2> 
 
    <p><img src="../images/yellow.gif" alt="Lemon Breeze Drink" /> The ultimate healthy drink, this elixir combines herbal botanicals, minerals, and vitamins with a twist of lemon into a smooth citrus wonder that will keep your immune system going all day 
 
    and all night. 
 
    </p> 
 
    <hr/> 
 
    <br/> 
 
    <table> 
 
    <thead> 
 
     <tr> 
 
     <th colspan="6">Our Drink Prices</th> 
 
     </tr> 
 
     <tr> 
 
     <th colspan="2">Drink</th> 
 
     <th colspan="2">Size</th> 
 
     <th colspan="2">Price</th> 
 
     </tr> 
 
    </thead> 
 
    <tbody> 
 
     <tr> 
 
     <td rowspan="2" colspan="2" class="drink-name">Green Tea Cooler</td> 
 
     <td colspan="2">16 oz.</td> 
 
     <td colspan="2">$3.75</td> 
 
     </tr> 
 
     <tr> 
 
     <td colspan="2">24 oz.</td> 
 
     <td colspan="2">$4.75</td> 
 
     </tr> 
 
     <tr> 
 
     <td rowspan="2" colspan="2" class="drink-name">Raspberry Ice Concentration</td> 
 
     <td colspan="2">16 oz.</td> 
 
     <td colspan="2">$3.75</td> 
 
     </tr> 
 
     <tr> 
 
     <td colspan="2">24 oz.</td> 
 
     <td colspan="2">$4.75</td> 
 
     </tr> 
 
     <tr> 
 
     <td colspan="2" class="drink-name">Cranberry Antioxidant Blast</td> 
 
     <td colspan="2">20 oz.</td> 
 
     <td colspan="2">$4.75</td> 
 
     </tr> 
 
     <tr> 
 
     <td rowspan="2" colspan="2" class="drink-name">Lemon Breeze</td> 
 
     <td colspan="2"> 
 
      <table> 
 
      <tr> 
 

 
       <td>Iced</td> 
 
       <td>16 oz.</td> 
 
      </tr> 
 
      <tr> 
 
       <td>Frozen</td> 
 
       <td>20 oz.</td> 
 
      </tr> 
 
      </table> 
 
     </td> 
 
     <td colspan="2"> 
 
      <table> 
 
      <tr> 
 
       <td>$3.75</td> 
 
      </tr> 
 
      <tr> 
 
       <td>$4.75</td> 
 
      </tr> 
 
      </table> 
 
     </td> 
 
     </tr> 
 
    </tbody> 
 
    </table> 
 
    <p><a href="../lounge.html">Back to the Lounge</a></p> 
 

 
    <footer> 
 
    &#169;2016, Head First Online Lounge<br /> All trademarks and registered trademarks appearing on this site are the property of their respective owners. 
 
    </footer> 
 
</body> 
 

 
</html>

希望這有助於!

+0

當我嘗試執行代碼時,會發生這種情況。當我實現你的代碼時,它會從原始表中分離出來,而不會停留在原始表的範圍內。有沒有一些方法可以編輯它,使它像我提供的圖片一樣? – SkryBlackfall

0

做這樣的(當然你需要修復的字體大小和造型,以及做其他方太,好試試這個。你仍然需要修復的字體大小

<table> 
<thead> 
    <tr> 
    <th colspan="6">Our Drink Prices</th> 
    </tr> 
    <tr> 
    <th colspan="2">Drink</th> 
    <th colspan="2">Size</th> 
    <th colspan="2">Price</th> 
    </tr> 
</thead> 
<tbody> 
    <tr> 
    <td rowspan="2" colspan="2" class="drink-name">Green Tea Cooler</td> 
    <td colspan="2">16 oz.</td> 
    <td colspan="2">$3.75</td> 
    </tr> 
    <tr> 
    <td colspan="2">24 oz.</td> 
    <td colspan="2">$4.75</td> 
    </tr> 
    <tr> 
    <td rowspan="2" colspan="2" class="drink-name">Raspberry Ice Concentration</td> 
    <td colspan="2">16 oz.</td> 
    <td colspan="2">$3.75</td> 
    </tr> 
    <tr> 
    <td colspan="2">24 oz.</td> 
    <td colspan="2">$4.75</td> 
    </tr> 
    <tr> 
    <td colspan="2" class="drink-name">Cranberry Antioxidant Blast</td> 
    <td colspan="2">20 oz.</td> 
    <td colspan="2">$4.75</td> 
    </tr> 
    <tr> 
    <td rowspan="2" colspan="2" class="drink-name">Lemon Breeze</td> 
    <td colspan="2" rowspan="2" class="nested"> 

    <table> 
    <tbody> 
    <tr><td>Iced</td><td>16 oz</td></tr> 
    <tr><td>Frozen</td><td>20 oz</td></tr> 
    </tbody> 
    </table> 


    </td> 

    <td>$3.75</td> 
    </tr> 
    <tr> 


    <td>$4.75</td> 
    </tr> 
</tbody> 

/* 
    CSS for Lounge Project 
    Filename: styles.css 

    Author: Justus Self 
    Date:  3/21/17 
    HTML5 and CSS3 Illustrated Unit I, Lounge Independent Project 
*/ 

/*Reset Styles*/ 
html, body{ 
    font-size: 14px; 
    width: 100%; 
    } 
a, body, footer, header, h1, h2, h3, img, li, nav, p, ul, table, tbody, td, tfoot, th, thead, tr{ 
    border: 0; 
    padding: 0; 
    margin: 0; 
} 
img { 
    max-width: 100%; 
    height: auto; 
    width: auto; 
} 

/*Drink font colors*/ 

h2.green { 
    color: green; 
} 
h2.blue { 
    color: blue; 
} 
h2.purple { 
    color: purple; 
} 
h2.red { 
    color: red; 
} 
h2.yellow { 
    color: gold; 
} 


/*center and border image*/ 

img.smlounge { 
    display: block; 
    margin-left: auto; 
    margin-right: auto; 
    border: 3px solid; 
    border-color: red; 
} 


/*Table styles*/ 

td, th, td td { 
    border: 1px solid black; 
    font-size: 1.3em; 
    margin-left: auto; 
    margin-right: auto; 
    text-align: center; 
    padding: 0 1em ; 
} 
table { 
    border-collapse: collapse; 
    margin: 0 auto; 
    width:100% 
} 
table table{ 
    margin:1%; 
    width:98%; 

} 
table table td{ 
    width:50%; 
} 
table table tr, table table tbody{ 
    width:100% 
} 
.nested{padding:0;} 
td[colspan="2"] { 
    width: 20%; 
} 
.drink-name { 
    text-align: left; 
} 
+0

當我嘗試這樣做時,會發生這種情況。這個例子還是不盡相同。 – SkryBlackfall

+0

我不想只是「爲你做」,因爲它看起來像你應該學習它。使用該表結構,但添加更多的嵌套CSS樣式,使其正確。所以你需要table td table td來定位嵌套表格中的單元格 – garek007

+0

如果你告訴我如何去做,它不會爲我做。你能告訴我我應該改變什麼嗎?邊界?字體大小?我自己完成了這個項目的其餘部分,我只是掛在這最後一件小事上。 – SkryBlackfall

1

嵌套這樣

/* 
 
    CSS for Lounge Project 
 
    Filename: styles.css 
 
    
 
    Author: Justus Self 
 
    Date:  3/21/17 
 
    HTML5 and CSS3 Illustrated Unit I, Lounge Independent Project 
 
*/ 
 

 

 
/*Reset Styles*/ 
 

 
html, 
 
body { 
 
    font-size: 14px; 
 
    width: 100%; 
 
} 
 

 
a, 
 
body, 
 
footer, 
 
header, 
 
h1, 
 
h2, 
 
h3, 
 
img, 
 
li, 
 
nav, 
 
p, 
 
ul, 
 
table, 
 
tbody, 
 
td, 
 
tfoot, 
 
th, 
 
thead, 
 
tr { 
 
    border: 0; 
 
    padding: 0; 
 
    margin: 0; 
 
} 
 

 
img { 
 
    max-width: 100%; 
 
    height: auto; 
 
    width: auto; 
 
} 
 

 

 
/*Drink font colors*/ 
 

 
h2.green { 
 
    color: green; 
 
} 
 

 
h2.blue { 
 
    color: blue; 
 
} 
 

 
h2.purple { 
 
    color: purple; 
 
} 
 

 
h2.red { 
 
    color: red; 
 
} 
 

 
h2.yellow { 
 
    color: gold; 
 
} 
 

 

 
/*center and border image*/ 
 

 
img.smlounge { 
 
    display: block; 
 
    margin-left: auto; 
 
    margin-right: auto; 
 
    border: 3px solid; 
 
    border-color: red; 
 
} 
 

 

 
/*Table styles*/ 
 

 
td, 
 
th { 
 
    border: 1px solid black; 
 
    font-size: 1.3em; 
 
    margin-left: auto; 
 
    margin-right: auto; 
 
    text-align: center; 
 
    padding: 0 1em; 
 
} 
 

 
table { 
 
    border-collapse: collapse; 
 
    margin: 0 auto; 
 
} 
 

 
td[colspan="2"] { 
 
    width: 20%; 
 
} 
 

 
.drink-name { 
 
    text-align: left; 
 
} 
 

 
.mintable { 
 
    border-collapse: initial; 
 
    font-size: 0.9em; 
 
} 
 

 
tbody { 
 
    border: solid black 1px 
 
}
<!DOCTYPE html> 
 
<html lang="en"> 
 

 
<head> 
 
    <meta charset="utf-8" /> 
 
    <title>Head First Lounge Elixirs</title> 
 

 
    <!-- 
 
    Elixir page for Lounge Project 
 
    Filename: elixir.html 
 
    
 
    Author: Justus Self 
 
    Date:  3/21/17 
 
    HTML5 and CSS3 Illustrated Unit I, Lounge Independent Project 
 
    --> 
 

 
    <link type="text/css" rel="stylesheet" href="../lounge.css" /> 
 
</head> 
 

 
<body> 
 
    <h1>Our Elixirs</h1> 
 

 
    <h2 class="green">Green Tea Cooler</h2> 
 
    <p> 
 
    <img src="../images/green.jpg" alt="Green Tea Cooler" /> Chock full of vitamins and minerals, this elixir combines the healthful benefits of green tea with a twist of chamomile blossoms and ginger root. 
 
    </p> 
 
    <hr/> 
 
    <h2 class="blue">Raspberry Ice Concentration</h2> 
 
    <p> 
 
    <img src="../images/lightblue.jpg" alt="Rasberry Ice Concentration" /> Combining raspberry juice with lemon grass, citrus peel and rosehips, this icy drink will make your mind feel clear and crisp. 
 
    </p> 
 
    <hr/> 
 
    <h2 class="purple">Blueberry Bliss Elixir</h2> 
 
    <p> 
 
    <img src="../images/blue.jpg" alt="Blueberry Bliss Elixir" /> Blueberries and cherry essence mixed into a base of elderflower herb tea will put you in a relaxed state of bliss in no time. 
 
    </p> 
 
    <hr/> 
 
    <h2 class="red">Cranberry Antioxidant Blast</h2> 
 
    <p> 
 
    <img src="../images/red.jpg" alt="Cranberry Antioxidant Blast" /> Wake up to the flavors of cranberry and hibiscus in this vitamin C rich elixir. 
 
    </p> 
 
    <hr/> 
 
    <h2 class="yellow"><a name="Yellow">Lemon Breeze</a></h2> 
 
    <p><img src="../images/yellow.gif" alt="Lemon Breeze Drink" /> The ultimate healthy drink, this elixir combines herbal botanicals, minerals, and vitamins with a twist of lemon into a smooth citrus wonder that will keep your immune system going all day 
 
    and all night. 
 
    </p> 
 
    <hr/> 
 
    <br/> 
 
    <table> 
 
    <thead> 
 
     <tr> 
 
     <th colspan="6">Our Drink Prices</th> 
 
     </tr> 
 
     <tr> 
 
     <th colspan="2">Drink</th> 
 
     <th colspan="2">Size</th> 
 
     <th colspan="2">Price</th> 
 
     </tr> 
 
    </thead> 
 
    <tbody> 
 
     <tr> 
 
     <td rowspan="2" colspan="2" class="drink-name">Green Tea Cooler</td> 
 
     <td colspan="2">16 oz.</td> 
 
     <td colspan="2">$3.75</td> 
 
     </tr> 
 
     <tr> 
 
     <td colspan="2">24 oz.</td> 
 
     <td colspan="2">$4.75</td> 
 
     </tr> 
 
     <tr> 
 
     <td rowspan="2" colspan="2" class="drink-name">Raspberry Ice Concentration</td> 
 
     <td colspan="2">16 oz.</td> 
 
     <td colspan="2">$3.75</td> 
 
     </tr> 
 
     <tr> 
 
     <td colspan="2">24 oz.</td> 
 
     <td colspan="2">$4.75</td> 
 
     </tr> 
 
     <tr> 
 
     <td colspan="2" class="drink-name">Cranberry Antioxidant Blast</td> 
 
     <td colspan="2">20 oz.</td> 
 
     <td colspan="2">$4.75</td> 
 
     </tr> 
 
     <tr> 
 
     <td rowspan="2" colspan="2" class="drink-name">Lemon Breeze</td> 
 
     <td rowspan="2" colspan="2"> 
 
      <table class="mintable"> 
 
      <tr> 
 
       <td>Iced</td> 
 
       <td>16 oz.</td> 
 
      </tr> 
 
      <tr> 
 
       <td>Frozen</td> 
 
       <td>20 oz.</td> 
 

 
      </tr> 
 

 
      </table> 
 

 
     </td> 
 
     <td> 
 
      <table class="mintable"> 
 
      <tr rowspan="2" colspan="2"> 
 
       <td>$4.75</td> 
 
      </tr> 
 
      <tr> 
 
       <td>$3.75</td> 
 
      </tr> 
 
      </table> 
 

 
     </td> 
 
     </tr> 
 
     <td></td> 
 
    </tbody> 
 
    </table> 
 

 
    <br> 
 
    <p><a href="../lounge.html">Back to the Lounge</a></p> 
 

 
    <footer> 
 
    &#169;2016, Head First Online Lounge<br /> All trademarks and registered trademarks appearing on this site are the property of their respective owners. 
 
    </footer> 
 
</body> 
 

 
</html>

+0

它很接近,但邊框仍然與圖片中的不一樣。 – SkryBlackfall