This question already has answers here:
Is there a CSS parent selector?
(33 answers)
Closed 6 years ago.
I would change the CSS rule for the background to li.li-class if it contain an h3.
I have an HTML like this:
<div class="div-class">
<ul>
<li class="li-class">
<span itemprop="something">
<h3>Some title</h3>
</span>
</li>
</ul>
</div>
and the CSS for li background is:
.div-class ul li:nth-child(2n+1) { background: #f4f4f4; }
Where is the CSS selector for the li.li-class?
I have look this question Is there a CSS parent selector? and i try the code, but not work in this case.
Thanks all
As mentioned in comments, this is not possible using pure CSS.
It would however be very straight forward using the jQuery .has():
$("li").has("h3").addClass("yellow");
For the HTML:
<style>
.yellow{
background-color:yellow;
}
</style>
<div class="div-class">
<ul>
<li class="li-class">
<span itemprop="something">
<h3>Some title</h3>
</span>
</li>
<li class="li-class">
<span itemprop="something">
<p>
Not a header
</p>
</span>
</li>
</ul>
</div>
Will look at every li element and if it has a descendant h3 element it will add the yellow class to the li.
JSFiddle example
You can also make the selector more specific, so it does not select all lis but only the ones in a <div class="div-class"> with something like
$("div.div-class").find("li").has("h3").addClass("yellow");
If you are creating the html via php you have ultimate control:
<?php
$html = '';
foreach ($rows as &$row) {
// just figure out if you need the class
// before making the html - php is power
if (!empty($row['h3'])) {
$lih3_class = 'h3-in-li';
} else {
$lih3_class = '';
}
$html .= '<li class="li-class '.$lih3_class.'">';
$html .= ''; // put in your li contents as normal
$html .= '</li>';
}
echo $html;
?>
Related
I am displaying a recordset using div and want to change the background color on alternating rows. I have done this successfully in the past using an HTML table, but I can't figure it out using div.
The data displays properly, but there is no color other than the page's background color.
I assume it is something very simple that I am doing wrong, but I can't see it.
Here is the relevant code section and the CSS styles I tried (in a related css file). I tried both odd and even. Neither worked. And the ".indexrow{background-color: #94C8F2;}" doesn't work either.
<div class="indexrow">
<?php
$wa_startindex = 0;
while(!$rsTitle->atEnd()) {
$wa_startindex = $rsTitle->Index;
?>
<div class="column left">
<?php echo($rsTitle->getColumnVal("Title")); ?>
</div>
<div class="column middle">
Issue: <?php echo($rsTitle->getColumnVal("IssueID")); ?>
</div>
<div class="column right">
Page: <span class="BlackHeadline3"><?php echo($rsTitle->getColumnVal("Page")); ?></span>
</div>
<?php
$rsTitle->moveNext();
}
$rsTitle->moveFirst(); //return RS to first record
unset($wa_startindex);
unset($wa_repeatcount);
?>
</div>
.indexrow{
background-color: #94C8F2;
width: 100%;
}
.indexrow:nth-child(even){
background: #f2f2f2;
}
.indexrow a:hover{
background-color: #FFFF00;
}
I think your mistake is putting the nth-child() on the parent? nth-child needs to go on the children you are listing out because nth-child(odd) is saying "every instance of this element that is an odd child. I think you have fallen for the common misconception that nth-child(odd) means: "any odd child of this element"
So I have a blog layout (theme) that displays the articles in 2 columns on a row, each article being a LI inside an UL.
I then have this CSS code that basically adds margin-top: 20px; to each LI starting from the third (so it doesn't add a margin to the first row and leave an empty space between them and the header) and it also adds clear: both; to each 2 LIs (first LI of each row).
.module-row.two-cols ul li:nth-child(n+3) {
margin-top: 20px;
}
.module-row.two-cols ul li:nth-child(2n+1) {
clear: both;
}
The code for this is:
<div class="content-wrap module-row two-cols clearfix">
<ul>
<?php while (have_posts()): the_post(); ?>
<li class="col-sm-6">
<div class="item content-out co-type1">
<?php get_template('article-column');?>
</div>
</li>
<?php endwhile; ?>
</ul>
</div>
My problem starts with the CSS when I try to insert an ad LI after every 4th LI - so it will basically display an ad after every 2 rows/4 columns. Is there a way to change the CSS so I can insert that ad correctly and also keep the current CSS logic (with the top margin and clear)? Also, the ad LI will be with one with the class col-sm-12 (so full width).
So I want this, with CSS if possible
Basically insert via the PHP code another LI with class col-sm-12 after each fourth col-sm-6 LI. Thanks!
I can do this in PHP, change the theme's logic and do the count there, not via CSS, but I want to know if possible to do it via CSS because I would like best to not change the theme, only CSS. "Hacking" it and doing it via PHP instead of CSS will look like this - so this is the logic I want in CSS:
Add a margin-top to all the LIs after the 3rd LI, add a clear: both from each 2 to 2 LIs starting from the first LI and add both margin-top and clear: both to each 5th LI.
Here's a possible solution.
The idea is that with help of modulo operation, you can target the third while loop, 6th, 9th and so far. I am not a php developer so I hope I hope there is no syntax mistake in here if so would appreciate if someone points me to it! :)
EDIT: made a quick fix
EDIT: The HTML structure is not correct, but I do not know what you want to achieve with the li and divs at the end result so I left it the way you wrote it in the question.
<div class="content-wrap module-row two-cols clearfix">
<ul>
<?php $counter = 0 ?>
<?php while (have_posts()): the_post(); ?>
<?php if ($counter % 3 == 0){
<div class="col">
// code for your ad here
</div>
<?php } else {?>
<li class="col-sm-6">
<div class="item content-out co-type1">
<?php get_template( 'row' );?>
</div>
</li>
<?php } ?>
<?php $counter = $counter + 1 ?>
<?php endwhile; ?>
</ul>
</div>
I'm having a problem with a dropdown list. Here is the HTML code I got.
Screenshot: http://i.imgur.com/zIAhQkj.png
<nav>
Hjem
<ul>
<li class="nav_link">
<a onclick="extern_link_warning()" class="dropdown_hover"
href="http://bbc.com">Innleveringer</a>
<ul class="preview_box">
<li>
<a onclick="extern_link_warning()" href="http://bbc.com">Link 1</a>
</li>
<li>
Link 2
</li>
</ul>
</li>
<li> Om meg </li>
<!-- Kun admin brukere ser disse sidene: -->
<?php
if ($isLoggedIn){
echo '<li> Admin </li>';
}
?>
</ul>
</nav>
My CSS code for the dropdown:
.preview_box {
display: none;
}
a:hover + .preview_box,.preview_box:hover {
display: block;
}
The code above works, but the problem is that I want the ".preview_box" to appear when hovering a ".nav_link" tag instead of an "a" tag, but I can't get it to work. This is what I've tried:
.nav_link:hover + .preview_box, .preview_box:hover {
}
.nav_link:hover + .nav_link .preview_box, .nav_link .preview_box:hover {
}
Any ideas how I can get this to work?
Well after reading and reading your questions I think what you want is when you hover the li with the class nav_linkyou want to appear the .preview-box instead of hovering the anchor inside it...
Well, this should fix the problem...
.nav_link:hover .preview_box {
display: block;
}
This question already has answers here:
CSS selector for first element with class
(23 answers)
Closed 8 years ago.
I have an aside with two <div class="sku"> elements. I'm trying to use CSS to manipulate the :first-child but it doesn't work. However, when trying to access the :last-child it does.
JSFiddle
HTML
<aside>
<h1>Product Name</h1>
<div class="sku">
<h3>
100 – Small
</h3>
<div class="dimension">
<ul>
<li>
<span class="title">
Product Dimensions
</span>
<span class="specs">
23.75w
x
17.75h
x
28d
</span>
</li>
</ul>
</div>
</div>
<div class="sku">
<h3>
200 – Large
</h3>
<div class="dimension">
<ul>
<li>
<span class="title">
Product Dimensions
</span>
<span class="specs">
29.75w
x
17.75h
x
28d
</span>
</li>
</ul>
</div>
</div>
</aside>
CSS
.sku:first-child {
display:none !important; /*doesn't hide the first child*/
}
.sku:last-child {
display:none !important; /*does hide the first child*/
}
Why won't :first-child select the first div?
You cannot use :first-child psuedo class since .sku is not the first child. A better option is to use either :first-of-type (for first child) or :nth-of-type (which can accept a number or an equation) pseudo classes:
.sku:nth-of-type(1) {
display: none;
}
Updated Demo
The :first-child means the first child. Which is in this case the H1. So this does not work. You can use:
h1 + .sku { }
But only if this is the order you place your HTML.
I want to make the easiest css for menus. There's an outer div and an inner anchor. If the outer's class is the same as the inner's id, that will be active styled.
Example code:
<div class='<?php echo $_GET['menu']; ?>'>
<a href="index.php?menu=menu1" id='menu1'>Menu 1</a>
<a href="index.php?menu=menu2" id='menu2'>Menu 2</a>
<a href="index.php?menu=menu3" id='menu3'>Menu 3</a>
<a href="index.php?menu=menu4" id='menu4'>Menu 4</a>
<a href="index.php?menu=menu5" id='menu5'>Menu 5</a>
</div>
and I don't want to write a lot of css, like:
.menu1 #menu1, .menu2 #menu2, .menu3 ....
{ /*active stlye*/}
so I want the following: if the classname is the same of the id under it would be active.
Thanks in advance.
You can't do that with CSS ; maybe you could instead use PHP to accomplish something close :
<?php for($i=1;$i<=5;$i++){ ?>
<a href="index.php?menu=menu<?php echo $i;?>" <?php if($_GET['menu'] == 'menu'.$i) echo 'class="selected"'; ?>>Menu <?php echo $i;?></a>
<?php } ?>
And the CSS :
.selected{
/*active style*/
}
Edit : a solution with PHP is better than one based on JS because : everyone will see the .selected class, even people who have JS deactivated + for the others, the menu will not blink. Honestly it is super annoying to see a menu CSS changed via JS.
That's not possible. CSS isn't a programming language, you have to fully specify every selector.
But since you already work with PHP it would be a lot easier if you apply the class to one of your anchor tags:
<div class='<?php echo $_GET['menu']; ?>'>
<a href="index.php?menu=menu1" <?php echo $a_class[1]; ?> id='menu1'>Menu 1</a>
<a href="index.php?menu=menu2" <?php echo $a_class[2]; ?> id='menu2'>Menu 2</a>
<a href="index.php?menu=menu3" <?php echo $a_class[3]; ?> id='menu3'>Menu 3</a>
<a href="index.php?menu=menu4" <?php echo $a_class[4]; ?> id='menu4'>Menu 4</a>
<a href="index.php?menu=menu5" <?php echo $a_class[5]; ?> id='menu5'>Menu 5</a>
</div>
You have to either specify a bunch of selectors or add code to your PHP script.
Let's suppose I got you well. Then an answer could be :
<div id="menu">
<?php
for($i = 0; $i < 5; $i++) {
if(isset($_GET['menu']) && $i == $_GET['menu'])
echo " Menu $i";
else
echo " Menu $i";
}
?>
</div>
And in your CSS
#menu a {
/* Whatever you want for your normal links */
}
#menu .menu_active {
/* Whatever you want for your active link */
}
And that's it !