So I was trying to create a 3 columns layout with:
left panel column: scrollable vertical menu. shown by default but toggleable.
right panel column: scrollable action menu. hidden by default but toggleable.
content: scrollable content that adjusts to the remaining content of the page so whenever left, right o bottom panels (see below point) are toggled the content will not be covered and the div will adjust itself.
bottom panel: not scrollable. hidden by default but toggleable. When showing it should be sticking at the bottom of the screen and stay visible there, even if I scroll the content div). This should also be as wide as the content panel described above.
So far I was able to create a snippet with toggleable right and left column and content is adjusting to it:
$(".header_container > .content").append(() => {
return "header<br>";
});
$(".right_container > .content").append(() => {
return "MENU ITEM RIGHT<br>".repeat(100);
});
$(".left_container > .content").append(() => {
return "MENU ITEM LEFT<br>".repeat(100);
});
$(".terminal_container > .content").append(() => {
return "terminal ".repeat(100);
});
$(".body_container > .content").append(() => {
return "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.<br>".repeat(100);
});
$(".hideleft").click(() => {
$(".left_container").toggle();
});
$(".hideright").click(() => {
$(".right_container").toggle();
});
.container {
width: 90%;
height: 180px;
border: 3px solid;
position: absolute;
}
.header_container {
width: 100%;
height: 50px;
background-color: #DDD;
}
.left_container {
height: 100%;
width: 200px;
background: green;
float: left;
overflow: scroll;
}
.center_container {
width: auto;
height: 100%;
background: red;
overflow: scroll;
}
.right_container {
height: 100%;
width: 200px;
background: blue;
float: right;
overflow: scroll;
display: none;
}
.terminal_container {
height: 200px;
overflow: hidden;
background-color: black;
color: white;
}
.content {
padding: 15px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="hideleft">Toggle Left</button>
<button class="hideright">Toggle Right</button>
<div class="container">
<div class="right_container">
<div class="content">
</div>
</div>
<div class="left_container">
<div class="content">
</div>
</div>
<div class="center_container">
<div class="body_container">
<div class="content">
</div>
</div>
<div class="terminal_container">
<div class="content">
</div>
</div>
</div>
</div>
However I cannot get the CSS to work for the bottom log panel (possibly with only css).
Could anyone help me out to figure out how to have the bottom panel?
Without Classes through jQuery
Try this. I'm pretty sure that the + selector is CSS3 though.
$(".header_container > .content").append(() => {
return "header<br>";
});
$(".right_container > .content").append(() => {
return "MENU ITEM RIGHT<br>".repeat(100);
});
$(".left_container > .content").append(() => {
return "MENU ITEM LEFT<br>".repeat(100);
});
$(".terminal_container > .content").append(() => {
return "terminal ".repeat(100);
});
$(".body_container > .content").append(() => {
return "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.<br>".repeat(100);
});
$(".hideleft").click(() => {
$(".left_container").toggle();
});
$(".hideright").click(() => {
$(".right_container").toggle();
});
$(".hidebottom").click(() => {
$(".terminal_container").toggle();
});
.container {
width: 90%;
height: 180px;
border: 3px solid;
position: absolute;
}
.header_container {
width: 100%;
height: 50px;
background-color: #DDD;
}
.left_container {
height: 100%;
width: 200px;
background: green;
float: left;
overflow: scroll;
}
.center_container {
width: auto;
height: 100%;
background: red;
}
.right_container {
height: 100%;
width: 200px;
background: blue;
float: right;
overflow: scroll;
display: none;
}
.body_container {
height: 100%;
overflow: scroll;
}
#bottom { display: none; }
#bottom:checked + .body_container {
height: 80%;
}
.terminal_container {
height: 20%;
overflow: hidden;
display: none;
background-color: black;
color: white;
}
.content {
padding: 15px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="hideleft">Toggle Left</button>
<button class="hideright">Toggle Right</button>
<label class="hidebottom" for="bottom">Toggle Bottom</label>
<div class="container">
<div class="right_container">
<div class="content">
</div>
</div>
<div class="left_container">
<div class="content">
</div>
</div>
<div class="center_container">
<input type="checkbox" id="bottom">
<div class="body_container">
<div class="content">
</div>
</div>
<div class="terminal_container">
<div class="content">
</div>
</div>
</div>
</div>
With Classes through jQuery
This solution adds classes when it toggles whether it displays or not, then sets the width through CSS.
I normally would all element's widths using percentages instead of pixels so that I don't have to use calc statements.
$(".hideleft").click(() => {
$(".left_container").toggle();
$("body").toggleClass("left");
});
$(".hideright").click(() => {
$(".right_container").toggle();
$("body").toggleClass("right");
});
$(".hidebottom").click(() => {
$(".terminal_container").toggle();
$("body").toggleClass("bottom");
});
.container {
width: 90%;
height: 70%;
border: 3px solid;
position: absolute;
}
.header_container {
width: 100%;
height: 50px;
background-color: #DDD;
}
.left_container {
height: 100%;
width: 20%;
background: green;
float: left;
overflow: scroll;
}
.right_container {
height: 100%;
width: 20%;
background: blue;
float: right;
overflow: scroll;
display: none;
}
.center_container {
width: auto;
height: 100%;
position: relative;
}
.body_container {
background: red;
overflow: scroll;
position: absolute;
top: 0;
height: 100%
}
.terminal_container {
height: 100px;
width: 100%;
overflow: hidden;
background-color: black;
color: white;
position: absolute;
bottom: 0;
}
.content {
padding: 15px;
}
/* Layout */
.left .center_container {
float: right;
width: 80%;
}
.right .center_container {
float: left;
width: 80%;
}
.left.right .center_container {
width: 60%;
}
.bottom .body_container {
bottom: 100px;
height: auto;
width: 100%;
}
<body class="left">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="right_container">
<div class="content">
MENU ITEM RIGHT<br>MENU ITEM RIGHT<br>MENU ITEM RIGHT<br>MENU ITEM RIGHT<br>MENU ITEM RIGHT<br>MENU ITEM RIGHT<br>MENU ITEM RIGHT<br>MENU ITEM RIGHT<br>MENU ITEM RIGHT<br>MENU ITEM RIGHT<br>MENU ITEM RIGHT<br>MENU ITEM RIGHT<br>MENU ITEM RIGHT<br>MENU ITEM RIGHT<br>MENU ITEM RIGHT<br>MENU ITEM RIGHT<br>MENU ITEM RIGHT<br>MENU ITEM RIGHT<br>MENU ITEM RIGHT<br>MENU ITEM RIGHT<br>MENU ITEM RIGHT<br>MENU ITEM RIGHT<br>MENU ITEM RIGHT<br>MENU ITEM RIGHT<br>MENU ITEM RIGHT<br>
</div>
</div>
<div class="left_container">
<div class="content">
MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>MENU ITEM LEFT<br>
</div>
</div>
<div class="center_container">
<div class="body_container">
<header>
<button class="hideleft">Toggle Left</button>
<button class="hideright">Toggle Right</button>
<button class="hidebottom">Toggle Bottom</button>
</header>
<div class="content">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.<br>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.<br>
</div>
</div>
<div class="terminal_container" style="display: none">
<div class="content">
terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal terminal
</div>
</div>
</div>
</div>
</body>
Related
How can make an element sticky, so it stays at the top of the viewport? I want the element to remain sticky even if it leaves it's container.
I tried this
HTML
<div class="page">
<div class="parent">
<div class="child-sticky">
<p>i want to be sticky, even when I'm outside my parent.</p>
</div>
</div>
</div>
CSS
.child-sticky {
position:sticky;
top:20px;
}
.page {
height: 3000px;
}
Here's a pen to illustrate the problem. Scroll down to see what I mean.
https://codepen.io/pwkip/pen/OxeMao
Sticky works that way, it will remain sticky relative to its parent. You need to use fixed.
Check this codepen
Already 7 months ago, but I found a CSS only solution if the element you want to be sticky is the last one of its parent, its very simple: Just give the parent element position: sticky; and also give it top: -xx;, depending on the height of the elements before the last one.
#parent {
position: -webkit-sticky;
position: sticky;
top: -3em;
}
#some_content {
height: 3em;
}
#sticky {
background-color: red;
}
#space {
height: 200vh;
}
<div id="parent">
<div id="some_content">Some Content</div>
<div id="sticky">Sticky div</div>
</div>
<div id="space"></div>
<p>Scroll here</p>
This is how position: sticky is intended to work. If you need it to also work outside the parent than you have to change the HTML structure.
See also the official definition: https://www.w3.org/TR/css-position-3/#sticky-pos
There is a little trick you can try.
In some cases it will break your layout and in others it won't. In my case, I have a header with some buttons but when I scroll down, I want to have access to some action buttons which are inside that one-line header. The only change is to set the display property of your parent to be inline.
.parent {
display: inline;
}
Based on https://stackoverflow.com/a/46913147/2603230 and https://stackoverflow.com/a/37797978/2603230's code, here's an combined solution that does not require hard-coded height.
$(document).ready(function() {
// Get the current top location of the nav bar.
var stickyNavTop = $('nav').offset().top;
// Set the header's height to its current height in CSS
// If we don't do this, the content will jump suddenly when passing through stickyNavTop.
$('header').height($('header').height());
$(window).scroll(function(){
if ($(window).scrollTop() >= stickyNavTop) {
$('nav').addClass('fixed-header');
} else {
$('nav').removeClass('fixed-header');
}
});
});
body { margin: 0px; padding: 0px; }
nav {
width: 100%;
background-color: red;
}
.fixed-header {
position: fixed;
top: 0;
left: 0;
width: 100%;
z-index: 10;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<header>
<div>
<h1 style="padding-bottom: 50px; background-color: blue;">
Hello World!
</h1>
</div>
<nav>
A nav bar here!
</nav>
</header>
<main style="height: 1000px;">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</main>
Like mentioned, sticky works that way. However there is a CSS hack that you can play around with.
Disclaimer
This is an ugly hack and will probably create a lot of problems with the following content. So I would not recommend it for most usecases. Having that said...
Negative margin hack
There is a hack you could play around with including negative margin. If you extend the height of the container and then give it some negative bottom margin, it could at least "visually leave" the container.
I altered your code and created a JSFiddle for demonstration.
HTML
<div class="page">
<div class="parent">
<div class="child"><p>...<br>...<br>...<br>...<br>...</p></div>
<div class="child-sticky">
<p>i want to be sticky, even when I'm outside my parent.</p>
</div>
<div class="child"><p>...<br>...<br>...<br>...<br>...</p></div>
<div class="child"><p>...<br>...<br>...<br>...<br>...</p></div>
</div>
<div class="following-content">
</div>
</div>
CSS
.child-sticky {
height: 200px;
background: #333366;
position:sticky;
top:20px;
color:#ffffff;
}
.parent {
height: 1250px;
background: #555599;
margin-bottom: -500px;
}
.following-content {
background: red;
height:500px;
}
.child {
background-color: #8888bb;
}
.page {
height: 3000px;
background: #999999;
width: 500px;
margin: 0 auto;
}
div {
padding: 20px;
}
https://jsfiddle.net/74pkgd9h/
Well, this is my first question here!
I've been having a problem in making background-attachment: fixed; happen on a <div> which is also assigned this property :- transform: translateZ(-1px) scale(2);. Actually on the first slide, the background image is rendering correctly, but on the rest slides, the image is not visible.
Also, the scroll bar is not working when I click or drag it.
I've tried a lot of times on google and it's been two days. At last I tried Stackoverflow, 'cause I was not able to get the specific answer which I wanted.
If I missed some link then please do help and pardon me.
Here's the page which I'm making - http://mynk-9.github.io/test/
[edit] Now, I've also added the code.
<!DOCTYPE html>
<html>
<head>
<title>Parallax Scrolling Effect</title>
<style>
body {
margin: 0px;
padding: 0px;
}
div.parallax-page {
position: relative;
height: 100vh;
width: 100vw;
overflow-y: auto;
overflow-x: hidden;
perspective: 1px;
-webkit-perspective-origin-x: 50%;
perspective-origin-x: 50%;
}
div.parallax-page > div.group {
position: relative;
height: 100%;
width: 100%;
left: 0px;
top: 0px;
-webkit-transform-style: preserve-3d;
transform-style: preserve-3d;
}
div.parallax-page > div.group {
margin-bottom: calc(100vh);
}
div.parallax-page > div.group:last-child {
margin-bottom: -25vh;
}
div.parallax-page > div.group > div.background {
transform: translateZ(-1px) scale(2);
position: fixed;
top: 0px;
left: 0px;
width: 100vw;
height: 100%;
/*background-attachment: fixed;*/
}
div.parallax-page > div.group > div.slide {
position: absolute;
width: 50%;
height: 50%;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: rgba(255,255,255,0.5);
}
div.parallax-page::-webkit-scrollbar {
/*display: none;*/
}
/* using formula -> scale = 1 + (translateZ * -1) / perspective */
</style>
</head>
<body>
<div class="parallax-page">
<div class="group">
<div class="background" style="background-image: url('sample-wallpaper.svg');"></div>
<div class="slide">
<h1 style="text-align: center;">A magical scroll!!</h1>
</div>
</div>
<div class="group">
<div class="background" style="background-image: url('sample-wallpaper-2.svg');"></div>
<div class="slide">
<h1 style="text-align: center;">A magical scroll!!</h1>
</div>
</div>
<div class="group">
<div class="background" style="background-image: url('sample-wallpaper-3.svg');"></div>
<div class="slide">
<h1 style="text-align: center;">A magical scroll!!</h1>
</div>
</div>
<div class="group">
<div class="background" style="background-image: url('sample-wallpaper-5.svg');"></div>
<div class="slide">
<h1 style="text-align: center;">A magical scroll!!</h1>
</div>
</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<title>W3.CSS</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://www.w3schools.com/lib/w3.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato">
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css">
<link rel="stylesheet" href="http://www.w3schools.com/lib/w3-theme-red.css">
<style>
body,h1,h2,h3,h4,h5,h6 {font-family: "Lato", sans-serif;}
body, html {
height: 100%;
color: #777;
line-height: 1.8;
}
/* Create a Parallax Effect */
.bgimg-1, .bgimg-2, .bgimg-3 {
opacity: 0.7;
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
/* First image */
.bgimg-1 {
background-image: url('https://pixabay.com/static/uploads/photo/2016/01/19/17/16/rainbow-background-1149610_960_720.jpg');
min-height: 100%;
}
/* Second image */
.bgimg-2 {
background-image: url("https://i.ytimg.com/vi/4AA4qYGunr4/maxresdefault.jpg");
min-height: 400px;
}
/* Adjust the position of the parallax image text */
.w3-display-middle {bottom: 45%;}
.w3-wide {letter-spacing: 10px;}
.w3-hover-opacity {cursor: pointer;}
</style>
<body>
<!-- Navbar (sit on top) -->
<div class="w3-top">
<ul class="w3-navbar" id="myNavbar">
<li>HOME</li>
<li class="w3-hide-small w3-right">
<i class="fa fa-search"></i>
</li>
</ul>
</div>
<!-- First Parallax Image with Text -->
<div class="bgimg-1 w3-opacity w3-display-container">
<div class="w3-display-middle">
<span class="w3-center w3-padding-xlarge w3-black w3-xlarge w3-wide w3-animate-opacity">MY <span class="w3-hide-small">WEBSITE</span> LOGO</span>
</div>
</div>
<!-- Container (About Section) -->
<div class="w3-content w3-container w3-padding-64" id="about">
<h3 class="w3-center">ABOUT ME</h3>
<p class="w3-center"><em>I love photography</em></p>
<p>We have created a fictional "personal" website/blog, and our fictional character is a hobby photographer. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa
qui officia deserunt mollit anim id est laborum consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
<!-- Second Parallax Image with Portfolio Text -->
<div class="bgimg-2 w3-display-container">
<div class="w3-display-middle">
<span class="w3-xxlarge w3-text-light-grey w3-wide">PORTFOLIO</span>
</div>
</div>
<footer class="w3-container w3-theme-dark w3-padding-16">
<p>Powered by csandreas1</p>
</footer>
</body>
</html>
How does toggle a class in vue.js?
I have the following:
<th class="initial " v-on="click: myFilter">
<span class="wkday">M</span>
</th>
new Vue({
el: '#my-container',
data: {},
methods: {
myFilter: function(){
// some code to filter users
}
}
});
When I click <th> tag I want to apply active as a class as follows:
<th class="initial active" v-on="click: myFilter">
<span class="wkday">M</span>
</th>
This needs to toggle i.e. each time its clicked it needs to add/remove the class.
You could have the active class be dependent upon a boolean data value:
<th
class="initial "
v-on="click: myFilter"
v-class="{active: isActive}">
<span class="wkday">M</span>
</th>
new Vue({
el: '#my-container',
data: {
isActive: false
},
methods: {
myFilter: function() {
this.isActive = !this.isActive;
// some code to filter users
}
}
})
Without the need of a method:
<!-- html element, will display'active' class if showMobile is true -->
<!-- clicking on the elment will toggle showMobileMenu to true and false alternatively -->
<div id="mobile-toggle"
:class="{ active: showMobileMenu }"
#click="showMobileMenu = !showMobileMenu">
</div>
vue.js app
data: {
showMobileMenu: false
}
If you don't need to access the toggle from outside the element, this code works without a data variable:
<a #click="e => e.target.classList.toggle('active')"></a>
This answer relevant for Vue.js version 2
<th
class="initial "
v-on:click="myFilter"
v-bind:class="{ active: isActive }"
>
<span class="wkday">M</span>
</th>
The rest of the answer by Douglas is still applicable (setting up the new Vue instance with isActive: false, etc).
Relevant docs: https://v2.vuejs.org/v2/guide/class-and-style.html#Object-Syntax and https://v2.vuejs.org/v2/guide/events.html#Method-Event-Handlers
This example is using Lists: When clicking in some li it turn red
html:
<div id="app">
<ul>
<li #click="activate(li.id)" :class="{ active : active_el == li.id }" v-for="li in lista">{{li.texto}}</li>
</ul>
</div>
JS:
var app = new Vue({
el:"#app",
data:{
lista:[{"id":"1","texto":"line 1"},{"id":"2","texto":"line 2"},{"id":"3","texto":"line 3"},{"id":"4","texto":"line 4"},{"id":"5","texto":"line 5"}],
active_el:0
},
methods:{
activate:function(el){
this.active_el = el;
}
}
});
css
ul > li:hover {
cursor:pointer;
}
.active {
color:red;
font-weight:bold;
}
Fiddle:
https://jsfiddle.net/w37vLL68/158/
#click="$event.target.classList.toggle('active')"
:class="{ active }"
#click="active = !active"
:class="'initial ' + (active ? 'active' : '')"
#click="active = !active"
:class="['initial', { active }]"
#click="active = !active"
Reference link: https://v2.vuejs.org/v2/guide/class-and-style.html
Demo:
new Vue({
el: '#app1'
});
new Vue({
el: '#app2',
data: { active: false }
});
new Vue({
el: '#app3',
data: { active: false }
});
new Vue({
el: '#app4',
data: { active: false }
});
.initial {
width: 300px;
height: 100px;
background: gray;
}
.active {
background: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<!-- directly manipulation: not recommended -->
<div id="app1">
<button
class="initial"
#click="$event.target.classList.toggle('active')"
>$event.target.classList.toggle('active')</button>
</div>
<!-- binding by object -->
<div id="app2">
<button
class="initial"
:class="{ active }"
#click="active = !active"
>class="initial" :class="{ active }"</button>
</div>
<!-- binding by expression -->
<div id="app3">
<button
:class="'initial ' + (active ? 'active' : '')"
#click="active = !active"
>'initial ' + (active ? 'active' : '')</button>
</div>
<!-- binding with object combined array -->
<div id="app4">
<button
:class="['initial', { active }]"
#click="active = !active"
>['initial', { active }]</button>
</div>
In addition to NateW's answer, if you have hyphens in your css class name, you should wrap that class within (single) quotes:
<th
class="initial "
v-on:click="myFilter"
v-bind:class="{ 'is-active' : isActive}"
>
<span class="wkday">M</span>
</th>
See this topic for more on the subject.
If you need more than 1 class
You can do this:
<i id="icon"
v-bind:class="{ 'fa fa-star': showStar }"
v-on:click="showStar = !showStar"
>
</i>
data: {
showStar: true
}
Notice the single quotes ' around the classes!
Thanks to everyone else's solutions.
This is how you would achieve that one with Vue3 + Composition API
<script setup>
import { ref } from 'vue'
const isTextRed = ref(false)
function toggleClass() {
isTextRed.value = !isTextRed.value
}
</script>
<template>
<h1 :class="isTextRed && 'fancy-color'">Hi community ๐๐ป</h1>
<button #click="toggleClass">Toggle color</button>
</template>
<style scoped>
.fancy-color {
color: Coral;
}
</style>
Here is a playground.
I've got a solution that allows you to check for different values of a prop and thus different <th> elements will become active/inactive. Using vue 2 syntax.
<th
class="initial "
#click.stop.prevent="myFilter('M')"
:class="[(activeDay == 'M' ? 'active' : '')]">
<span class="wkday">M</span>
</th>
...
<th
class="initial "
#click.stop.prevent="myFilter('T')"
:class="[(activeDay == 'T' ? 'active' : '')]">
<span class="wkday">T</span>
</th>
new Vue({
el: '#my-container',
data: {
activeDay: 'M'
},
methods: {
myFilter: function(day){
this.activeDay = day;
// some code to filter users
}
}
})
for nuxt link and bootstrap v5 navbar-nav, I used a child component
<nuxt-link
#click.prevent.native="isDropdwonMenuVisible = !isDropdwonMenuVisible"
to=""
:title="item.title"
:class="[item.cssClasses, {show: isDropdwonMenuVisible}]"
:id="`navbarDropdownMenuLink-${index}`"
:aria-expanded="[isDropdwonMenuVisible ? true : false]"
class="nav-link dropdown-toggle"
aria-current="page"
role="button"
data-toggle="dropdown"
>
{{ item.label }}
</nuxt-link>
data() {
return {
isDropdwonMenuVisible: false
}
},
You don't need to use a prop, you can alternatively call the data into another data variable set using an axios response or any other way you use to pull data into a specified vue component.
You can modify the number 32 below in all places to change the length of text you want to display before and after selecting more/less.
Works with Vue2 & Vue3.
"fw-bold" is a Bootstrap 5 class. It has no affect on this implementation.
props: {
content: Object,
},
data() {
return {
// UX
showComplete: false,
// DATA
shortText: "",
longText: "",
};
},
methods: {
shortenText(body) {
const length = body.length;
if (length >= 32) {
this.shortText = body.slice(0, 32);
this.longText = body.slice(32, -1);
}
},
toggleFull() {
this.showComplete = !this.showComplete;
}
},
created() {
this.shortenText(this.content.body);
},
In the vue template simply do this:
<span>{{ shortText }}</span>
<span v-if="showComplete">{{ longText }}</span>
<span v-if="!showComplete" class="fw-bold" #click="toggleFull"> more...</span>
<span v-if="showComplete" class="fw-bold" #click="toggleFull"> less...</span>
new Vue({
el: '#fsbar',
data:{
isActive: false
},
methods: {
toggle: function(){
this.isActive = !this.isActive;
}
}
});
/*
DEMO STYLE
*/
#import "https://fonts.googleapis.com/css?family=Poppins:300,400,500,600,700";
body {
font-family: 'Poppins', sans-serif;
background: #fafafa;
}
p {
font-family: 'Poppins', sans-serif;
font-size: 1.1em;
font-weight: 300;
line-height: 1.7em;
color: #999;
}
a, a:hover, a:focus {
color: inherit;
text-decoration: none;
transition: all 0.3s;
}
.navbar {
padding: 15px 10px;
background: #fff;
border: none;
border-radius: 0;
margin-bottom: 40px;
box-shadow: 1px 1px 3px rgba(0, 0, 0, 0.1);
}
.navbar-btn {
box-shadow: none;
outline: none !important;
border: none;
}
.line {
width: 100%;
height: 1px;
border-bottom: 1px dashed #ddd;
margin: 40px 0;
}
i, span {
display: inline-block;
}
/* ---------------------------------------------------
SIDEBAR STYLE
----------------------------------------------------- */
.wrapper {
display: flex;
align-items: stretch;
}
#sidebar {
min-width: 250px;
max-width: 250px;
background: #7386D5;
color: #fff;
transition: all 0.3s;
}
#sidebar.active {
min-width: 80px;
max-width: 80px;
text-align: center;
}
#sidebar.active .sidebar-header h3, #sidebar.active .CTAs {
display: none;
}
#sidebar.active .sidebar-header strong {
display: block;
}
#sidebar ul li a {
text-align: left;
}
#sidebar.active ul li a {
padding: 20px 10px;
text-align: center;
font-size: 0.85em;
}
#sidebar.active ul li a i {
margin-right: 0;
display: block;
font-size: 1.8em;
margin-bottom: 5px;
}
#sidebar.active ul ul a {
padding: 10px !important;
}
#sidebar.active a[aria-expanded="false"]::before, #sidebar.active a[aria-expanded="true"]::before {
top: auto;
bottom: 5px;
right: 50%;
-webkit-transform: translateX(50%);
-ms-transform: translateX(50%);
transform: translateX(50%);
}
#sidebar .sidebar-header {
padding: 20px;
background: #6d7fcc;
}
#sidebar .sidebar-header strong {
display: none;
font-size: 1.8em;
}
#sidebar ul.components {
padding: 20px 0;
border-bottom: 1px solid #47748b;
}
#sidebar ul li a {
padding: 10px;
font-size: 1.1em;
display: block;
}
#sidebar ul li a:hover {
color: #7386D5;
background: #fff;
}
#sidebar ul li a i {
margin-right: 10px;
}
#sidebar ul li.active > a, a[aria-expanded="true"] {
color: #fff;
background: #6d7fcc;
}
a[data-toggle="collapse"] {
position: relative;
}
a[aria-expanded="false"]::before, a[aria-expanded="true"]::before {
content: '\e259';
display: block;
position: absolute;
right: 20px;
font-family: 'Glyphicons Halflings';
font-size: 0.6em;
}
a[aria-expanded="true"]::before {
content: '\e260';
}
ul ul a {
font-size: 0.9em !important;
padding-left: 30px !important;
background: #6d7fcc;
}
ul.CTAs {
padding: 20px;
}
ul.CTAs a {
text-align: center;
font-size: 0.9em !important;
display: block;
border-radius: 5px;
margin-bottom: 5px;
}
a.download {
background: #fff;
color: #7386D5;
}
a.article, a.article:hover {
background: #6d7fcc !important;
color: #fff !important;
}
/* ---------------------------------------------------
CONTENT STYLE
----------------------------------------------------- */
#content {
padding: 20px;
min-height: 100vh;
transition: all 0.3s;
}
/* ---------------------------------------------------
MEDIAQUERIES
----------------------------------------------------- */
#media (max-width: 768px) {
#sidebar {
min-width: 80px;
max-width: 80px;
text-align: center;
margin-left: -80px !important ;
}
a[aria-expanded="false"]::before, a[aria-expanded="true"]::before {
top: auto;
bottom: 5px;
right: 50%;
-webkit-transform: translateX(50%);
-ms-transform: translateX(50%);
transform: translateX(50%);
}
#sidebar.active {
margin-left: 0 !important;
}
#sidebar .sidebar-header h3, #sidebar .CTAs {
display: none;
}
#sidebar .sidebar-header strong {
display: block;
}
#sidebar ul li a {
padding: 20px 10px;
}
#sidebar ul li a span {
font-size: 0.85em;
}
#sidebar ul li a i {
margin-right: 0;
display: block;
}
#sidebar ul ul a {
padding: 10px !important;
}
#sidebar ul li a i {
font-size: 1.3em;
}
#sidebar {
margin-left: 0;
}
#sidebarCollapse span {
display: none;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<!DOCTYPE html>
<html>
<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>Collapsible sidebar using Bootstrap 3</title>
<!-- Bootstrap CSS CDN -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- Our Custom CSS -->
<link rel="stylesheet" href="style4.css">
</head>
<body>
<div class="wrapper" id="fsbar">
<!-- Sidebar Holder -->
<nav id="sidebar" :class="{ active: isActive }">
<div class="sidebar-header">
<h3>Bootstrap Sidebar</h3>
<strong>BS</strong>
</div>
<ul class="list-unstyled components">
<li class="active">
<a href="#homeSubmenu" data-toggle="collapse" aria-expanded="false">
<i class="glyphicon glyphicon-home"></i>
Home
</a>
<ul class="collapse list-unstyled" id="homeSubmenu">
<li>Home 1</li>
<li>Home 2</li>
<li>Home 3</li>
</ul>
</li>
<li>
<a href="#">
<i class="glyphicon glyphicon-briefcase"></i>
About
</a>
<a href="#pageSubmenu" data-toggle="collapse" aria-expanded="false">
<i class="glyphicon glyphicon-duplicate"></i>
Pages
</a>
<ul class="collapse list-unstyled" id="pageSubmenu">
<li>Page 1</li>
<li>Page 2</li>
<li>Page 3</li>
</ul>
</li>
<li>
<a href="#">
<i class="glyphicon glyphicon-link"></i>
Portfolio
</a>
</li>
<li>
<a href="#">
<i class="glyphicon glyphicon-paperclip"></i>
FAQ
isActive: false, </a>
</li>
<li>
<a href="#">
<i class="glyphicon glyphicon-send"></i>
Contact
</a>
</li>
</ul>
<ul class="list-unstyled CTAs">
<li>Download source</li>
<li>Back to article</li>
</ul>
</nav>
<!-- Page Content Holder -->
<div id="content">
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" id="sidebarCollapse" class="btn btn-info navbar-btn" #click="toggle()">
<i class="glyphicon glyphicon-align-left"></i>
<span>Toggle Sidebar</span>
</button>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li>Page</li>
<li>Page</li>
<li>Page</li>
<li>Page</li>
</ul>
</div>
</div>
</nav>
<h2>Collapsible Sidebar Using Bootstrap 3</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<div class="line"></div>
<h2>Lorem Ipsum Dolor</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<div class="line"></div>
<h2>Lorem Ipsum Dolor</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<div class="line"></div>
<h3>Lorem Ipsum Dolor</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
</div>
<!-- jQuery CDN -->
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<!-- Bootstrap Js CDN -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
/<script type="text/javascript">
// $(document).ready(function () {
// $('#sidebarCollapse').on('click', function () {
// $('#sidebar').toggleClass('active');
// });
// }); jquery equivalent to vue
</script>
</body>
</html>
Try :
<template>
<th :class="'initial '+ active" v-on="click: myFilter">
<span class="wkday">M</span>
</th>
</template>
<script lang="ts">
active:string=''
myFilter(){
this.active='active'
}
</script>
<style>
.active{
/***your action***/
}
</style>
Good Day
I want to align text in the center of a div. Now that is easy with text-align: center on parent div.
But If I want to left align the text inside the div to the left of the centered div, how do I do that?
See my fiddle: http://jsfiddle.net/DvXzB/5/
HTML:
<div id="aboutContent" class="row-fluid">
<div id="aboutHeaderText" class="span12"><span title="">About Us</span></div>
<div id="aboutHeaderBody" class="span12">
<p><a title="">asdasd</a> is a free mobile application available for <a href="#"
title="">iOS</a>, Androidand the Blackberry operating
systems.</p>
<p>sdefsadfsdfldflkjlj lkjlkjdlfsldfjlkj ljlsdjflj lkj ljklj lk; ;l;l; ;k;k
l;kgjh jhg gjjh jhgjhgjh jhgjh gjg jgjhgjg</p>
<div id="cities"><a title="">asdasdasd</a> currently only displays events and
specials in <strong>asdasd</strong> (our hometown), but the following locations
will be available before you know it:
<ul>
<li><span>asdg</span>
</li>
<li><span>asdwn</span>
</li>
<li><span>Pasdasdroom</span>
</li>
<li><span>Dasdaf</span>
</li>
<li><span>Bergrin</span>
</li>
<li><span>Sersch</span>
</li>
<li><span>Graergwn</span>
</li>
</ul>
</div>
<p>Visit our Facebook page for more
up to date information, and feel free to contact us with
any queries.</p>
<br />
</div>
</div>
CSS:
#aboutContent {
color: #222;
margin-top: 50px;
margin: 0 auto;
text-align: center;
}
#aboutHeaderText span {
font-family:"Kozuka Gothic Pr6N", sans-serif !important;
color: #eeeeee;
font-size: 26px;
font-weight: bold;
}
#aboutHeaderText img {
margin-top: -18px;
margin-left: 8px;
}
#aboutHeaderBody {
position: relative;
padding: 0px;
font-size: 16px;
}
#cities ul {
list-style: none;
margin-top: 10px;
}
#cities ul li {
font-family:'Open Sans', sand-serif;
padding: 3px 0px;
font-size: 20px;
color: #222;
}
I want the text in the middle of the page to be justified, but when I justify or left align it, it aligns it to the absolute left again. How do I do this without using fixed paddings or margins? Basically what I want is what they have on this page here(see the 'about us' section): http://www.villagebicycle.co.za/
Note: I am using a fluid layout, so fixed paddings etc won't work
Thank you
You need to center align the container with margin:0 auto after setting its width to a specific size like width:400px. Then align each element separately using text-align.
See this demo: http://jsfiddle.net/DvXzB/16/
If you want your container to have 100% width then do not use text-align:center to your parent div. Instead, use width:100% (optional) and again text-align each block as desired.
You've to use a wraper div to envolve all content. I've done a JsFiddle, I think is what you're looking for :)
.wraper {
position: relative;
width: 500px;
margin:0 auto;
}
Afther that you can align a <p> to the left, right, justify, etc. as you can see:
p.left{text-align:left;}
p.justify{text-align:justify;}
And the HTML for testing:
<div class="wraper">
<p class="left">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </p>
<p class="justify">Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>
What exactly is the difference between the inline and inline-block values of CSS display?
A visual answer
Imagine a <span> element inside a <div>. If you give the <span> element a height of 100px and a red border for example, it will look like this with
display: inline
display: inline-block
display: block
Code: http://jsfiddle.net/Mta2b/
Elements with display:inline-block are like display:inline elements, but they can have a width and a height. That means that you can use an inline-block element as a block while flowing it within text or other elements.
Difference of supported styles as summary:
inline: only margin-left, margin-right, padding-left, padding-right
inline-block: margin, padding, height, width
display: inline; is a display mode to use in a sentence. For instance, if you have a paragraph and want to highlight a single word you do:
<p>
Pellentesque habitant morbi <em>tristique</em> senectus
et netus et malesuada fames ac turpis egestas.
</p>
The <em> element has a display: inline; by default, because this tag is always used in a sentence.
The <p> element has a display: block; by default, because it's neither a sentence nor in a sentence, it's a block of sentences.
An element with display: inline; cannot have a height or a width or a vertical margin. An element with display: block; can have a width, height and margin.
If you want to add a height to the <em> element, you need to set this element to display: inline-block;. Now you can add a height to the element and every other block style (the block part of inline-block), but it is placed in a sentence (the inline part of inline-block).
One thing not mentioned in answers is inline element can break among lines while inline-block can't (and obviously block)! So inline elements can be useful to style sentences of text and blocks inside them, but as they can't be padded you can use line-height instead.
<div style="width: 350px">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
<div style="display: inline; background: #F00; color: #FFF">
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
</div>
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
<hr/>
<div style="width: 350px">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
<div style="display: inline-block; background: #F00; color: #FFF">
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
</div>
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
All answers above contribute important info on the original question. However, there is a generalization that seems wrong.
It is possible to set width and height to at least one inline element (that I can think of) โ the <img> element.
Both accepted answers here and on this duplicate state that this is not possible but this doesnโt seem like a valid general rule.
Example:
img {
width: 200px;
height: 200px;
border: 1px solid red;
}
<img src="#" />
The img has display: inline, but its width and height were successfully set.
splattne's answer probably covered most of everything so I won't repeat the same thing, but: inline and inline-block behave differently with the direction CSS property.
Within the next snippet you see one two (in order) is rendered, like it does in LTR layouts. I suspect the browser here auto-detected the English part as LTR text and rendered it from left to right.
body {
text-align: right;
direction: rtl;
}
h2 {
display: block; /* just being explicit */
}
span {
display: inline;
}
<h2>
ูุฐุง ุนููุงู ุทููู
<span>one</span>
<span>two</span>
</h2>
However, if I go ahead and set display to inline-block, the browser appears to respect the direction property and render the elements from right to left in order, so that two one is rendered.
body {
text-align: right;
direction: rtl;
}
h2 {
display: block; /* just being explicit */
}
span {
display: inline-block;
}
<h2>
ูุฐุง ุนููุงู ุทููู
<span>one</span>
<span>two</span>
</h2>
I don't know if there are any other quirks to this, I only found about this empirically on Chrome.
inline elements
Have respect for their left & right margin and padding. not for top/bottom.
Cannot set width or height.
Allow other elements to sit to their left and right.
Inline-Block elements:
Respect all sides for margin and padding.
Can set width and height.
Allow other elements to sit to their left & right.
Block elements:
Respect all sides for margin and padding
Acquire full-width (in case the width is not defined)
Force a line break after them
A visual example looks like this:
Check out the snippet below for an extra visualization example
.block{
background: green;
width: 50px;
height: 50px;
margin-top: 10px;
margin-bottom: 10px;
display: block;
}
.inline-block{
background: green;
width: 50px;
height: 50px;
margin-top: 10px;
margin-bottom: 10px;
display: inline-block;
}
.inline{
background: green;
width: 50px;
height: 50px;
margin-top: 10px;
margin-bottom: 10px;
display: inline;
}
<div class="block">
block
</div>
<div class="block">
block
</div>
<div class="inline-block">
inline block
</div>
<div class="inline-block">
inline block
</div>
<div class="inline">
inline
</div>
<div class="inline">
inline
</div>
Block - Element take complete width.All properties height , width, margin , padding work
Inline - element take height and width according to the content. Height , width , margin bottom and margin top do not work .Padding and left and right margin work. Example span and anchor.
Inline block - 1. Element don't take complete width, that is why it has *inline* in its name. All properties including height , width, margin top and margin bottom work on it. Which also work in block level element.That's why it has *block* in its name.