How do I select the h2 element in the 2nd box? [duplicate] - html

This question already has an answer here:
Why doesn't nth-of-type/nth-child work on nested elements?
(1 answer)
Closed 3 years ago.
I am using bootstrap grid structure. I coded a block containing 3 boxes with css. But I cannot select the 2nd and 3rd h2 elements.
<!-- 1st BOX-->
<div class="col-lg-6">
<div class="kutu">
<h2>1st TITLE</h2>
<div class="items">
</div>
<h3>test</h3>
</div>
test
</div>
<!-- 1st BOX END-->
<!-- 2st BOX-->
<div class="col-lg-6">
<div class="kutu">
<h2>2nd TITLE</h2>
<div class="items">
</div>
<h3>test</h3>
</div>
test
</div>
<!-- 2st BOX END-->
<!-- 3st BOX-->
<div class="col-lg-6">
<div class="kutu">
<h2>3rd TITLE</h2>
<div class="items">
</div>
<h3>test</h3>
</div>
test
</div>
<!-- 3st BOX END-->
I tried these methods, but I am not successful. I used nth-child and nth-of-type, but my desired result was not. How can I do that? Thank you
.kutu {
width: 600px;
height: 300px;
overflow: hidden;
}
.kutu:nth-of-type(2) h2{
color: red;
}
.kutu:nth-of-type(3) h2{
color: green;
}

Problem is the class is inside of the elements that are siblings
.kutu {
width: 600px;
height: 300px;
overflow: hidden;
}
.kutu:nth-of-type(2) h2{
color: red;
}
.kutu:nth-of-type(3) h2{
color: green;
}
<!-- 1st BOX-->
<div class="col-lg-6 kutu">
<div class="">
<h2>1st TITLE</h2>
<div class="items">
</div>
<h3>test</h3>
</div>
test
</div>
<!-- 1st BOX END-->
<!-- 2st BOX-->
<div class="col-lg-6 kutu">
<div class="">
<h2>2nd TITLE</h2>
<div class="items">
</div>
<h3>test</h3>
</div>
test
</div>
<!-- 2st BOX END-->
<!-- 3st BOX-->
<div class="col-lg-6 kutu">
<div class="">
<h2>3rd TITLE</h2>
<div class="items">
</div>
<h3>test</h3>
</div>
test
</div>
<!-- 3st BOX END-->
I tried these methods, but I am not successful. I used nth-child and nth-of-type, but my desired result was not. How can I do that? Thank you

Related

Horizontally align columns inside nested HTML list

