While following a tutorial on YouTube, I encountered an issue where my expanding content, when exceeding the size of the window in a small screen view, prevented me from scrolling to the top. Interestingly, I could scroll to the bottom and back up to that point, but not beyond it.
const accordion = document.getElementsByClassName('content-container');
for (i = 0; i < accordion.length; i++) {
accordion[i].addEventListener('click',
function() {
this.classList.toggle('active'); // toggles the class name on or off
}
)
}
body {
background: url(/projects/images/project-3-image.jpg) no-repeat center center/cover;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.accordion {
background-color: #fff;
width: 65%;
padding: 30px;
border-radius: 30px;
}
.accordion .accordion-title {
margin-bottom: 30px;
text-align: center;
}
.accordion .content-container .question {
padding: 18px 0px;
font-size: 22px;
cursor: pointer;
border-bottom: 1px solid #000;
position: relative;
transition: .5s;
}
.accordion .content-container .question::after {
content: '+';
position: absolute;
right: -5px;
}
.accordion .content-container .answer {
padding-top: 15px;
font-size: 22px;
line-height: 1.5;
width: 100%;
height: 0px;
overflow: hidden;
transition: .5s;
}
/* JS Styling Link */
.accordion .content-container.active .answer {
height: 200px;
}
.accordion .content-container.active .question {
font-size: 18px;
transition: .5s;
}
.accordion .content-container.active .question::after {
content: '-';
font-size: 30px;
transition: .5s;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Accordion</title>
<link rel="stylesheet" href="project3.css">
</head>
<body>
<div class="accordion">
<h2 class="accordion-title">Frequently Asked Questions</h2>
<div class="content-container">
<div class="question">What is the Return Policy?</div>
<div class="answer">Lorem ipsum dolor sit amet consectetur adipisicing elit. Error, animi! Accusamus temporibus tenetur perferendis delectus asperiores libero cumque repudiandae, nesciunt totam deleniti quis nostrum? Obcaecati minima consequatur incidunt id vero?</div>
</div>
<div class="content-container">
<div class="question">What is the Return Policy?</div>
<div class="answer">Lorem ipsum dolor sit amet consectetur adipisicing elit. Error, animi! Accusamus temporibus tenetur perferendis delectus asperiores libero cumque repudiandae, nesciunt totam deleniti quis nostrum? Obcaecati minima consequatur incidunt id vero?</div>
</div>
<div class="content-container">
<div class="question">What is the Return Policy?</div>
<div class="answer">Lorem ipsum dolor sit amet consectetur adipisicing elit. Error, animi! Accusamus temporibus tenetur perferendis delectus asperiores libero cumque repudiandae, nesciunt totam deleniti quis nostrum? Obcaecati minima consequatur incidunt id vero?</div>
</div>
</div>
<script src="project3.js"></script>
</body>
</html>
As someone new to CSS and HTML, I attempted to use margin: auto;
instead of justify-content: center;
and align-items: center;
in order to resolve the issue, but unfortunately, it didn't work. It seems that the problem arises because flex prioritizes centering content regardless of whether it extends beyond the outer limits.