我创建了以下HTML页面,然后将两个DIV元素(lista
div中包含具有scheda
和parent
id的元素)对齐到页面顶部的相同位置。这是我的代码:
#tabella1,
#tabella1 td {
border: solid 1px;
}
th {
border: solid 1px;
font-style: bold;
background-color: white;
}
#tabella1 tr:nth-child(odd) {
background-color: gray;
}
#tabella1 tr:nth-child(even) {
background-color: lightgray;
}
#tabella1 tr:nth-child(n):not(:nth-child(1)):hover {
background-color: blue;
color: yellow;
cursor: pointer;
}
.titolo {
text-align: center;
font-weight: bold;
}
#lista {
display: inline-block;
margin: 30px;
position: relative;
top: 0px;
}
button {
width: 60px;
height: 20px;
}
form {
display: inline-block;
}
#msg {
font-weight: bold;
}
#scheda {
display: inline-block;
position: relative;
top: 0px;
text-align: center;
}
#tabella2 td:nth-child(even) {
border: solid 1px;
}
.cellaVuota {
width: 140px;
}
<div id="carica">
<form method="post" action="carica2.php">
<label for "n">Inserisci nome:
<input type="text" id="n" name="nome" size="10" />
</label>
<button type="submit">Invia</button>
</form>
<form method="post" action="carica2.php">
<button type="submit" name="logout">Logout</button>
</form>
</div>
<div id="parent">
<div id="lista">
<table id="tabella1">
<tr>
<th>Modello</th>
<th>27.5</th>
<th>29</th>
</tr>
<tr>
<td>Riga 1</td>
<td>Riga 2</td>
<td>Riga 3</td>
</tr>
</table>
<p class="titolo">Bici Mountain Bike</p>
</div>
<div id="scheda">
<p class="titolo">Bici selezionata</p>
<table id="tabella2">
<tr>
<td>Modello</td>
<td class="cellaVuota"></td>
</tr>
<tr>
<td>Misura 27.5</td>
<td class="cellaVuota"></td>
</tr>
<tr>
<td>Misura 29</td>
<td class="cellaVuota"></td>
</tr>
</table>
</div>
</div>
在发布此问题之前,我在这里进行了搜索和搜索(请参阅CSS代码中的position
和〜top`属性,但我不知道如何达到目的。
只需将vertical-align: top;
添加到两个div。您也可以删除position: relative;
和top: 0;
属性
将float: left
属性添加到CSS的#lista块中。