开发者

Can I overlap CSS borders? Trying to underline from edge of page to end of text

I'm trying to center a heading (with variable width) and have the underline running from the left hand edg开发者_JS百科e of the page to the end of the text. Unless I'm missing something, there doesn't seem to be an easy way of doing this! The closest I've come to what I want is:

<style type="text/css">

#wrapper1 {
margin-right: 50%;
border-bottom: 4px solid red;
}

#wrapper2 {
text-align: center;
position: relative;
left: 50%;
}

h1 {
display: inline-block;
margin: 0 auto;
border-bottom: 4px solid red;
}

</style>

<div id="wrapper1"><div id="wrapper2"><h1>Centered.</h1></div></div>

This way, the text is centered with a border acting as an underline, and the border on wrapper1 extends from the left hand edge to the center. BUT, because the heading is within the wrapper, and the border on the wrapper is outside of the content, the wrapper border is below the heading border.

Any suggestions gratefully received - this is driving me mad!


In your #wrapper2:

bottom: -4px;

Will make it move 4 pixels downwards to overlap the other line.

(Tested in Safari, works)


Try removing both the padding-bottom and margin-bottom on both wrappers (set to 0), then add it back in on the inner one only until it looks right.

OK, I had a go, and this works for me. I had to put position relative on both wrappers, which then allows you to push the inner wrapper down a couple of pixels from it's original location.

<html>
<head><title>test</title></head>
<body>
<style type="text/css">

#wrapper1 {
margin-right: 50%;
margin-bottom:0;
padding-bottom:0;
border-bottom: 4px solid red;
position:relative;
}

#wrapper2 {
text-align: center;
position: relative;
left: 50%;
margin-bottom:0;
padding-bottom:0;
position:relative;
top:4px; /*The width of the border doing the underlining*/
}

h1 {
display: inline-block;
margin: 0 auto;
border-bottom: 4px solid red;
}

</style>

<div id="wrapper1"><div id="wrapper2"><h1>Centered.</h1></div></div>

</body>
</html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