我应该如何改变CSS,
如果width of screen
> 1100px
然后left nav
宽度始终为555px
,但empty -space
= width of screen
1100px
缩小为0
如果width of screen
< 1100px
然后left nav
宽度正在缩小,empty -space
div仍为0px。
右侧部分(div class=button
和div class=right
)保持不变
.wrapper {
display: flex;
margin: 0 auto;
max-width: 1310px;
font-size:0.6rem;
}
.wrapper .left {
flex-basis: 555px;
background-color: lightblue;
}
.left__nav {
display: inline-block;
padding: 5px;
color: darkblue;
text-decoration: none;
}
.empty-space {
flex-basis: 100px;
flex-shrink:1;
}
.wrapper .right {
background-color: orange;
flex-basis: 462px;
display: flex;
justify-content: flex-end; /* align right (at end) */
}
.wrapper .button {
flex-basis: 193px;
background-color: #3ab8a4;
text-align:center;
}
/* style for this demo */
.wrapper > div {
box-sizing: border-box;
background-color: lightgray;
border-collapse: collapse;
}
.mark-center {
text-align: center;
font-size: 30px;
color: red;
}
.container-small {
text-align: center;
background-color: lightgray;
max-width: 1100px;
margin: auto;
padding:1rem 0;
}

<div class="wrapper">
<div class="empty-space">empty space
</div>
<div class="left">
<a class="left__nav" href="#">Link3</a>
Left Nav
</div>
<div class="right">
Links right aligned
</div>
<div class="button">
Button
</div>
</div>
<div class="mark-center">
↑
</div>
<div class="container-small">
Some content
</div>
&#13;
答案 0 :(得分:1)
最简单的方法是添加2个包装器,一个用于left
侧,另一个用于right
侧,并且您可以轻松地居中左/右链接包装器相遇的地方。
删除标记/ CSS中的<div class="empty-space">empty space</div>
元素,然后在left
侧的左侧创建空格,只需添加margin-left: auto
到left_inner
,它将被推到右边。
Stack snippet
.wrapper {
display: flex;
margin: 0 auto;
max-width: 1310px;
font-size: 0.6rem;
}
.wrapper .left,
.wrapper .right {
flex-basis: 50%;
display: flex;
}
.wrapper .left_inner {
margin-left: auto;
flex-basis: 555px;
background-color: lightblue;
}
.wrapper .right_inner {
flex-basis: 462px;
background-color: orange;
display: flex;
justify-content: flex-end; /* align content right (at end) */
}
.wrapper .button_inner {
flex-basis: 193px;
background-color: #3ab8a4;
display: flex;
justify-content: center;
align-items: center;
}
.left__nav {
display: inline-block;
padding: 5px;
color: darkblue;
text-decoration: none;
}
.right__nav {
display: inline-block;
padding: 5px;
color: darkred;
text-decoration: none;
}
/* style for this demo */
.wrapper>div {
box-sizing: border-box;
background-color: lightgray;
border-collapse: collapse;
}
.mark-center {
text-align: center;
font-size: 30px;
color: red;
}
.container-small {
text-align: center;
background-color: lightgray;
max-width: 1100px;
margin: auto;
padding: 1rem 0;
}
&#13;
<div class="wrapper">
<div class="left">
<div class="left_inner">
<a class="left__nav" href="#">Link left</a>
</div>
</div>
<div class="right">
<div class="right_inner">
<a class="right__nav" href="#">Link right</a>
</div>
<div class="button_inner">
Button
</div>
</div>
</div>
<div class="mark-center">
↑
</div>
<div class="container-small">
Some content
</div>
&#13;
更新
为了能够满足上述两个要求并将它们垂直,居中,在较窄的屏幕上叠加,您需要媒体查询或脚本。
以下是使用小于1000像素(max-width: 999px
)
Stack snippet
.wrapper {
display: flex;
margin: 0 auto;
max-width: 1310px;
font-size: 0.6rem;
}
.wrapper .left,
.wrapper .right {
flex-basis: 50%;
display: flex;
}
.wrapper .left_inner {
margin-left: auto;
flex-basis: 555px;
background-color: lightblue;
}
.wrapper .right_inner {
flex-basis: 462px;
background-color: orange;
display: flex;
justify-content: flex-end; /* align content right (at end) */
}
.wrapper .button_inner {
flex-basis: 193px;
background-color: #3ab8a4;
display: flex;
justify-content: center;
align-items: center;
}
.left__nav {
display: inline-block;
padding: 5px;
color: darkblue;
text-decoration: none;
}
.right__nav {
display: inline-block;
padding: 5px;
color: darkred;
text-decoration: none;
}
@media (max-width: 999px) {
.wrapper,
.wrapper .left,
.wrapper .right {
flex-basis: auto;
flex-direction: column;
align-items: center;
}
.wrapper .left_inner {
flex-basis: auto;
margin-left: 0;
}
.wrapper .right_inner,
.wrapper .button_inner {
flex-basis: auto;
}
}
/* style for this demo */
.wrapper>div {
box-sizing: border-box;
background-color: lightgray;
border-collapse: collapse;
}
.mark-center {
text-align: center;
font-size: 30px;
color: red;
}
.container-small {
text-align: center;
background-color: lightgray;
max-width: 1100px;
margin: auto;
padding: 1rem 0;
}
&#13;
<div class="wrapper">
<div class="left">
<div class="left_inner">
<a class="left__nav" href="#">Link left</a>
</div>
</div>
<div class="right">
<div class="right_inner">
<a class="right__nav" href="#">Link right</a>
</div>
<div class="button_inner">
Button
</div>
</div>
</div>
<div class="mark-center">
↑
</div>
<div class="container-small">
Some content
</div>
&#13;
如果您希望链接和按钮包装器填充宽度,请为它们指定宽度并使其内容居中对齐。
Stack snippet
.wrapper {
display: flex;
margin: 0 auto;
max-width: 1310px;
font-size: 0.6rem;
}
.wrapper .left,
.wrapper .right {
flex-basis: 50%;
display: flex;
}
.wrapper .left_inner {
margin-left: auto;
flex-basis: 555px;
background-color: lightblue;
}
.wrapper .right_inner {
flex-basis: 462px;
background-color: orange;
display: flex;
justify-content: flex-end; /* align content right (at end) */
}
.wrapper .button_inner {
flex-basis: 193px;
background-color: #3ab8a4;
display: flex;
justify-content: center;
align-items: center;
}
.left__nav {
display: inline-block;
padding: 5px;
color: darkblue;
text-decoration: none;
}
.right__nav {
display: inline-block;
padding: 5px;
color: darkred;
text-decoration: none;
}
@media (max-width: 999px) {
.wrapper,
.wrapper .left,
.wrapper .right {
flex-basis: auto;
flex-direction: column;
justify-content: center;
align-items: center;
width: 100%;
}
.wrapper .left_inner {
flex-basis: auto;
margin-left: 0;
text-align: center;
width: 100%;
}
.wrapper .right_inner,
.wrapper .button_inner {
flex-basis: auto;
text-align: center;
justify-content: center;
width: 100%;
}
}
/* style for this demo */
.wrapper>div {
box-sizing: border-box;
background-color: lightgray;
border-collapse: collapse;
}
.mark-center {
text-align: center;
font-size: 30px;
color: red;
}
.container-small {
text-align: center;
background-color: lightgray;
max-width: 1100px;
margin: auto;
padding: 1rem 0;
}
&#13;
<div class="wrapper">
<div class="left">
<div class="left_inner">
<a class="left__nav" href="#">Link left</a>
</div>
</div>
<div class="right">
<div class="right_inner">
<a class="right__nav" href="#">Link right</a>
</div>
<div class="button_inner">
Button
</div>
</div>
</div>
<div class="mark-center">
↑
</div>
<div class="container-small">
Some content
</div>
&#13;
答案 1 :(得分:0)
尝试使用媒体查询:
@media only screen and (max-width: 1100px) {
/*your css code */
}
@media only screen and (min-width: 1100px) {
/*your css code */
}
这里的w3c学校关于媒体查询的链接。