I have a nested list and each list item works like a table row (using flex). The nested items i.e. rows are indented from the left so the width is different at different levels. It currently has tree levels but this may change at some point.
I want to:
Align all "columns" other than first one, on a single verticle line, and
Apply % widths on the columns excepts first one.
So far, I have only been able to achieve the alignment part using fixed pixel widths. But I really need to set the column .second and .third width based on % of top-most ul, otherwise the columns would not align perfectly. Fixed pixel widths aren't good for responsiveness.
I have two options in my mind:
Removing the left margin on list items and adding left-margin on .first. But this would mean manually adding borders on each "cell" and also some other code duplication.
Maybe there is a way to use CSS calc() somehow. With recursively rendered list, this seems quite complicated though. I am using this markup in a React.js component and I need to pass the level as prop and then use calc() in inline style. Doesn't look clean.
ul {
list-style-type: none;
padding: 0;
margin-left: 2rem;
}
.row {
display: flex;
border: 1px solid gray;
}
.first {
flex-grow: 1;
}
.second,
.third {
width: 200px;
}
<ul>
<li>
<div class="row">
<div class="first">
Title
</div>
<div class="second">
Description
</div>
<div class="third">
Date
</div>
</div>
<ul>
<li>
<div class="row">
<div class="first">
Title
</div>
<div class="second">
Description
</div>
<div class="third">
Date
</div>
</div>
</li>
<li>
<div class="row">
<div class="first">
Title
</div>
<div class="second">
Description
</div>
<div class="third">
Date
</div>
</div>
<ul>
<li>
<div class="row">
<div class="first">
Title
</div>
<div class="second">
Description
</div>
<div class="third">
Date
</div>
</div>
</li>
<li>
<div class="row">
<div class="first">
Title
</div>
<div class="second">
Description
</div>
<div class="third">
Date
</div>
</div>
</li>
</ul>
</li>
<li>
<div class="row">
<div class="first">
Title
</div>
<div class="second">
Description
</div>
<div class="third">
Date
</div>
</div>
</li>
</ul>
</li>
<li>
<div class="row">
<div class="first">
Title
</div>
<div class="second">
Description
</div>
<div class="third">
Date
</div>
</div>
</li>
<li>
<div class="row">
<div class="first">
Title
</div>
<div class="second">
Description
</div>
<div class="third">
Date
</div>
</div>
</li>
</ul>
I found a simple workaround. For my use case, simply setting the width of the columns to percentage of viewport does the job. i.e. width: 25vw
Now looks nice on both narrow and wider screens. Also quite happy with the browser support: https://caniuse.com/viewport-units
Please compare with above snippet on "Full page" view to see column widths are responsive on this one and are not responsive in the question snippet.
ul {
list-style-type: none;
padding: 0;
margin-left: 2rem;
}
.row {
display: flex;
border: 1px solid gray;
}
.first {
flex-grow: 1;
}
.second,
.third {
width: 25vw;
}
<ul>
<li>
<div class="row">
<div class="first">
Title
</div>
<div class="second">
Description
</div>
<div class="third">
Date
</div>
</div>
<ul>
<li>
<div class="row">
<div class="first">
Title
</div>
<div class="second">
Description
</div>
<div class="third">
Date
</div>
</div>
</li>
<li>
<div class="row">
<div class="first">
Title
</div>
<div class="second">
Description
</div>
<div class="third">
Date
</div>
</div>
<ul>
<li>
<div class="row">
<div class="first">
Title
</div>
<div class="second">
Description
</div>
<div class="third">
Date
</div>
</div>
</li>
<li>
<div class="row">
<div class="first">
Title
</div>
<div class="second">
Description
</div>
<div class="third">
Date
</div>
</div>
</li>
</ul>
</li>
<li>
<div class="row">
<div class="first">
Title
</div>
<div class="second">
Description
</div>
<div class="third">
Date
</div>
</div>
</li>
</ul>
</li>
<li>
<div class="row">
<div class="first">
Title
</div>
<div class="second">
Description
</div>
<div class="third">
Date
</div>
</div>
</li>
<li>
<div class="row">
<div class="first">
Title
</div>
<div class="second">
Description
</div>
<div class="third">
Date
</div>
</div>
</li>
</ul>

How to select only the first element of a certain class, not it's grand children

I am trying to achieve something like this
<div class="parent">
<div class="child">
<div class="item"> <!-- select this -->
<div class="item"> <!-- not this -->
</div>
</div>
</div>
</div>
I have tried using first-child like this -
.parent .child .item:first-child p {
background: red;
}
<div class="parent">
<div class="child">
<div class="item"> <!-- select this -->
<p>This should be changed</p>
<div class="item"> <!-- not this -->
<p>This should NOT be changed</p>
</div>
</div>
</div>
</div>
But it didn't work. Is there any pure CSS way of doing it?
You're probably looking for the child combinator:
.parent .child > .item > p {
background: red;
}
<div class="parent">
<div class="child">
<div class="item"> <!-- select this -->
<p>This should be changed</p>
<div class="item"> <!-- not this -->
<p>This should NOT be changed</p>
</div>
</div>
</div>
.parent .child .item:first-child > p {
background: red;
}
<div class="parent">
<div class="child">
<div class="item"> <!-- select this -->
<p>This should be changed</p>
<div class="item"> <!-- not this -->
<p>This should NOT be changed</p>
</div>
</div>
</div>
</div>
You can also use the :first-of-type pseudo-class.
The :first-of-type CSS pseudo-class represents the first element of its type among a group of sibling elements.
Here you're selecting the first div, and then, the first p element.
.parent > .child > .item:first-of-type > p:first-of-type {
background: red;
}
<div class="parent">
<div class="child">
<div class="item"> <!-- select this -->
<p>This should be changed</p>
<div class="item"> <!-- not this -->
<p>This should NOT be changed</p>
</div>
</div>
</div>
</div>

Layout issue with Grids:SkeletonFramework

