Issue with vertical centering of text [duplicate] - html

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>

Related

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

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.

Locating picture to the left, but the text centered

I want to have my the picture.png to be exactly on the very left of the title "ribbon", and the text to be centred about the ribbon (regardless of the presence of the picture). I've been struggling with CSS and flex boxes for a lot of time trying to achieve just this effect. Here I'm using some bootstrap classes, but .heading is my class.
That being said, is it actually possible to do all this alignment without using flexbox, but something more natural and simple? Such as maybe something from CSS1, without all those flex boxes?
.heading {
background-color: rgba(15, 156, 199, 0.829);
height: 5%;
display: flex;
text-align: center;
justify-content: space-between;
/* not helpful of course */
}
/* Here's what I'm trying to achieve. I made this by artificially adding the following CSS code: */
.name {
margin-right: 31%;
}
<div class="container-sm">
<div class="card-body">
<div class="heading">
<img class="rounded img-fluid" src="picture.png" alt="...">
<div class="name">
<h4>Web server monitoring and maintenance.</h4>
<div>Monitoring and upgrades.</div>
</div>
</div>
</div>
</div>
Of course I want this to be naturally centred, not artificially.
TL;DR. Make your .name also a flex container.
Please see the code snippet below. I didn't edit the HTML except the placeholder image. The main CSS change is in .name class to make it flex container and have its content vertically centered. Also, its parent .heading styles are a little adjusted to make it work properly.
.heading {
background-color: rgba(15, 156, 199, 0.829);
height: 5%;
display: flex;
text-align: center;
}
img {
width: 50px;
height: 50px;
flex: 0;
}
.name {
width: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
h4, div {
margin: 0;
padding: 0;
line-height: 1;
}
<div class="container-sm">
<div class="card-body">
<div class="heading">
<img class="rounded img-fluid" src="https://picsum.photos/50" alt="...">
<div class="name">
<h4>Web server monitoring and maintenance.</h4>
<div>Monitoring and upgrades.</div>
</div>
</div>
</div>
</div>
Bootstrap relies on Flexbox for the alignment and layouts. So it will be odd to avoid using it.
Here is a code snippet that will help you achieve what you want to do.
Helpful links:
Bootstrap Flexbox: Flexbox utility classes
CSS Flexbox: MDN flexbox docs
.heading {
background-color: rgba(15, 156, 199, 0.829);
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<div class="container-sm">
<div class="card-body">
<div class="heading d-flex text-center">
<img class="rounded img-fluid" src="picture.png" alt="...">
<div class="name flex-grow-1">
<h4>Web server monitoring and maintenance.</h4>
<p>Monitoring and upgrades.</p>
</div>
</div>
</div>
</div>
Note: Bootstrap is all about pre-made components and utility classes so try to avoid writing custom CSS as much as possible. Another downside of this is that you're repeating yourself and making Bootstrap useless in the manner you're using it.
using flexbox
img,.image {
height:50px;
}
.box {
display: flex;
align-items: center;
width: 100%;
text-align: center;
height:50px;
}
.text-wrapper{
background-color: rgba(15, 156, 199, 0.829);
width:100%;
height:50px;
display: flex;
flex-direction: column;
justify-content: center;
}
.container{
padding:5px;
}
*{
margin:0px;
padding:0px;
}
<div class="container">
<div class="box">
<div class="image">
<img src="https://placekitten.com/640/360" alt="...">
</div>
<div class="text-wrapper">
<div class="text">
<h4>Web server monitoring and maintenance.</h4>
<p>Monitoring and upgrades.</p>
</div>
</div>
</div>
</div>

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>

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>

Text not aligning in center

I have created 4 columns. The first 3 columns are supposed to contain text vertically oriented. The problem is I am unable to align it. justify-content and align-items are not positioning the text in the center of the column. If you run the code you'll see it's positioned to the right. I want it to be exactly in center.
body{
margin:0;
padding: 0;
height: 100vh;
width: 100vw;
}
.forAll{
height: 100vh;
display: flex;
justfiy-content: center;
align-items: center;
text-align: center;
padding: 0;
}
.verticalOption{
transform: rotate(270deg);
line-height: 10px;
}
.verticalOption a{
text-decoration: none;
color: white;
font-size: 10px;
line-height: 5px;
white-space: nowrap;
}
<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">
<body>
<div class="no-gutters">
<div class="fluid-container d-flex flex-row-reverse">
<div class="container col-xl-9 col-9 col1 forAll bg-primary"></div>
<div class="container col-xl-1 col-1 col2 forAll bg-warning">
<h1 class="verticalOption">Sample Text</h1>
</div>
<div class="container col-xl-1 col-1 col3 forAll bg-danger">
<h1 class="verticalOption">Sample Text</h1>
</div>
<div class="container col-xl-1 col-1 col4 forAll bg-success">
<h1 class="verticalOption">Sample Text</h1>
</div>
</div>
</div>
</body>
I added another flex container to the column, so alignment can be controlled further into the HTML nesting. I also justified the content of the column to center. I also made the h1 display inline-flex to remove all extra white-space.
Here is where all the classes have been added:
<div class="container col-xl-1 col-1 col2 forAll bg-warning d-flex justify-content-center">
<h1 class="verticalOption d-inline-flex m-0">
Sample Text
</h1>
</div>
Demo
body{
margin:0;
padding: 0;
height: 100vh;
width: 100vw;
}
.forAll{
height: 100vh;
display: flex;
justfiy-content: center;
align-items: center;
text-align: center;
padding: 0;
}
.verticalOption{
transform: rotate(270deg);
line-height: 10px;
}
.verticalOption a{
text-decoration: none;
color: white;
font-size: 10px;
line-height: 5px;
width: 200px;
white-space: nowrap;
}
<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">
<body>
<div class="no-gutters">
<div class="fluid-container d-flex flex-row-reverse">
<div class="container col-xl-9 col-9 col1 forAll bg-primary"></div>
<div class="container col-xl-1 col-1 col2 forAll bg-warning d-flex justify-content-center">
<h1 class="verticalOption d-inline-flex m-0">Sample Text</h1>
</div>
<div class="container col-xl-1 col-1 col3 forAll bg-danger d-flex justify-content-center">
<h1 class="verticalOption d-inline-flex m-0">Sample Text</h1>
</div>
<div class="container col-xl-1 col-1 col4 forAll bg-success d-flex justify-content-center">
<h1 class="verticalOption d-inline-flex m-0">Sample Text</h1>
</div>
</div>
</div>
</body>
Just do text-align:center on your h1.
If that doesn't work, its most likely cause you're hardcoding your width to your a tag, and that 200px is probably larger than the container itself.
You just have to add two lines in .verticalOption:
vertical-align:center;
display:table-cell;
and delete justfiy-content: center;
Sorry, I had a typo in justify-content: center which was causing this problem.