Make img held within div brighten/glow while hovering the div ONLY - html

I'd like to make an image held within a div brighten but do so while hovering the div only, so that only the image within the div changes. My code may be a little sloppy but here it is, thank you.
#textimg{
background-image: url('images/stock1.jpg');
height:100%;
width:100%;
min-height: 340px;
max-width: 400px;
}
#textimg:hover{
-webkit-filter: drop-shadow(0px 0px 6px rgba(0, 0, 0, 0.3));
-webkit-transition: all 0.3s linear;
-o-transition: all 0.3s linear;
transition: all 0.3s linear;
}
#textimg img:hover{
-webkit-filter: brightness(140%);
-webkit-transition: all 0.3s linear;
-o-transition: all 0.3s linear;
transition: all 0.3s linear;
}
<div id="textimg">
<img src="images/sometext.png">
</div>

use this :
#textimage:hover img {
-webkit-filter: brightness(140%);
-webkit-transition: all 0.3s linear;
-o-transition: all 0.3s linear;
transition: all 0.3s linear;
}

The term you are looking for is opacity.
Change the opacity vale on hover and it works.
There is an example for your reference:-
.container:hover .image {
opacity: 0.3;
}
.container:hover .middle {
opacity: 1;
}
change your div according to the needs

Try adding a class to the image
body,html{
margin: 0;
height: 100%;
}
.container{
width: 100%;
height: 90%;
}
.img1{
width: 100%;
height: 100%;
transition: opacity 2s;
}
.img1:hover{
opacity: 0.5;
}
<div class="container">
<img src="https://images.pexels.com/photos/162389/lost-places-old-decay-ruin-162389.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb" class="img1" />
</div>

Related

Css Transform: Scale Only Works in Dev Tools

I'm trying to achieve a simple :hover effect where the image hovered scales up when hovered. Strangely, when I click the :hover radio button in the "Force element state" in Chrome dev tools, the effect works as expected, but when I actually hover over the image, no dice. Any insight would be much appreciated!
This is my markup (the image has a semi opaque overlay)
<article class="news-loop_item">
<img
src="http://imgsv.imaging.nikon.com/lineup/lens/zoom/normalzoom/af-
s_dx_18-300mmf_35-56g_ed_vr/img/sample/sample4_l.jpg" class="news-
loop_img" alt="featured image">
<div class="news-loop_overlay">
<h2 class="news-loop_title">Title</h2>
<p class="news-loop_date">Date</p>
<div class="news-loop_summary">Summary</div>
</div>
</article>
And my scss
.news-loop {
flex-wrap: wrap;
margin: -1rem;
}
.news-loop_item {
flex-basis: calc(100% / 2 - 2rem);
height: 20rem;
margin: 1rem;
overflow: hidden;
position: relative;
}
.news-loop_img {
height: 100%;
object-fit: cover;
overflow: hidden;
-webkit-transition: all .3s ease-in-out;
-moz-transition: all .3s ease-in-out;
-ms-transition: all .3s ease-in-out;
-o-transition: all .3s ease-in-out;
transition: all .3s ease-in-out;
position: relative;
vertical-align: middle;
width: 100%;
}
.news-loop_img:hover {
-webkit-transform: scale(1.15, 1.15);
transform: scale(1.15, 1.15);
}
.news-loop_overlay {
background: rgba(0, 0, 0, .75);
height: 100%;
left: 0;
position: absolute;
top: 0;
width: 100%;
-webkit-transition: all .3s ease-in-out;
-moz-transition: all .3s ease-in-out;
-ms-transition: all .3s ease-in-out;
-o-transition: all .3s ease-in-out;
transition: all .3s ease-in-out;
}
.news-loop_overlay:hover {
background: rgba(0, 0, 0, .5);
}
https://codepen.io/evanhickman/pen/ZXOmWb
The reason why is that your hover selector is wrong, check out this:
.news-loop_item:hover .news-loop_img {
-webkit-transform: scale(1.15, 1.15);
transform: scale(1.15, 1.15);
}
See the parent element needs the hover event.
The issue is because your .news-loop_overlay class div is 'capturing' the hover effect.
Try adding a z-index: -1; to that div and you'll see that the scale works.

Using sprites as hover backgrounds on images

