简体   繁体   English

如何在页脚中显示垂直的白色边框?

[英]How can I show a vertical white border in my footer?

I need to make a vertical white border in my footer at around 90% of the width. 我需要在页脚中制作一个垂直的白色边框,宽度约为90%。

This is what it should look like: 它应该是这样的:

在此处输入图片说明

I currently have the same as in the image but without the white line on the right. 我目前与图片中的图片相同,但右侧没有白线。

 footer { width: 100%; background-color: #CDDE47; padding: 10px; } 
 <footer> <h2>Contact us</h2> <p><em>You can find our physical location on:<br> 42 Tree lane, CB2345, Cambridge, Cambridgeshire, United Kingdom</em> </p> <a href=""><em>+44 (2345) 345 456</em></a> <br> <a href=""><em>info@growiteatit.co.uk</em></a> <ul> <li><a href=""><em>Check out our facebook page</em></a> </li> <li><a href=""><em>Check out our twitter feed</em></a> </li> </ul> </footer> 

Give your footer position: relative and add a div.verticalLine inside of it defined as so: 设置页脚position: relative并在其内部添加div.verticalLine,定义为:

.verticalLine{
    position: absolute;
    top: 0;
    right: 10%;
    width: 30px;
    height: 100%;
    background: #fff;
}

 footer { width: 100%; background-color: #CDDE47; padding: 10px; position: relative; /* 1 */ } footer::after { content: ""; /* 2 */ position: absolute; width: 25px; height: 100%; background-color: white; top: 0; right: 10%; } 
 <footer> <h2>Contact us</h2> <p><em>You can find our physical location on:<br> 42 Tree lane, CB2345, Cambridge, Cambridgeshire, United Kingdom</em> </p> <a href=""><em>+44 (2345) 345 456</em></a> <br> <a href=""><em>info@growiteatit.co.uk</em></a> <ul> <li><a href=""><em>Check out our facebook page</em></a> </li> <li><a href=""><em>Check out our twitter feed</em></a> </li> </ul> </footer> 

Notes: 笔记:

  1. Establish nearest positioned ancestor for absolute positioning . 建立最近定位的祖先以进行绝对定位
  2. Use an absolutely-positioned pseudo-element to create the border. 使用绝对定位的伪元素创建边框。

You could add a pseudo element with CSS 您可以使用CSS添加一个伪元素

footer {
  width: 100%;
  background-color: #CDDE47;
  padding: 10px;
  position:relative;
}
footer:after{
    content:'';
    position:absolute;
    right:10%;
    width:10px;
    top:0;
    bottom:0;
    background-color:#fff;
}

Full demo 完整演示

 footer { width: 100%; background-color: #CDDE47; padding: 10px; position:relative; } footer:after{ content:''; position:absolute; right:10%; width:10px; top:0; bottom:0; background-color:#fff; } 
 <footer> <h2>Contact us</h2> <p><em>You can find our physical location on:<br> 42 Tree lane, CB2345, Cambridge, Cambridgeshire, United Kingdom</em> </p> <a href=""><em>+44 (2345) 345 456</em></a> <br> <a href=""><em>info@growiteatit.co.uk</em></a> <ul> <li><a href=""><em>Check out our facebook page</em></a> </li> <li><a href=""><em>Check out our twitter feed</em></a> </li> </ul> </footer> 

This can be achieved quite easily using the :after pseudoelement, which won't require you to modify your markup. 使用:after伪元素可以很容易地做到这一点,它不需要您修改标记。 Give the footer position:relative , then use CSS to place an absolutely-positioned bar inside it: 设置页脚position:relative ,然后使用CSS在其中放置一个绝对定位的栏:

 footer { width: 100%; background-color: #CDDE47; padding: 10px; position: relative; } footer:after { display: block; position: absolute; top: 0; bottom: 0; width: 10px; right: 50px; background: white; content: ""; } 
 <footer> <h2>Contact us</h2> <p><em>You can find our physical location on:<br> 42 Tree lane, CB2345, Cambridge, Cambridgeshire, United Kingdom</em> </p> <a href=""><em>+44 (2345) 345 456</em></a> <br> <a href=""><em>info@growiteatit.co.uk</em></a> <ul> <li><a href=""><em>Check out our facebook page</em></a> </li> <li><a href=""><em>Check out our twitter feed</em></a> </li> </ul> </footer> 

add one div into footer 在页脚中添加一个div

 footer { width: 100%; background-color: #CDDE47; } .a1 { width: 90%; border-right: 10px solid #fff; padding: 10px; } 
 <footer> <div class="a1"> <h2>Contact us</h2> <p><em>You can find our physical location on:<br> 42 Tree lane, CB2345, Cambridge, Cambridgeshire, United Kingdom</em></p> <a href=""><em>+44 (2345) 345 456</em></a><br> <a href=""><em>info@growiteatit.co.uk</em></a> <ul> <li><a href=""><em>Check out our facebook page</em></a></li> <li><a href=""><em>Check out our twitter feed</em></a></li> </ul> </div> </footer> 

You can add an inner wrapper and apply the border & padding to that. 您可以添加一个内部包装,并对其应用边框和填充。

 footer { width: 100%; background-color: #CDDE47; } .borderWrapper{ border-right: solid 8px #fff; width: 90%; padding: 10px; } 
 <footer> <div class="borderWrapper"> <h2>Contact us</h2> <p><em>You can find our physical location on:<br> 42 Tree lane, CB2345, Cambridge, Cambridgeshire, United Kingdom</em> </p> <a href=""><em>+44 (2345) 345 456</em></a> <br> <a href=""><em>info@growiteatit.co.uk</em></a> <ul> <li><a href=""><em>Check out our facebook page</em></a> </li> <li><a href=""><em>Check out our twitter feed</em></a> </li> </ul> </div> </footer> 

maybe use an :after pseudo element to create the white divider, and ensure the footer has position:relatve to contain the absolute element? 也许使用:after伪元素来创建白色分隔线,并确保页脚具有position:reveve以包含绝对元素? eg 例如

footer {
  width: 100%;
  background-color: #CDDE47;
  padding: 10px;
  position: relative;
}

footer::after {
 content: '';
 position: absolute;
 width: 30px;
 background: white;
 right: calc(10% - 15px);
 top: 0;
 height: 100%;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM