简体   繁体   English

字段集和图例的自定义边框

[英]Custom border for a fieldset and legend

sorry if this question has been asked before, my friend asked me to do this sort of fieldset for their website. 抱歉,如果以前已经问过这个问题,我的朋友要我为他们的网站做这种字段设置。

screenshot in this link custom fieldset border 此链接中的屏幕快照自定义字段集边框

it looks like a normal one but I'm curious how do I get that little vertical line on the left and the right side of "Aim to preserve" text. 它看起来很正常,但我很好奇如何在“旨在保留”文本的左侧和右侧获得一条小的垂直线。

Help would be highly appreciated. 帮助将不胜感激。

Regards, 问候,

You can use the :before and :after pseudo elements in order to style these two specific vertical lines: 您可以使用:before:after伪元素来设置这两条特定的垂直线的样式:

 fieldset { border:1px solid gray; } legend { padding: 0.2em 0.5em; color: gray; font-size:90%; text-align:center; position: relative; } legend:before { position: absolute; content: ''; height: 8px; border-left: 1px solid gray; left: 0px; top: 7px; } legend:after { position: absolute; content: ''; height: 8px; border-right: 1px solid gray; right: 0px; top: 7px; } 
 <form> <fieldset> <legend>Subscription info</legend> <label for="name">Username:</label> <input type="text" name="name" id="name" /> <br /> <label for="mail">E-mail:</label> <input type="text" name="mail" id="mail" /> <br /> <label for="address">Address:</label> <input type="text" name="address" id="address" size="40" /> </fieldset> </form> 

Here is some improvement. 是一些改进。

 fieldset { border:1px solid gray; } legend { position: relative; left: 50%; /* Move the legend to the center of the fieldset's top border */ transform: translateX(-50%); /* Fix the alignment to center perfectly */ background-color: white; /* Put whatever color you want */ padding: 0.2em 0.5em; color: gray; font-size:90%; text-align:center; position: relative; } legend:before { position: absolute; content: ''; height: 8px; border-left: 1px solid gray; left: 0px; top: 7px; } legend:after { position: absolute; content: ''; height: 8px; border-right: 1px solid gray; right: 0px; top: 7px; } #line { position: absolute; top: 19px; /* Position the line */ left: 12px; border-top: 1px solid gray; min-width: 20%; /* Fix this according to the white space to hide */ } 
 <form> <fieldset> <!-- Add a div here to make up a line to hide the space left by the legend --> <div id="line"></div> <legend>Subscription info</legend> <label for="name">Username:</label> <input type="text" name="name" id="name" /> <br /> <label for="mail">E-mail:</label> <input type="text" name="mail" id="mail" /> <br /> <label for="address">Address:</label> <input type="text" name="address" id="address" size="40" /> </fieldset> </form> 

Hope it helps... 希望能帮助到你...

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

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