Vuejs Course


Binding inline styles

With v-bind:style we can dynamically set CSS style properties in html tags.
- Syntax:
<div v-bind:style='{propName:value, otherProp:value}'>content</div>

If you want to use CSS notation (with dash '-'), put the property name between quotes:
<div v-bind:style='{"prop-name":value, "other-prop":value}'>content</div>

- Example, increase and decrease the font-size with two buttons:
<div id='demo'>
 <button @click='fontSize++'>Increase font size</button>
 <button @click='fontSize--'>Decrease font size</button>
 <p v-bind:style='{color:color, fontSize:fontSize +"px"}'>Font size is: {{ fontSize }}</p>
</div>

<script>
var vm = new Vue({
 el: '#demo',
 data:{ color:'#00d', fontSize:12 }
});
</script>

You can bind to a style the object with CSS properties defined directly in Vue data.
- Example:
<div id='demo'>
 <p v-bind:style='styleOb'>This world is cured when the perception is corrected.</p>
</div>

<script>
var vm = new Vue({
 el: '#demo',
 data:{
 styleOb:{color:'#00d', fontSize:'20px', fontWeight:700}
 }
});
</script>

Array of styles

We can add an array with multiple style objects to the v-bind:style directive

- Example:
<div id='demo'>
 <p v-bind:style='[baseStyles, overrideStyles]'>The thought precedes the perception.</p>
</div>

<script>
var vm = new Vue({
 el: '#demo',
 data:{
 baseStyles:{color:'#00d', fontSize:'20px', fontWeight:700},
 overrideStyles:{color:'#fe1234'}
 }
});
</script>

Binding CSS Classes Dynamically

With v-bind:class we can dynamically set CSS clases to html elements.
- Syntax:
<div v-bind:class='{class_name: addClass}'>content</div>
The class_name is added to class attribute when the addClass property is True.
v-bind:class can also coexist with the plain class attribute.

- Example, we build a menu with v-for. When an item in Menu is clicked, we set to be added a CSS class (here sel_li) to the current item. We use v-bind:class to set sel_li class to selected item:
<style>
.m_li {background:#ebebeb; cursor:pointer; display:inline; font-size:20px; margin:4px;}
.sel_li {background:#efef00; color:#00d; text-decoration:underline;}
</style>

<div id='demo'>
<ul>
 <li class='m_li' v-for='li in lists' @click='sel_li =li' v-bind:class='{ sel_li: sel_li ==li}'>
 {{li}}
 </li>
</ul>
</div>

<script>
const menu_li =['Home', 'About', 'Contact'];

var vm = new Vue({
 el: '#demo',
 data:{
 sel_li: menu_li[0],
 lists: menu_li
 }
});
</script>

You can have multiple classes toggled by having more fields in the object.
- Syntax:
<div class='a_clas' v-bind:class='{class_name:addClass, class_2:addCls2}'>content</div>

Multiple classes added into an Array

We can pass an array to v-bind:class to apply a list of classes from data object.
- HTML:
<div v-bind:class='[active_cls, error_cls]'>Content</div>
- In Vue JS:
data: {
 active_cls: 'active',
 error_cls: 'error_msg'
}
Which will render:
<div class='active error_msg'>Content</div>

CSS class in Vue Components

When you use the class attribute on a custom component, those classes will be added to the component's root element. Existing classes will not be overwritten.

- Example with a component:
<div id='demo'>
 <comp_test class='cls_comp'></comp_test>
</div>

<script>
Vue.component('comp_test', {
 template: "<div class='cls_a cls_b'><h3 class='cls_title'>Title</h3><div>Content</div></div>"
});

var vm = new Vue({el:'#demo'});
</script>
The rendered HTML will be:
<div class='cls_a cls_b cls_comp'><h3 class='cls_title'>Title</h3><div>Content</div></div>

- The same is with class bindings in component:
<div id='demo'>
 <comp_test v-bind:class='[add_cls]'></comp_test>
</div>

<script>
Vue.component('comp_test', {
 template: "<div class='cls_a cls_b'><h3 class='cls_title'>Title</h3><div>Content</div></div>"
});

var vm = new Vue({
 el:'#demo',
 data:{add_cls: 'cls_comp'}
});
</script>
The rendered HTML will be:
<div class='cls_a cls_b cls_comp'><h3 class='cls_title'>Title</h3><div>Content</div></div>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which meta tag provides a short description of the page?
<meta content="..."> <meta description="..."> <meta http-equiv="...">
<meta name="description" content="70-160 characters that describes the content of the page" />
Which CSS property is used to stop the wrapping effect of the "float"?
clear text-align position
#some_id {
  clear: both;
}
Click on the method which gets an array with all the elements in the document that have a specified tag name.
getElementsByName() getElementById() getElementsByTagName()
var divs = document.getElementsByTagName("div");
var nr_divs = divs.length;
alert(nr_divs);
Indicate the PHP function which returns the number of elements in array.
is_[) count() strlen()
$arr =[7, 8, "abc", 10);
$nri = count($arr);
echo $nri;        // 4
Class and Style with Vue.js

Last accessed pages

  1. Simple Admin Login PHP Script (10894)
  2. jQuery parent, children and nth-child() (13826)
  3. Display UL bullets and OL numbers on the right side (8088)
  4. Convert BBCode to HTML and HTML to BBCode with JavaScript (9298)
  5. jsSHA - SHA Hashes and HMAC in JavaScript (3445)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (201)
  2. Read Excel file data in PHP - PhpExcelReader (66)
  3. The Mastery of Love (56)
  4. PHP Unzipper - Extract Zip, Rar Archives (51)
  5. Working with MySQL Database (34)
Chat
Chat or leave a message for the other users
Full screenInchide