How to use :nth-child for alternate rows [duplicate] - html

This question already has answers here:
How can I style even and odd elements?
(9 answers)
Closed 3 years ago.
I want to style my accordion with border-top and border-bottom for even number of rows Ex: 2, 4 ,6 etc.
I tried with :nth-child but it is not reflecting my parent div but it is reflecting my child element.
code
<div className="accordion" id="adsdfsdf">
<div className="accordionitem">
<div className="accordion-card">
<div className="accordion-card-header" id="aada">
<h3 className="mb-0 w-100">
<a
className="collapsed"
role="button"
>
<span className="icon-left">
<i className=" a-minus" />
<i className="a-plus" />
</span>
</a>
</h3>
</div>
</div>
</div>
<div className="accordion" id="adsdfsdf">
<div className="accordionitem">
<div className="accordion-card">
<div className="accordion-card-header" id="aada">
<h3 className="mb-0 w-100">
<a
className="collapsed"
role="button"
>
<span className="icon-left">
<i className=" a-minus" />
<i className="a-plus" />
</span>
</a>
</h3>
</div>
</div>
</div>
css
.accordion-card-header {
boder-top: 1px solid red;
border-bottom: 1px solid red;
}

You are almost there. you need to use :nth-child(even) to style even rows and :nth-child(odd) to style odd rows. For the further reference you can go through nth-child() Selector
Here is a small demo
div:nth-child(odd) {
height: 100px;
width: 100px;
background: yellow;
margin: 20px auto;
}
div:nth-child(even) {
height: 100px;
width: 100px;
background: green;
border-top: 5px solid black;
border-bottom: 5px solid red;
margin: 20px auto;
}
<div>odd</div>
<div>even</div>
<div>odd</div>
<div>even</div>

Related

align equal column without using table

I have li like this
<li>0 - 10 Years old</li>
<li>11 - 20 Years old</li>
They have different lengths. I want to center 2 column each row and align them left. But my attempt is not perfect, due to different length they aren't align left equally.
I thought of using table but what's a modern solution? Note that I didn't use any css framework for this.
https://jsfiddle.net/coeh77Lp/
Just update CSS in your fiddle to
https://jsfiddle.net/coeh77Lp/3/
ul {
width: 400px;
column-count: 2;
column-gap: 10px;
}
You can use a fixed width for the li:
https://jsfiddle.net/w25m1a81/1/
You could set the float property of each selected <li> as left or right.
Also remember to specify nth-child(2n-1) property to give a table like alignment.
.li{
float: left;
}
li:nth-child(2n){
float right
}
also display: inline-block can be use:
.li{
display: inline-block;
width: 150px;
height: 75px;
margin: 10px;
}
Also providing fixed width, height and margin for the container will improve the alignment
There are some adjustments we should go through so you understand how to achieve the solution you want:
Avoid inline css styles like on the <ul>
Mixing list markup with a lot of other elements can create chaotic code very fast, lets rename all <li> to <div>.
Next we want to wrap all the numeric vales in a container too, because then we have more control over the layout: e.g. <span class="numerics">51 - 60</span>
Now we build up our flexible grid system. We can use the .customLegend containers for this but I would recommend to add a new class for columns so you can separate your individual styles from those for the layout. https://www.sitepoint.com/understanding-css-grid-systems/
I added some styles for free, you can go though the css. I created a jsfiddle too if you want to try further adjustments on your own: https://jsfiddle.net/coeh77Lp/2/
.customLegend {
float: left;
width: 50%;
max-width: 170px; /* stack up cols if there is enough space */
margin-bottom: 5px;
}
.customLegend .symbol {
background: #333333;
display: inline-block;
width: 10px;
height: 10px;
border-radius: 5px;
margin-right: 3px;
}
.numerics {
text-align: right; /* better readability */
min-width: 50px;
display: inline-block;
padding: 2px 4px 2px 2px;
margin-right: 2px;
border-bottom: 2px solid #ddd;
}
.customLegend .label {
display: inline-block;
margin-right: 20px;
}
<div class="customLegend">
<span class="symbol" style="background: rgb(51, 51, 51);"></span>
<div class="label"><span class="numerics">0 - 10</span> Years old</div>
</div>
<div class="customLegend">
<span class="symbol" style="background: rgb(102, 102, 102);"></span>
<div class="label"><span class="numerics">11 - 20</span> Years old</div>
</div>
<div class="customLegend">
<span class="symbol" style="background: rgb(153, 153, 153);"></span>
<div class="label"><span class="numerics">21 - 30</span> Years old</div>
</div>
<div class="customLegend">
<span class="symbol" style="background: rgb(221, 221, 221);"></span>
<div class="label"><span class="numerics">31 - 40</span> Years old</div>
</div>
<div class="customLegend">
<span class="symbol" style="background: rgb(238, 238, 238);"></span>
<div class="label"><span class="numerics">41 - 50</span> Years old</div>
</div>
<div class="customLegend">
<span class="symbol" style="background: red;"></span>
<div class="label"><span class="numerics">51 - 60</span> Years old</div>
</div>
<div class="customLegend">
<span class="symbol" style="background: blue;"></span>
<div class="label"><span class="numerics">61 - 70</span> Years old</div>
</div>
<div class="customLegend">
<span class="symbol" style="background: pink;"></span>
<div class="label"><span class="numerics">71 - 80</span> Years old</div>
</div>
<div class="customLegend">
<span class="symbol" style="background: green;"></span>
<div class="label"><span class="numerics">81 - 0</span> Years old</div>
</div>

