实用的60个CSS代码片段[转]

论坛 期权论坛     
选择匿名的用户   2021-5-30 00:17   146   0
<h4>1、垂直对齐</h4>
<hr>
<p>如果你用<code>CSS</code>,则你会有困惑:我该怎么垂直对齐容器中的元素?现在,利用<code>CSS3</code>的<code>Transform</code>,可以很优雅的解决这个困惑:</p>
<pre class="blockcode"><code class="language-css"><code class="css"><span class="hljs-class">.verticalcenter</span><span class="hljs-rules">{
    <span class="hljs-rule"><span class="hljs-attribute">position</span>:<span class="hljs-value"> relative</span></span>;
    <span class="hljs-rule"><span class="hljs-attribute">top</span>:<span class="hljs-value"> <span class="hljs-number">50%</span></span></span>;
    <span class="hljs-rule"><span class="hljs-attribute">-webkit-transform</span>:<span class="hljs-value"> <span class="hljs-function">translateY</span>(-<span class="hljs-number">50%</span>)</span></span>;
    <span class="hljs-rule"><span class="hljs-attribute">-o-transform</span>:<span class="hljs-value"> <span class="hljs-function">translateY</span>(-<span class="hljs-number">50%</span>)</span></span>;
    <span class="hljs-rule"><span class="hljs-attribute">transform</span>:<span class="hljs-value"> <span class="hljs-function">translateY</span>(-<span class="hljs-number">50%</span>)</span></span>;
}</span></code></code></pre>
<p>使用这个技巧,从单行文本、段落到box,都会垂直对齐。目前浏览器对<code>Transform</code>的支持是需要关注的,<br> <code>Chrome 4</code>, <code>Opera 10</code>, <code>Safari 3</code>, <code>Firefox 3</code>, <code>and Internet Explorer 9</code>均支持该属性</p>
<h4>2、伸展一个元素到窗口高度</h4>
<hr>
<p>在具体场景中,你可能想要将一个元素伸展到窗口高度,基本元素的调整只能调整容器的大小,因此要使一个元素伸展到窗口高度,<br> 我们需要伸展顶层元素:<code>html</code>和<code>body</code>:</p>
<pre class="blockcode"><code class="language-css"><code class="css"><span class="hljs-tag">html</span>,
<span class="hljs-tag">body</span> <span class="hljs-rules">{
    <span class="hljs-rule"><span class="hljs-attribute">height</span>:<span class="hljs-value"> <span class="hljs-number">100%</span></span></span>;
}</span></code></code></pre>
<p>然后将<code>100%</code>应用到任何元素的高</p>
<pre class="blockcode"><code class="language-css"><code class="css"><span class="hljs-tag">div</span> <span class="hljs-rules">{
    <span class="hljs-rule"><span class="hljs-attribute">height</span>:<span class="hljs-value"> <span class="hljs-number">100%</span></span></span>;
}</span></code></code></pre>
<h4>3、基于文件格式使用不同的样式</h4>
<hr>
<p>为了更容易知道链接的目标,有时你想让一些链接看起来和其它的不同。下面的片段在文本链接前添加一个图标,对不同的资源使用不同的图标或图片:</p>
<pre class="blockcode"><code class="language-css"><code class="css"><span class="hljs-tag">a</span><span class="hljs-attr_selector">[href^&#61;&#34;http://&#34;]</span><span class="hljs-rules">{
    <span class="hljs-rule"><span class="hljs-attribute">padding-right</span>:<span class="hljs-value"> <span class="hljs-number">20px</span></span></span>;
    <span class="hljs-rule"><span class="hljs-attribute">background</span>:<span class="hljs-value"> <span class="hljs-function">url</span>(external.gif) no-repeat center right</span></span>;
}</span>
<span class="hljs-comment">/* emails */</span>
<span class="hljs-tag">a</span><span class="hljs-attr_selector">[href^&#61;&#34;mailto:&#34;]</span><span class="hljs-rules">{
    <span class="hljs-rule"><span class="hljs-attribute">padding-right</span>:<span class="hljs-value"> <span class="hljs-number">20px</span></span></span>;
    <span class="hljs-rule"><span class="hljs-attribute">background</span>:<span class="hljs-value"> <span class="hljs-function">url</span>(email.png) no-repeat center right</span></span>;
}</span>

<span class="hljs-comment">/* pdfs */</span>
<span class="hljs-tag">a</span><span class="hljs-attr_selector">[href$&#61;&#34;.pdf&#34;]</span><span class="hljs-rules">{
    <span class="hljs-rule"><span class="hljs-attribute">padding-right</span>:<span class="hljs-value"> <span class="hljs-number">20px</span></span></span>;
    <span class="hljs-rule"><span class="hljs-attribute">background</span>:<span class="hljs-value"> <span class="hljs-function">url</span>(pdf.png) no-repeat center right</span></span>;
}</span></code></code></pre>
<p><a href="http://runjs.cn/code/a3uuodvh" rel="noopener noreferrer" target="_blank">效果演示</a></p>
<h4>4、创建跨浏览器的图像灰度</h4>
<hr>
<p>灰度有时看起来简约和优雅,能为网站呈现更深层次的色调。在示例中,我们将对一个SVG图像添加灰度过滤:</p>
<pre class="hljs xml"><code class="xml"><span class="hljs-tag">&lt;<span class="hljs-title">svg</span> <span class="hljs-attribute">xmlns</span>&#61;<span class="hljs-value">&#34;http://www.w3.org/2000/svg&#34;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">filter</span> <span class="hljs-attribute">id</span>&#61;<span class="hljs-value">&#34;grayscale&#34;</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">feColorMatrix</span> <span class="hljs-attribute">ty
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP