I am not very well versed in HTML and CSS. I am currently building a website and I integrated a sidebar into my web application using Bootstrap, but it has messed my layouts, and I can't seem to figure out how to move the content in a block to the left of side of my sidebar.
Please view the image on this link
[1]: https://i.stack.imgur.com/XFaVv.png
As seen in the picture above, my sidebar is located at the right and my content is located at the below of my sidebar. Below are my codes for my sidebar and the base template.
// Not bringing sidebar when active
const menu_toggle = document.querySelector('.menu-toggle');
const sidebar = document.querySelector('.sidebar');
// When the button is clicked the sidebar will be active in the following code
menu_toggle.addEventListener('click', () => {
menu_toggle.classList.toggle('is-active');
sidebar.classList.toggle('is-active');
})
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
/* Sidebar Section */
.app {
display: flex;
min-height: 100vh;
}
.sidebar {
flex: 1 1 0;
max-width: 300px;
padding: 20px;
background: linear-gradient(#ed7014, #fa5b3d);
/* With Scrollbar */
position: fixed;
top: 0;
bottom: 0;
left: 0;
z-index: 1000;
display: block;
overflow-x: hidden;
overflow-y: auto;
/* Scrollable contents if viewport is shorter than content. */
border-right: 1px solid #eee;
}
.sidebar h3 {
font-family: 'Bungee', cursive;
color: #fff;
}
.sidebar .menu {
margin: 0 -1rem;
}
.sidebar .menu .menu-item {
display: block;
padding: 1em;
color: #fff;
text-decoration: none;
transition: 0.2s linear;
}
.sidebar .menu .menu-item:hover,
.sidebar .menu .menu-item.is-active {
color: rgb(255, 238, 151);
;
border-right: 5px solid rgb(255, 238, 151);
}
/* Content Scrollbar */
::-webkit-scrollbar {
width: 10px;
}
::-webkit-scrollbar-track {
border-radius: 5px;
}
::-webkit-scrollbar-thumb {
border-radius: 5px;
background-color: #ffa500;
}
::-webkit-scrollbar-thumb:hover {
background-color: #d67229;
}
/* Content Section */
.content {
flex: 1 1 0;
padding: 2rem;
background: linear-gradient(#dd5713, #d67229);
}
.content img {
width: 100%;
max-width: 300px;
display: block;
margin: auto;
}
.content h1 {
font-family: 'Bungee', cursive;
color: #fff;
margin-bottom: 1rem;
}
.content strong {
font-family: 'Bungee', cursive;
color: #fff;
}
.content p {
text-align: justify;
color: #fff;
}
/* Sample Code Content */
.sample-code {
width: 65%;
background: rgb(255, 238, 151);
padding: 1em;
margin-bottom: 2em;
border-radius: 10px;
box-shadow: 15px 15px;
}
.sample-code p {
color: #000;
}
/* Button Sidebar section */
.menu-toggle {
display: none;
position: fixed;
top: 2rem;
right: 2rem;
width: 60px;
height: 60px;
border-radius: 99px;
background-color: #ed7014;
cursor: pointer;
}
.button {
position: relative;
top: calc(50% - 2px);
left: 50%;
transform: translate(-50%, -50%);
width: 32px;
}
.button>span,
.button>span::before,
.button>span::after {
display: block;
position: absolute;
width: 100%;
height: 4px;
border-radius: 99px;
background-color: #fff;
transition-duration: .25s;
}
.button>span::before {
content: '';
top: -8px;
}
.button>span::after {
content: '';
top: 8px;
}
/* Button Animation */
.menu-toggle.is-active .button>span {
transform: rotate(45deg);
}
.menu-toggle.is-active .button>span::before {
top: 0;
transform: rotate(0deg);
}
.menu-toggle.menu-toggle.is-active .button>span::after {
top: 0;
transform: rotate(90deg);
}
/* Sidebar Footer */
.white-section {
text-align: center;
}
.p-white-sec {
color: #fff;
}
.container-fluid {
padding: 7px 15px;
}
.footer-icon {
margin: 20px 10px;
color: #fff;
}
.footer-icon:hover {
color: #ffa500;
transition: 0.2s linear;
}
/* Mobile Size */
#media (max-width: 1024px) {
/* Sidebar Section */
.sidebar {
max-width: 200px;
}
.sidebar img {
height: 6.5rem;
width: 10rem;
}
}
#media (max-width: 768px) {
/* Sidebar Section */
.menu-toggle {
display: block;
}
.sidebar {
position: fixed;
top: 0;
left: -300px;
height: 100vh;
width: 100%;
max-width: 300px;
transition: 0.2s linear;
}
.sidebar.is-active {
left: 0;
}
.sidebar img {
height: 6.5rem;
width: 10rem;
}
/* Content */
.content {
padding-top: 8rem;
}
}
<!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>Codin'up | HTML Course</title>
<!-- Bootstrap -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<!-- Google Fonts -->
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght#100;400;900&family=Ubuntu:ital,wght#0,300;1,400;1,700&display=swap" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css2?family=Bungee&family=Poppins&display=swap" rel="stylesheet">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Source+Code+Pro&display=swap" rel="stylesheet">
<!-- Favicon -->
<link rel="shortcut icon" href="/favicon.png">
<!-- CSS Stylesheet -->
<link rel="stylesheet" href="/CSS/List of Course/HTML Course.css">
</head>
<body>
<section class="app">
<!-- Button for Sidebar -->
<div class="menu-toggle">
<div class="button">
<span></span>
</div>
</div>
<!-- Sidebar Section -->
<aside class="sidebar">
<a href="/HTML/List of Course/HTML Course.html">
<img src="/Logo/Codin'up Logo (1) White.png" alt="Codin'up">
</a>
<h3>HTML Course</h3>
<nav class="menu">
Home
Courses
</nav>
<hr>
<nav class="menu">
HTML Introduction
HTML Course no.
HTML Course no.
HTML Course no.
HTML Course no.
HTML Course no.
HTML Course no.
HTML Course no.
HTML Course no.
HTML Course no.
HTML Course no.
HTML Course no.
HTML Course no.
HTML Course no.
HTML Course no.
HTML Course no.
HTML Course no.
HTML Course no.
HTML Course no.
HTML Course no.
</nav>
<hr>
<!-- Sidebar Footer -->
<footer class="white-section" id="footer">
<div class="container-fluid">
<i class="fa-brands fa-twitter footer-icon"></i>
<i class="fa-brands fa-facebook-f footer-icon"></i>
<i class="fa-brands fa-instagram footer-icon"></i>
<i class="fa-solid fa-envelope footer-icon"></i>
<p class="p-white-sec">© 2022 ITMAWD21 Group 3</p>
</div>
</footer>
</aside>
<!-- Content Section -->
<main class="content">
<h1>Introduction To HTML</h1>
<img src="/Logo/HTML Logo.png" alt="Codin'up Logo">
<strong>Description</strong>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Praesentium ipsa, quod, placeat cupiditate enim hic natus maiores nobis sequi nisi odit voluptas earum quas perspiciatis qui explicabo recusandae ullam quibusdam? Lorem ipsum dolor sit amet
consectetur, adipisicing elit. Dignissimos obcaecati exercitationem voluptatem accusamus, beatae sunt voluptatibus quam odio magnam, mollitia minima dicta distinctio, amet accusantium! Dolore debitis qui quis iusto. Lorem, ipsum dolor sit amet
consectetur adipisicing elit. Accusantium harum corrupti nisi ratione aperiam commodi rerum. Delectus illum suscipit distinctio unde iste veritatis voluptate nulla, enim rem harum ullam rerum. Lorem ipsum dolor sit amet consectetur adipisicing
elit. Ut cumque totam ipsum, quia voluptate quam esse reprehenderit explicabo atque, molestiae doloribus earum repellendus architecto aperiam minima impedit dicta autem laboriosam! Lorem ipsum dolor sit amet consectetur adipisicing elit. Similique,
corrupti adipisci. Itaque sint, nemo laborum a ratione corrupti laboriosam tenetur soluta pariatur nostrum fuga, earum voluptate ducimus iure ipsum saepe? Lorem, ipsum dolor sit amet consectetur adipisicing elit. Commodi repudiandae recusandae
iste, molestiae modi ea numquam vero repellat. Doloremque iusto labore nobis placeat perspiciatis optio repellendus eius adipisci voluptas expedita. Lorem ipsum dolor sit, amet consectetur adipisicing elit. Dolor doloremque rem debitis? Sed quod
eligendi inventore deleniti aliquid velit, magni perferendis, quaerat officiis in consequuntur tenetur ipsum animi tempore asperiores.</p>
<!-- Sample Code Content -->
<div class="sample-code">
<p><html></p>
<p><span style="padding-left: 20px;"><head></span></p>
<p><span style="padding-left: 40px;"><title>HTML Course</title></span></p>
<p><span style="padding-left: 20px;"></head></span></p>
<p><span style="padding-left: 20px;"><body></span></p>
<p><span style="padding-left: 40px;"><p>Hello World<p></span></p>
<p><span style="padding-left: 20px;"></body></span></p>
<p></html></p>
</div>
<strong>Code Description</strong>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Qui iusto, eveniet repudiandae ratione sint harum pariatur id numquam doloremque sequi eaque quo accusamus ab nihil aut alias inventore! Cumque, tempore.</p>
</main>
</section>
<!-- Font Awesome -->
<script src="https://kit.fontawesome.com/c9df5e490e.js" crossorigin="anonymous"></script>
<!-- Bootstrap JS & External JS -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
<script src="/JS/List of Course/HTML Course.js"></script>
</body>
</html>
Can someone help me fit the content to the right beside my sidebar especially its layout? Thanks for the big help!
I would just apply position: sticky and height: 100vh on the sidebar:
position: sticky;
height: 100vh;
Giving it height: 100vh makes sure the sidebar always stretches to the entire height of the screen. position: sticky works in a similiar fashion to position: fixed but it doesn't affect the document flow in the same way. You can then apply positioning values such as top: 0 and left: 0, but those properties are already applied in your snippet.
// Not bringing sidebar when active
const menu_toggle = document.querySelector('.menu-toggle');
const sidebar = document.querySelector('.sidebar');
// When the button is clicked the sidebar will be active in the following code
menu_toggle.addEventListener('click', () => {
menu_toggle.classList.toggle('is-active');
sidebar.classList.toggle('is-active');
})
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
/* Sidebar Section */
.app {
display: flex;
min-height: 100vh;
}
.sidebar {
flex: 1 1 0;
max-width: 300px;
padding: 20px;
background: linear-gradient(#ed7014, #fa5b3d);
/* With Scrollbar */
/* Edit start */
position: sticky;
height: 100vh;
/* Edit end */
top: 0;
bottom: 0;
left: 0;
z-index: 1000;
display: block;
overflow-x: hidden;
overflow-y: auto;
/* Scrollable contents if viewport is shorter than content. */
border-right: 1px solid #eee;
}
.sidebar h3 {
font-family: 'Bungee', cursive;
color: #fff;
}
.sidebar .menu {
margin: 0 -1rem;
}
.sidebar .menu .menu-item {
display: block;
padding: 1em;
color: #fff;
text-decoration: none;
transition: 0.2s linear;
}
.sidebar .menu .menu-item:hover,
.sidebar .menu .menu-item.is-active {
color: rgb(255, 238, 151);
;
border-right: 5px solid rgb(255, 238, 151);
}
/* Content Scrollbar */
::-webkit-scrollbar {
width: 10px;
}
::-webkit-scrollbar-track {
border-radius: 5px;
}
::-webkit-scrollbar-thumb {
border-radius: 5px;
background-color: #ffa500;
}
::-webkit-scrollbar-thumb:hover {
background-color: #d67229;
}
/* Content Section */
.content {
flex: 1 1 0;
padding: 2rem;
background: linear-gradient(#dd5713, #d67229);
}
.content img {
width: 100%;
max-width: 300px;
display: block;
margin: auto;
}
.content h1 {
font-family: 'Bungee', cursive;
color: #fff;
margin-bottom: 1rem;
}
.content strong {
font-family: 'Bungee', cursive;
color: #fff;
}
.content p {
text-align: justify;
color: #fff;
}
/* Sample Code Content */
.sample-code {
width: 65%;
background: rgb(255, 238, 151);
padding: 1em;
margin-bottom: 2em;
border-radius: 10px;
box-shadow: 15px 15px;
}
.sample-code p {
color: #000;
}
/* Button Sidebar section */
.menu-toggle {
display: none;
position: fixed;
top: 2rem;
right: 2rem;
width: 60px;
height: 60px;
border-radius: 99px;
background-color: #ed7014;
cursor: pointer;
}
.button {
position: relative;
top: calc(50% - 2px);
left: 50%;
transform: translate(-50%, -50%);
width: 32px;
}
.button>span,
.button>span::before,
.button>span::after {
display: block;
position: absolute;
width: 100%;
height: 4px;
border-radius: 99px;
background-color: #fff;
transition-duration: .25s;
}
.button>span::before {
content: '';
top: -8px;
}
.button>span::after {
content: '';
top: 8px;
}
/* Button Animation */
.menu-toggle.is-active .button>span {
transform: rotate(45deg);
}
.menu-toggle.is-active .button>span::before {
top: 0;
transform: rotate(0deg);
}
.menu-toggle.menu-toggle.is-active .button>span::after {
top: 0;
transform: rotate(90deg);
}
/* Sidebar Footer */
.white-section {
text-align: center;
}
.p-white-sec {
color: #fff;
}
.container-fluid {
padding: 7px 15px;
}
.footer-icon {
margin: 20px 10px;
color: #fff;
}
.footer-icon:hover {
color: #ffa500;
transition: 0.2s linear;
}
/* Mobile Size */
#media (max-width: 1024px) {
/* Sidebar Section */
.sidebar {
max-width: 200px;
}
.sidebar img {
height: 6.5rem;
width: 10rem;
}
}
#media (max-width: 768px) {
/* Sidebar Section */
.menu-toggle {
display: block;
}
.sidebar {
position: fixed;
top: 0;
left: -300px;
height: 100vh;
width: 100%;
max-width: 300px;
transition: 0.2s linear;
}
.sidebar.is-active {
left: 0;
}
.sidebar img {
height: 6.5rem;
width: 10rem;
}
/* Content */
.content {
padding-top: 8rem;
}
}
<!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>Codin'up | HTML Course</title>
<!-- Bootstrap -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<!-- Google Fonts -->
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght#100;400;900&family=Ubuntu:ital,wght#0,300;1,400;1,700&display=swap" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css2?family=Bungee&family=Poppins&display=swap" rel="stylesheet">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Source+Code+Pro&display=swap" rel="stylesheet">
<!-- Favicon -->
<link rel="shortcut icon" href="/favicon.png">
<!-- CSS Stylesheet -->
<link rel="stylesheet" href="/CSS/List of Course/HTML Course.css">
</head>
<body>
<section class="app">
<!-- Button for Sidebar -->
<div class="menu-toggle">
<div class="button">
<span></span>
</div>
</div>
<!-- Sidebar Section -->
<aside class="sidebar">
<a href="/HTML/List of Course/HTML Course.html">
<img src="/Logo/Codin'up Logo (1) White.png" alt="Codin'up">
</a>
<h3>HTML Course</h3>
<nav class="menu">
Home
Courses
</nav>
<hr>
<nav class="menu">
HTML Introduction
HTML Course no.
HTML Course no.
HTML Course no.
HTML Course no.
HTML Course no.
HTML Course no.
HTML Course no.
HTML Course no.
HTML Course no.
HTML Course no.
HTML Course no.
HTML Course no.
HTML Course no.
HTML Course no.
HTML Course no.
HTML Course no.
HTML Course no.
HTML Course no.
HTML Course no.
</nav>
<hr>
<!-- Sidebar Footer -->
<footer class="white-section" id="footer">
<div class="container-fluid">
<i class="fa-brands fa-twitter footer-icon"></i>
<i class="fa-brands fa-facebook-f footer-icon"></i>
<i class="fa-brands fa-instagram footer-icon"></i>
<i class="fa-solid fa-envelope footer-icon"></i>
<p class="p-white-sec">© 2022 ITMAWD21 Group 3</p>
</div>
</footer>
</aside>
<!-- Content Section -->
<main class="content">
<h1>Introduction To HTML</h1>
<img src="/Logo/HTML Logo.png" alt="Codin'up Logo">
<strong>Description</strong>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Praesentium ipsa, quod, placeat cupiditate enim hic natus maiores nobis sequi nisi odit voluptas earum quas perspiciatis qui explicabo recusandae ullam quibusdam? Lorem ipsum dolor sit amet
consectetur, adipisicing elit. Dignissimos obcaecati exercitationem voluptatem accusamus, beatae sunt voluptatibus quam odio magnam, mollitia minima dicta distinctio, amet accusantium! Dolore debitis qui quis iusto. Lorem, ipsum dolor sit amet
consectetur adipisicing elit. Accusantium harum corrupti nisi ratione aperiam commodi rerum. Delectus illum suscipit distinctio unde iste veritatis voluptate nulla, enim rem harum ullam rerum. Lorem ipsum dolor sit amet consectetur adipisicing
elit. Ut cumque totam ipsum, quia voluptate quam esse reprehenderit explicabo atque, molestiae doloribus earum repellendus architecto aperiam minima impedit dicta autem laboriosam! Lorem ipsum dolor sit amet consectetur adipisicing elit. Similique,
corrupti adipisci. Itaque sint, nemo laborum a ratione corrupti laboriosam tenetur soluta pariatur nostrum fuga, earum voluptate ducimus iure ipsum saepe? Lorem, ipsum dolor sit amet consectetur adipisicing elit. Commodi repudiandae recusandae
iste, molestiae modi ea numquam vero repellat. Doloremque iusto labore nobis placeat perspiciatis optio repellendus eius adipisci voluptas expedita. Lorem ipsum dolor sit, amet consectetur adipisicing elit. Dolor doloremque rem debitis? Sed quod
eligendi inventore deleniti aliquid velit, magni perferendis, quaerat officiis in consequuntur tenetur ipsum animi tempore asperiores.</p>
<!-- Sample Code Content -->
<div class="sample-code">
<p><html></p>
<p><span style="padding-left: 20px;"><head></span></p>
<p><span style="padding-left: 40px;"><title>HTML Course</title></span></p>
<p><span style="padding-left: 20px;"></head></span></p>
<p><span style="padding-left: 20px;"><body></span></p>
<p><span style="padding-left: 40px;"><p>Hello World<p></span></p>
<p><span style="padding-left: 20px;"></body></span></p>
<p></html></p>
</div>
<strong>Code Description</strong>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Qui iusto, eveniet repudiandae ratione sint harum pariatur id numquam doloremque sequi eaque quo accusamus ab nihil aut alias inventore! Cumque, tempore.</p>
</main>
</section>
<!-- Font Awesome -->
<script src="https://kit.fontawesome.com/c9df5e490e.js" crossorigin="anonymous"></script>
<!-- Bootstrap JS & External JS -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
<script src="/JS/List of Course/HTML Course.js"></script>
</body>
</html>
I don't know if i understand well but.
Try in the content class to add:
width:100%;
padding-left:300px; (the width of the menu)
.sidebar has a position; fixed.
You could apply styling to the main content <main class="content" style="left: 272px;position: absolute;">. But you must also account for the media queries adjusting the width of the sidebar. E.g.
#media (max-width: 1024px){
.sidebar {
max-width: 200px;
}
main.content {
left: 200px;
}
}
and
#media (max-width: 768px) {
.sidebar {
position: fixed;
top: 0;
left: -300px;
height: 100vh;
width: 100%;
max-width: 300px;
transition: 0.2s linear;
}
main.content {
left: 0;
height: 100%;
}
}
I would also use width property over max-width. To be sure that the content is aligns always as expected.
I have checked the layout maybe you can try using rows and columns to sort it out
<div class="row">
<div class="col-lg-3 col-xs-4">
//Sidebar code
</div>
<div class="col-lg-9 col-xs-8">
//Content code
</div>
</div>
This could be better option and will also be responsive.
If you are not comfortable or familiar then maybe like others suggested you can remove position:fixed styling in .sidebar will solve this problem temporarily
Related
The concept is simple, when the text gets too long for the container, it would infinitely animate (scroll) from left to right, and repeat this processes after it reaches the end.
How should I go about making this?
<!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" />
</head>
<body>
<div class="auto-scroll">
<p>
This text should scroll. Lorem ipsum dolor sit amet consectetur adipisicing elit. Excepturi commodi magni quo officia error ab veniam aspernatur. Quos ducimus, hic, maxime dicta optio quidem
nostrum sint commodi velit, ut distinctio. And return to beginning.
</p>
</div>
<style>
.auto-scroll {
background: black;
padding: 18px;
}
.auto-scroll p {
font-family: monospace;
font-size: 18px;
color: white;
margin: 0;
white-space: nowrap;
overflow: auto;
}
.auto-scroll ::-webkit-scrollbar {
height: 10px; /* will be 0px */
}
.auto-scroll ::-webkit-scrollbar-track {
background: #000;
}
.auto-scroll ::-webkit-scrollbar-thumb {
background: #fff;
}
</style>
</body>
</html>
You don't need javascript you can make it in css like this following code
div{
width: 100%;
background-color: black;
white-space: nowrap;
overflow: hidden;
box-sizing: border-box;
}
p{
display: inline-block;
color: #FFF;
padding-left: 100%;
animation: move 25s linear infinite;
}
#keyframes move {
0% { transform: translate(0, 0); }
100% { transform: translate(-100%, 0); }
}
<div>
<p>This text should scroll. Lorem ipsum dolor sit amet consectetur adipisicing elit. Excepturi commodi magni quo officia error ab veniam aspernatur. Quos ducimus, hic, maxime dicta optio quidem nostrum sint commodi velit, ut distinctio. And return to beginning.</p>
</div>
I have created a Flipbook using HTML, CSS3 and JavaScript. It is working fine on Google Chrome but it is not working properly on Firefox. I have Googled and tried to find answer. In most cases it is said that Firefox handle hoisting differently than Chrome. But there is no solution to it. Output is in the link below. If open with Chrome, it works fine but creates problems if opened with Firefox.
My code is:
var front = document.querySelector('.face-front');
var back = document.querySelector('.face-back');
var flip = document.querySelector('.book-content');
var uno = document.querySelectorAll('.book');
var portada = document.querySelectorAll('#portada');
var contZindex = 2;
var customZindex = 1;
// javascript hoisting function starts
// javascript hoisting function ends
for(var i = 0; i < uno.length; i++){
uno[i].style.zIndex = customZindex;
customZindex--;
uno[i].addEventListener('click', function(e){
var tgt = event.target;
var unoThis = this;
unoThis.style.zIndex = contZindex;
contZindex++;
if(tgt.getAttribute('class') == 'face-front'){
unoThis.style.zIndex = contZindex;
contZindex += 20;
setTimeout(function(){
unoThis.style.transform = 'rotateY(-180deg)';
}, 500);
// setTimeout(buntyTimeoutFront(), 500);
}
if(tgt.getAttribute('class') == 'face-back'){
unoThis.style.zIndex = contZindex;
contZindex += 20;
setTimeout(function(){
unoThis.style.transform = 'rotateY(-0deg)';
}, 500);
// setTimeout(buntyTimeoutBack(), 500);
}
if(tgt.getAttribute('id') == 'portada'){
flip.classList.remove("trnsf-reset");
flip.classList.add("trnsf");
}
if(tgt.getAttribute('id') == 'trsf'){
flip.classList.remove("trnsf");
flip.classList.add("trnsf-reset");
}
});
}
*{
pddding: 0;
margin: 0;
font-family: sans-serif;
}
p{
margin-top: 0;
font-size: 14px;
text-align: justify;
}
/* classes for javascript starts*/
.trnsf{
/* transform for diff browser starts */
-webkit-transform: translateX(80px);
-moz-transform: translateX(80px);
-ms-transform: translateX(80px);
-o-transform: translateX(80px);
transform: translateX(80px);
/* transform for diff browser ends */
}
.trnsf-reset{
/* transform for diff browser starts */
-webkit-transform: translateX(0px);
-moz-transform: translateX(0px);
-ms-transform: translateX(0px);
-o-transform: translateX(0px);
transform: translateX(0px);
/* transform for diff browser ends */
}
/* classes for javascript ends*/
.container, .book-content{
/* display flex starts*/
display: flex;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
/* display flex ends*/
/* justify-content starts */
justify-content: center;
-webkit-box-pack: center;
-webkit-justify-content: center;
-moz-box-pack: center;
-ms-flex-pack: center;
/* justify-content ends */
/* align items center */
align-items: center;
-webkit-box-align: center;
-moz-box-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
/* align items center */
}
.container{
width: 100%;
height: 100vh;
background: #2d6f75;
}
.book-content{
width: 65%;
min-width: 250px;
max-width: 400px;
height: 330px;
position: relative;
/* perspective for css starts */
-webkit-perspective: 1000px;
-moz-perspective: 1000px;
-ms-perspective: 1000px;
-o-perspective: 1000px;
perspective: 1000px;
/* perspective for css ends */
transition: 1s;
}
.book{
position: absolute;
width: 65%;
height: 100%;
transition: 1s;
/* transform-style starts */
transform-style: preserve-3d;
-webkit-transform-style: preserve-3d;
-moz-transform-style: preserve-3d;
-ms-transform-style: preserve-3d;
-o-transform-style: preserve-3d;
/* transform-style ends */
/* transform-oregin starts */
transform-origin: left;
-webkit-transform-origin: left;
-moz-transform-origin: left;
-ms-transform-origin: left;
-o-transform-origin: left;
/* transform-oregin ends */
}
.face-front, .face-back{
width: 100%;
height: 100%;
padding: 15px;
background: white;
box-sizing: border-box;
overflow: hidden;
}
.face-front{
box-shadow: inset 3px 0px 20px -7px black;
border-top-left-radius: 3px;
border-bottom-left-radius: 3px;
}
.face-back{
position: absolute;
top: 0;
left: 0;
/* transform for diff browser starts */
transform: translateZ(-1px) rotateY(180deg);
-webkit-transform: translateZ(-1px) rotateY(180deg);
-moz-transform: translateZ(-1px) rotateY(180deg);
-ms-transform: translateZ(-1px) rotateY(180deg);
-o-transform: translateZ(-1px) rotateY(180deg);
/* transform for diff browser ends */
box-shadow: inset -3px 1px 20px -7px black;
border-top-right-radius: 3px;
border-bottom-right-radius: 3px;
}
#portada{
background: url('../images/front.jpg');
}
#portada-back{
background: url('../images/back.jpg');
}
#portada, #portada-back{
background-size: 100% 100%;
}
/* responsive part starts here */
#media screen and (max-width: 800px){
p{
font-size: 12px;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="css/style.css">
<title>Document</title>
</head>
<body>
<div class="container" id = "container">
<div class="book-content">
<!-- book container -->
<div class="book">
<div class="face-front" id = "portada"></div>
<div class="face-back" id = "trsf">
<h1>Title 1</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dicta cupiditate, at aut harum necessitatibus doloremque inventore dolores dolorem facilis quas consectetur, incidunt voluptatibus blanditiis assumenda ratione, culpa iusto exercitationem vel.</p>
</div>
</div>
<!-- book container -->
<!-- book container -->
<div class="book">
<div class="face-front">
<h1>Title 2</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dicta cupiditate, at aut harum necessitatibus doloremque inventore dolores dolorem facilis quas consectetur, incidunt voluptatibus blanditiis assumenda ratione, culpa iusto exercitationem vel.</p>
</div>
<div class="face-back">
<h1>Title 3</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dicta cupiditate, at aut harum necessitatibus doloremque inventore dolores dolorem facilis quas consectetur, incidunt voluptatibus blanditiis assumenda ratione, culpa iusto exercitationem vel.</p>
</div>
</div>
<!-- book container -->
<!-- book container -->
<div class="book">
<div class="face-front">
<h1>Title 4</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dicta cupiditate, at aut harum necessitatibus doloremque inventore dolores dolorem facilis quas consectetur, incidunt voluptatibus blanditiis assumenda ratione, culpa iusto exercitationem vel.</p>
</div>
<div class="face-back">
<h1>Title 5</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dicta cupiditate, at aut harum necessitatibus doloremque inventore dolores dolorem facilis quas consectetur, incidunt voluptatibus blanditiis assumenda ratione, culpa iusto exercitationem vel.</p>
</div>
</div>
<!-- book container -->
<!-- book container -->
<div class="book">
<div class="face-front">
<h1>Title 6</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dicta cupiditate, at aut harum necessitatibus doloremque inventore dolores dolorem facilis quas consectetur, incidunt voluptatibus blanditiis assumenda ratione, culpa iusto exercitationem vel.</p>
</div>
<div class="face-back" id = "portada-back"></div>
</div>
<!-- book container -->
</div>
</div>
<script src = "js/script.js"></script>
</body>
</html>
The output is in the link below.
https://studyoob.com/myCustomFlipbook/
Your help would get me the project done.
I have pieced together a portfolio website using several tutorials and I can't figure out why the hamburger menu is not visible as it downsizes. It works fine when I take out the main content in the HTML with the sections, but if there is any main content then the burger menu does not appear.
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght#400;500&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Audiowide&display=swap" rel="stylesheet">
<title>Portfolio</title>
</head>
<body>
<nav>
<div class="logo">
<h4>Portfolio</h4>
</div>
<ul class="nav-links">
<li>Home</li>
<li>About</li>
<li>Portfolio</li>
<li>Contact</li>
</ul>
<div class="burger">
<div class="line1"></div>
<div class="line2"></div>
<div class="line3"></div>
</div>
</nav>
<main>
<div class="about-area" id="about">
<div class="text-part">
<h1>About</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore ullam,
exercitationem voluptate enim nobis id quas praesentium, fugit ipsum porro omnis!
Illo eum exercitationem eius, quam quae deserunt? Distinctio, voluptatibus. Lorem
ipsum dolor sit amet consectetur adipisicing elit. Repellendus non ab iure, debitis
minima at maxime nisi? Illum nesciunt nihil hic, provident dolorem molestias distinctio
optio officia suscipit nemo natus!</p>
</div>
</div>
<div class="portfolio-area" id="portfolio">
<div class="text-part">
<h1>Portfolio</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore ullam,
exercitationem voluptate enim nobis id quas praesentium, fugit ipsum porro omnis!
Illo eum exercitationem eius, quam quae deserunt? Distinctio, voluptatibus. Lorem
ipsum dolor sit amet consectetur adipisicing elit. Repellendus non ab iure, debitis
minima at maxime nisi? Illum nesciunt nihil hic, provident dolorem molestias distinctio
optio officia suscipit nemo natus!</p>
</div>
</div>
<div class="contact-area" id="contact">
<div class="text-part">
<h1>Contact</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore ullam,
exercitationem voluptate enim nobis id quas praesentium, fugit ipsum porro omnis!
Illo eum exercitationem eius, quam quae deserunt? Distinctio, voluptatibus. Lorem
ipsum dolor sit amet consectetur adipisicing elit. Repellendus non ab iure, debitis
minima at maxime nisi? Illum nesciunt nihil hic, provident dolorem molestias distinctio
optio officia suscipit nemo natus!</p>
</div>
</div>
</main>
<script src="app.js"></script>
</body>
</html>
CSS:
* {
margin: 0px; /* Remove unwanted inherent sizing */
padding: 0px;
box-sizing: border-box;
font-family: 'Poppins', sans-serif;
}
nav {
display: flex; /*icons are next to each other */
justify-content: space-around; /* */
align-items: center; /* Center items vertically */
min-height: 8vh;
background-color: #0e9763;
font-family: 'Poppins', sans-serif;
}
.logo {
color: rgb(226, 226, 226);
text-transform: uppercase;
letter-spacing: 5px;
font-size: 20px;
}
.nav-links {
display: flex; /* spreads out text */
justify-content: space-around; /* using flex adds more space around the items using width */
width: 30%;
}
.nav-links li {
list-style: none; /* Removes bullet points */
}
.nav-links a{
color: rgb(226, 226, 226);
text-decoration: none; /* Removes default link colors */
letter-spacing: 3px;
font-size: 14px;
}
.burger {
display: none; /* Hides the burger menu */
cursor: pointer;
}
.burger div { /*the details of the burger menu bars design */
width: 25px;
height: 3px;
background-color: rgb(226, 226, 226);
margin: 5px;
transition: all 0.3s ease;
}
/* Main Area Content */
.about-area, .portfolio-area, .contact-area {
position: relative;
display: flex;
justify-content: space-around;
align-items: center;
flex-wrap: wrap;
flex-direction: row;
width: 100%;
height: 700px;
}
.text-part {
width: 65%;
height: 80%;
}
h1 {
font-size: 50px;
font-family: 'Audiowide', cursive;
}
p {
font-size: 24px;
line-height: 50px;
}
.about-area {
background: #fefefe;
}
.portfolio-area {
background: #0e9763;
color: #fff;
}
.contact-area {
background: #fefefe;
}
html {
scroll-behavior: smooth;
}
a:hover {
color: #000;
}
/* Quick fix breakpoint between max size and hamburger menu trigger */
#media screen and (max-width:1024px){
.nav-links {
width: 60%;
}
}
#media screen and (max-width:768px){
body {
overflow: hidden;
}
.nav-links {
position: absolute;
right: 0px;
height: 92vh;
top: 8vh;
background-color: #0e9763;
display: flex;
flex-direction: column;
align-items: center;
width: 50%;
transform: translateX(100%); /* Moves content off screen */
transition: transform 0.5s ease-in;
}
.nav-links li {
opacity: 0;
}
.burger {
display: block;
}
}
.nav-active {
transform: translateX(0%); /* When button is clicked the nav gets the transform */
}
/* Nav animation */
#keyframes navLinkFade{
from{
opacity: 0;
transform: translateX(50px);
}
to {
opacity: 1;
transform: translateX(0px);
}
}
.toggle .line1 {
transform: rotate(-45deg) translate(-5px,6px);
}
.toggle .line2 {
opacity: 0;
}
.toggle .line3 {
transform: rotate(45deg) translate(-5px,-6px);
}
JS:
const navSlide = () => { //navSlide is = to an anonymous function
const burger = document.querySelector('.burger');
const nav = document.querySelector('.nav-links');
const navLinks = document.querySelectorAll('.nav-links li');
burger.addEventListener('click',() => { //on click run a function
//Toggle Nav
nav.classList.toggle('nav-active');
//Animate Links
navLinks.forEach((link, index) => {
if (link.style.animation){
link.style.animation = ''
} else {
link.style.animation = `navLinkFade 0.5s ease forwards ${index / 7 + 0.5}s`
}
});
//Burger Animation
burger.classList.toggle('toggle');
});
}
//Invoke function
navSlide();
Here is a link to the codepen:
https://codepen.io/justadirtgrub/pen/NWGJrde
You need to add z-index css property to class .nav-active to show up in front of main content
const navSlide = () => {
//navSlide is = to an anonymous function
const burger = document.querySelector(".burger");
const nav = document.querySelector(".nav-links");
const navLinks = document.querySelectorAll(".nav-links li");
burger.addEventListener("click", () => {
//on click run a function
//Toggle Nav
nav.classList.toggle("nav-active");
//Animate Links
navLinks.forEach((link, index) => {
if (link.style.animation) {
link.style.animation = "";
} else {
link.style.animation = `navLinkFade 0.5s ease forwards ${
index / 7 + 0.5
}s`;
}
});
//Burger Animation
burger.classList.toggle("toggle");
});
};
//Invoke function
navSlide();
* {
margin: 0px; /* Remove unwanted inherent sizing */
padding: 0px;
box-sizing: border-box;
font-family: "Poppins", sans-serif;
}
nav {
display: flex; /*icons are next to each other */
justify-content: space-around; /* */
align-items: center; /* Center items vertically */
min-height: 8vh;
background-color: #0e9763;
font-family: "Poppins", sans-serif;
}
.logo {
color: rgb(226, 226, 226);
text-transform: uppercase;
letter-spacing: 5px;
font-size: 20px;
}
.nav-links {
display: flex; /* spreads out text */
justify-content: space-around; /* using flex adds more space around the items using width */
width: 30%;
}
.nav-links li {
list-style: none; /* Removes bullet points */
}
.nav-links a {
color: rgb(226, 226, 226);
text-decoration: none; /* Removes default link colors */
letter-spacing: 3px;
font-size: 14px;
}
.burger {
display: none; /* Hides the burger menu */
cursor: pointer;
}
.burger div {
/*the details of the burger menu bars design */
width: 25px;
height: 3px;
background-color: rgb(226, 226, 226);
margin: 5px;
transition: all 0.3s ease;
}
/* Main Area Content */
.about-area,
.portfolio-area,
.contact-area {
position: relative;
display: flex;
justify-content: space-around;
align-items: center;
flex-wrap: wrap;
flex-direction: row;
width: 100%;
height: 700px;
}
.text-part {
width: 65%;
height: 80%;
}
h1 {
font-size: 50px;
font-family: "Audiowide", cursive;
}
p {
font-size: 24px;
line-height: 50px;
}
.about-area {
background: #fefefe;
}
.portfolio-area {
background: #0e9763;
color: #fff;
}
.contact-area {
background: #fefefe;
}
html {
scroll-behavior: smooth;
}
a:hover {
color: #000;
}
/* Quick fix breakpoint between max size and hamburger menu trigger */
#media screen and (max-width: 1024px) {
.nav-links {
width: 60%;
}
}
#media screen and (max-width: 768px) {
body {
overflow: hidden;
}
.nav-links {
position: absolute;
right: 0px;
height: 92vh;
top: 8vh;
background-color: #0e9763;
display: flex;
flex-direction: column;
align-items: center;
width: 50%;
transform: translateX(100%); /* Moves content off screen */
transition: transform 0.5s ease-in;
}
.nav-links li {
opacity: 0;
}
.burger {
display: block;
}
}
.nav-active {
transform: translateX(0%);
z-index: 100; /* When button is clicked the nav gets the transform */
}
/* Nav animation */
#keyframes navLinkFade {
from {
opacity: 0;
transform: translateX(50px);
}
to {
opacity: 1;
transform: translateX(0px);
}
}
.toggle .line1 {
transform: rotate(-45deg) translate(-5px, 6px);
}
.toggle .line2 {
opacity: 0;
}
.toggle .line3 {
transform: rotate(45deg) translate(-5px, -6px);
}
<!DOCTYPE html>
<head>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght#400;500&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Audiowide&display=swap" rel="stylesheet">
<title>Portfolio</title>
</head>
<body>
<nav>
<div class="logo">
<h4>Portfolio</h4>
</div>
<ul class="nav-links">
<li>Home</li>
<li>About</li>
<li>Portfolio</li>
<li>Contact</li>
</ul>
<div class="burger">
<div class="line1"></div>
<div class="line2"></div>
<div class="line3"></div>
</div>
</nav>
<main>
<div class="about-area" id="about">
<div class="text-part">
<h1>About</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore ullam,
exercitationem voluptate enim nobis id quas praesentium, fugit ipsum porro omnis!
Illo eum exercitationem eius, quam quae deserunt? Distinctio, voluptatibus. Lorem
ipsum dolor sit amet consectetur adipisicing elit. Repellendus non ab iure, debitis
minima at maxime nisi? Illum nesciunt nihil hic, provident dolorem molestias distinctio
optio officia suscipit nemo natus!</p>
</div>
</div>
<div class="portfolio-area" id="portfolio">
<div class="text-part">
<h1>Portfolio</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore ullam,
exercitationem voluptate enim nobis id quas praesentium, fugit ipsum porro omnis!
Illo eum exercitationem eius, quam quae deserunt? Distinctio, voluptatibus. Lorem
ipsum dolor sit amet consectetur adipisicing elit. Repellendus non ab iure, debitis
minima at maxime nisi? Illum nesciunt nihil hic, provident dolorem molestias distinctio
optio officia suscipit nemo natus!</p>
</div>
</div>
<div class="contact-area" id="contact">
<div class="text-part">
<h1>Contact</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore ullam,
exercitationem voluptate enim nobis id quas praesentium, fugit ipsum porro omnis!
Illo eum exercitationem eius, quam quae deserunt? Distinctio, voluptatibus. Lorem
ipsum dolor sit amet consectetur adipisicing elit. Repellendus non ab iure, debitis
minima at maxime nisi? Illum nesciunt nihil hic, provident dolorem molestias distinctio
optio officia suscipit nemo natus!</p>
</div>
</div>
</main>
</body>
</html>
I've got two different pricing cards:
<div class="card card1" id="open">
<span class=title>Basic</span>
<h1>$99 USD</h1>
<h4>Lorem ipsum dolor sit amet.</h4>
<h2>YOU GET:</h2>
<ul>
<li>Access to AM</li>
<li>Unlimited Households</li>
<li>White-Labeling</li>
</ul>
</div>
<div class="card card2" id="open2">
<span class=title>Pro</span>
<h1>$129 USD</h1>
<h4>Lorem ipsum dolor sit amet.</h4>
<h2>YOU GET:</h2>
<ul>
<li>Cash-Flow Reporting</li>
<li>Beneficiary Visualization</li>
<li>Unlimited TMs</li>
</ul>
</div>
When clicked, each should bring up a different modal:
<div class="modal-container" id="modal_container">
<div class="modal">
<h1>Modal goes here</h1>
<p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Atque consectetur repudiandae quia deleniti nulla, suscipit tempora magnam blanditiis ex culpa mollitia quisquam quod? Dolores, sunt dicta adipisci rem consequuntur fugit repudiandae quia deleniti nulla.</p>
<button id="close">Close me</button>
</div>
<div class="modal-container" id="modal_container2">
<div class="modal">
<h1>Modal 2 goes here</h1>
<p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Atque consectetur repudiandae quia deleniti nulla, suscipit tempora magnam blanditiis ex culpa mollitia quisquam quod? Dolores, sunt dicta adipisci rem consequuntur fugit repudiandae quia deleniti nulla.</p>
<button id="close2">Close me</button>
</div>
All of the ID's are unique. With this JS I am able to bring up the first modal but not the second:
const open = document.getElementById('open');
const modal_container = document.getElementById('modal_container');
const close = document.getElementById('close');
open.addEventListener('click', () => {
modal_container.classList.add('show');
});
close.addEventListener('click', () => {
modal_container.classList.remove('show');
});
I added unique IDs for the second card and modal, but when I pull them into the JS by adding this:
const open2 = document.getElementById('open2');
const modal_container2 = document.getElementById('modal_container2');
const close2 = document.getElementById('close2');
open2.addEventListener('click', () => {
modal_container2.classList.add('show');
});
close2.addEventListener('click', () => {
modal_container2.classList.remove('show');
});
I am not getting the same effect.
Here is the relevant CSS:Added relevant CSS
.modal-container {
background-color: rgba(0,0,0,0.3);
position: fixed;
top:0;
left:0;
width: 100vw;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
opacity: 0;
pointer-events: none;
}
.modal-container.show {
opacity: 1;
pointer-events: auto;
}
there was some error on your codepen, double body open and close tag, the second modal was inside the first one's container, Ids were duplicated and on the JS side the listeners for the second modal were missing, try this:
const open = document.getElementById('open');
const modal_container = document.getElementById('modal_container');
const close = document.getElementById('close');
const open2 = document.getElementById('open2');
const modal_container2 = document.getElementById('modal_container2');
const close2 = document.getElementById('close2');
open.addEventListener('click', () => {
modal_container.classList.add('show');
});
close.addEventListener('click', () => {
modal_container.classList.remove('show');
});
open2.addEventListener('click', () => {
modal_container2.classList.add('show');
});
close2.addEventListener('click', () => {
modal_container2.classList.remove('show');
});
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background: #f4efed;
font-family: 'Muli', sans-serif;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
cursor: crosshair;
}
.card {
background-color: #fff;
border-radius: 10px;
padding: 60px 30px;
text-align: center;
max-width: 450px;
min-width: 275px;
margin: 1em;
transition: .2s ease all;
}
.card:hover {
transform: translateY(-20px);
box-shadow: 0 10px 20px rgba(0,0,0,.3);
cursor: pointer;
}
.card1 {
background-image: linear-gradient(to bottom right, #41464d, #2d3137);
color: white;
}
.card2 {
background-image: linear-gradient(to bottom right, #dbb898, #b98351);
color: white;
}
.card .title {
font-size: .8em;
}
.card1 .title{
font-weight: bold;
background-color: #444950;
border-radius: 10px;
padding: 12px 10px;
}
.card2 .title{
font-weight: bold;
background-color: white;
border-radius: 10px;
padding: 12px 10px;
color: #2d3137;
}
h1 {
margin: 30px 0 30px 0;
}
h4 {
font-weight: 100;
font-size: .75em;
}
h2 {
margin: 25px;
font-size: .8em;
}
ul {
font-size: .75em;
line-height: 1.75em;
list-style: none;
}
.modal-container {
background-color: rgba(0,0,0,0.3);
position: fixed;
top:0;
left:0;
width: 100vw;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
opacity: 0;
pointer-events: none;
}
.modal-container.show {
opacity: 1;
pointer-events: auto;
}
.modal {
background-color: white;
border-radius: 10px;
width: 800px;
max-width: 100%;
padding: 30px 50px;
box-shadow: 0 2px 4px rgba(0,0,0,0.2);
text-align: center;
}
.modal h1 {
margin: .5em;
}
.modal p {
font-size: 14px;
opacity: 0.7;
}
.modal-container button {
background-color: #444950;
color: white;
border: 0;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0,0,0,0.2);
font-size: 14px;
padding: 10px 25px;
margin: 15px 0;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Pricing Cards</title>
</head>
<body>
<div class="card card1" id="open">
<span class=title>Basic</span>
<h1>$99 USD</h1>
<h4>Lorem ipsum dolor sit amet.</h4>
<h2>YOU GET:</h2>
<ul>
<li>Access to Asset-Map</li>
<li>Unlimited Households</li>
<li>White-Labeling</li>
</ul>
</div>
<div class="card card2" id="open2">
<span class=title>Pro</span>
<h1>$129 USD</h1>
<h4>Lorem ipsum dolor sit amet.</h4>
<h2>YOU GET:</h2>
<ul>
<li>Cash-Flow Reporting</li>
<li>Beneficiary Visualization</li>
<li>Unlimited Target-Maps</li>
</ul>
</div>
<div class="modal-container" id="modal_container">
<div class="modal">
<h1>Modal goes here</h1>
<p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Atque consectetur repudiandae quia deleniti nulla, suscipit tempora magnam blanditiis ex culpa mollitia quisquam quod? Dolores, sunt dicta adipisci rem consequuntur fugit repudiandae quia deleniti nulla.</p>
<button id="close">Close me</button>
</div>
</div>
<div class="modal-container" id="modal_container2">
<div class="modal">
<h1>Modal 2 goes here</h1>
<p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Atque consectetur repudiandae quia deleniti nulla, suscipit tempora magnam blanditiis ex culpa mollitia quisquam quod? Dolores, sunt dicta adipisci rem consequuntur fugit repudiandae quia deleniti nulla.</p>
<button id="close2">Close me</button>
</div>
</div>
</body>
</html>
I'm pretty new to CSS and JavaScript and I was wondering if you could make a script that allows you to change what stylesheet the site uses.
Say: you had a green theme where everything is shades of green. What would you do so the user can change it to red with the press of a button?
Has anyone any idea how to do this?
You can set an Id to the link tag and switch the css at runtime.
HTML
<link type="text/css" rel="stylesheet" media="all" href="../green.css" id="theme_css" />
JS
document.getElementById('buttonID').onclick = function () {
document.getElementById('theme_css').href = '../red.css';
};
Quick Demo:
$( "#datepicker" ).datepicker();
$('button').button().on('click', function () {
let linkHref = 'https://code.jquery.com/ui/1.11.4/themes/{THEME}/jquery-ui.css';
if ($('#swapTheme').prop('href').indexOf('pepper-grinder') >= 0) {
$('#swapTheme').prop('href', linkHref.replace('{THEME}', 'black-tie'));
} else {
$('#swapTheme').prop('href', linkHref.replace('{THEME}', 'pepper-grinder'));
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU="
crossorigin="anonymous"></script>
<link href="https://code.jquery.com/ui/1.11.4/themes/pepper-grinder/jquery-ui.css" id="swapTheme" rel="stylesheet">
<div id="datepicker"></div>
<button style="padding: 5px 15px;"> Switch Theme </button>
You could use CSS Variables (also known as Custom Properties) for such changes, given that they are supported by all modern browsers.
So, let's say you have a green/white theme, where green being the primary color, and white complementing it. The site might look like the following:
body {
font-family: "Segoe UI", "Serif", "Verdana", "Arial";
}
.container {
display: grid;
grid-template-columns: 120px 1fr;
grid-template-rows: 50px 1fr 30px;
grid-gap: 5px;
}
.header {
grid-row: 1;
grid-column: 1 / 3;
background-color: #ddd;
}
.nav {
grid-column: 1;
grid-row: 2;
border-right: 1px solid green;
}
.content {
grid-column: 2;
grid-row: 2;
padding: 10px;
}
.footer {
grid-row: 3;
grid-column: 1 / 3;
text-align: center;
}
/* Child items. */
.header-logo {
float: left;
width: 100px;
background-color: green;
height: 50px;
color: white;
font-size: 30px;
padding: 3px 5px;
box-sizing: border-box;
}
.nav-links {
list-style: none;
margin: 5px 0 0 0;
padding: 0;
}
.nav-links--link {
color: green;
background-color: white;
width: 100%;
height: 30px;
margin: 0 0 5px 0;
padding: 4px 5px;
box-sizing: border-box;
cursor: pointer;
}
.nav-links--link.active,
.nav-links--link:hover {
background-color: green;
color: white;
}
.footer-note {
color: green;
background-color: white;
padding: 3px 0;
display: block;
}
<div class="container">
<div class="header">
<div class="header-logo">
LOGO
</div>
</div>
<div class="nav">
<ul class="nav-links">
<li class="nav-links--link active">
Home
</li>
<li class="nav-links--link">
About
</li>
<li class="nav-links--link">
Contact Us
</li>
</ul>
</div>
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dicta, ea.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Facilis id tenetur repudiandae deserunt, ipsam quisquam dicta tempora. Temporibus, vel, veritatis.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consequuntur dignissimos odio id repellat quam, assumenda magnam! Beatae perferendis error exercitationem molestias explicabo earum ipsam maiores ullam natus dolorum, dolorem itaque reiciendis
nihil placeat incidunt aperiam tempora, commodi eveniet. Minima iusto porro iste quae dignissimos neque dolor adipisci non, soluta nisi!</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates, dolorem blanditiis exercitationem excepturi, impedit iure.</p>
</div>
<div class="footer">
<span class="footer-note">
Lorem ipsum dolor sit amet.
</span>
</div>
</div>
So the first step to move to CSS Variables would be to refactor out the theme colors into variables --primary-color and --secondary-color. Here's how your stylesheet looks after the change.
You could begin by setting the default theme colors into the :root element style declaration:
:root {
--primary-color: green;
--secondary-color: white;
}
Then, you can replace all instances of hardcoded "green" in the stylesheet with a call to the primary color variable: var(--primary-color). So the styles for your logo might look like the following:
.header-logo {
...
background-color: var(--primary-color);
color: var(--secondary-color);
...
}
:root {
--primary-color: green;
--secondary-color: white;
}
body {
font-family: "Segoe UI", "Serif", "Verdana", "Arial";
}
.container {
display: grid;
grid-template-columns: 120px 1fr;
grid-template-rows: 50px 1fr 30px;
grid-gap: 5px;
}
.header {
grid-row: 1;
grid-column: 1 / 3;
background-color: #ddd;
}
.nav {
grid-column: 1;
grid-row: 2;
border-right: 1px solid var(--primary-color);
}
.content {
grid-column: 2;
grid-row: 2;
padding: 10px;
}
.footer {
grid-row: 3;
grid-column: 1 / 3;
text-align: center;
}
/* Child items. */
.header-logo {
float: left;
width: 100px;
background-color: var(--primary-color);
height: 50px;
color: var(--secondary-color);
font-size: 30px;
padding: 3px 5px;
box-sizing: border-box;
}
.nav-links {
list-style: none;
margin: 5px 0 0 0;
padding: 0;
}
.nav-links--link {
color: var(--primary-color);
background-color: var(--secondary-color);
width: 100%;
height: 30px;
margin: 0 0 5px 0;
padding: 4px 5px;
box-sizing: border-box;
cursor: pointer;
}
.nav-links--link.active,
.nav-links--link:hover {
background-color: var(--primary-color);
color: var(--secondary-color);
}
.footer-note {
color: var(--primary-color);
background-color: var(--secondary-color);
padding: 3px 0;
display: block;
}
<div class="container">
<div class="header">
<div class="header-logo">
LOGO
</div>
</div>
<div class="nav">
<ul class="nav-links">
<li class="nav-links--link active">
Home
</li>
<li class="nav-links--link">
About
</li>
<li class="nav-links--link">
Contact Us
</li>
</ul>
</div>
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dicta, ea.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Facilis id tenetur repudiandae deserunt, ipsam quisquam dicta tempora. Temporibus, vel, veritatis.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consequuntur dignissimos odio id repellat quam, assumenda magnam! Beatae perferendis error exercitationem molestias explicabo earum ipsam maiores ullam natus dolorum, dolorem itaque reiciendis
nihil placeat incidunt aperiam tempora, commodi eveniet. Minima iusto porro iste quae dignissimos neque dolor adipisci non, soluta nisi!</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates, dolorem blanditiis exercitationem excepturi, impedit iure.</p>
</div>
<div class="footer">
<span class="footer-note">
Lorem ipsum dolor sit amet.
</span>
</div>
</div>
Now, if you intend to change the color from "green" to "red", all you need to do is change the value of --primary-color to "red". And, you can do that using Javascript!
With plain JS, you can set the color to red using:
document.body.style.setProperty("--primary-color", "red");
Or, with jQuery you could do:
$(document.body).css("--primary-color", "red");
$(document.body).css("--primary-color", "red");
:root {
--primary-color: green;
--secondary-color: white;
}
body {
font-family: "Segoe UI", "Serif", "Verdana", "Arial";
}
.container {
display: grid;
grid-template-columns: 120px 1fr;
grid-template-rows: 50px 1fr 30px;
grid-gap: 5px;
}
.header {
grid-row: 1;
grid-column: 1 / 3;
background-color: #ddd;
}
.nav {
grid-column: 1;
grid-row: 2;
border-right: 1px solid var(--primary-color);
}
.content {
grid-column: 2;
grid-row: 2;
padding: 10px;
}
.footer {
grid-row: 3;
grid-column: 1 / 3;
text-align: center;
}
/* Child items. */
.header-logo {
float: left;
width: 100px;
background-color: var(--primary-color);
height: 50px;
color: var(--secondary-color);
font-size: 30px;
padding: 3px 5px;
box-sizing: border-box;
}
.nav-links {
list-style: none;
margin: 5px 0 0 0;
padding: 0;
}
.nav-links--link {
color: var(--primary-color);
background-color: var(--secondary-color);
width: 100%;
height: 30px;
margin: 0 0 5px 0;
padding: 4px 5px;
box-sizing: border-box;
cursor: pointer;
}
.nav-links--link.active,
.nav-links--link:hover {
background-color: var(--primary-color);
color: var(--secondary-color);
}
.footer-note {
color: var(--primary-color);
background-color: var(--secondary-color);
padding: 3px 0;
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<div class="header">
<div class="header-logo">
LOGO
</div>
</div>
<div class="nav">
<ul class="nav-links">
<li class="nav-links--link active">
Home
</li>
<li class="nav-links--link">
About
</li>
<li class="nav-links--link">
Contact Us
</li>
</ul>
</div>
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dicta, ea.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Facilis id tenetur repudiandae deserunt, ipsam quisquam dicta tempora. Temporibus, vel, veritatis.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consequuntur dignissimos odio id repellat quam, assumenda magnam! Beatae perferendis error exercitationem molestias explicabo earum ipsam maiores ullam natus dolorum, dolorem itaque reiciendis
nihil placeat incidunt aperiam tempora, commodi eveniet. Minima iusto porro iste quae dignissimos neque dolor adipisci non, soluta nisi!</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates, dolorem blanditiis exercitationem excepturi, impedit iure.</p>
</div>
<div class="footer">
<span class="footer-note">
Lorem ipsum dolor sit amet.
</span>
</div>
</div>
I went one step further and created some buttons to show how you could allow the user to choose a set of theme colors - and change the values CSS Variables using jQuery.
Here's how the snippet below looks on Chrome 64 - in case you are still using a browser that doesn't support CSS Variables:
$(".header--theme-button").on("click", function() {
var primaryColor = $(this).css("--theme-primary");
var secondaryColor = $(this).css("--theme-secondary");
$(".header--theme-button").removeClass("active");
$(this).addClass("active");
$(document.body).css("--primary-color", primaryColor);
$(document.body).css("--secondary-color", secondaryColor);
});
:root {
--primary-color: orange;
--secondary-color: white;
--theme-primary: black;
--theme-secondary: white;
}
body {
font-family: "Segoe UI", "Serif", "Verdana", "Arial";
}
.container {
display: grid;
grid-template-columns: 120px 1fr;
grid-template-rows: 50px 1fr 30px;
grid-gap: 5px;
}
.header {
grid-row: 1;
grid-column: 1 / 3;
}
.nav {
grid-column: 1;
grid-row: 2;
border-right: 1px solid var(--primary-color);
}
.content {
grid-column: 2;
grid-row: 2;
padding: 10px;
}
.footer {
grid-row: 3;
grid-column: 1 / 3;
text-align: center;
}
/* Child items. */
.header-logo {
float: left;
width: 100px;
background-color: var(--primary-color);
height: 50px;
color: var(--secondary-color);
font-size: 30px;
padding: 3px 5px;
box-sizing: border-box;
}
.header-settings {
float: right;
height: 50px;
}
.nav-links {
list-style: none;
margin: 5px 0 0 0;
padding: 0;
}
.nav-links--link {
color: var(--primary-color);
background-color: var(--secondary-color);
width: 100%;
height: 30px;
margin: 0 0 5px 0;
padding: 4px 5px;
box-sizing: border-box;
cursor: pointer;
}
.nav-links--link.active,
.nav-links--link:hover {
background-color: var(--primary-color);
color: var(--secondary-color);
}
.footer-note {
color: var(--primary-color);
background-color: var(--secondary-color);
padding: 3px 0;
display: block;
}
.header--theme-button {
height: 30px;
width: 30px;
margin: 10px 5px 0 0;
display: inline-block;
border-top: 15px solid var(--theme-primary);
border-bottom: 15px solid var(--theme-secondary);
border-right: 0;
border-left: 0;
padding: 0;
box-shadow: 0 0 3px gray;
}
.header--theme-button:hover {
box-shadow: 2px 2px 2px gray;
}
.header--theme-button.active {
box-shadow: 3px 3px 3px gray;
}
<div class="container">
<div class="header">
<div class="header-logo">
LOGO
</div>
<div class="header-settings">
<button type="button" class="header--theme-button active" style="--theme-primary:orange; --theme-secondary:white;">
</button>
<button type="button" class="header--theme-button" style="--theme-primary:#2196F3; --theme-secondary:#eee;">
</button>
<button type="button" class="header--theme-button" style="--theme-primary:purple; --theme-secondary:white;">
</button>
<button type="button" class="header--theme-button" style="--theme-primary:#F44336; --theme-secondary:white;">
</button>
<button type="button" class="header--theme-button" style="--theme-primary:green; --theme-secondary:white;">
</button>
<button type="button" class="header--theme-button" style="--theme-primary:#FFEB3B; --theme-secondary:#222;">
</button>
</div>
</div>
<div class="nav">
<ul class="nav-links">
<li class="nav-links--link active">
Home
</li>
<li class="nav-links--link">
About
</li>
<li class="nav-links--link">
Contact Us
</li>
</ul>
</div>
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dicta, ea.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Facilis id tenetur repudiandae deserunt, ipsam quisquam dicta tempora. Temporibus, vel, veritatis.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consequuntur dignissimos odio id repellat quam, assumenda magnam! Beatae perferendis error exercitationem molestias explicabo earum ipsam maiores ullam natus dolorum, dolorem itaque reiciendis
nihil placeat incidunt aperiam tempora, commodi eveniet. Minima iusto porro iste quae dignissimos neque dolor adipisci non, soluta nisi!</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates, dolorem blanditiis exercitationem excepturi, impedit iure.</p>
</div>
<div class="footer">
<span class="footer-note">
Lorem ipsum dolor sit amet.
</span>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Yes, you can change CSS with Javascript. See this tutorial for more info. It basically links multiple stylesheets like this:
<link rel="stylesheet" type="text/css" title="blue"
href="http://example.com/css/blue.css">
<link rel="alternate stylesheet" type="text/css" title="pink"
href="http://example.com/css/pink.css">
And then uses Javascript to change it:
<form>
<input type="submit" onclick="switch_style('blue');return false;" name="theme" value="Blue Theme" id="blue">
<input type="submit" onclick="switch_style('pink');return false;" name="theme" value="Pink Theme" id="pink">
</form>
The switch_style() function is written in that tutorial.
You don't even need any Javascript:
input[type=radio][value=light]:checked ~ article {
color: #222;
background-color: #FFEEAA;
}
input[type=radio][value=dark]:checked ~ article {
color: #EEE;
background-color: grey;
}
<body>
<input type="radio" name="theme" value="light" checked="checked">Light<br>
<input type="radio" name="theme" value="dark">Dark<br>
<article>
<h1>My super page!</h1>
<p>
Quibusdam sit repudiandae consequuntur doloremque illum ut ex quo. Esse temporibus est id suscipit repellat. Distinctio voluptatem voluptates asperiores dolorem dolorem placeat corporis quae. Quaerat voluptatem magni dignissimos rerum distinctio odio id.
</p>
</article>
</body>
Or just put your theme css in a css file and pass it to the function, On page load the if (localStorage.getItem("theme") != "") checks if theme has been set.. Here you have an example:
if (localStorage.getItem("theme") != "") {
loadcssfile(localStorage.getItem("theme"));
}
function loadcssfile(filename) {
if (filename != "") {
localStorage.setItem("theme", filename);
}
var fileref = document.createElement("link");
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", filename);
if (typeof fileref != "undefined")
document.getElementsByTagName("head")[0].appendChild(fileref);
}
<div onclick="loadcssfile('css/pink.css')" id="pink">
Pink
</div>
<div
onclick="loadcssfile('css/blue.css')" id="blue">
Blue
</div>