CSS 功能区无法正确缩放
CSS ribbon doesnt scale properly
我尝试创建响应式 CSS 功能区时遇到问题。我无法让功能区针对移动设备正确缩小。 (见屏幕)
.ribbon {
margin: 10px auto;
padding: 0 10px 0;
position: relative;
color: #444;
background: #fff;
border: 1px solid #d2d2d2;
border-radius: 3px;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}
.ribbon h3 {
display: block;
height: 30px;
max-width: 102%;
line-height: 1.3;
width: 724px;
margin: 0;
padding: 5px 10px;
position: relative;
left: -16px;
top: 8px;
color: #fff;
text-shadow: 0 1px 1px #111;
border-top: 1px solid #922d38;
border-bottom: 1px solid #922d38;
background: #922d38;
background: linear-gradient(top, #383838 0%, #262626 100%);
border-radius: 2px 2px 0 0;
box-shadow: 0 1px 2px rgba(0,0,0,0.3);
}
.ribbon h3::before,
.ribbon h3::after {
content: '';
display: block;
width: 0;
height: 0;
position: absolute;
bottom: -11px;
z-index: -10;
border: 5px solid;
border-color: #242424 transparent transparent transparent;
}
.ribbon h3::before {left: 0;}
.ribbon h3::after {right: 0;}
有人有什么建议吗? Link: http://www.welzendesign.com/drielandenpunt/dagje-drielandenpunt/herberg/
将最大宽度设置为 104%
.ribbon h3 { max-width: 104%; }
我在您的 url 中进行测试,它有效
编辑
我建议您为此目的进行媒体查询,因为 104% 不是正确的值,仅适用于小屏幕,但在大屏幕上,功能区会向右扩展很多。
祝你好运
将.ribbon h3
更改为:
width: calc(100% + 12px);
right: -16px;
同时删除 max-width
如果能够添加就更好了:
box-sizing: border-box;
width: calc(100% + 12px);
因为这比您做的更有意义 16*2
但我不建议在 1 个元素上使用 border-box
,其余元素为 content-box
这里的技巧是将 <h3>
延伸到 .ribbon
的边缘一个已知的量。您可以使用 position: absolute,并将左右属性设置为负值来执行此操作。然后,你也可以将你的伪元素三角形左右放置 - 然后,无论盒子有多宽,丝带总是伸出正确的量。
这是代码(我已经简化以仅展示技术 - 您应该能够很容易地添加您的装饰样式)。我为色带悬垂的 'size' 使用了 0.3em 的值,因此您可以看到 0.3em 和 -0.3em 的各种组合 - 您可以将其放大或缩小以适合。
.ribbon {
border: 1px solid #CCC;
margin: 2em;
min-height: 20em;
position: relative;
}
.ribbon h3 {
background: red;
color: #FFF;
height: 3em;
left: -0.3em;
line-height: 3em;
position: absolute;
right: -0.3em;
text-align: center;
top: 1em;
}
.ribbon h3::before,
.ribbon h3::after {
border-color: transparent;
border-style: solid;
bottom: -0.3em;
content: '';
display: block;
height: 0;
position: absolute;
width: 0;
}
.ribbon h3::before {
border-right-color: black;
border-width: 0 0.3em 0.3em 0;
left: 0;
}
.ribbon h3::after {
border-top-color: black;
border-width: 0.3em 0.3em 0 0;
right: 0;
}
我尝试创建响应式 CSS 功能区时遇到问题。我无法让功能区针对移动设备正确缩小。 (见屏幕)
.ribbon {
margin: 10px auto;
padding: 0 10px 0;
position: relative;
color: #444;
background: #fff;
border: 1px solid #d2d2d2;
border-radius: 3px;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}
.ribbon h3 {
display: block;
height: 30px;
max-width: 102%;
line-height: 1.3;
width: 724px;
margin: 0;
padding: 5px 10px;
position: relative;
left: -16px;
top: 8px;
color: #fff;
text-shadow: 0 1px 1px #111;
border-top: 1px solid #922d38;
border-bottom: 1px solid #922d38;
background: #922d38;
background: linear-gradient(top, #383838 0%, #262626 100%);
border-radius: 2px 2px 0 0;
box-shadow: 0 1px 2px rgba(0,0,0,0.3);
}
.ribbon h3::before,
.ribbon h3::after {
content: '';
display: block;
width: 0;
height: 0;
position: absolute;
bottom: -11px;
z-index: -10;
border: 5px solid;
border-color: #242424 transparent transparent transparent;
}
.ribbon h3::before {left: 0;}
.ribbon h3::after {right: 0;}
有人有什么建议吗? Link: http://www.welzendesign.com/drielandenpunt/dagje-drielandenpunt/herberg/
将最大宽度设置为 104%
.ribbon h3 { max-width: 104%; }
我在您的 url 中进行测试,它有效
编辑
我建议您为此目的进行媒体查询,因为 104% 不是正确的值,仅适用于小屏幕,但在大屏幕上,功能区会向右扩展很多。
祝你好运
将.ribbon h3
更改为:
width: calc(100% + 12px);
right: -16px;
同时删除 max-width
如果能够添加就更好了:
box-sizing: border-box;
width: calc(100% + 12px);
因为这比您做的更有意义 16*2
但我不建议在 1 个元素上使用 border-box
,其余元素为 content-box
这里的技巧是将 <h3>
延伸到 .ribbon
的边缘一个已知的量。您可以使用 position: absolute,并将左右属性设置为负值来执行此操作。然后,你也可以将你的伪元素三角形左右放置 - 然后,无论盒子有多宽,丝带总是伸出正确的量。
这是代码(我已经简化以仅展示技术 - 您应该能够很容易地添加您的装饰样式)。我为色带悬垂的 'size' 使用了 0.3em 的值,因此您可以看到 0.3em 和 -0.3em 的各种组合 - 您可以将其放大或缩小以适合。
.ribbon {
border: 1px solid #CCC;
margin: 2em;
min-height: 20em;
position: relative;
}
.ribbon h3 {
background: red;
color: #FFF;
height: 3em;
left: -0.3em;
line-height: 3em;
position: absolute;
right: -0.3em;
text-align: center;
top: 1em;
}
.ribbon h3::before,
.ribbon h3::after {
border-color: transparent;
border-style: solid;
bottom: -0.3em;
content: '';
display: block;
height: 0;
position: absolute;
width: 0;
}
.ribbon h3::before {
border-right-color: black;
border-width: 0 0.3em 0.3em 0;
left: 0;
}
.ribbon h3::after {
border-top-color: black;
border-width: 0.3em 0.3em 0 0;
right: 0;
}