I'm trying to get a nice fade-out effect at the bottom of a section of text as a 'read more' indicator.
I've been following a bit off this and other tutorials, and my code currently is as follows:
html
<section>
<p>malesuada fames ac turpis egestas...leo.</p>
<p>malesuada fames ac turpis egestas...leo.</p>
<div class="fadeout"></div>
</section>
<p>Stuff after</p>
css
.fadeout {
position: relative;
bottom: 4em;
height: 4em;
background: -webkit-linear-gradient(
rgba(255, 255, 255, 0) 0%,
rgba(255, 255, 255, 1) 100%
);
}
The problem is, even when I position the transparent div over the body of text, the 4em's of space still exists between and 'Other Stuff.'
Any ideas?
A relatively position element is not removed from the normal html flow, so if you move it around the initial space reserved for it still remains, however with absolute positioning this is not the case
.fadeout {
position: absolute;
bottom: 0em;
width:100%;
height: 4em;
background: -webkit-linear-gradient(
rgba(255, 255, 255, 0) 0%,
rgba(255, 255, 255, 1) 100%
);
background-image: -moz-linear-gradient(
rgba(255, 255, 255, 0) 0%,
rgba(255, 255, 255, 1) 100%
);
background-image: -o-linear-gradient(
rgba(255, 255, 255, 0) 0%,
rgba(255, 255, 255, 1) 100%
);
background-image: linear-gradient(
rgba(255, 255, 255, 0) 0%,
rgba(255, 255, 255, 1) 100%
);
background-image: -ms-linear-gradient(
rgba(255, 255, 255, 0) 0%,
rgba(255, 255, 255, 1) 100%
);
}
section {position:relative}
Came to this late to the party, but this can also be done without the .fadeout
div, using a ::before
or ::after
pseudo-element:
section {
position: relative;
}
section::after {
content: '';
position: absolute;
bottom: 0;
width: 100%;
height: 15px;
background: -webkit-linear-gradient(
rgba(255, 255, 255, 0) 0%,
rgba(255, 255, 255, 1) 100%
);
background-image: -moz-linear-gradient(
rgba(255, 255, 255, 0) 0%,
rgba(255, 255, 255, 1) 100%
);
background-image: -o-linear-gradient(
rgba(255, 255, 255, 0) 0%,
rgba(255, 255, 255, 1) 100%
);
background-image: linear-gradient(
rgba(255, 255, 255, 0) 0%,
rgba(255, 255, 255, 1) 100%
);
background-image: -ms-linear-gradient(
rgba(255, 255, 255, 0) 0%,
rgba(255, 255, 255, 1) 100%
);
}
Simple add .fade-out
onto the element you want to "fade-out":
.fade-out {
position: relative;
max-height: 350px; // change the height
}
.fade-out:after {
content: '';
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-image: linear-gradient( rgba(255, 255, 255, 0) 50%, rgba(255, 255, 255, 1) 100% );
}
Just replace bottom: 4em
with margin-top: -4em
. Works perfect for me.
Added overflow:hidden;
to .fade-out {}
above.
来源:https://stackoverflow.com/questions/11837606/fading-out-text-at-bottom-of-a-section-with-transparent-div-but-height-stays-un