I am using Skeleton Framework, and the layout I made using the grid system is like this:
<div class="container">
<!-- columns should be the immediate child of a .row -->
<div class="row">
<div class="three columns">
<br>
</div>
<div class="six columns" style="margin-top: 25px">
<img src="img/okay.jpg" width="50px" style="display:inline"/>
<p id="title" style="display:inline">{{title}}</p>
<p id="excerpt" style="display:inline">{{description}}</p>
<div id="describe me" style="display:inline"><span style="display:inline">{{name}}</span><span style="display:inline">{{date}}</span></div>
</div>
<div class="three columns">
<br>
</div>
</div>
</div>
It shows something like this:
But what I want is something like this:
How can this be achieved?
Result:
Responding to your code, although there are many alternate solutions for this, maybe even in their documentation.
First, try to avoid using inline styles.
Wrap your content inside a
class.
Vetical align the image to the top.
Remove the default margin
on the paragraph items.
.desc {
display: inline-block;
}
.desc p {
margin-bottom: 0;
}
.six img {
vertical-align: top;
}
<link href="//cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css" rel="stylesheet" />
<div class="container">
<!-- columns should be the immediate child of a .row -->
<div class="row">
<div class="three columns">
<br>
</div>
<div class="six columns">
<img src="http://placehold.it/50x75" width="50px" />
<div class="desc">
<p id="title">{{title}}</p>
<p id="excerpt">{{description}}</p>
<div id="describe me"><span>{{name}}</span><span>{{date}}</span>
</div>
</div>
</div>
<div class="three columns">
<br>
</div>
</div>
</div>

CSS nth-child expression to remove margin-bottom of last row

