nth-child alternating layout not working (not skipping cards) [duplicate] - html

This question already has answers here:
Can I combine :nth-child() or :nth-of-type() with an arbitrary selector?
(8 answers)
Closed 4 months ago.
I have a section where every even card has margin, to give it that masonry type effect.
I have two types of cards, they vary only based on the widths they occupy.
In my demo, I have each sm card wrapped in the .customCard__column--sm class. This class is what differentiates it from the wider card.
In my CSS, I've defined my even .customCard's that have the class .customCard__column--sm to have margin.
However, in my demo, card 4 is using the nth-child(odd) css.
Cards 4 and 5 should follow the same pattern as cards 1 and 2.
Why is this happening?
main {
background: #000000;
font-family: "Poppins", sans-serif;
color: #ffffff;
}
/* listing */
.listing {
padding: 100px 0;
}
.customCard {
border: 1px solid #ffffff;
padding: 30px;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
margin-bottom: 80px;
height: 300px;
}
#media (min-width: 768px) {
.customCard__column--sm:nth-child(odd) .customCard {
margin-right: 18px;
}
}
#media (min-width: 768px) {
.customCard__column--sm:nth-child(even) .customCard {
margin-top: 120px;
margin-left: 18px;
margin-right: 0;
}
}
#media (min-width: 1200px) {
.customCard__column--sm:nth-child(even) .customCard {
margin-top: 178px;
}
}
.customCard__column--sm .customCard--large {
margin-right: 0;
}
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght#700&display=swap" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<main class="listing">
<div class="container">
<div class="row">
<div class="col-12 col-md-6 d-flex customCard__column customCard__column--sm">
<article class="customCard">Card 1 (SM)</article>
</div>
<div class="col-12 col-md-6 d-flex customCard__column customCard__column--sm">
<article class="customCard">Card 2 (SM)</article>
</div>
<div class="col-12 d-flex customCard__column">
<article class="customCard customCard--large">Card 3 (LG)</article>
</div>
<div class="col-12 col-md-6 d-flex customCard__column customCard__column--sm">
<article class="customCard">Card 4 (SM)</article>
</div>
<div class="col-12 col-md-6 d-flex customCard__column customCard__column--sm">
<article class="customCard">Card 5 (SM)</article>
</div>
</div>
</div>
</main>

:nth-child means "nth child". It doesn't mean "nth child only counting the other features of this selector".
Card 4 is the 5th child (of <div class="row">), which is an odd numbered one.
You'll need to use markup (e.g. another class name) to handle distinguishing when your sequence resets.

Related

Can't figure out where the left and right margin come from

