让我们使用两个媒体查询来根据屏幕的宽度动态处理表格和图像的布局。当屏幕宽度大于 500 像素时,图像将浮动在表格左侧;当屏幕宽度小于 500 像素时,图像将浮动在表格上方,
此示例假设有一个 jpg 图像“image.jpg”与 HTML 文件位于同一文件夹中。您可以将图像标签的 src 属性更改为图像的实际路径。
@media screen and(min - width: 500 px) {
th,
td {
font - size: 16 px;
}
img {
float: left;
margin - right: 20 px;
}
}
@media screen and(max - width: 499 px) {
th,
td {
font - size: 10 px; /* Adjust font size for smaller screens */
}
img {
display: block;
margin - bottom: 40 px;
}
}
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 30px;
}
th,
td {
border: 2px solid #ccc;
padding: 6px;
text-align: left;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Responsive Table</title>
</head>
<body>
<!-- Image for screens 500px and larger -->
<img src="image.jpg"
alt="Description of your image">
<table>
<thead>
<tr>
<th>Heading1</th>
<th>Heading2</th>
</tr>
</thead>
<tbody>
<tr>
<td>TableData1</td>
<td>TableData2</td>
</tr>
</tbody>
</table>
</body>
</html>