Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Targeting itemprop in CSS?

Tags:

css

I have the following two sections of code generated by a Wordpress theme. This first section of code is generated for a WP Page:

    <div class="postinner">         <div itemprop="name">             <h1 class="pagetitle">My Title</h1>         </div>         <p>First line of text</p>         <p>Second line of text</p>     </div> 

This second section of code is generated for a WP Post:

    <div class="postinner">         <article itemtype="http://schema.org/Article" itemscope="" role="article">             <div itemprop="headline">                 <h1 class="pagetitle">Hello World!</h1>             </div>         </article>     </div> 

I cannot figure out the CSS selector to specifically target and center the text of the H1 tag within the "itemprop DIV" for the 1st section of code.

Also, I would also like to target and style the H1 tag in the WP Post with a different text color but again, cannot figure out CSS selector.

like image 520
IanDude Avatar asked Nov 04 '13 22:11

IanDude


People also ask

What is CSS Itemprop?

The itemprop global attribute is used to add properties to an item. Every HTML element can have an itemprop attribute specified, and an itemprop consists of a name-value pair. Each name-value pair is called a property, and a group of one or more properties forms an item.

What is meta Itemprop name?

Meta item prop is a technical term used by search engine optimisation professionals to describe the property of a website page that is used to identify the content of the page. Meta item prop is a code that you can add to your website's code to help Google better understand the content of your pages.


2 Answers

You could try using CSS Attribute Selectors, such as:

div[itemprop="name"] h1 {    color: red; } 

and:

div[itemprop="headline"] h1 {    color: yellow; } 

example: http://jsfiddle.net/bEUk8/

like image 177
Stuart Kershaw Avatar answered Sep 28 '22 05:09

Stuart Kershaw


The [att=val] selector (as suggested by Stuart Kershaw’s) works if the itemprop attribute has only this token as value.

But the itemprop attribute can have multiple tokens as value, in which case the [att=val] wouldn’t match anymore.

So you might want to use the [att~=val] selector, which matches in both cases: if it’s the only token or if it’s one of multiple tokens.

Example

Although both span elements have the name token as value of itemprop, only the first one is matched by the CSS rule with the [itemprop="name"] selector:

[itemprop="name"] {font-size:200%;}  [itemprop~="name"] {color:red;}
<div itemscope>    <span itemprop="name">'name'</span>    <span itemprop="name headline">'name' and 'headline'</span>  </div>
like image 25
unor Avatar answered Sep 28 '22 06:09

unor