There's a left and right margin for the columned-footer which I did not define or at least I can't find the definition now. So, to get rid of it, I did define margin-left and right to be 0 but that had no effect on it.
I don't get why the two columns are so next to each other and far from the screen borders. Also when the address is a longer text, the margin disappears and it sticks to the screen border! How to remove that annoying margin, and how to make it responsive?
.columned-footer {
display: flex;
align-items: center;
justify-content: center;
background-color: rgb(70, 66, 66);
height: 8rem;
color: rgb(243, 240, 235);
width: 100%;
}
.footer-container {
display: grid;
gap: 1rem 3rem;
grid-template-columns: 1fr 1fr;
}
.address {
float: right;
display:inline;
}
.tel {
float: left;
display:inline;
}
<footer>
<div class="columned-footer">
<div class="footer-container">
<div class="address">address
<div>
Right around the corner
</div>
</div>
<div class="tel">tel
<div> 8877887788
</div>
</div>
</div>
</div>
</footer>
The side spaces are due to the application of the justify-content style. The spaces at the top are due to the align-items style being applied.
You can use the following footer that I edited using Bootstrap 5. By editing the column widths, you adjust both the left and right spacing and the design is responsive.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Footer</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css">
</head>
<body>
<footer class="bg-dark text-center text-white">
<div class="container p-4 pb-0">
<section class="">
<form action="">
<div class="row d-flex justify-content-center">
<div class="col-md-3">
<div class="form-outline form-white mb-4">
<div><strong>Address</strong></div>London
</div>
</div>
<div class="col-md-5">
<!-- Something -->
</div>
<div class="col-md-3 col-12">
<div class="form-outline form-white mb-4">
<div><strong>Phone</strong></div>+12 345 678 90 12
</div>
</div>
</div>
</form>
</section>
</div>
<div class="text-center p-3" style="background-color: rgba(0, 0, 0, 0.2);">
#2021
<a class="text-white" href="https://mdbootstrap.com/">example.com</a>
</div>
</footer>
</body>
</html>
Do, body {margin: 0} (as this value come from browser-default values for various html-elements.
Good Habit
Whenever, you try to make your own website, or project, you must always make default reset in your main css file. This means:
* {margin:0; padding:0; box-sizing:border-box}
When you don't do this, browser's default CSS applies to your various HTML-Elements that you are using, untill you are overriding them specifically, while writing your CSS.
.columned-footer {
display: flex;
align-items: center;
justify-content: center;
background-color: rgb(70, 66, 66);
height: 8rem;
color: rgb(243, 240, 235);
width: 100%;
}
.footer-container {
display: grid;
gap: 1rem 3rem;
grid-template-columns: 1fr 1fr;
}
.address {
float: right;
display:inline;
}
body {background: red; margin: 0;}
.tel {
float: left;
display:inline;
}
<footer>
<div class="columned-footer">
<div class="footer-container">
<div class="address">
<div>address</div>
<div>Right around the corner</div>
</div>
<div class="tel">
<div>tel</div>
<div>8877887788</div>
</div>
</div>
</div>
</footer>

Bootstrap : Is it possible to have rows in columns like this?

with Bootstrap and its layout system is it possible to have rows in columns to produce this type of result :
I tried this but without success :
<div className="col-lg-3 col-12">
<div className="row">
<displaySquare />
</div>
<div className="row">
<displaySquare />
</div>
<div className="row">
<displaySquare />
</div>
</div>
<div className="col-lg-9 col-12">
<div className="row">
<displayBigSquare />
</div>
<div className="row">
<displaySquare />
<displaySquare />
</div>
</div>
ty
UPDATE (23/08/2021):
it's actually better with grid. I dropped bootstrap and I use GRID instead which allows to define all the properties related to CSS grids (GRID explanations on https://developer.mozilla.org/fr/docs/Web/CSS/grid).
HTML code:
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Griiiiid!</title>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<h1 class="centre">Welcome</h1>
<div class="container">
<div class="items item1">1</div>
<div class="items item2">2</div>
<div class="items item3">3</div>
<div class="items item4">4</div>
<div class="items item5">5</div>
<div class="items item6">6</div>
</div>
</body>
</html>
CSS code:
*,
::before,
::after {
box-sizing: border-box;
}
body {
background-color: #333;
margin: 0;
padding: 0;
font-family: Georgia, "Times New Roman", Times, serif;
}
h1 {
color: salmon;
}
.centre {
text-align: center;
}
.container {
padding: 30px ;
width: 100%;
background-color: #eee;
margin: 30px auto;
display: grid;
grid-template-rows: 150px;
grid-template-columns: repeat (3, 150px);
grid-auto-rows: 150px;
gap: 30px;
}
.items {
padding: 20px;
font-size: 30px;
font-family: sans-serif;
}
.item1 {
background-color: lightcoral;
}
.item2 {
grid-row: 1/3;
grid-column: 2/4;
background-color: lightgreen;
}
.item3 {
background-color: lime;
}
.item4 {
background-color: chocolate;
}
.item5 {
background-color: darkgoldenrod;
}
.item6 {
background-color: darkseagreen;
}
Result:
As of Bootstrap v5.0.2, the answer is no. The issue is that in order to have the orange rectangle in your example span multiple "rows", you need to use CSS Grid styling, and that is not yet part of Bootstrap.
However, very soon in v5.1.0, they are including an opt-in option to replace the flexbox-based grid system they currently use with a CSS Grid-based one instead. It will probably have some growing pains, and I'm not sure that they have support for doing what you're trying to do here just yet, but at least CSS Grid will be there to try out. There will be documentation added for how to enable the option and how to use it once that version goes live, but you can read through the feature's PR in the meantime.
If you are wanting that exact layout, then you would have to do it in two rows:
The margin bottom of the square need to equal double your gutter
.square {
width: 100%;
padding-top: 100%;
background: orange;
margin-bottom: 30px;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<div class="container">
<div class="row">
<div class="col-4">
<div class="square"></div>
<div class="square"></div>
</div>
<div class="col-8">
<div class="square"></div>
</div>
</div>
<div class="row">
<div class="col-4">
<div class="square"></div>
</div>
<div class="col-4">
<div class="square"></div>
</div>
<div class="col-4">
<div class="square"></div>
</div>
</div>
</div>

Issue with vertical centering of text [duplicate]

This question already has answers here:
How to remove line spacing between HTML heading elements?
(4 answers)
Space before heading larger than space after heading with HTML and CSS
(3 answers)
Closed 1 year ago.
I am having an issue getting text to be centered vertically within a div. I've tried both CSS and bootstrap options, but not having luck. The closest that I get is with using align-items: center, however, the test is still offset from what should be middle.
Current code:
.code-quote {
max-width: 70%;
height: 40px;
background-color: #707793;
display: flex;
align-items: center;
justify-content: center;
}
.code-text {
font-size: 1.5rem;
color: #3BBA9C;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
<div class="row">
<div class="col">
<div class="code-quote mx-auto">
<h3 class="code-text">This is my text</h3>
</div>
</div>
</div>
This is what I get:
If I remove the align-items: center, then it's much closer to the top of the element.
The h3 element has a bottom margin by default. Adding margin: 0 might do what you're after:
.code-quote {
max-width: 70%;
height: 40px;
background-color: #707793;
display: flex;
align-items: center;
justify-content: center;
}
.code-text {
font-size: 1.5rem;
color: #3BBA9C;
margin: 0;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
<div class="row">
<div class="col">
<div class="code-quote mx-auto">
<h3 class="code-text">This is my text</h3>
</div>
</div>
</div>
Bootstrap provides all the alignment and flex stuff you could ask for. Have a look at the docs.
Also note my adjustment to margin on the heading element.
.code-quote {
max-width: 70%;
height: 40px;
background-color: #707793;
}
.code-text {
font-size: 1.5rem;
margin-bottom: 0;
color: #3BBA9C;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
<div class="container-fluid">
<div class="row">
<div class="col">
<div class="code-quote mx-auto d-flex flex-column
align-items-center justify-content-center">
<h3 class="code-text">This is my text</h3>
</div>
</div>
</div>
</div>

Re-order items when I hit a custom breakpoint [duplicate]

This question already has answers here:
Order columns through Bootstrap4
(6 answers)
Closed 3 years ago.
I have four items with different sizes in a row.
On extra-large screens everything looks fine. But when I hit large screens (1199.98px / bootstrap 'lg'), I need to re-order my div items.
The third div need to be the last.
I also made a code pen for this
<!-- https://codepen.io/ibrahim-kunushefci/pen/OKJWzq -->
.hotelPr,
.hotelPr2,
.hotelPr3 {
height: 100px;
display: flex;
justify-content: center;
align-items: center;
color: white;
background: #3161a3;
border: 2px solid black;
}
.custom {
height: 100px;
display: flex;
justify-content: center;
align-items: center;
color: white;
background: #bbbbbb;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<div class="row">
<div class="col-xl-2 col-lg-4">
<div class="hotelPr">
<p>Hotel</p>
</div>
</div>
<div class="col-xl-2 col-lg-4">
<div class="hotelPr2">
<p>Hotel</p>
</div>
</div>
<div class="col-xl-5 col-lg-10">
<div class="custom">
<p>This needs to be the last on small, medium and large screens. But not in extra large</p>
</div>
</div>
<div class="col-xl-3 col-lg-4">
<div class="hotelPr3">
<p>Hotel</p>
</div>
</div>
</div>
Add d-flex to your row container:
Add order-1 order-xl-0 to your the col you need to re-order
You can specify the order of the displayed siblings on a parent flexbox element.
I changed the display of your .row div in a flexbox, and I added an order property to your col-xl-5 div.
.row{
display: flex;
}
.hotelPr,
.hotelPr2,
.hotelPr3 {
height: 100px;
display: flex;
justify-content: center;
align-items: center;
color: white;
background: #3161a3;
border: 2px solid black;
}
.custom {
height: 100px;
display: flex;
justify-content: center;
align-items: center;
color: white;
background: #bbbbbb;
}
.col-xl-5{
order: 1; /* here */
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<div class="row">
<div class="col-xl-2 col-lg-4">
<div class="hotelPr">
<p>Hotel</p>
</div>
</div>
<div class="col-xl-2 col-lg-4">
<div class="hotelPr2">
<p>Hotel</p>
</div>
</div>
<div class="col-xl-5 col-lg-10">
<div class="custom">
<p>This needs to be the last on small, medium and large screens. But not in extra large</p>
</div>
</div>
<div class="col-xl-3 col-lg-4">
<div class="hotelPr3">
<p>Hotel</p>
</div>
</div>
</div>

Bootstrap 5 column layout with border

I was wondering what the best approach would be to create a 5 column layout in Bootstrap and give those divs a border and spacing.
I've created a new class to make the grid suitable for 5 columns, like so:
.col-xs-15,
.col-sm-15,
.col-md-15,
.col-lg-15 {
position: relative;
min-height: 1px;
}
.col-xs-15 {
width: 20%;
float: left;
}
#media (min-width: 768px) {
.col-sm-15 {
width: 20%;
float: left;
}
}
#media (min-width: 992px) {
.col-md-15 {
width: 20%;
float: left;
}
}
#media (min-width: 1200px) {
.col-lg-15 {
width: 20%;
float: left;
}
}
<div class="item col-md-15">
<div class="item-wrap">
.....
</div>
</div>
What I try to do is to give each column 10px margin on the right (except for last column offcourse). Further I want to give each column or item-wrap a 1px border.
Whatever I try I always end up with no margin.
.item {
border: 1px solid #efefef;
padding:10px;
}
.item.last {
margin-right: 0;
}
See my fiddle
Currently, 5, 7 and 9 column layouts are not supported in native Bootstrap, as the default 12 column structure isn't evenly divisible by those numbers. In order to get a 5 column layout, you would need to visit http://getbootstrap.com/customize/#grid-system and modify the #grid-columns value to 15 (or really, anything that is evenly divisible by 5).
After customizing and downloading your personal version of Bootstrap, you could then implement a 5 column layout using:
<div class="col-xs-3">Column 1</div>
<div class="col-xs-3">Column 2</div>
<div class="col-xs-3">Column 3</div>
<div class="col-xs-3">Column 4</div>
<div class="col-xs-3">Column 5</div>
And you wouldn't have to mess with CSS to try and mimic the existing Bootstrap classes and styles. Just be cautious using this approach, as any existing columnar layouts may be affected by this change.
If you want to give all columns a margin for spacing, you can use pseudo classes to achieve this:
.item:not(:last-child) {
margin-right:10px;
}
Basically this will give all elements with the .item class a right margin, except for the last element. Here is your updated fiddle.
With the release of bootstrap-4, we saw lots of new features and changes it came up with. One such change was dropping floats and percentages, and using the flex-box for layouts in bootstrap-4.
Bootstrap-4 is built on top of the flex-layout.
So whenever we have such a requirement where our layout is not fitting to the usual 12-grid layout of our bootstrap-4 ,we can use our customized CSS (using flexbox properties) to create any number column responsive layout(in your case which is a 5 column layout), since flexbox gives us so much flexibility in doing so. Use the below code that I've wrote to achieve this.
HTML5:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link rel="stylesheet" href="./style.css" />
<title>5 column layout in flexbox</title>
</head>
<body>
<main class="flex-container">
<div class="flex-item col1">col1</div>
<div class="flex-item col2">col1</div>
<div class="flex-item col3">col3</div>
<div class="flex-item col4">col4</div>
<div class="flex-item col5">col5</div>
</main>
</body>
</html>
CSS:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
padding-top: 1rem;
}
/* --- This much code will do it for you --- */
.flex-container {
display: flex;
}
.flex-item {
min-height: 400px;
border: 4px solid #03a503;
margin: 0 0.5rem;
display: flex;
align-items: center;
justify-content: center;
flex: 1;
}
.flex-item:first-of-type,
.flex-item:last-of-type {
margin-right: 1rem;
}
/* ----- Basic break-point ----- */
#media screen and (max-width: 768px) {
.flex-container {
flex-direction: column;
}
.flex-item {
min-height: 100px;
margin: 0.5rem;
}
.flex-item:first-of-type,
.flex-item:last-of-type {
margin: 0.5rem;
}
}
Here is the full code https://jsfiddle.net/cLmq0otv/3/
Hope this code solves the problem.
You have to use Auto-layout columns, column classes without an explicit numbered. You may use predefined grid classes, grid mixins, or inline widths to set the width of one column and have the sibling columns automatically resize.
.col{
padding:1rem;
}
.col div{
border:1px solid red;
min-height:5rem;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col">
<div>content column 1</div>
</div>
<div class="col">
<div>content column 2</div>
</div>
<div class="col">
<div>content column 3</div>
</div>
<div class="col">
<div>content column 4</div>
</div>
<div class="col">
<div>content column 5</div>
</div>
</div>
</div>