Tumblr 1st post different style using nth or first child - html

I'd like to know exactly which element on my HTML to implement nth-child or first-child property so that the first post has a different style. A grid usually seen in many blogs. I found this same question here in SO. But since my html elements are different, I can't seem to make it work.
I tried using:
#content #posts:first-child {...}
But it doesn't work. Knowing Tumblr's limitations, I'm not sure if there's a workaround for this. Anyhow, here is my html:
<div id="content">
<div id="posts">
{block:Text}<!--textpost-->
{block:Title}
<h1 class="title">
{Title}
</h1>
{/block:Title}
<div class="text">{Body}</div>
{block:More}
<div class='rmlink'>Read more</div>
{/block:More}
{/block:Text}<!--textpost-->
{block:Photo}<!--photopost-->
<h1 id="ribbon">
Photo of the day
</h1>
{LinkOpenTag}
<div id="photopost">
<img src="{PhotoURL-HighRes}"/></div>
{LinkCloseTag}
{block:Caption}
<div class="caption">{Caption}</div>
{block:More}
<div class='rmlink'>
Read more
</div>
{/block:More}
{/block:Caption}
{/block:Photo}<!--photopost-->
</div><!--posts-->
</div><!--content-->

You need a wrapper around each post type. The usual markup I have seen is a div or article with the class post. That will allow you to target nth children. So Inside each post type block {block:Text} you need to wrap the content in a wrapper that has the same class.
Then you can write some css like this:
#posts .post {
color:black;
}
#posts .post:first-child {
color:#CCCCCC;
}
See my fiddle here: http://jsfiddle.net/4fsh7rvr/
The #posts element will always be an only child as it has an ID which is unique.

Related

CSS selector that includes only top matches in a recursive structure