I am noticing a pretty hefty amount of loading time on the first visit to a page I created. My goal was to make a "meet us" page for my wife's company where when the user hovers over a photo, a different photo + text fade in appears. I finally figured this out and I love the the effect. The columns are responsive and I have no issues other than the fact that when I first visit the page, each time I hover over an image it's taking around 1 full second to load.
I read an article about sprites, which apparently help loading times. The problem is, the article formats html/css in a specific way, much different than the way I formatted my columns. I am not eager to completely re-work my code so I was wondering if someone could explain to me how to implement sprites with the code I currently have.
Also, I am sure my code is super messy and parts of it are redundant. I'm also pretty sure I could condense all of the redundant parts I'm just not 100% on how.
Here is the code:
.meetuscontainer {
max-width: 1280px;
margin: 0 auto;
clear: both;
}
.meetusimg1 {
margin: 10px;
width: 350px;
height: 525px;
background-image: url("https://cdn.shopify.com/s/files/1/2185/4785/files/sLL_Proofs-141chelseaport.jpg?6924964667493824007");
background-repeat: no-repeat;
background-position: center;
background-size: cover;
}
.meetusimg2 {
margin: 10px;
width: 350px;
height: 525px;
background-image: url("https://cdn.shopify.com/s/files/1/2185/4785/files/sLL_Proofs-256heatherport.jpg?6924964667493824007");
background-repeat: no-repeat;
background-position: center;
background-size: cover;
}
.meetusimg3 {
margin: 10px;
width: 350px;
height: 525px;
background-image: url("https://cdn.shopify.com/s/files/1/2185/4785/files/SMLL_Proofs-312momport.jpg?6924964667493824007");
background-repeat: no-repeat;
background-position: center;
background-size: cover;
}
.meetusimg4 {
margin: 10px;
width: 350px;
height: 525px;
background-image: url("https://cdn.shopify.com/s/files/1/2185/4785/files/SMLL_Proofs-287magport.jpg?6924964667493824007");
background-repeat: no-repeat;
background-position: center;
background-size: cover;
}
.meetusimg5 {
margin: 10px;
width: 350px;
height: 525px;
background-image: url("https://cdn.shopify.com/s/files/1/2185/4785/files/LL_Proofs-120annieports.jpg?6924964667493824007");
background-repeat: no-repeat;
background-position: center;
background-size: cover;
}
.meetusimg6 {
margin: 10px;
width: 350px;
height: 525px;
background-image: url("https://cdn.shopify.com/s/files/1/2185/4785/files/LL_Proofs-055kimports.jpg?6924964667493824007");
background-repeat: no-repeat;
background-position: center;
background-size: cover;
}
.meetusimg7 {
margin: 10px;
width: 350px;
height: 525px;
background-image: url("https://cdn.shopify.com/s/files/1/2185/4785/files/sLL_Proofs-257connorport.jpg?6924964667493824007");
background-repeat: no-repeat;
background-position: center;
background-size: cover;
}
.meetusimg8 {
margin: 10px;
width: 350px;
height: 525px;
background-image: url("https://cdn.shopify.com/s/files/1/2185/4785/files/LL_Proofs-088danielleports.jpg?6924964667493824007");
background-repeat: no-repeat;
background-position: center;
background-size: cover;
}
.meetusimgtext {
opacity: 0;
width: 350px;
text-align: center;
font-weight: 600;
font-size: 29px;
flex-wrap: wrap;
justify-content: center;
padding-top: 350px;
color: #fff;
font-family: "Poppins", sans-serif;
text-shadow: 2px 2px 2px rgba(0, 0, 0, 0.3);
}
.meetusimgtext2 {
opacity: 0;
width: 350px;
font-weight: 400;
text-align: center;
font-size: 22px;
flex-wrap: wrap;
justify-content: center;
padding-top: 8px;
color: #a1ffd2;
font-family: "Pacifico", sans-serif;
text-shadow: 2px 2px 2px rgba(0, 0, 0, 0.3);
}
/* Gallery */
.gallery {
display: flex;
flex-wrap: wrap;
justify-content: center;
margin-top: 20px;
}
.meetusimg1:hover {
background-image: url("https://cdn.shopify.com/s/files/1/2185/4785/files/sLL_Proofs-141chelseaport2.jpg?6924964667493824007");
}
.meetusimg2:hover {
background-image: url("https://cdn.shopify.com/s/files/1/2185/4785/files/sLL_Proofs-268heatherport.jpg?6924964667493824007");
}
.meetusimg3:hover {
background-image: url("https://cdn.shopify.com/s/files/1/2185/4785/files/LL_Proofs-014momports.jpg?6924964667493824007");
}
.meetusimg4:hover {
background-image: url("https://cdn.shopify.com/s/files/1/2185/4785/files/SLL_Proofs-403magport.jpg?6924964667493824007");
}
.meetusimg5:hover {
background-image: url("https://cdn.shopify.com/s/files/1/2185/4785/files/sLL_Proofs-111annieport.jpg?6924964667493824007");
}
.meetusimg6:hover {
background-image: url("https://cdn.shopify.com/s/files/1/2185/4785/files/LL_Proofs-048kimports.jpg?6924964667493824007");
}
.meetusimg7:hover {
background-image: url("https://cdn.shopify.com/s/files/1/2185/4785/files/amLL_Proofs-271connorport.jpg?6924964667493824007");
}
.meetusimg8:hover {
background-image: url("https://cdn.shopify.com/s/files/1/2185/4785/files/SLL_Proofs-500daniellepor2t.jpg?6924964667493824007");
}
.meetusimg1:hover .meetusimgtext {
-webkit-transition: opacity 0.6s ease-in;
-moz-transition: opacity 0.6s ease-in;
-o-transition: opacity 0.6s ease-in;
opacity: 1;
}
.meetusimg1:hover .meetusimgtext2 {
-webkit-transition: opacity 0.6s ease-in;
-moz-transition: opacity 0.6s ease-in;
-o-transition: opacity 0.6s ease-in;
opacity: 1;
}
.meetusimg2:hover .meetusimgtext {
-webkit-transition: opacity 0.4s ease-in;
-moz-transition: opacity 0.4s ease-in;
-o-transition: opacity 0.4s ease-in;
opacity: 1;
}
.meetusimg2:hover .meetusimgtext2 {
-webkit-transition: opacity 0.4s ease-in;
-moz-transition: opacity 0.4s ease-in;
-o-transition: opacity 0.4s ease-in;
opacity: 1;
}
.meetusimg3:hover .meetusimgtext {
-webkit-transition: opacity 0.4s ease-in;
-moz-transition: opacity 0.4s ease-in;
-o-transition: opacity 0.4s ease-in;
opacity: 1;
}
.meetusimg3:hover .meetusimgtext2 {
-webkit-transition: opacity 0.4s ease-in;
-moz-transition: opacity 0.4s ease-in;
-o-transition: opacity 0.4s ease-in;
opacity: 1;
}
.meetusimg4:hover .meetusimgtext {
-webkit-transition: opacity 0.4s ease-in;
-moz-transition: opacity 0.4s ease-in;
-o-transition: opacity 0.4s ease-in;
opacity: 1;
}
.meetusimg4:hover .meetusimgtext2 {
-webkit-transition: opacity 0.4s ease-in;
-moz-transition: opacity 0.4s ease-in;
-o-transition: opacity 0.4s ease-in;
opacity: 1;
}
.meetusimg5:hover .meetusimgtext {
-webkit-transition: opacity 0.4s ease-in;
-moz-transition: opacity 0.4s ease-in;
-o-transition: opacity 0.4s ease-in;
opacity: 1;
}
.meetusimg5:hover .meetusimgtext2 {
-webkit-transition: opacity 0.4s ease-in;
-moz-transition: opacity 0.4s ease-in;
-o-transition: opacity 0.4s ease-in;
opacity: 1;
}
.meetusimg6:hover .meetusimgtext {
-webkit-transition: opacity 0.4s ease-in;
-moz-transition: opacity 0.4s ease-in;
-o-transition: opacity 0.4s ease-in;
opacity: 1;
}
.meetusimg6:hover .meetusimgtext2 {
-webkit-transition: opacity 0.4s ease-in;
-moz-transition: opacity 0.4s ease-in;
-o-transition: opacity 0.4s ease-in;
opacity: 1;
}
.meetusimg7:hover .meetusimgtext {
-webkit-transition: opacity 0.4s ease-in;
-moz-transition: opacity 0.4s ease-in;
-o-transition: opacity 0.4s ease-in;
opacity: 1;
}
.meetusimg7:hover .meetusimgtext2 {
-webkit-transition: opacity 0.4s ease-in;
-moz-transition: opacity 0.4s ease-in;
-o-transition: opacity 0.4s ease-in;
opacity: 1;
}
.meetusimg8:hover .meetusimgtext {
-webkit-transition: opacity 0.4s ease-in;
-moz-transition: opacity 0.4s ease-in;
-o-transition: opacity 0.4s ease-in;
opacity: 1;
}
.meetusimg8:hover .meetusimgtext2 {
-webkit-transition: opacity 0.4s ease-in;
-moz-transition: opacity 0.4s ease-in;
-o-transition: opacity 0.4s ease-in;
opacity: 1;
}
.meetusimg9:hover .meetusimgtext {
-webkit-transition: opacity 0.4s ease-in;
-moz-transition: opacity 0.4s ease-in;
-o-transition: opacity 0.4s ease-in;
opacity: 1;
}
.meetusimg9:hover .meetusimgtext2 {
-webkit-transition: opacity 0.4s ease-in;
-moz-transition: opacity 0.4s ease-in;
-o-transition: opacity 0.4s ease-in;
opacity: 1;
}
#media only screen and (min-width: 320px) and (max-width: 480px) {
.meetusimg1,
.meetusimg2,
.meetusimg3,
.meetusimg4,
.meetusimg5,
.meetusimg6,
.meetusimg7,
.meetusimg8,
.meetusimg9,
.meetusimgtext,
.meetusimgtext2 {
max-width: 280px;
}
<div class="meetuscontainer">
<div class="gallery">
<figure class="meetusimg1">
<div class="meetusimgtext">CHELSEA EARLY</div>
<div class="meetusimgtext2">Founder + Designer</div>
</figure>
<figure class="meetusimg2">
<div class="meetusimgtext">HEATHER WALKER</div>
<div class="meetusimgtext2">Founder + Designer</div>
</figure>
<figure class="meetusimg3">
<div class="meetusimgtext">KAREN SAARI</div>
<div class="meetusimgtext2">Founder + Administrator</div>
</figure>
</div>
</div>
<div class="clearfix"></div>
<div class="meetuscontainer">
<div class="gallery">
<figure class="meetusimg4">
<div class="meetusimgtext">MAGGIE HABROS</div>
<div class="meetusimgtext2">Cutter</div>
</figure>
<figure class="meetusimg5">
<div class="meetusimgtext">ANNIE MIHULKA</div>
<div class="meetusimgtext2">Serger</div>
</figure>
<figure class="meetusimg6">
<div class="meetusimgtext">KIM THEISEN</div>
<div class="meetusimgtext2">Cutter</div>
</figure>
</div>
</div>
<div class="clearfix"></div>
<div class="meetuscontainer">
<div class="gallery">
<figure class="meetusimg7">
<div class="meetusimgtext">CONNOR JONES</div>
<div class="meetusimgtext2">Top Stitcher</div>
</figure>
<figure class="meetusimg8">
<div class="meetusimgtext">DANIELLE PFANNENSTIEL</div>
<div class="meetusimgtext2">Elastics</div>
</figure>
<!--<figure class="meetusimg9">
<div class="meetusimgtext">STEPHANIE TRAWICK</div>
<div class="meetusimgtext2">Cutter</div>
</figure>-->
</div>
</div>
EDIT: Including transition
.meetusimg1 {
margin: 10px;
width: 350px;
height: 525px;
background-image: url("https://i.stack.imgur.com/54AbA.jpg");
background-repeat: no-repeat;
background-position: 0 0;
background-size: cover;
transition:background-position 1s ease;
}
.meetusimg1:hover {
background-position: -350px 0;
}
<figure class="meetusimg1">
<div class="meetusimgtext">CHELSEA EARLY</div>
<div class="meetusimgtext2">Founder + Designer</div>
</figure>
Thank you guys for all the answers. I went ahead and revised my code based off of everything you said. I think it looks a TON better. Only issue I'm having is that between h2 and P there is a strange amount of padding that I didn't tell the css to put there. I changed the padding and margin to 0px on the staff-text p and that seemed to help
/* Container */
.staff-container {
max-width: 1280px;
margin: 0 auto;
clear: both;
}
/* Gallery */
.staff-gallery {
display: flex;
flex-wrap: wrap;
justify-content: center;
margin-top: 20px;
}
/* All Staff Image Styling */
.staff-img {
margin: 10px;
width: 350px;
height: 525px;
background-repeat: no-repeat;
background-position: 0 0;
background-size: cover;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
}
/* All Staff Text Styling */
.staff-text {
opacity: 0;
width: 350px;
text-align: center;
flex-wrap: wrap;
justify-content: center;
color: #fff;
text-shadow: 2px 2px 2px rgba(0,0,0,0.3);
}
.staff-text h2{
padding-top: 275px;
font-family: "Poppins", sans-serif;
font-size: 27px;
text-transform: uppercase;
}
.staff-text p{
font-family: "Pacifico", sans-serif;
font-size: 22px;
}
.staff-img:hover .staff-text {
-webkit-transition: opacity 0.6s ease-in;
-moz-transition: opacity 0.6s ease-in;
-o-transition: opacity 0.6s ease-in;
opacity: 1;
}
/* Img Hover */
.staff-img:hover {
background-position: -350px 0;
}
.chelsea {
background-image: url("https://cdn.shopify.com/s/files/1/2185/4785/files/1-ChelseaSprite_f89345e7-ea1e-4186-af48-d8a957b0d8b5.jpg?5470095217541866382");
}
<div class="staff-container">
<div class="staff-gallery">
<div class="staff-img chelsea">
<div class="staff-text">
<h2>Chelsea Early</h2>
<p>Founder + Designer</p>
</div>
</div>
<div class="staff-img chelsea">
<div class="staff-text">
<h2>Chelsea Early</h2>
<p>Founder + Designer</p>
</div>
</div>
<div class="staff-img chelsea">
<div class="staff-text">
<h2>Chelsea Early</h2>
<p>Founder + Designer</p>
</div>
</div>
</div>
</div>

