Archivi tag: css

SetStyle: Set CSS Properties Without Surprises

Ultimo aggiornamento: 31-01-2014

Introduction

Using JavaScript to make HTML element, often is used elem.setAttribute("style", "property1:val; property2:val;") to set CSS properties in easy way; but there’s a problem if you had just set a CSS property with elem.style.prop="val" : it will be reset to default browser value!! (Run the test code showed below.)

So I made a simple function to solve this problem and continue to use a setAttribute-like approach.

Using the Code

I wrote the setStyle method in HTMLElement interface, that represents any HTML element, so to use it easily.

This is the function:

HTMLElement.prototype.setStyle = function(str) {    
    var props = str.split(";"); //get properties
    for(var i = 0; i < props.length; i++) {
        var t = props[i].split(":"); //t[0] = property - t[1] = value
        this.style[fixPropName(t[0].trim())] = t[1].trim(); //trim removes white space(s)
    }
}

String.prototype.capitalize = function() {
    return this.charAt(0).toUpperCase() + this.slice(1);
}

//trasform text, for example, from background-color to backgroundColor
function fixPropName(str) {
    var tArray = str.split("-");
    var prop = tArray[0];
    for(var i = 1; i < tArray.length; i++) {
        prop += tArray[i].capitalize();
    }
    return prop
}

Now you can use this function, in this way:

var div = document.getElementById("IdElement");
div.setStyle("background:blue; color: white");

This function is just a millisecond (on my PC, and sometimes) slower than setAttribute method, but now you are sure to haven’t unexpected CSS properties change.

To make a test, run this code:

<script>
HTMLElement.prototype.setStyle = function(str) {    
    var props = str.split(";"); //get properties
    for(var i = 0; i < props.length; i++) {
        var t = props[i].split(":"); //t[0] = property - t[1] = value
        this.style[fixPropName(t[0].trim())] = t[1].trim(); //trim removes white space(s)
    }
}

String.prototype.capitalize = function() {
    return this.charAt(0).toUpperCase() + this.slice(1);
}

//trasform text, for example, from background-color to backgroundColor
function fixPropName(str) {
    var tArray = str.split("-");
    var prop = tArray[0];
    for(var i = 1; i < tArray.length; i++) {
        prop += tArray[i].capitalize();
    }
    return prop
}
window.addEventListener("DOMContentLoaded", 
function(e) {

var div = document.getElementById("cc");
div.style.opacity = "1";
str = "Opacity: " + div.style.opacity + "<br />";
var endTime, startTime = Date.now(); 
div.setAttribute("style","background:green;color:black");
endTime = Date.now(); 
str += "TimeExec: " +(startTime-endTime) + "<br />";
str += "Opacity: " + div.style.opacity + "<br /><br />";

div.style.opacity = "1";
str += "Opacity: " + div.style.opacity + "<br />";
startTime = Date.now(); 
div.setStyle("background:blue; color: white");
endTime = Date.now(); 
str += "TimeExec: " +(endTime-startTime) + "<br />";
str += "Opacity: " + div.style.opacity + "<br />";

div.innerHTML = str;
}, false);
</script>

<div id="cc">To to to</div>

Article on CodeProject

WordPress Orbit Slider with nice description visualization

I was working on a my friend site, when I needed to use a slideshow, so I decided to use: WP Orbit Slider, it’s a nice wordpress plugin but it hasn’t a good description visualization, so I edited a bit the default css to have this result:

deluca

First I inserted a slideshow into Twenty Eleven homepage (I had already created slides)   editing index.php, where I write this code before /* Start the Loop */ comment

<?php 
echo do_shortcode('[orbit-slider]') 
?>

Then I edited WP Orbit plugin‘s default.css, I added this rule

.slide-content {
top: 230px !important;
left: 200px !important;
padding: 2%;
background: black;
opacity: 0.7;
filter: alpha(opacity=70);
height: 30px !important;
text-align: center;
width: 180px !important;
color: white;
}

and I edited “#orbit-inside a” in this way:

#orbit-inside a {
	display:block;
	width:100%;

        color:white !important;
        text-decoration: underline !important;
}

The end!

How to make responsive the Vanilla Forum default theme

Ultimo aggiornamento: 17-04-2013

