{"id":193,"date":"2024-05-21T09:41:04","date_gmt":"2024-05-21T07:41:04","guid":{"rendered":"https:\/\/blog.sutilweb.eu\/?page_id=193"},"modified":"2024-05-21T09:41:04","modified_gmt":"2024-05-21T07:41:04","slug":"36-el-atributo-class","status":"publish","type":"page","link":"https:\/\/sutilweb.eu\/index.php\/lenguajes\/html5\/html5-manual\/36-el-atributo-class\/","title":{"rendered":"36. El atributo class"},"content":{"rendered":"\n<p>El atributo <strong><em>class<\/em> <\/strong>es usado para especificar una clase para un elemento HTML. M\u00faltiples elementos HTML pueden utilizar la misma clase.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Usar el atributo class<\/h2>\n\n\n\n<p>El atributo <strong><em>class<\/em><\/strong> se usa a menudo para apuntar a un nombre de clase en una hoja de estilo. Tambi\u00e9n puede ser utilizado por <strong>JavaScript<\/strong> para acceder y manipular elementos con el nombre de clase espec\u00edfico.<\/p>\n\n\n\n<p>En el siguiente ejemplo tenemos tres elementos <strong><em>&lt;div&gt;<\/em><\/strong> con un atributo <strong><em>class<\/em><\/strong> con el valor de \u00abciudad\u00bb. Los tres elementos <strong><em>&lt;div&gt;<\/em><\/strong> tendr\u00e1n el mismo estilo de acuerdo con la definici\u00f3n de estilo .<strong><em>city<\/em><\/strong> en la secci\u00f3n principal.<\/p>\n\n\n\n<p><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;style&gt;\n.city {\n&nbsp;&nbsp;background-color: tomato;\n&nbsp; color: white;\n&nbsp; border: 2px solid black;\n&nbsp; margin: 20px;\n&nbsp; padding: 20px;\n} \n&lt;\/style&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n\n&lt;div class=\"city\"&gt;\n&nbsp; &lt;h2&gt;London&lt;\/h2&gt;\n&nbsp; &lt;p&gt;London is the capital of England.&lt;\/p&gt;\n&lt;\/div&gt;\n\n&lt;div class=\"city\"&gt;\n&nbsp; &lt;h2&gt;Paris&lt;\/h2&gt;\n&nbsp; &lt;p&gt;Paris is the capital of France.&lt;\/p&gt;\n&lt;\/div&gt;\n\n&lt;div class=\"city\"&gt;\n&nbsp; &lt;h2&gt;Tokyo&lt;\/h2&gt;\n&nbsp; &lt;p&gt;Tokyo is the capital of Japan.&lt;\/p&gt;\n&lt;\/div&gt;\n\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>Nota<\/strong>: el atributo <strong><em>class<\/em><\/strong> se puede usar en cualquier elemento HTML. \u00a1El nombre de la clase distingue entre may\u00fasculas y min\u00fasculas!.<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">Sintaxis para class<\/h2>\n\n\n\n<p>Para crear una clase, escribe un car\u00e1cter de punto (.), seguido de un nombre de clase. Luego, define las propiedades CSS entre llaves {}.<\/p>\n\n\n\n<p><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;style&gt;<strong>\n.city {\n&nbsp; background-color: tomato;\n&nbsp; color: white;\n&nbsp; padding: 10px;\n}\n<\/strong>&lt;\/style&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n\n&lt;h2 class=\"city\"&gt;London&lt;\/h2&gt;\n&lt;p&gt;London is the capital of England.&lt;\/p&gt;\n\n&lt;h2 class=\"city\"&gt;Paris&lt;\/h2&gt;\n&lt;p&gt;Paris is the capital of France.&lt;\/p&gt;\n\n&lt;h2 class=\"city\"&gt;Tokyo&lt;\/h2&gt;\n&lt;p&gt;Tokyo is the capital of Japan.&lt;\/p&gt;\n\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">M\u00faltiples clases<\/h2>\n\n\n\n<p>Los elementos HTML pueden pertenecer a m\u00e1s de una clase. Para definir varias clases, separa los nombres de las clases con un espacio, por ejm <strong><em>&lt;div class=\u00bbciudad principal\u00bb&gt;<\/em><\/strong>. El elemento se dise\u00f1ar\u00e1 de acuerdo con todas las clases especificadas.<\/p>\n\n\n\n<p>En el siguiente ejemplo, el primer elemento <strong><em>&lt;h2&gt;<\/em><\/strong> pertenece tanto a la clase de ciudad como a la clase principal, y obtendr\u00e1 los estilos CSS de ambas clases.<\/p>\n\n\n\n<p><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;style&gt;\n.city {\nbackground-color: tomato;\ncolor: white;\npadding: 10px;\n}\n\n.main {\ntext-align: center;\n}\n&lt;\/style&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n\n&lt;h2&gt;Multiple Classes&lt;\/h2&gt;\n&lt;p&gt;Here, all three h2 elements belongs to the \"city\" class. \nIn addition, London also belongs to the \"main\" class, \nwhich center-aligns the text.&lt;\/p&gt;\n\n&lt;h2 class=\"city main\"&gt;London&lt;\/h2&gt;\n&lt;h2 class=\"city\"&gt;Paris&lt;\/h2&gt;\n&lt;h2 class=\"city\"&gt;Tokyo&lt;\/h2&gt;\n\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Diferentes elementos pueden compartir la misma clase<\/h2>\n\n\n\n<p>Diferentes elementos HTML pueden apuntar al mismo nombre de clase.&nbsp;En el siguiente ejemplo, tanto <strong><em>&lt;h2&gt;<\/em><\/strong> como <strong><em>&lt;p&gt;<\/em><\/strong> apuntan a la clase \u00abcity\u00bb y compartir\u00e1n el mismo estilo.<\/p>\n\n\n\n<p><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;style&gt;\n.city {\nbackground-color: tomato;\ncolor: white;\npadding: 10px;\n} \n&lt;\/style&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n\n&lt;h2&gt;Different Elements Can Share Same Class&lt;\/h2&gt;\n\n&lt;p&gt;Even if the two elements do not have the same tag name, they can both point to the same class, and get the same CSS styling:&lt;\/p&gt;\n\n&lt;h2 class=\"city\"&gt;Paris&lt;\/h2&gt;\n&lt;p class=\"city\"&gt;Paris is the capital of France.&lt;\/p&gt;\n\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Uso del atributo class en JavaScript<\/h2>\n\n\n\n<p><strong>JavaScript<\/strong> tambi\u00e9n puede usar el nombre de la clase para realizar ciertas tareas para elementos espec\u00edficos.&nbsp;<strong>JavaScript<\/strong> puede acceder a elementos con un nombre de clase espec\u00edfico con el m\u00e9todo <strong><em>getElementsByClassName()<\/em><\/strong>.<\/p>\n\n\n\n<p><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;body&gt;\n\n&lt;h2&gt;Use of The class Attribute in JavaScript&lt;\/h2&gt;\n&lt;p&gt;Click the button to hide all elements with class name \"city\":&lt;\/p&gt;\n\n&lt;button onclick=\"myFunction()\"&gt;Hide elements&lt;\/button&gt;\n\n&lt;h2 class=\"city\"&gt;London&lt;\/h2&gt;\n&lt;p&gt;London is the capital of England.&lt;\/p&gt;\n\n&lt;h2 class=\"city\"&gt;Paris&lt;\/h2&gt;\n&lt;p&gt;Paris is the capital of France.&lt;\/p&gt;\n\n&lt;h2 class=\"city\"&gt;Tokyo&lt;\/h2&gt;\n&lt;p&gt;Tokyo is the capital of Japan.&lt;\/p&gt;\n\n&lt;script&gt;\nfunction myFunction() {\nvar x = document.getElementsByClassName(\"city\");\nfor (var i = 0; i &lt; x.length; i++) {\nx[i].style.display = \"none\";\n}\n}\n&lt;\/script&gt;\n\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>El atributo class es usado para especificar una clase para un elemento HTML. M\u00faltiples elementos HTML pueden utilizar la misma [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":41,"menu_order":35,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"class_list":["post-193","page","type-page","status-publish","hentry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Sutil Web","author_link":"https:\/\/sutilweb.eu\/index.php\/author\/sutilweb\/"},"uagb_comment_info":0,"uagb_excerpt":"El atributo class es usado para especificar una clase para un elemento HTML. M\u00faltiples elementos HTML pueden utilizar la misma [&hellip;]","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/193","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/comments?post=193"}],"version-history":[{"count":2,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/193\/revisions"}],"predecessor-version":[{"id":195,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/193\/revisions\/195"}],"up":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/41"}],"wp:attachment":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/media?parent=193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}