This question already has answers here:
How can I horizontally center an element?
(133 answers)
Closed 1 year ago.
I am learning CSS grid. While practicing I got an issue. justify-content: center is not centering section-3-nav div.
Here is my code:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
text-decoration: none;
list-style: none;
font-family: 'Roboto', sans-serif;
}
.container {
width: 85%;
margin: auto;
padding: 3rem 0;
}
.container > .constant {
display: grid;
justify-items: center;
}
.box-top-title > h1 {
font-weight: bold;
margin-top: 2rem;
}
.box-top-subtitle > p {
text-transform: uppercase;
color: #a8b9c0;
margin: 0.5rem 0;
font-size: 15px;
}
.box-top-hr > hr {
width: 250px;
background-color: #00c6bf;
border-width: 0;
height: 1.5px;
}
.section-3 {
background: #eff7fa;
width: 100%;
}
.section-3 > .container > .section-3-nav {
display: grid;
grid-template-columns: repeat(5, 1fr);
justify-content: center;
margin: 3rem 0;
width: 50%;
border: 1px solid black;
}
.section-3 > .container > .section-3-nav a {
transition: 0.5s;
}
.section-3 > .container > .section-3-nav a:hover {
background: #ff3150;
padding: 10px 20px;
border-radius: 15px;
color: white;
}
<div class="section-3">
<div class="container">
<div class="constant">
<div class="box-top-img"><img src="./images/2-Bondi---PSD-Landing-Page.png" alt=""></div>
<div class="box-top-title"><h1>We Make This</h1></div>
<div class="box-top-subtitle"><p>Prepare to be amazed</p></div>
<div class="box-top-hr"><hr></div>
</div>
<div class="section-3-nav">
<div>All</div>
<div>Design</div>
<div>Code</div>
<div>Photography</div>
<div>Apps</div>
</div>
<div class="section-3-stuffs">
</div>
</div>
</div>
Where is the problem with the code? Where to fix and what to fix?
It will be very helpful for me if you help to solve the problem.
Thank you.
Usually you should think about positioning an element from the perspective of the parent element, not the element itself.
If you want to center the section-3-nav element as part of a grid layout, you have to either
Do it in the parent element (.container) with justify-content: center or
Use justify-self: center in the element itself
In both cases, the parent element (.container) has to use display: grid, too.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
text-decoration: none;
list-style: none;
font-family: 'Roboto', sans-serif;
}
.container {
width: 85%;
margin: auto;
padding: 3rem 0;
}
.container > .constant {
display: grid;
justify-items: center;
}
.box-top-title > h1 {
font-weight: bold;
margin-top: 2rem;
}
.box-top-subtitle > p {
text-transform: uppercase;
color: #a8b9c0;
margin: 0.5rem 0;
font-size: 15px;
}
.box-top-hr > hr {
width: 250px;
background-color: #00c6bf;
border-width: 0;
height: 1.5px;
}
.section-3 {
background: #eff7fa;
width: 100%;
}
.section-3 > .container > .section-3-nav {
display: grid;
grid-template-columns: repeat(5, 1fr);
text-align: center;
margin: 3rem 0;
width: 100%;
border: 1px solid black;
}
.section-3 > .container > .section-3-nav a {
transition: 0.5s;
}
.section-3 > .container > .section-3-nav a:hover {
background: #ff3150;
padding: 10px 20px;
border-radius: 15px;
color: white;
}
<div class="section-3">
<div class="container">
<div class="constant">
<div class="box-top-img"><img src="./images/2-Bondi---PSD-Landing-Page.png" alt=""></div>
<div class="box-top-title"><h1>We Make This</h1></div>
<div class="box-top-subtitle"><p>Prepare to be amazed</p></div>
<div class="box-top-hr"><hr></div>
</div>
<div class="section-3-nav">
<div>All</div>
<div>Design</div>
<div>Code</div>
<div>Photography</div>
<div>Apps</div>
</div>
<div class="section-3-stuffs">
</div>
</div>
</div>
I am presuming you want to center the div to the page itself.
Try adding:
margin: auto;
It would center the div when used with width: 50%; Else, it would stretch and center.
Related
This question already has answers here:
How can I position my div at the bottom of its container?
(25 answers)
Closed 10 months ago.
I want to keep the button at bottom previously which I was making it possible by writing more text to push it to bottom but it wasn't responsive for every device any way I can keep the button in a div at bottom?
:root {
--clr-primary: #651fff;
--clr-gray: #37474f;
--clr-gray-light: #b0bec5;
}
* {
box-sizing: border-box;
font-family: "Open Sans", sans-serif;
margin: 0;
padding: 0;
}
body {
color: var(--clr-gray);
margin: 2rem;
}
.wrapper-grid {
display: grid;
grid-template-columns: repeat(auto-fit, 20rem);
justify-content: center;
}
.container {
overflow: hidden;
box-shadow: 0px 2px 8px 0px var(--clr-gray-light);
background-color: white;
text-align: center;
border-radius: 1rem;
position: relative;
margin: 2rem 0.5rem;
}
.banner-img {
position: absolute;
background-image: url(https://gaito.000webhostapp.com/im/istockphoto-1307289824-640x640.jpg);
height: 10rem;
width: 100%;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.profile-img {
width: 8rem;
clip-path: circle(60px at center);
margin-top: 4.5rem;
height: 8rem;
}
.name {
font-weight: bold;
font-size: 1.5rem;
}
.description {
margin: 1rem 2rem;
font-size: 0.9rem;
}
.btn {
width: 100%;
border: none;
font-size: 1rem;
font-weight: bold;
color: white;
padding: 1rem;
background-color: var(--clr-primary);
}
<div class="wrapper-grid">
<div class="container">
<div class='banner-img'></div>
<img src='https://i.pinimg.com/originals/49/09/f9/4909f9e82c492b1e4d52c2bcd9daaf97.jpg' class="profile-img">
<h1 class="name">Slime</h1>
<p class="description">Slimes also commonly called ooze are common types of</p>
<button class='btn'>Attack this dungeon </button>
</div>
<div class="container">
<div class='banner-img'></div>
<img src='https://static.wikia.nocookie.net/kumo-desu-ga-nani-ka/images/4/4c/Mother_1.png/' alt='profile image' class="profile-img">
<h1 class="name">Gaint spider</h1>
<p class="description">This creature shoots sticky strands of webbing from its abdomen which are most commonly found underground, making their lairs on ceilings or in dark, web-filled crevices.</p>
<button class='btn'>Attack this dungeon </button>
</div>
</div>
Codepen
My solution would be:
.container {
display: flex;
flex-flow: column nowrap; /* Flex everything inside your cards vertically */
}
.description {
flex-grow: 1;
/*
When a card has more space (because another card is taller
with more info) - grow the description
*/
}
Here is a working snippet, click Full page top right to see it working:
:root {
--clr-primary: #651fff;
--clr-gray: #37474f;
--clr-gray-light: #b0bec5;
}
* {
box-sizing: border-box;
font-family: "Open Sans", sans-serif;
margin: 0;
padding: 0;
}
body {
color: var(--clr-gray);
margin: 2rem;
}
.wrapper-grid {
display: grid;
grid-template-columns: repeat(auto-fit, 20rem);
justify-content: center;
}
.container {
overflow: hidden;
display: flex;
flex-flow: column nowrap;
align-items: center;
box-shadow: 0px 2px 8px 0px var(--clr-gray-light);
background-color: white;
text-align: center;
border-radius: 1rem;
position: relative;
margin: 2rem 0.5rem;
}
.banner-img {
position: absolute;
background-image: url(https://gaito.000webhostapp.com/im/istockphoto-1307289824-640x640.jpg);
height: 10rem;
width: 100%;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.profile-img {
width: 8rem;
clip-path: circle(60px at center);
margin-top: 4.5rem;
height: 8rem;
}
.name {
font-weight: bold;
font-size: 1.5rem;
}
.description {
flex-grow: 1;
margin: 1rem 2rem;
font-size: 0.9rem;
}
.btn {
width: 100%;
border: none;
font-size: 1rem;
font-weight: bold;
color: white;
padding: 1rem;
background-color: var(--clr-primary);
}
<div class="wrapper-grid">
<div class="container">
<div class='banner-img'></div>
<img src='https://i.pinimg.com/originals/49/09/f9/4909f9e82c492b1e4d52c2bcd9daaf97.jpg' class="profile-img">
<h1 class="name">Slime</h1>
<p class="description">Slimes also commonly called ooze are common types of</p>
<button class='btn'>Attack this dungeon </button>
</div>
<div class="container">
<div class='banner-img'></div>
<img src='https://static.wikia.nocookie.net/kumo-desu-ga-nani-ka/images/4/4c/Mother_1.png/' alt='profile image' class="profile-img">
<h1 class="name">Gaint spider</h1>
<p class="description">This creature shoots sticky strands of webbing from its abdomen which are most commonly found underground, making their lairs on ceilings or in dark, web-filled crevices.</p>
<button class='btn'>Attack this dungeon </button>
</div>
</div>
I have a Popular News section where I'm trying to display six news articles in a flex pattern. The problem I'm having is that I cannot get the flex items to be closer together. How do I do that?
EDIT: I've added the entire code for the bottom half.
This is how it currently looks:
This is how I want it to look:
.firstsection {
width: 100%;
height: 100;
}
.firstsection h1 {
color: rgb(255, 255, 255);
display: block;
font-size: 36px;
font-weight: 300;
line-height: 42.0001px;
padding-bottom: 14px;
text-align: center;
}
.firstsection {
display: block;
width: 100%;
height: 400px;
background-color: #414141;
float: left;
}
.bottomheader {
margin-top: 40px;
color: white;
font-size: 40px;
position: relative;
top: -20px;
}
.READMORE {
display: inline;
position: relative;
top: -40px;
left: 642px;
font-size: 16px;
text-align: center;
border: 1px solid white;
height: 50px;
padding: 20px;
}
.pop .READMORE a {
color: white;
text-decoration: none;
}
.h1,
.h2,
.h3,
.h4,
.h5,
.h6 {
color: black;
width: 30%;
border-top: 3px solid red;
background-color: white;
height: 80px;
}
.firstsection {
display: flex;
flex-wrap: wrap;
justify-content: space-around;
}
.pop {
float: right;
height: 100px;
width: 100%;
text-align: center;
}
<section style="background-color: #293352" class="pop">
<h1 class="bottomheader">Popular News</h1>
<h4 class="READMORE">READ MORE</h4>
</section>
<section style="background-color: #293352" class="firstsection">
<h3 class="h1">content</h3>
<h3 class="h2">content</h3>
<h3 class="h3">content</h3>
<h3 class="h4">content</h3>
<h3 class="h5">content</h3>
<h3 class="h6">content</h3>
</section>
All you really need to do is add align-content: flex-start. This aligns wrapped lines to the start of the flex container and has similar options as align-items. See align-content
I created a fiddle
.firstsection {
display: flex;
flex-wrap: wrap;
justify-content: space-around;
align-content: flex-start;
}
In this example I changed the 'cards' to divs and used a card class, and added a little padding. This may or may not be what you want but maybe it helps.
You can use the CSS GRID to align them properly.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.firstsection {
display: block;
width: 100%;
background-color: #293352;
padding: 50px 20px;
}
h3 {
color: black;
width: 100%;
border-top: 3px solid red;
background-color: white;
height: 130px;
margin: 0;
}
.firstsection {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}
<section class="firstsection">
<h3>content</h3>
<h3>content</h3>
<h3>content</h3>
<h3>content</h3>
<h3>content</h3>
<h3>content</h3>
</section>
Codepen: https://codepen.io/manaskhandelwal1/pen/XWjobLx
here is another approach with grid , using pseudo elements to shrink the visible rows to the center, and sizing a few elements via width and max-width:
example below to run in fullpage mode then resize the window to see if the behavior is what you expect.
body {
margin: 0;
background-color: #293352;
}
.grid {
display: grid;
grid-template-rows: auto 1fr;
min-height: 100vh;
}
.pop {
color: white;
text-align: center;
position: relative;
padding: 1em 10em;
}
.pop h4 {
position: absolute;
right: 1rem;
top: 0.5rem;
border: solid 1px;
padding: 1em;
}
section.firstsection {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: 1fr repeat(auto-fill, max-content) 1fr;
align-items: center;
width: max-content;
max-width:100%;
margin: auto;
gap: 1em;
}
.firstsection:before,
.firstsection:after {
content: "";
grid-column: span 3;
}
.firstsection > * {
box-sizing: border-box;
min-width: 26%;
background: white;
height: 100%;
padding: 1em;
border-top: solid red;
max-width:30vw;
}
<div class="grid">
<section class="pop">
<h1 class="bottomheader">Popular News</h1>
<h4 class="READMORE">READ MORE</h4>
</section>
<section class="firstsection">
<h3 class="h1">content</h3>
<h3 class="h2">content</h3>
<h3 class="h3">content</h3>
<h3 class="h4">content <br> and an extra line</h3>
<h3 class="h5">content</h3>
<h3 class="h6">content or grow the column to my width if there's enough room.</h3>
</section>
</div>
here is a codepen with a grid of 9 boxes to play with : https://codepen.io/gc-nomade/pen/dypwYvY
This question already has answers here:
Center one and right/left align other flexbox element
(11 answers)
Closed 3 years ago.
I'm trying to center the title of an html card within the header div, which has an "x" button.
I've tried centering using margin: 0 auto but it centers within what is left/remainder space of the div. What i want is to center within the full length of the div (true center).
<div class="timer-card">
<div class="head">
<p class="title">Timer title</p>
<button class="close">X</button>
</div>
<div class="body">
<p class="countdown">0:00</p>
<div class="footer"><button>Stop</button><button>Play</button><button>Reset</button></div>
</div>
</div>
.timer-card {
border-color: grey;
border-width: 1px;
border-style: solid;
width: 200px;
background-color: grey;
box-sizing: border-box;
padding: 10px;
}
.timer-card .head {
display: flex;
justify-content: space-between;
}
.timer-card p.title {
text-align: center;
}
https://codepen.io/anon/pen/PvZqZz
position: relative on the .head element;
position: absolute; right: 0; on the .close element;
margin: 0 auto; on the .title element.
Code snippet below:
.timer-card {
border-color: grey;
border-width: 1px;
border-style: solid;
width: 200px;
background-color: grey;
box-sizing: border-box;
padding: 10px;
}
.timer-card .countdown {
font-size: 40px;
}
.timer-card button.close, .timer-card .title {
display: inline-block;
}
.timer-card .head {
display: flex;
justify-content: space-between;
position: relative;
}
.timer-card .title {
margin: 0 auto;
}
.timer-card .close {
position: absolute;
right: 0;
}
.timer-card .body p.countdown {
margin: 0 auto;
text-align: center;
}
.timer-card .footer {
padding-top: 15px;
display: flex;
justify-content: center;
}
.timer-card p.title {
text-align: center;
}
<div class="timer-card">
<div class="head">
<p class="title">Timer title</p>
<button class="close">X</button>
</div>
<div class="body">
<p class="countdown">0:00</p>
<div class="footer"><button>Stop</button><button>Play</button><button>Reset</button></div>
</div>
</div>
A quick dirty hack would be to just add half the size of the X button as margin-left on your CSS
.title {
width: 100%;
margin-left: 25px;
}
This question already has answers here:
How can I vertically center a div element for all browsers using CSS?
(48 answers)
Best way to center a <div> on a page vertically and horizontally? [duplicate]
(30 answers)
Closed 4 years ago.
I've tried vertical-align: middle; and margin: auto 0; but it doesn't seem to be working? Is there something I'm missing here? My logic is, I put the vertical-align: middle; in the parent container. Shouldn't that center the "children" class into the center vertically? I've also tried adding, padding:auto 0; to the "children" class but that didn't seem to do anything either...
body {
margin: 0;
padding: 0;
}
h1 {
margin: 0;
font-size: 50px;
}
.btn {
border: 4px solid #079992;
padding: 2px 15px;
color: #079992;
font-size: 1.5em;
font-weight: 800;
display: inline-block;
}
.btn:hover {
background-color: #38ada9;
cursor: pointer;
}
.content {
display: block;
background-color: #b2bec3;
text-align: center;
height: 100vh;
font-family: helvetica;
vertical-align: middle;
}
<div class="bg-img">
<div class="content">
<div class="children">
<h1>Random Quote Generator</h1>
<p>Press the button below for an inspirational quote!</p>
<div class="btn">Click Me Bro!</div>
</div>
</div>
</div>
Try to use display:flex here in the parent class .content and margin:auto in .children class to center it horizontally and vertically...
body {
margin: 0;
padding: 0;
}
h1 {
margin: 0;
font-size: 50px;
}
.btn {
border: 4px solid #079992;
padding: 2px 15px;
color: #079992;
font-size: 1.5em;
font-weight: 800;
display: inline-block;
}
.btn:hover {
background-color: #38ada9;
cursor: pointer;
}
.content {
display: flex;
background-color: #b2bec3;
text-align: center;
height: 100vh;
font-family: helvetica;
vertical-align: middle;
}
.content .children {
margin: auto;
}
<body>
<div class="bg-img">
<div class="content">
<div class="children">
<h1>Random Quote Generator</h1>
<p>Press the button below for an inspirational quote!</p>
<div class="btn">Click Me Bro!</div>
</div>
</div>
</div>
</body>
However vertical-align works on tables with display:table-cell...and also you will need to apply vertical-align:middle in the elements itself not in the parent
body {
margin: 0;
padding: 0;
}
h1 {
margin: 0;
font-size: 50px;
}
.btn {
border: 4px solid #079992;
padding: 2px 15px;
color: #079992;
font-size: 1.5em;
font-weight: 800;
display: inline-block;
}
.btn:hover {
background-color: #38ada9;
cursor: pointer;
}
.content {
display: table;
width: 100%;
background-color: #b2bec3;
text-align: center;
height: 100vh;
font-family: helvetica;
}
.content .children {
vertical-align: middle;
display: table-cell;
}
<body>
<div class="bg-img">
<div class="content">
<div class="children">
<h1>Random Quote Generator</h1>
<p>Press the button below for an inspirational quote!</p>
<div class="btn">Click Me Bro!</div>
</div>
</div>
</div>
</body>
body{
margin: 0;
padding: 0;
}
h1{
margin: 0;
font-size: 50px;
}
.btn{
border: 4px solid #079992;
padding: 2px 15px;
color: #079992;
font-size: 1.5em;
font-weight: 800;
display: inline-block;
}
.btn:hover{
background-color: #38ada9;
cursor: pointer;
}
.content{
display: flex;
background-color: #b2bec3;
text-align: center;
height: 100vh;
width:100%;
font-family: helvetica;
justify-content:center;
align-items:center;
}
<div class="bg-img">
<div class="content">
<div class="children">
<h1>Random Quote Generator</h1>
<p>Press the button below for an inspirational quote!</p>
<div class="btn">Click Me Bro!</div>
</div>
</div>
</div>
use flex method to vertical align.
https://jsfiddle.net/raj_mutant/529bcr98/
so I'm trying to do a "cast" section for one of my assignments and I want the actor's character to appear when the actor is hovered over. How would I achieve this? When hiding the display of the deadpool div, it leaves a big gap in the page. I want this to not show until Ryan Reynolds is hovered over.
article {
display: flex;
flex-wrap: wrap;
margin: auto;
padding-top: 12px;
padding-bottom: 12px;
background-color: #8b2323;
width: 48vw;
min-height: 200px;
min-width: 391px;
font-family: verdana, sans-serif;
justify-content: center;
}
.castcontainer {
flex-wrap: wrap;
min-width: 215px;
width: 20%;
height: 30%;
overflow: hidden;
padding: 5px;
}
#cast {
border-radius: 50%;
width: 100%;
}
.cast2 {
display: none;
text-align: center;
background-color: #8b1a1a;
border-radius: 10px;
padding: 10px;
}
.cast:hover+.cast2 {
display: block;
}
.cast {
text-align: center;
background-color: #8b1a1a;
border-radius: 10px;
padding: 10px;
}
p {
background: white;
}
<article>
<div class="castcontainer">
<div class="cast">
<img src="https://pbs.twimg.com/profile_images/741703039355064320/ClVbjlG-.jpg" id="cast">
<p><b>Ryan Reynalds</b></p>
</div>
</div>
<div class="castcontainer">
<div class="cast2">
<img src="http://cdn03.cdn.justjared.com/wp-content/uploads/headlines/2017/08/joi-harris-rip.jpg" id="cast">
<p><b>Wade Wilson</b></p>
</div>
</div>
</article>
Let me offer a more radical departure from your current code:
.cast * {
box-sizing: border-box;
}
.cast {
border-radius: 10px;
background: #8b2323;
font-family: Verdana, sans-serif;
text-align: center;
padding: 12px;
}
.cast img {
border-radius: 50%;
max-height: 300px;
}
.cast strong {
background: white;
display: block;
border-radius: 10px;
margin-top: 5px;
}
.cast .actor,
.cast .role {
width: 100%;
}
.cast .actor {
display: block;
z-index: 2;
}
.cast .role {
display: none;
z-index: 1;
}
.cast:hover .actor {
display: none;
}
.cast:hover .role {
display: block;
}
<article class="cast">
<div class="actor">
<img src="https://pbs.twimg.com/profile_images/741703039355064320/ClVbjlG-.jpg">
<strong>Ryan Reynalds</strong>
</div>
<div class="role">
<img src="http://cdn03.cdn.justjared.com/wp-content/uploads/headlines/2017/08/joi-harris-rip.jpg">
<strong>Wade Wilson</strong>
</div>
</article>
This reduces the number of child elements and (in my opinion) makes selecting which element to show/hide that much easier. You're targeting the :hover event of the parent wrapper and instead of trying to use an ID (which cannot be reused) you're targeting .actor and .role.
One concern would be to make sure that the images for each were the same dimension, otherwise on change you could get some transition that was unappealing if the box had to resize.
Might this be what you're looking to do?
Added:
article:hover .cast {
display: none;
}
article:hover .cast2 {
display: block;
}
article {
display: flex;
flex-wrap: wrap;
margin: auto;
padding-top: 12px;
padding-bottom: 12px;
background-color: #8b2323;
width: 48vw;
min-height: 200px;
min-width: 391px;
font-family: verdana, sans-serif;
justify-content: center;
}
article:hover .cast {
display: none;
}
article:hover .cast2 {
display: block;
}
.castcontainer {
flex-wrap: wrap;
min-width: 215px;
width: 20%;
height: 30%;
overflow: hidden;
padding: 5px;
}
#cast {
border-radius: 50%;
width: 100%;
}
.cast2 {
display: none;
text-align: center;
background-color: #8b1a1a;
border-radius: 10px;
padding: 10px;
}
.cast:hover+.cast2 {
display: block;
}
.cast {
text-align: center;
background-color: #8b1a1a;
border-radius: 10px;
padding: 10px;
}
p {
background: white;
}
<article>
<div id="one" class="castcontainer">
<div class="cast">
<img src="https://pbs.twimg.com/profile_images/741703039355064320/ClVbjlG-.jpg" id="cast">
<p><b>Ryan Reynalds</b></p>
</div>
</div>
<div id="two"class="castcontainer">
<div class="cast2">
<img src="http://cdn03.cdn.justjared.com/wp-content/uploads/headlines/2017/08/joi-harris-rip.jpg" id="cast">
<p><b>Wade Wilson</b></p>
</div>
</div>
</article>
<article>
<div class="castcontainer" id="show1">
<div class="cast">
<img src="https://pbs.twimg.com/profile_images/741703039355064320/ClVbjlG-.jpg" class="castImg" id="CastImgRef">
<p><b>Ryan Reynalds</b></p>
</div>
</div>
<div class="castcontainer" id="show2">
<div class="cast2">
<img src="http://cdn03.cdn.justjared.com/wp-content/uploads/headlines/2017/08/joi-harris-rip.jpg" class="castImg">
<p><b>Wade Wilson</b></p>
</div>
</div>
</article>
jQuery(function ($) {
$('#show1').hover(function () {
$(this).find('img').attr('src', function (i, src) {
return src.replace('https://pbs.twimg.com/profile_images/741703039355064320/ClVbjlG-.jpg', 'http://cdn03.cdn.justjared.com/wp-content/uploads/headlines/2017/08/joi-harris-rip.jpg')
})
$('#textChange').text('Wade Wilson');
}, function () {
$(this).find('img').attr('src', function (i, src) {
return src.replace('http://cdn03.cdn.justjared.com/wp-content/uploads/headlines/2017/08/joi-harris-rip.jpg', 'https://pbs.twimg.com/profile_images/741703039355064320/ClVbjlG-.jpg')
})
$('#textChange').text('Ryan Reynalds');
})
})
Add thisjquery and it will work fine
https://jsfiddle.net/dLwxm2ox/8/
article {
display: flex;
flex-wrap: wrap;
margin: auto;
padding-top: 12px;
padding-bottom: 12px;
background-color: #8b2323;
width: 48vw;
min-height: 200px;
min-width: 391px;
font-family: verdana, sans-serif;
justify-content: center;
}
article:hover .cast {
display: none;
}
article:hover .cast2 {
display: block;
}
.castcontainer {
flex-wrap: wrap;
min-width: 215px;
width: 20%;
height: 30%;
overflow: hidden;
padding: 5px;
}
#cast {
border-radius: 50%;
width: 100%;
}
.cast2 {
display: none;
text-align: center;
background-color: #8b1a1a;
border-radius: 10px;
padding: 10px;
}
.cast:hover+.cast2 {
display: block;
}
.cast {
text-align: center;
background-color: #8b1a1a;
border-radius: 10px;
padding: 10px;
}
p {
background: white;
}
<article>
<div id="one" class="castcontainer cast">
<img src="https://pbs.twimg.com/profile_images/741703039355064320/ClVbjlG-.jpg" id="cast">
<p><b>Ryan Reynalds</b></p>
</div>
<div id="two"class="castcontainer cast2">
<img src="http://cdn03.cdn.justjared.com/wp-content/uploads/headlines/2017/08/joi-harris-rip.jpg" id="cast">
<p><b>Wade Wilson</b></p>
</div>
</article>
The inner div seems to be unnecessary where class="cast" and class="cast2". Remove the div's and add the class to its parent.