Sky*_*lon 4 html css image pseudo-element
我想知道如何在::after
伪元素中格式化内容CSS ?
我正试图在h1
其左侧和右侧有一个小图像的标题.
我的代码到目前为止:
HTML:
<h1>This Week's Photo Features</h1>
Run Code Online (Sandbox Code Playgroud)
CSS:
h1 {
text-align: center;
}
h1:before {
content: ("images/NikonD100_40x30.jpg");
}
Run Code Online (Sandbox Code Playgroud)
在内容属性还可以用此格式接受一个网址:
content: url("the url")
Run Code Online (Sandbox Code Playgroud)
演示:
h1 {
text-align: center;
}
h1:before {
content: url("http://lorempixel.com/40/40/animals");
}
h1:after {
content: url("http://lorempixel.com/40/40/cats");
}
Run Code Online (Sandbox Code Playgroud)
<h1>This Week's Photo Features</h1>
Run Code Online (Sandbox Code Playgroud)
另一种选择是将图像设置为伪元素的背景:
演示:
h1 {
text-align: center;
}
h1:before, h1:after {
display: inline-block;
width: 40px;
height: 40px;
content: '';
}
h1:before {
background: url("http://lorempixel.com/40/40/animals");
}
h1:after {
background: url("http://lorempixel.com/40/40/cats");
}
Run Code Online (Sandbox Code Playgroud)
<h1>This Week's Photo Features</h1>
Run Code Online (Sandbox Code Playgroud)
如果您将图像用作背景,则可以通过在H1
元素上使用多个背景来抛弃伪元素.
演示:
h1 {
height: 40px;
padding: 0 40px;
text-align: center;
background: url("http://lorempixel.com/40/40/animals") left no-repeat, url("http://lorempixel.com/40/40/cats") right no-repeat;
}
Run Code Online (Sandbox Code Playgroud)
<h1>This Week's Photo Features</h1>
Run Code Online (Sandbox Code Playgroud)