2011-10-12 26 views
0

我是新來的HTML(我在Android上工作)。我嘗試創建一個網頁。我的網頁包含h2(標題),表格,圖片,編輯文字和按鈕。其中一個h2位於網頁的左側,另一個h2位於網頁的右側。我正在使用以下代碼:如何在HTML中設置h2和按鈕的邊距?

<html> 
<head> 
<style type="text/css"> 
body{ 
    background-color:#d0e4fe; 
} 
h2{ 
    color:orange; 
    text-align:left; 
} 
h2{ 
    color:orange; 
    text-align:right; 
} 
p{ 
font-family:"Times New Roman"; 
font-size-size:18px; 
} 
</style> 
</head> 

<title>VacantTable And Waiter Status</title> 
</head> 
<h2>Allot Table Number</h2> 
<h2>Table status style</h21> 
</html> 

但它顯示在網頁的右側。我能夠顯示錶格,但是如何在表格的底部顯示一個按鈕?請幫幫我。

+0

有你代碼中有很多錯誤。首先,您在h2上應用css,並且您沒有爲每個h2指定不同的類。所以它會一直使用第二個CSS來表示h2,即text-align:right ;. –

+0

您無法爲同一個標記編寫不同的CSS。通過定義,它會覆蓋前一個。相反,你可以將它包裝在一個'div'中,並在該div中指定'h2'的屬性。 – Mahesh

+0

請提供示例代碼設置邊距 – suresh

回答

0

有申請樣式很多方法,你可以試試這個:

<html> 
<head> 
<title>VacantTable And Waiter Status</title> 
<style type="text/css"> 
body{ 
    background-color:#d0e4fe; 
} 

div.leftH2{ 
    color:orange; 
    text-align:left; 
    float: right; 
} 

div.rightH2{ 
    color:orange; 
    text-align:right; 
    float: left; 
} 

p{ 
font-family:"Times New Roman"; 
font-size-size:18px; 
} 
</style> 
</head> 

<body> 
    <div class="leftH2"> 
     <h2>Allot Table Number</h2> 
    </div> 
    <div class="rightH2"> 
     <h2>Table status style</h2> 
    </div> 
</body> 
</html> 
+0

對於按鈕,在其他TR下創建另一個TR並向其添加按鈕標記。舉個例子,假設你有兩列表:​​​​ – Coder

0

你應該使用id(通常每頁唯一)或類(非唯一的每頁)來玩。這樣您就可以區分共享相同標籤的兩個部分。在the CSS section at W3schools

的sanest選項

更多信息在這裏似乎:

<html> 
<head> 
<style type="text/css"> 
body{ 
    background-color:#d0e4fe; 
} 
h2{ 
    color:orange; 
} 
h2.right{ 
    text-align:right; 
} 
h2.left{ 
    text-align:left; 
} 

p{ 
font-family:"Times New Roman"; 
font-size-size:18px; 
} 
</style> 
</head> 

<title>VacantTable And Waiter Status</title> 
</head> 
<h2 class="left">Allot Table Number</h2> 
<h2 class="right">Table status style</h21> 
</html> 
+0

有比w3schools更好的來源。退房http://w3fools.com/ – 2011-10-12 06:21:33

+0

我能夠顯示錶格,但如何顯示table.pls底部的按鈕幫助我? – suresh

1

做一兩件事給每個H2不同類別和不同應用CSS給他們。

<div class="main_container"> 
<h2 class="left_heading">Allot Table Number</h2> 
<h2 class="right_heading">Table status style</h2> 
</div> 

以上是對你的CSS寫您的html.Then:

h2.left_heading{ 
    color:orange; 
    float:left; 
} 
h2.right_heading{ 
color:orange; 
float:right; 
} 
+0

我可以顯示錶格,但是如何在table.pls底部顯示按鈕幫助我? – suresh

+1

請提供完整的html,以便我可以提供幫助。或者把你的代碼放入jsfiddle.net並在這裏粘貼鏈接。這樣我們可以提供幫助。我們需要完整的信息。 –

0

我看到的代碼的兩個主要問題,因爲你擁有它。首先,在實際的HTML中,第二個h2標籤以</h21>而不是</h2>關閉。其次,你似乎對CSS的工作原理有些誤解。當你有兩個'重疊'的規則時,當標籤使用它們時,它們會被合併。當兩個規則對同一個屬性具有不同的值時,CSS有幾個技巧來決定保留哪個規則的屬性。

第一個訣竅是規則的「權重」,它只是ids數量,然後是類別數量,然後是規則中的HTML標籤數量。第二個技巧是!important屬性,它告訴count這個規則沒有加權。第三招是你在這裏遇到的那個。如果兩個規則具有相同的權重,則最後定義的優先級應該優先,這意味着在這裏,您的兩個h2標籤將右對齊。你可以在這裏尋找更多關於CSS precedence的信息。

無論如何,你可能想要做的是爲你的h2標籤創建不同的類,這樣每個類都有一個類,使它的屬性與標準h2標籤不同。這可能是這樣的:

CSS:

h2 { 
    color:orange; 
} 
/* This .right is a class that can be included on any tag, no matter what type, 
    and then that tag will inherit all these properties. */ 
.right { 
    text-align: right; 
} 
.left { 
    text-align: left; 
} 

HTML:

<h2 class="right">Allot Table Number</h2> 
<h2 class="left">Table status style</h2>