CSS3 - Opacity transition is not working

I'm trying to change a div opacity depending on having the class active or not.
When the div has the active class, I want to change the opacity to 1. If the div does not have the active class, I want to change the opacity to 0.
Follows my CSS code:
.high-light{
position: fixed;
width: 100%;
height: 100%;
top: 0;
background-color: black;
background-color: rgba(0, 0, 0, 0.61);
opacity:0;
left: 0;
-webkit-transition: opacity 0.5s;
-moz-transition: opacity 0.5s;
transition: opacity 3s linear;
}
#multicanvasArea.active .high-light {
opacity:1;
-webkit-transition: opacity 0.5s;
-moz-transition: opacity 0.5s;
transition: opacity 0.5s linear;
}
Thank you
[EDIT]
One of the problems was that I change the css property to "block" and "none". The other was solve by the answer choosen.
When the div has the active class, I want to change the opacity to 1. If the div does not have the active class, I want to change the opacity to 0.
You need to combine the classes like so.
As it was you have .highlight as a child of .active.
.high-light{
position: fixed;
width: 100%;
height: 100%;
top: 0;
background-color: black;
background-color: rgba(0, 0, 0, 0.61);
opacity:0;
left: 0;
-webkit-transition: opacity 0.5s;
-moz-transition: opacity 0.5s;
transition: opacity 3s linear;
}
.high-light.active {
opacity:1;
-webkit-transition: opacity 0.5s;
-moz-transition: opacity 0.5s;
transition: opacity 0.5s linear;
}
The problem here is not the transition, it's the height 100% which is not taking effect because the parent element (body) is not tall 100%.
$('button').on('click', function(e) {
$("#multicanvasArea").toggleClass('active');
})
.high-light{
position: fixed;
width: 100%;
height: 30px;
top: 0;
background-color: black;
opacity:0;
left: 0;
color: white;
transition: opacity 3s linear;
}
#multicanvasArea.active .high-light {
opacity:1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="multicanvasArea">
<p class="high-light">Highlight</p>
<p class="">Other text</p>
</div>
<button>Toggle class</button>
Your code was 95% there, just a few tweaks I made seem to do the trick. Here is the new css:
#multicanvasArea .high-light{
position: fixed;
width: 100%;
height: 100%;
top: 0;
background-color: black;
background-color: rgba(0, 0, 0, 0.61);
opacity:0;
left: 0;
-webkit-transition: opacity 0.5s;
-moz-transition: opacity 0.5s;
transition: opacity 3s linear;
}
#multicanvasArea.active .high-light {
opacity:1;
}
Here is a link to a pen with a working example:
http://codepen.io/anon/pen/pEjrJo
this is a working example. The Javascript is only to toggle the class.
.box {
width: 200px;
height: 200px;
background: pink;
opacity: 0;
transition: opacity 3s linear;
&.active {
opacity: 1;
}
}
https://plnkr.co/edit/RZRygZieCUMVWiOVEJR8?p=preview

