Reputation:
I currently have the following:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hotel Occupancy</title>
</head>
<body>
<c:forEach begin="1" end="${noHotels}" varStatus="loop">
<h1 align="center">${hotelLoc[loop.index - 1]}</h1>
<form action='cartController' method='POST'>
<table style="width:1000px" style="text-align:center">
<tr style="text-align:center">
<th>Room Type</th>
<th>Occupied</th>
<th>Available</th>
</tr>
<c:forEach var="item" items="${hotelOccupancy}">
<c:if test="${item.hotelID == loop.index}">
<tr style="text-align:center">
<td>${item.roomType}</td>
<td>${item.occupied}</td>
<td>${item.available}</td>
</tr>
</c:if>
</c:forEach>
</table>
</form>
</c:forEach>
The output is as follows:
Could someone help me align the table properly?
Upvotes: 0
Views: 5230
Reputation: 5968
Your problem is that you have:
h1
) that takes up the full width of the body
. The text of this title is center-aligned, so it appears at the center of the body
.A possible solution is to place both the title and table inside a div
. Set the table width to 100%, then set the width of the div
to whatever you want. I've put up a fiddle to illustrate this approach.
However, I wouldn't recommend setting fixed width to any of your elements unless absolutely necessary, since this will result in a more rigid layout. The layout you provided, for example, will not suit anyone with a screen resolution below 1000 px.
tr, h2 {
text-align:center;
}
table {
background-color: #FFFFCC;
}
h2 {
background-color: #FFCCFF;
}
div {
border : 1px solid lightgray;
}
<h2>Title</h2>
<table style="width:300px">
<thead>
<tr>
<th>A</th>
<th>B</th>
<th>C</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>2</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>3</td>
<td>3</td>
</tr>
</tbody>
</table>
<hr>
<div style="width:300px;">
<h2>Title</h2>
<table style="width:100%;">
<thead>
<tr>
<th>A</th>
<th>B</th>
<th>C</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>2</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>3</td>
<td>3</td>
</tr>
</tbody>
</table>
</div>
Upvotes: 1