I have a "self-similar" (or recursive) HTML structure where I need to build a CSS Selector that catches only the top matches in this structure. For instance, given the following HTML:
<div class="option-block">
<div class="option">
<p>main content</p>
</div>
<div class="detail">
<div class="option-block">
<div class="option">
<p>child content</p>
</div>
<div class="detail">
<p>child detail</p>
</div>
</div>
</div>
</div>
I need a CSS Selector that gives me the first div within the top option-block, IOW, the div that contains the text main content.
I've tried with .option-block > div:first-child (and several variants like it), but that gives me also the div with the text 'child content'.
One thing to note is that this particular structure could be anywhere within the HTML document, so I don't have a prior reference to use as anchor (like they do here or here).
Another thing to note is that this structure could be self-replicating to more levels: the .detail component of an .option-block container could always have another .option-block container inside, so I cannot rely on counting or anything like that.
UPDATE: thanks for the answers so far. Something that I should have mentioned is that the selector is to be used programmatically to locate the items, not to apply styles. Also, the selector is to be applied from the context of an existing item, which may be already inside a this hierarchy.
I'm going to assume you can't just change your HTML structure (otherwise just add an extra class to the 1st level, or wrap the whole thing and check for direct ancestry).
You could check the ancestors of .option-block. This works if you're fairly sure there won't be other .option-block > .detail structures outside of your block.
:not(.option-block) > :not(.detail) > .option-block > div:first-child {
background: red;
}
<div class="option-block">
<div class="option">
<p>main content</p>
</div>
<div class="detail">
<div class="option-block">
<div class="option">
<p>child content</p>
</div>
<div class="detail">
<p>child detail</p>
</div>
</div>
</div>
</div>
I believe there is no support for recursive functions for :first-child and :last-child selectors (check here)
A possible solution may be giving the "main content" div.option another class to have an unique selector. Otherwise you can set a class in the .option-block divs that are inside a .detail div and use the :not selector:
.option-block:not(.inner-option) .option{ ... }
From what I’ve heard, there doesn’t seem to be a way to do what I need with a CSS selector. However, after some additional research it seems that XPath can work, something like this:
(.//div[#class=“option-block”])[1]/div

How to place many text elements side by side, that you see no difference between the text?

how can I place many paragraphs or div elements next to each other, that I see no difference between them? For example:
<div>
<div class="text1">hey, how are</div>
<div class="text2"> you?</div>
</div>
They are placed under each other, but I want them to be side by side, and please don't write me other ways, for example something like: 'you can write the text in one div'... :) I saw this question several times from other users, but they had a bit different problem like mine, so please answer me this question
Thanks
<div>
<span>hey, how ar</span>
<span> you?</span>
</div>
or
.text {
display: inline-block;
}
<div>
<div class="text">hey, how are</div>
<div class="text"> you?</div>
</div>
or
.container {
display: flex;
}
<div class="container">
<div>hey, how are</div>
<div> you?</div>
</div>
div is a block-level element, which means that it will take up the whole of a 'row' on the screen, unlike inline elements.
I'd suggest you have a read through of the MDN pages on these two categories to get a better understanding of this:
Block-level elements
Inline elements
Among the many ways of solving this (change divs to an inline element type like span, using flexbox, etc), one option is to force your div to be inline by changing their CSS:
.text1, .text2 {
display: inline-block;
}
You can do this in many ways. Here I'll be showing classic ways to do this.
using display:inline-block property of css.
.text1{
display:inline-block;
}
.text2{
display:inline-block;
}
using display:flex property of css.
html:
<div class="text-wrapper">
<div class="text1">hey, how are</div>
<div class="text2"> you?</div>
</div>
css:
.text-wrapper{
display: flex;
}
There are several ways of accomplishing this. You could do it with CSS Flexbox, CSS Grid, CSS float, or you could change the display property on the DIV's.
As you have tagged your question with "Angular", it is very common to use the Angular Flex Layout package for positioning (https://github.com/angular/flex-layout). If you add this package to your project, you could solve it like this:
<div fxLayout="row" fxLayoutAlign="start">
<div class="text1">hey, how are</div>
<div class="text2"> you?</div>
</div>
Why don't you use bootsrap? This is a typical scenario for placing things side by side.
<div class="d-flex justify-content-center">
<p></p>
<p></p>
<p></p>
</div>

nth-child, change style of every other div with class name

I have got some elements on my page, they should all be styled the same except for every other one, where I just want to change some styling.
Here is the CSS which I was hoping would select the div inside the stack of different elements:
.stagger_reviews[class=inner]:nth-child(2n+2) {
background-color:#003;
}
Here is the HTML:
<div class="stagger_reviews">
<!-- Later use PHP to load reviews, CSS should switch the images from left to right -->
<article class="container box style1">
<a style="background-image:url(images/blank-user.jpg); " href="#" class="image fit"></a>
<div class="inner">
<header>
<h2>Martyn Ball</h2>
</header>
<p>
I found this service on a Google Search, didn't expect it to be so great!
</p>
</div>
</article>
<article class="container box style1">
<a style="background-image:url(images/blank-user.jpg); " href="#" class="image fit"></a>
<div class="inner">
<header>
<h2>Martyn Ball</h2>
</header>
<p>
I found this service on a Google Search, didn't expect it to be so great!
</p>
</div>
</article>
</div>
As you can see I just want to adjust the one div inside each article which has the class name inner. And maybe some other elements as well but once I have this working I can do that.
The style isn't being applied to the second inner div, I have made about 4 copies of the article and none are being changed.
Here is the solution, I put the nth-child in the wrong place.
.stagger_reviews > article:nth-child(2n+2) div[class=inner]

CSS pseudo-classes

I have a problem, maybe it's obvious, but I couldn't find any answers how to do this.
I have a structure like this on my website:
<div class="row-even">
<article class="featured-job">a</article>
</div>
<div class="row-odd">
<article class="featured-job">b</article>
</div>
<div class="row-even">
<article class="regular-job">a</article>
</div>
<div class="row-odd">
<article class="regular-job">b</article>
</div>
<div class="row-even">
<article class="regular-job">c</article>
</div>
This tiny thing is generated by PHP for listing some articles from two types, a Featured job, and a Regular job. I want to separate these two content types by adding a margin-top for the first one of the .regular-job articles. I tried using first-line, first-child, first-of-type, all from the first-* and even tried nth-child, but nothing worked for me.
(I know these separators working on the parent of the element I am using on.)
Is there any way it can be done?
The problem with first-child and regular-job is that the articles are not directly in the same parent because they are nested in row-even & row-odd. You either could wrap the regular-job rows in another div which gets the margin applied or add another class to the first row containing a regular-job. You could even add a class directly to the first regular-job.
I don't know how you PHP loop looks like, but maybe try to use a counter for that matter.
If needed I will gladly provide an HTMl/CSS example!
You would probably want to do someting like that:
div:first-child .regular-job {
margin-top: 20px;
}
You select the first parent element that has the child of .featured-job.
PS. Be careful where you are applying the margin, it won't work on inline elements or if you want to separate the parent elements then applying it to the article is not a good idea.
As Sven says, the problem is that you need the elements to be siblings for this selectors to work.
Set a class in the parent, matching the one of the child:
HTML
<div class="row-even featured-father">
<article class="featured-job">a</article>
</div>
<div class="row-odd featured-father">
<article class="featured-job">b</article>
</div>
<div class="row-even regular-father">
<article class="regular-job">a</article>
</div>
<div class="row-odd regular-father">
<article class="regular-job">b</article>
</div>
<div class="row-even regular-father">
<article class="regular-job">c</article>
</div>
Then, it is easy to set the CSS. for instance:
.featured-father + .regular-father article {
background-color: red;
}
fiddle
while iterating in the PHP, add another dummy class with index.
Then it will be a piece of cake to make your custom class:
<div class="row-even featured_1">
<article class="featured-job">a</article>
</div>
<div class="row-odd featured_2">
<article class="featured-job">b</article>
</div>
<div class="row-even regular_1">
<article class="regular-job">a</article>
</div>
<div class="row-odd regular_2">
<article class="regular-job">b</article>
</div>
<div class="row-even regular_3">
<article class="regular-job">c</article>
</div>
notice the two introduced classes: featured_X and regular_X.
then:
.regular_1{
margin-top:50px;
}

id vs class declarations in css

I have the following html structure
<div id="content">
<div id="transport">
<div id="header">Header Text</div>
<div id="image"></div>
<div id="right_content">Lots of text</div>
</div>
</div>
Is there a better way to arrange the css for the above rather than use ids for all of the divs?
IDs can only be used once in a document. Classes can be reused throughout the document. Styles attached to IDs trump styles attached to classes.
Other than that, it's entirely up to you and the particular content you are marking up.
Looking at your sample code, I would recommend using an actual header tag instead of a div with an ID of header.
Why not change those to classes and have only the top level container with an ID? That way you can target it with the top level ID.
You should also remove the header DIV and use a H2 or H3 tag.
<div id="content">
<div class="transport">
<h2>Header</h2>
<div class="image"></div>
<div class="right_content">Lots of text</div>
</div>
</div>
Your CSS would look like
#content .transport {}
#content h2 {}
#content .image