Como mudar propriedades CSS com JQuery
Veja como mudar propriedades CSS com JQuery de uma forma simples e fácil, esta dica vai ajudar você a montar seu layout de modo dinâmico e interativo.
[ad#texto]
É comum surgir a necessidade de manipularmos CSS em tempo de execução e a melhor forma de realizar esta tarefa é utilizando JQuery.

Veja como mudar propriedades CSS com JQuery de forma muito simplificada:
HTML:
<div>
<p>Exemplo de parágrafo!!!</p>
</div>
Agora vamos adicionar um fundo cinza em tempo de execução:
$("div").css("background", "#ccc");
Agora vamos explicar como mudar propriedades CSS com JQuery, o primeiro trecho é puro HTML, ele é que será manipulado e o segundo trecho é um pequeno código-fonte JQuery que vai efetivamente alterar a renderização através do .css() do JQuery.
No $() você vai selecionar o elemento pelo TAGNAME, pela classe ou pelo ID do elemento e o .css() é adicionado a propriedade CSS que será manipulada e em seguida o seu valor.
Porém é preciso tomar um cuidado, embora várias propriedades CSS sejam iguais em um arquivo CSS “puro” e no JQuery alguma delas terão pequenas diferenças de escrita, veja uma tabela de conversão:
| Propriedade – CSS | Referência – JavaScript |
|---|---|
| background | background |
| background-attachment | backgroundAttachment |
| background-color | backgroundColor |
| background-image | backgroundImage |
| background-position | backgroundPosition |
| background-repeat | backgroundRepeat |
| border | border |
| border-bottom | borderBottom |
| border-bottom-color | borderBottomColor |
| border-bottom-style | borderBottomStyle |
| border-bottom-width | borderBottomWidth |
| border-color | borderColor |
| border-left | borderLeft |
| border-left-color | borderLeftColor |
| border-left-style | borderLeftStyle |
| border-left-width | borderLeftWidth |
| border-right | borderRight |
| border-right-color | borderRightColor |
| border-right-style | borderRightStyle |
| border-right-width | borderRightWidth |
| border-style | borderStyle |
| border-top | borderTop |
| border-top-color | borderTopColor |
| border-top-style | borderTopStyle |
| border-top-width | borderTopWidth |
| border-width | borderWidth |
| clear | clear |
| clip | clip |
| color | color |
| cursor | cursor |
| display | display |
| filter | filter |
| font | font |
| font-family | fontFamily |
| font-size | fontSize |
| font-variant | fontVariant |
| font-weight | fontWeight |
| height | height |
| left | left |
| letter-spacing | letterSpacing |
| line-height | lineHeight |
| list-style | listStyle |
| list-style-image | listStyleImage |
| list-style-position | listStylePosition |
| list-style-type | listStyleType |
| margin | margin |
| margin-bottom | marginBottom |
| margin-left | marginLeft |
| margin-right | marginRight |
| margin-top | marginTop |
| overflow | overflow |
| padding | padding |
| padding-bottom | paddingBottom |
| padding-left | paddingLeft |
| padding-right | paddingRight |
| padding-top | paddingTop |
| page-break-after | pageBreakAfter |
| page-break-before | pageBreakBefore |
| position | position |
| float | styleFloat |
| text-align | textAlign |
| text-decoration | textDecoration |
| text-decoration: blink | textDecorationBlink |
| text-decoration: line-through | textDecorationLineThrough |
| text-decoration: none | textDecorationNone |
| text-decoration: overline | textDecorationOverline |
| text-decoration: underline | textDecorationUnderline |
| text-indent | textIndent |
| text-transform | textTransform |
| top | top |
| vertical-align | verticalAlign |
| visibility | visibility |
| width | width |
| z-index | zIndex |
Feito, agora você já sabe como mudar propriedades CSS com JQuery em tempo de execução.
Descubra mais sobre ViamaisBLOG
Assine para receber nossas notícias mais recentes por e-mail.