css first child not wokring

I have the following html
.resources .resource:first-child {
border-top: 1px solid #D9B310;
}
.resources .resource {
border-bottom: 1px solid #D9B310;
padding: 10px 0;
}
<div class="resources">
<h2 class="text-center">Ресурси</h1>
<div class="resource">
<h4>Минерали <span class="resource-quantity">3700</span></h3>
</div>
<div class="resource">
<h4>Газ <span class="resource-quantity">500</span></h3>
</div>
</div>
the selector without the first-child works fine. But the first-child selector it's not wrking in my case. Can someone tell me why ?
Because that the .resource element that you want to stylize is the second child of it's parent, the first child is the h1 tag (careful, your heading tags were not properly closed).
You may be mistaken :first-child with :first-of-type.
:first-child only selects the .resource elements that are the first child within their parent container.
:first-of-type selects the .resource elements that are the first element matching the given selector (.resource) within their parent container, but not necessarily on the first-child position.
You can either use :nth-child(2) or :first-of-type, as in your case the element is the second child.
.resources .resource:nth-child(2){
border-top: 1px solid #D9B310;
}
.resources .resource {
border-bottom: 1px solid #D9B310;
padding: 10px 0;
}
<div class="resources">
<h1 class="text-center">Ресурси</h1>
<div class="resource">
<h3>Минерали <span class="resource-quantity">3700</span></h3>
</div>
<div class="resource">
<h3>Газ <span class="resource-quantity">500</span></h3>
</div>
</div>
Use :first-of-type because it's second child of it's parent.
.resources .resource:first-of-type {
border-top: 1px solid #D9B310;
}
.resources .resource {
border-bottom: 1px solid #D9B310;
padding: 10px 0;
}
<div class="resources">
<h2 class="text-center">Ресурси</h2>
<div class="resource">
<h4>Минерали <span class="resource-quantity">3700</span></h4>
</div>
<div class="resource">
<h4>Газ <span class="resource-quantity">500</span></h4>
</div>
</div>
.resources .resource:first-of-type {
border-top: 1px solid #D9B310;
}
.resources .resource {
border-bottom: 1px solid #D9B310;
padding: 10px 0;
}
<div class="resources">
<h2 class="text-center">Ресурси</h2>
<div class="resource">
<h4>Минерали <span class="resource-quantity">3700</span></h4>
</div>
<div class="resource">
<h4>Газ <span class="resource-quantity">500</span></h4>
</div>
</div>

Link causes double border

Hez guys, I have some elements on page, which I've wrapped with border. The elements are close to each other so of course, on the place, where they touch each other, I've got double border.
Think is, I've made links from these elements, so now, some additional border is applyed and I don't know how to remove it.
Code below usefully removed double border when element's we're not links.
Can you please help me to remove this border which comes from link ?
Thank you}
HTML
<section class="popular-items clearfix">
<a href="detail.html">
<div class="item">
<img src="img/pink.png" alt="pink"><br>
<span>2015</span>
<h4 class="pink">pink</h4>
<p class="out-of-stock">Abc</p>
</div>
</a>
<a href="detail.html">
<div class="item">
<img src="img/white.png" alt="white"><br>
<h4 class="green">white</h4>
<p>5 €</p>
</div>
</a>
<a href="detail.html">
<div class="item">
<img src="img/pink.png" alt="pink"><br>
<h4 class="pink">Pink</h4>
<p class="coming-soon">Pink</p>
</div>
</a>
<a href="detail.html">
<div class="item">
<img src="img/red.png" alt="red"><br>
<h4 class="red">Red</h4>
<p>5 €</p>
</div>
</a>
</section>
SCSS
.popular-items {
width: 100%;
.item {
float: left;
width: 25%;
border: 1px solid #dddddd;
padding: 1%;
border-right-width: 0;
height: 420px;
&:last-child {
border-right-width: 1px;
}
&:hover + & {
border-left-width: 0;
}
&:hover {
border: 1px $dark solid;
}
}
}
Well i think you can fix that by setting the border of the links to 0px.
border : none;

How to center div with two inline buttons in it (css)? [duplicate]