Making text show up on hover and blur effect blinking fix

/* CSS used here will be applied after bootstrap.css */
body{
background-color:yellow;
}
img{
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
-ms-transition: all 0.5s ease;
transition: all 0.5s ease;
}
img:hover{
-webkit-filter:blur(5px);
}
<img src="http://i.stack.imgur.com/K0jNI.png">
When you hover over the image the borders of the image flash for a bit before settling.. Is there a way to fix that?
And how do i make a text show up on the middle of the image when i hover over it?
EDIT: This now looks great in Chrome
I don't think it's entirely possible to get a super clean transition when using webkit blur. I've had a lot of rendering issues and glitches when using it before. It's a resource hog too when used on a lot of elements. My advice to change your easing to linear and target only the blur. That should tighten it up a little bit.
img{
-webkit-transition: -webkit-filter 0.5s linear;
-moz-transition: -webkit-filter 0.5s linear;
-o-transition: -webkit-filter 0.5s linear;
-ms-transition: -webkit-filter 0.5s linear;
transition: -webkit-filter 0.5s linear;
}
As for the text fade in. You'll need to add in an element that is initially opacity:0; but then changed to opacity:1; when the parent block is hovered. Initial HTML changed to this:
<div class='block'>
<img src="https://www.nasa.gov/sites/default/files/styles/image_card_4x3_ratio/public/thumbnails/image/leisa_christmas_false_color.png?itok=Jxf0IlS4">
<span>Hey there</span>
</div>
And the new CSS
/* CSS used here will be applied after bootstrap.css */
body {
background-color: yellow;
}
img {
-webkit-transition: -webkit-filter 0.5s linear;
transition: -webkit-filter 0.5s linear;
}
.block {
position: relative;
width: 400px;
}
.block img {
width: 100%;
}
.block span {
opacity: 0;
-webkit-transition: all .3s;
transition: all .3s;
color: white;
position: absolute;
top: 50%;
-webkit-transform: translateY(-50%);
transform: translateY(-50%);
text-align: center;
left: 0;
right: 0;
}
.block:hover > span {
opacity: 1;
}
img:hover {
-webkit-filter: blur(4px);
}
Example here
http://codepen.io/jcoulterdesign/pen/58d613e80e4a768cc9e54aa1e7aaa0af