I am creating lists that display in columns of two, every .customer-review-container has a margin-bottom like so:
<div class="col-md-6">
<div class="customer-review-container">
</div> <!-- end customer-review-container -->
</div>
<div class="col-md-6">
<div class="customer-review-container">
</div> <!-- end customer-review-container -->
</div>
<div class="col-md-6">
<div class="customer-review-container">
</div> <!-- end customer-review-container -->
</div>
<div class="col-md-6">
<div class="customer-review-container">
</div> <!-- end customer-review-container -->
</div>
What I wish to do is create an nth-child expression to remove the margin-bottom of on the last row (3rd and 4th), so I try with the code:
div:nth-child(3n+1).customer-review-container{
margin-bottom: 0;
}
But it only removes the margin-bottom of last div (4th).
This is what you need:
.col-md-6:nth-last-child(-n+2) .customer-review-container {
margin-bottom: 0;
}
Sample DEMO
div:nth-child(3n+1).customer-review-container, div:nth-child(3n+0).customer-review-container{
margin-bottom: 0;
}
you can use the nth-last-child(n) selector like so:
.customer-review-container:nth-last-child(-n+2) {
margin-bottom: 0;
}
this will select the last two children
Use:
div:nth-child(n+3) .customer-review-container{
margin-bottom: 0;
}
:nth-child(n+3) will target every div with an index superior or equal to 3 (indexes stars to 1).
EDIT:
If you want to target the last row, whatever if there's one or two elements, use this selector:
div:nth-last-child(-n+2):nth-child(odd) > .customer-review-container,
div:nth-last-child(-n+2):nth-child(odd) + div > .customer-review-container {
margin-bottom: 0;
}
Example:
.wrapper {
border: 1px solid red;
overflow: hidden;
}
.customer-review-container {
border: 1px solid black;
margin-bottom: 20px;
}
div:nth-last-child(-n+2):nth-child(odd) > .customer-review-container,
div:nth-last-child(-n+2):nth-child(odd) + div > .customer-review-container {
margin-bottom: 0;
background: blue;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="wrapper">
<div class="col-md-6">
<div class="customer-review-container">
Lorem
</div>
</div>
<div class="col-md-6">
<div class="customer-review-container">
Lorem
</div>
</div>
<div class="col-md-6">
<div class="customer-review-container">
Lorem
</div>
</div>
<div class="col-md-6">
<div class="customer-review-container">
Lorem
</div>
</div>
<div class="col-md-6">
<div class="customer-review-container">
Lorem
</div>
</div>
<div class="col-md-6">
<div class="customer-review-container">
Lorem
</div>
</div>
<div class="col-md-6">
<div class="customer-review-container">
Lorem
</div>
</div>
<div class="col-md-6">
<div class="customer-review-container">
Lorem
</div>
</div>
<div class="col-md-6">
<div class="customer-review-container">
Lorem
</div>
</div>
</div>

How come one of my columns in bootstrap is not functioning properly?

I am making a site for my high school robotics team and I am completely new to html and css. How come one of the columns (the member 10 one) is not functioning like the others?
It seems one of the columns is off center. I'm not sure exactly how to describe it but I can post an image if necessary.
HTML:
<!DOCTYPE html>
<html>
<head>
<title>Robotics Team</title>
<!-- Link to stylesheet -->
<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="css/index1.css">
<!-- Mobile Scaling -->
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<!-------------------- UNIFORM CODE ------------------------->
<!-- Navbar -->
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="/Home">Team 3774</a>
</div>
<div class="navbar-collapse collapse" style="height: 0.866667px;">
<ul class="nav navbar-nav">
<li>Team Bio</li>
<li>Our Robot</li>
<li>Our Coach</li>
<li>Gallery</li>
<li>Outreach</li>
<li>Youtube</li>
</ul>
</div>
</div>
</div>
<!-- Banner -->
<div id="top-jumbotron" class="jumbotron">
<img src="/Images/Banner.png" class="img-responsive" alt="Responsive image">
</div>
<!----------------------------------------------------------->
<div class="jumbotron">
<h1>Team Member Bio</h1>
<p>Here you can find links to every member with some information on each of them.</p>
</div>
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="bubble">
<h2>Member 1 </h2>
<p>Team Captain, Engineer, Coder</p>
Read More
</div>
</div>
<div class="col-md-4">
<div class="bubble">
<h2>Member 2</h2>
<p>Head Engineer, Assistant Captain</p>
Read More
</div>
</div>
<div class="col-md-4">
<div class="bubble">
<h2>Member 3</h2>
<p>Head Coder, Head Web-master</p>
Read More
</div>
</div>
<div class="col-md-4">
<div class="bubble">
<h2>Member 4</h2>
<p>Coder, Head Documenter</p>
Read More
</div>
</div>
<div class="col-md-4">
<div class="bubble">
<h2>Member 5</h2>
<p>Engineer, Head 3D Modelling</p>
Read More
</div>
</div>
<div class="col-md-4">
<div class="bubble">
<h2>Member 6</h2>
<p>Coder, Web-master, Engineer</p>
Read More
</div>
</div>
<div class="col-md-4">
<div class="bubble">
<h2>Member 7</h2>
<p>Financial Advisor, Engineer</p>
Read More
</div>
</div>
<div class="col-md-4">
<div class="bubble">
<h2>Member 8</h2>
<p>Engineer, Documenter</p>
Read More
</div>
</div>
<div class="col-md-4">
<div class="bubble">
<h2>Member 9</h2>
<p>Engineer, Coder</p>
Read More
</div>
<div class="col-md-4">
<div class="bubble" id="special">
<h2>Member 10</h2>
<p>Secretary, Mascot</p>
Read More
</div>
</div>
</div>
</div>
</body>
</html>
#top-jumbotron
{
padding-left:0;
padding-right:0;
padding-bottom:0;
margin-bottom: 0;
}
body
{
background-color: #E8E8E8;
}
.bubble
{
background-color: #ffffff;
padding: 20px;
width: 95%;
height: 175px;
border-radius: 15px;
margin-top: 10px;
margin-bottom: 10px;
}
#special
{
float: left;
}
You're just missing a /div - I notice you have a div inside a div, remember you need to close both. I use Netbeans for html and it will tell you if there is a problem. If you are on a Mac, I am told Komodo is really good, too. A good editing program is a life saver!
Fixed code:
<div class="col-md-4">
<div class="bubble">
<h2>Mina Hanna</h2>
<p>Engineer, Coder</p>
Read More
</div>
</div> <!-- this one! -->
edit there are a few things wrong with the code and michael points one out - as he says, Bootstrap is a grid. So Imagine (I actually draw it usually) a box split into 12 columns. For every row div, you should only have columns that add up to 12 inside it. You can break this and it will often still work because the div will just overflow and move down, but it's not best practice. If fixing the missing div end tag doesn't work, try fixing the row structure so it only adds up to 12 columns across the page
You can have a layout like the one you specify, however it wont necessarily work per Bootstraps intended functionality, and as such its usually a good idea to follow their recommended row structure.
Bootstrap includes a responsive, mobile first fluid grid system that appropriately scales up to 12 columns as the device or viewport size increases.
Instead of
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4"></div>
<div class="col-md-4"></div>
<div class="col-md-4"></div>
<div class="col-md-4"></div>
<div class="col-md-4"></div>
...
</div>
You should do, e.g:
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-2"></div>
<div class="col-md-2"></div>
<div class="col-md-2"></div>
<div class="col-md-2"></div>
<div class="col-md-2"></div>
</div>
Within a row, the sum of the numbers following the hyphen in each column definition should add up to 12.