This question already has answers here:
How can I horizontally center an element?
(133 answers)
Closed 6 years ago.
I'm currently designing a webpage using Bootstrap and I have two <a> buttons laid inline with each other inside of the <div class="jumbotron"></div>
Here's the HTML code:
<div class="jumbotron hub-jumbo">
<div class="hub-header">
<div class="container">
<h2 class="txt-white">Jason's Summer Vacation</h2>
<p class="txt-white">There doesn't seem to be a description here!</p>
</div>
</div>
<div class="hub-btns">
<a class="copy-link btn btn-lg btn-wide bg-white" href="" role="button">Copy Link</a>
<a class="create-drop btn btn-primary btn-lg btn-wide txt-white bg-blue border-blue ripple" href="" role="button">Create Drop</a>
</div>
</div>
And here's the CSS of the div wrapping the buttons and the buttons themselves:
.hub-btns {
display: inline-block;
margin: auto;
}
.hub-btns .copy-link {
border-bottom-color: #EBEBEB;
}
.hub-btns .create-drop:hover {
background-color: #03a9f4;
}
.hub-btns .create-drop:focus {
background-color: #03a9f4;
border-bottom-color: #0398db;
}
.hub-btns a {
margin: 0 10px;
}
I'm still not great with CSS so I'm probably missing something obvious here. But, I simply want to take the div that is wrapping the two buttons and center that div within the jumbotron div.
Any help is greatly appreciated!
I made some changes in a class name (removed spaces) and put some borders for your better understanding of the div placing. Here is the code:
<html>
<head>
<style>
.jumbotron-hub-jumbo {
margin: 0 auto;
border:2px solid black;
width:300px;
}
.hub-btns {
margin: 0 auto;
width:200px;
border:2px solid green;
}
.hub-btns .copy-link {
border-bottom-color: #EBEBEB;
}
.hub-btns .create-drop:hover {
background-color: #03a9f4;
}
.hub-btns .create-drop:focus {
background-color: #03a9f4;
border-bottom-color: #0398db;
}
.hub-btns a {
width:200px;
border: 2px solid red;
margin: 0 10px;
}
</style>
</head>
<body>
<div class="jumbotron-hub-jumbo">
<div class="hub-header">
<div class="container">
<h2 class="txt-white">Jason's Summer Vacation</h2>
<p class="txt-white">There doesn't seem to be a description here!</p>
</div>
</div>
<div class="hub-btns">
<a class="copy-link btn btn-lg btn-wide bg-white" href="" role="button">Copy Link</a>
<a class="create-drop btn btn-primary btn-lg btn-wide txt-white bg-blue border-blue ripple" href="" role="button">Create Drop</a>
</div>
</div>
</body>
</html>
In the HTML I just changed class jumbotron hub-jumbo
I saw you said you are new to CSS, don't be afraid seeing HTML and CSS merged together. Just cut style part and paste it into your style.css.
It's a bit tricky to help without seeing what you're trying to do.
Adding text-align: center; inside the .hub-btns should be enough to center align those buttons.
Also, I would probably move the margin: 0 10px; to .hub-btnsinstead of applying it the anchors/buttons directly.
Lastly, if you don't need to support old browsers, try to invest some time learning the basics of flexbox. It makes our life a lot easier when it comes to 'aligning things'.

Adding focus state to container with embedded link

I have an Angular ng-repeat that creates boxes and returns a container with a little info. I have the hover effect working, but am struggling getting the focus to work. Basically, these containers are allowed to be clicked, then show a larger box with additional info. Showing my code below. Ideas please?
html:
<h1>Pending Swipes</h1>
<div class="swipeBoxes" ng-repeat="swipe in swipes" ng-required="true">
<a href="" ng-click="editSwipe(swipe)" >
<div class="swipeBox col-md-12">
<div class="col-md-12 claimObject">
<span class="claimLine" style="width: 3px; position: absolute; top: 5px; left: 5px;">{{ $index + 1 }}.</span>
<span class="claimedLeft">swipe date:</span>
<span class="claimedRight">{{ swipe.date | date: 'MM/dd/yyyy'}}</span>
</div>
<div class="col-md-12 claimObject">
<span class="claimedLeft">provider:</span
><span class="claimedRight">{{ swipe.merchant }}</span>
</div>
<div class="col-md-12 claimObject">
<span class="claimedLeft">amount:</span>
<span class="claimedRight">{{ swipe.amount | currency }}</span>
</div>
</div>
</a>
</div>
And the css is to make this work kinda:
.swipeBoxes a {
text-decoration: none;
}
.swipeBoxes a:hover {
text-decoration: none;
}
.swipeBox, .recurBox {
background-color: #ffffff;
margin-top: 10px;
min-height: 85px;
box-shadow: 0 0 8px rgba(0,0,0,.5);
border-radius: 2px;
color: #000000;
}
.swipeBox:hover, .swipeBox:active, .swipeBox:focus {
background-color: #efefef;
}
.swipeBox >a:active, .swipeBox >a:focus {
background-color: #efefef;
}
The :focus selector is only allowed on elements that accept keyboard events or other user inputs.
So it will work on a elements but not on div.
Your css rule:
.swipeBox >a:active, .swipeBox >a:focus {
background-color: #efefef;
}
is not working because you have no a element as first child of .swipeBox