Ease-in works for both text and background but ease-out only works for text. Why?

Ease-in only works for text and background but ease-out only works for text but not background.
article {
width: 100%;
height: 1000px;
background-color: #fffff;
color: #00000;
}
article .topnav {
opacity: 0;
transition: background-color .9s ease-out;
transition: background-color .9s ease-in;
-moz-transition: background-color .9s ease-in;
-webkit-transition: background-color .9s ease-in;
}
article .topnav {
background: rgba(0,0,0,0);
transition: opacity .9s ease-out;
transition: opacity .9s ease-in;
-moz-transition: opacity .9s ease-in;
-webkit-transition: opacity .9s ease-in;
}
article:hover p.topnav {
opacity: 0.7;
background-color: #808080;
}
.topnav {
visibility: invisible;
text-align: center;
margin: auto;
width: 50%;
}
<article>
<p class="topnav">I am topnav</p>
</article>
Please see fiddle.
The idea is so that when I hover in and out of , both the text and the background eases in and out together.
Please help.
You are setting both transitions on the unhovered state, thus the second one is overwriting the first rule. You need to apply transition rules to both unhovered and hovered state.
article {
width: 100%;
height: 1000px;
background-color: #fffff;
color: #00000;
}
article p.topnav {
opacity: 0;
background-color: #000;
transition: all .9s ease-out;
}
article:hover p.topnav {
opacity: 0.7;
background-color: #808080;
transition: all .9s ease-in;
}
.topnav {
text-align: center;
margin: auto;
width: 50%;
}
<article>
<p class="topnav">I am topnav</p>
</article>