I was working on Vanilla Forum version 2.0.18.4 theme for a web site, and I saw that it wasn’t responsive for tablets, so I made a path: you can paste this code into custom.css or directly into style.css

/*! Vanilla Forum css fix for responsive design on desktop and tablet */

.Item .OptionsMenu {
visibility:visible;
opacity:0.7;
}

.Open.OptionsMenu, .Item:hover .OptionsMenu {
opacity:1;
}

i.ArrowSprite {
background-position: right top;
}
a:hover i.SpriteUp {
background-position: left top;
}

i.SpriteDown {
background-position: right bottom;
}

a:hover i.SpriteDown {
background-position: left bottom;
}

ul.MessageList div.Meta span.Votes {
right:auto;
position:static;
float:right;
}
.MessageList .Message {
clear: both;
}

#Foot div {
width:auto;
}

ul.Discussions div.ItemContent a.Title {
max-width: 370px;
}

#Head form input.InputBox {
width: 150px;
}

@media only screen and (max-width: 980px) {
#Body, #Panel, .Menu {
width: 680px;
margin:0 auto 0 auto;
}
#Menu {
margin:auto;
}
#Head {
background: #143F7B; /* set the background if change*/
}
#Head .Menu {
text-align: center;
background: #143F7B; /* for Safari iPad: set again the background color*/
}
body #Head h1 {
display:block;
}

}

And I suggest you to edit “default.master.php” in this way:

  1. Put the search div before the menu
  2. Set  “AddLink ‘User'” second parameter to: ‘My Account’

Customize HTML5 form validation with JavaScript and CSS

Ultimo aggiornamento: 11-05-2013

Introduction

With HTML5, to check forms validity, new input type values (email, URL, etc.) and also the pattern attribute were introduced, but the control is done when the form is submitted.

Is there a way to do this before that?

JavaScript

There are many ways to personalize control validation, not only the message of error with setCustomValidity or the use of title and x-moz-errormessage into input tag, but also calling the checkValidity() function to check inputs values on some input event as onchange.

Here the JavaScript code of example:

window.onload=function() {
    var query = ["input[pattern]", "input[type=email]"];
    for(var q = 0; q < query.length; q++) {
        var inp = document.querySelectorAll(query[q]);
        for(var i = 0; i < inp.length; i++) {
                inp[i].onkeyup = validation;
        }
    }
};

function validation(event) {
    var p = this.parentNode.querySelector("p");
    if (this.checkValidity() == false) { //the control is here!
        p.innerHTML=this.getAttribute("title");
    } else {
        p.innerHTML="";
    }     
}

And this is the HTML code:

<form>
  <fieldset>
    <legend>Form</legend>
    <div>
      <label for="input01">Name</label>
          <div>
            <input type="text" pattern="[a-zA-Z ]+" 
              required title="Insert only letters of the alphabet" id="input01" name="name" >
            <p class="err-msg"></p>
          </div>
      </div>
       <div>
          <label for="input02">Pin</label>
          <div >
            <input type="text" pattern="[a-zA-z0-9]{16}"  
              required  title="Insert 16 chars" maxlength="16" id="input02"  name="pin"> 
            <p  class="err-msg"></p>
          </div>
      </div>
       <div>     
           <label for="input03">Cellphone</label>
          <div>
            <input type="text"  pattern="[0-9]+" 
              required  title="Insert only numbers chars" id="input03" name="cell" >
            <p class="err-msg"></p>
           </div>
      </div>
      <div>     
          <label for="input04">Email</label>
          <div >
              <input type="email" required title="Insert a valid email" id="input04" name="email">
            <p class="err-msg"></p>
          </div>
      </div>

     <div style="width:100%; text-align:center;">
       <input type="submit">
       <input type="reset" >  
     </div>
  </fieldset>
</form>

Take a look:

  • The title attribute value is used by browsers to personalize the error message for an input.
  • The required attribute  specifies that an input field must be filled out before submitting the form.

CSS

You can personalize also the inputs style with pseudo-classes: :invalid, :valid, :required.

Other pseudo-classes can be found here: CSS Basic User Interface Module Level 3 (CSS3 UI)

For example, with the following CSS rule, input texts will be red until the input is invalid.

input:invalid {
    color:red;
}