[英]How to make a header ribbon responsive
I have the following JSFiddle: http://jsfiddle.net/eotamvwy/ 我有以下JSFiddle: http : //jsfiddle.net/eotamvwy/
HTML: HTML:
<div class="infobox-container">
<div class="triangle-l"></div>
<div class="triangle-r"></div>
<div class="infobox">
<h3><span>This is the Header</span></h3>
<p>This is the content of the infobox.<p/>
</div>
</div>
How can I modify the CSS so that it is responsive? 如何修改CSS以使其具有响应性?
I have a div which has the following style: 我有一个具有以下样式的div:
width: 98%
padding: 0 1% 0 1%
I want to insert the infobox-container
inside and stretch it 100% and resize based on the above div. 我想在其中插入infobox-container
并将其拉伸100%,然后根据上述div调整大小。
Use percentage units for responsiveness and for triangles you don't need extra elements, you could use :after
and :before
:pseudo-elements on .infobox h3
. 使用百分比单位表示响应性,对于不需要额外元素的三角形,可以在.infobox h3
上使用:after
和:before
:pseudo-elements。
body { width: 100%; margin: 0; } .main-container { width: 98%; padding: 0 1% 0 1%; text-align: center; } .infobox-container { position: relative; display: inline-block; margin: 0; padding: 0; width: 100%; } .infobox { width: 80%; padding: 10px 5px 5px 5px; margin: 10px; position: relative; z-index: 1; -webkit-border-radius: 2px; -moz-border-radius: 2px; border-radius: 2px; -webkit-box-shadow: 0px 0px 3px rgba(0, 0, 0, 0.55); -moz-box-shadow: 0px 0px 3px rgba(0, 0, 0, 0.55); box-shadow: 0px 0px 3px rgba(0, 0, 0, 0.55); background: #424242; background-image: -webkit-gradient(linear, left top, left bottom, from(#6a6b6b), to(#424242)); background-image: -moz-linear-gradient(top, #6a6a6a, #424242); color: #fff; font-size: 90%; } .infobox h3 { position: relative; width: calc(100% + 22px); color: #fff; padding: 10px 5px; margin: 0; left: -15px; -webkit-box-shadow: 0px 0px 3px rgba(0, 0, 0, 0.55); -moz-box-shadow: 0px 0px 3px rgba(0, 0, 0, 0.55); box-shadow: 0px 0px 3px rgba(0, 0, 0, 0.55); background: #3198dd; background-image: -webkit-gradient(linear, left top, left bottom, from(#33acfc), to(#3198dd)); background-image: -moz-linear-gradient(top, #33acfc, #3198dd); font-size: 160%; text-align: center; text-shadow: #2187c8 0 -1px 1px; font-weight: bold; } .infobox h3:before, .infobox h3:after { content: ''; border-color: transparent #2083c2 transparent transparent; border-style: solid; border-width: 12px; height: 0; width: 0; position: absolute; left: -12px; top: 100%; transform: translateY(-50%); z-index: -1; /* displayed under infobox */ } .infobox h3:after { border-color: transparent transparent transparent #2083c2; left: 100%; margin-left: -12px; } .infobox a { color: #35b0ff; text-decoration: none; border-bottom: 1px dotted transparent; } .infobox a:hover, .infobox a:focus { text-decoration: none; border-bottom: 1px dotted #35b0ff; }
<div class="main-container"> <div class="infobox-container"> <div class="infobox"> <h3><span>This is the Header</span></h3> <p>This is the content of the infobox.</p> </div> </div> </div>
I think you can just make a couple of small changes to make all the sizes responsive at least to the content: 我认为您可以进行一些小的更改,以使所有大小至少响应内容:
The most important changes: 最重要的变化:
Use 'Calc' to set the width. 使用“计算”来设置宽度。 Support is reasonable well (see caniuse ), but you could also solve this differently using negative margins (or probably other ways as well). 支持是合理的(请参见caniuse ),但是您也可以使用负边距来解决此问题(或者也可以使用其他方法)。
.infobox h3 {
width: calc(100% + 20px);
}
The right arrow can simply be solved by setting right
to -12px
, just as the left one has left: -12px
. 只需将right
设置为-12px
即可解决向右箭头,就像left: -12px
。
.infobox-container .triangle-r {
right: -12px;
}
.infobox-container { position: relative; display: inline-block; margin: 0; padding: 0; width: auto; } .infobox { padding: 10px 5px 5px 5px; margin:10px; position: relative; z-index: 90; -webkit-border-radius: 2px; -moz-border-radius: 2px; border-radius: 2px; -webkit-box-shadow: 0px 0px 3px rgba(0,0,0,0.55); -moz-box-shadow: 0px 0px 3px rgba(0,0,0,0.55); box-shadow: 0px 0px 3px rgba(0,0,0,0.55); background: #424242; background-image: -webkit-gradient(linear, left top, left bottom, from(#6a6b6b), to(#424242)); background-image: -moz-linear-gradient(top,#6a6a6a,#424242); color: #fff; font-size: 90%; } .infobox h3 { position: relative; width: calc(100% + 20px); color: #fff; padding: 10px 5px; margin: 0; left: -15px; z-index: 100; -webkit-box-shadow: 0px 0px 3px rgba(0,0,0,0.55); -moz-box-shadow: 0px 0px 3px rgba(0,0,0,0.55); box-shadow: 0px 0px 3px rgba(0,0,0,0.55); background: #3198dd; background-image: -webkit-gradient(linear, left top, left bottom, from(#33acfc), to(#3198dd)); background-image: -moz-linear-gradient(top,#33acfc,#3198dd); font-size: 160%; text-align: center; text-shadow: #2187c8 0 -1px 1px; font-weight: bold; } .infobox-container .triangle-l { border-color: transparent #2083c2 transparent transparent; border-style:solid; border-width:13px; height:0; width:0; position: absolute; left: -13px; top: 54px; z-index: 2; /* displayed under infobox */ } .infobox-container .triangle-r { border-color: transparent transparent transparent #2083c2; border-style:solid; border-width:13px; height:0; width:0; position: absolute; right: -12px; top: 54px; z-index: 2; /* displayed under infobox */ } .infobox a { color: #35b0ff; text-decoration: none; border-bottom: 1px dotted transparent; } .infobox a:hover, .infobox a:focus { text-decoration: none; border-bottom: 1px dotted #35b0ff; }
<div class="infobox-container"> <div class="triangle-l"></div> <div class="triangle-r"></div> <div class="infobox"> <h3><span>This is the Headewefewfewfewfewfewfewfr</span></h3> <p>This is the content of the infobox.</p> </div> </div>
If you want this header ribbon to be responsive, you need to get away from using fixed-widths and instead combine width:100%;
如果要使此标题功能区响应,则需要避免使用固定宽度,而要结合width:100%;
and max-width: 270px;
和max-width: 270px;
(or whatever). (管他呢)。
When you define the width attribute to be 270px, you are telling the browser you want this particular element to have both a minimum and maximum width of 270px. 当您将width属性定义为270px时,就是在告诉浏览器您希望该特定元素的最小和最大宽度均为270px。 If you are thinking responsively, what you actually want is for your element to expand as much as possible ( width:100%
), but to max-out at 270px ( max-width: 270px;
). 如果您反应灵敏,那么您真正想要的是让元素尽可能扩大( width:100%
),但最大扩展为270px( max-width: 270px;
)。
Thats the responsive bit. 多数民众赞成在响应位。
What you are actually after is something closer to this: 您实际上所追求的是更接近于此的东西:
http://jsfiddle.net/TheIronDeveloper/eotamvwy/3/ http://jsfiddle.net/TheIronDeveloper/eotamvwy/3/
* { box-sizing: border-box; } .infobox-container { position: relative; display: block; margin: 0; padding: 0; max-width: 500px; width: 100%; } .infobox { padding: 3em 5px 5px; margin:10px; position: relative; z-index: 90; -webkit-border-radius: 2px; -moz-border-radius: 2px; border-radius: 2px; -webkit-box-shadow: 0px 0px 3px rgba(0,0,0,0.55); -moz-box-shadow: 0px 0px 3px rgba(0,0,0,0.55); box-shadow: 0px 0px 3px rgba(0,0,0,0.55); background: #424242; background-image: -webkit-gradient(linear, left top, left bottom, from(#6a6b6b), to(#424242)); background-image: -moz-linear-gradient(top,#6a6a6a,#424242); color: #fff; font-size: 90%; } .infobox-ribbon { position: absolute; top: 10px; width: 100%; color: #fff; padding: 10px 5px; margin: 0; z-index: 100; -webkit-box-shadow: 0px 0px 3px rgba(0,0,0,0.55); -moz-box-shadow: 0px 0px 3px rgba(0,0,0,0.55); box-shadow: 0px 0px 3px rgba(0,0,0,0.55); background: #3198dd; background-image: -webkit-gradient(linear, left top, left bottom, from(#33acfc), to(#3198dd)); background-image: -moz-linear-gradient(top,#33acfc,#3198dd); font-size: 160%; text-align: center; text-shadow: #2187c8 0 -1px 1px; font-weight: bold; } .infobox-container .triangle-l { border-color: transparent #2083c2 transparent transparent; border-style:solid; border-width:13px; height:0; width:0; position: absolute; left: -12px; top: 45px; z-index: 0; /* displayed under infobox */ } .infobox-container .triangle-r { border-color: transparent transparent transparent #2083c2; border-style:solid; border-width:13px; height:0; width:0; position: absolute; right: -12px; top: 45px; z-index: 0; /* displayed under infobox */ } .infobox a { color: #35b0ff; text-decoration: none; border-bottom: 1px dotted transparent; } .infobox a:hover, .infobox a:focus { text-decoration: none; border-bottom: 1px dotted #35b0ff; }
<div class="infobox-container"> <div class="triangle-l"></div> <div class="triangle-r"></div> <h3 class="infobox-ribbon">This is the Header</h3> <div class="infobox"> <p>This is the content of the infobox.</p> </div> </div>
I did a few things here: 我在这里做了几件事:
* {box-sizing:border-box;}
, which does a nicer job at making elements "mold" to the widths that I tell them to (regardless of margins), more details here 我应用了* {box-sizing:border-box;}
,它在将元素“模制”到我告诉它们的宽度(不考虑边距)方面做得更好, 这里有更多详细信息 width:100%;max-width:500px;
就像我之前提到的,我将信息框容器的固定宽度更改为width:100%;max-width:500px;
. 。 If you try resizing down, the ribbon stays in place. 如果尝试缩小尺寸,色带会留在原处。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.