使用 CSS 伪类之前和之后
所以我尝试前后尝试CSS伪类。我尝试使用这些伪元素来创建标题元素。这是为了减少使用 div 来保存左右图像。这是 HTML 代码,
<header id="mastHead">
<h1><a href="#">Branding</a></h1>
</header>
所以我有 3 个图像来创建传统的标题元素,左侧和右侧宽度为 20 像素,高度为 100 像素,中间宽度为 1 像素,高度为 100 像素,将水平重复。这里是我的 CSS
#mastHead {
background:url(images/headMiddle.jpg) repeat-x top left;
width:1000px;
height:100px;
overflow:hidden;
}
#mastHead:before {
content:"";
display:block;
background:url(images/headLeft.jpg) no-repeat top left;
width:20px;
height:100px;
float:left;
}
#mastHead:after {
content:"";
display:block;
background:url(images/headRight.jpg) no-repeat top left;
width:20px;
height:100px;
float:right;
}
#mastHead h1 a {
display:block;
width:200px;
height:41px;
background:url(images/logo.png) no-repeat;
}
所以问题是如果我删除 h1 元素,它会完美对齐,但如果我放置这些元素,它会将 ::after 伪类向下推,并且会根据其高度占用剩余空间。我怎样才能使这个 h1 元素只占用中间空间而不影响 ::after 空间?
So I tried to experiment with CSS pseudo class before and after. I tried to use those pseudo to create header element.This is to reduce using div to hold left and right images. This is code for HTML
<header id="mastHead">
<h1><a href="#">Branding</a></h1>
</header>
So I have 3 images to create traditional header element which is 20px width for left and right side with 100px height and for the middle, 1px width and 100px height which will repeat horizontal. And here my CSS
#mastHead {
background:url(images/headMiddle.jpg) repeat-x top left;
width:1000px;
height:100px;
overflow:hidden;
}
#mastHead:before {
content:"";
display:block;
background:url(images/headLeft.jpg) no-repeat top left;
width:20px;
height:100px;
float:left;
}
#mastHead:after {
content:"";
display:block;
background:url(images/headRight.jpg) no-repeat top left;
width:20px;
height:100px;
float:right;
}
#mastHead h1 a {
display:block;
width:200px;
height:41px;
background:url(images/logo.png) no-repeat;
}
So the problem is if I remove h1 element, it will align perfectly but if I put these element, it will push the ::after pseudo-class down and it will take leftover space according to it height.How can I make this h1 element to take just middle space without affecting the ::after space?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我用你的例子做了一个小提琴: http://jsfiddle.net/3Dcw3/ (仅将宽度设置为 500以适合小提琴并设置背景以可视化它们)
这是一个固定版本: http://jsfiddle.net/3Dcw3/1/
要点是:
position:relative;
添加到标头。I made a fiddle with your example: http://jsfiddle.net/3Dcw3/ (only set width to 500 to fit in a fiddle and set background to visualize them)
And here is a fixed version: http://jsfiddle.net/3Dcw3/1/
The points are:
position:relative;
to the header.