低于 767,我希望中间列日期、截止日期和 id 应该在另一列下方堆叠(截止日期、日期、然后是 id 或日期、截止日期和 id 其中到期日期和 id 在中间的日期列下方),这意味着应该有 3 列,而不是宽度高于 767 的五列。 如何使用 css 实现这一点。如果有更好的东西可以使其响应,请分享。
我是否应该为移动设备创建一个单独的行并将其隐藏在较大的设备上。 或者可以使用同一行,并使中间的三个div一个接一个地堆叠。
@media only screen and (max-width: 767px) {
}
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<style type="text/css">
.container {
width: 80%;
margin: 0 auto;
border: 1px solid grey;
height: 100%;
}
.invo-to,.due,.date,.id {
float: left;
}
.invo-from {
float: right;
text-align: right;
}
.invo-to {
width: 25%;
}
.due,.date,.id {
width: 16%;
}
.invo-from {
width: 25%;
}
h6 {
font-size: 18px;
margin: 0;
}
p {
font-size: 16px;
}
</style>
<body>
<div class="container">
<div class="invo-to">
<h6>Invoice To</h6>
<p>John Mason</p>
<p>john@gmail.com</p>
</div>
<div class="due">
<h6>Due Date</h6>
<p>11-05-1990</p>
</div>
<div class="date">
<h6>Date</h6>
<p>11-05-1990</p>
</div>
<div class="id">
<h6>Invoice Id</h6>
<p>66</p>
</div>
<div class="invo-from">
<h6>Invoice From</h6>
<p>Xskd Lksdds di LTD</p>
<p>lksdds@gmail.com</p>
</div>
</div>
</body>
</html>
替换此代码
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<style type="text/css">
.container {
width: 80%;
margin: 0 auto;
border: 1px solid grey;
padding: 15px;
}
.details {
display: flex;
}
.invo-to {
width: 15%;
}
.invo-from {
width: 20%;
}
.due_date_id {
width: 65%;
display: flex;
justify-content: space-around;
}
h6 {
font-size: 18px;
margin: 0;
}
p {
font-size: 16px;
}
@media screen and (max-width: 767px) {
.container {
width:100%;
}
.due_date_id {
width: 33%;
display: flex;
flex-direction: column;
}
.invo-to {
width: 33%;
}
.invo-from {
width: 33%;
}
}
</style>
<body>
<div class="container">
<div class="details">
<div class="invo-to">
<h6>Invoice To</h6>
<p>John Mason</p>
<p>john@gmail.com</p>
</div>
<div class="due_date_id">
<div class="due">
<h6>Due Date</h6>
<p>11-05-1990</p>
</div>
<div class="date">
<h6>Date</h6>
<p>11-05-1990</p>
</div>
<div class="id">
<h6>Invoice Id</h6>
<p>66</p>
</div>
</div>
<div class="invo-from">
<h6>Invoice From</h6>
<p>Xskd Lksdds di LTD</p>
<p>lksdds@gmail.com</p>
</div>
</div>
</div>
</body>
</html>
为了创建一个响应式网页,最好使用bootstrap,这是更容易和更快的方法,但是如果你暂时不能学习bootstrap,你可以使用flexbox和媒体查询。
- 弹性框使您能够在不使用浮点的情况下创建响应式布局 属性,建议学习它,因为它非常强大,在这种情况下,您将需要 2 个属性,
display:flex;
启用 flexbox 和flex-direction:row
或flex-direction: column;
来设置 Flexbox 内部块的方向 https://www.w3schools.com/css/css3_flexbox.asp - 媒体查询以定义特定条件下的css属性,它可以用于不同类型的屏幕,此工具对于创建响应式网站非常有用,要启用它,您将需要
@media condition
条件可以是屏幕宽度或屏幕类型...https://www.w3schools.com/css/css_rwd_mediaqueries.asp
下面的代码将更好地解释它:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<style type="text/css">
.container {
width: 90%;
border: 1px solid grey;
margin: 0 auto;
height: 100%;
display:flex;
flex-direction: row;
justify-content: space-between;
}
.invo-from {
padding-right:15px;
text-align: right;
width: 30%;
}
.invo-to {
width: 30%;
padding-left:15px;
}
.due,.date,.id {
width: 100%;
}
h6 {
font-size: 18px;
margin: 0;
}
p {
font-size: 16px;
}
.stacked{
width: 50%;
display:flex;
flex-direction: row;
}
@media screen and (max-width: 767px) {
.stacked{
width: 40%;
text-align:center;
flex-direction: column;
}
}
</style>
<body>
<div class="container">
<div class="invo-to">
<h6>Invoice To</h6>
<p>John Mason</p>
<p>john@gmail.com</p>
</div>
<div class="stacked">
<div class="due">
<h6>Due Date</h6>
<p>11-05-1990</p>
</div>
<div class="date">
<h6>Date</h6>
<p>11-05-1990</p>
</div>
<div class="id">
<h6>Invoice Id</h6>
<p>66</p>
</div>
</div>
<div class="invo-from">
<h6>Invoice From</h6>
<p>Xskd Lksdds di LTD</p>
<p>lksdds@gmail.com</p>
</div>
</div>
</body>
</html>