centering text positioning using flexbox - html

Trying to do a simple website recreation using flexbox. Here is the final goal (for this step):
Pretty simple. But I am struggling with aligning the text. I am able to get the header to center just fine. It's mainly the paragraph underneath that I am having trouble centering. Is there a way to center that paragraph vertically and horizontally to its outer container? New to flexbox and would like any tips! Here is a link to my codepen and relevant code: https://codepen.io/gkunthara/pen/qjympg
HTML
<div class = "content-container">
<h1 class = "main-header"> Connect with Subscribers Effortlessly </h1>
<p class = "sub-header"> Email Marketing Platform for Bloggers &
Authors. </p>
</div>
CSS
.content-container .main-header{
box-sizing: border-box;
font-weight: 600;
font-size: 45px;
height:75px;
padding-left: 5px;
padding-right: 5px;
color: rgb(51, 51, 51);
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
border-style: solid;
}
.content-container .sub-header {
border-style: solid;
display: flex;
height: 25px;
margin-top: 150px;
}

You just need to do two things.
1. Add
flex-direction: column;
align-items: center;
to your .content-container class.
2. Remove margin-top: 150px from .content-container .sub-header and it should be it.
* {
box-sizing: border-box;
}
.content-container {
width: 100%;
height: 300px;
display: flex;
justify-content: center;
border-style: solid;
margin-top: 15px;
flex-direction: column;
align-items: center;
}
.content-container .main-header {
box-sizing: border-box;
font-weight: 600;
font-size: 30px;
height: 75px;
padding-left: 5px;
padding-right: 5px;
color: rgb(51, 51, 51);
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
border-style: solid;
}
.content-container .sub-header {
border-style: solid;
height: 25px;
}
<div class="content-container">
<h1 class="main-header"> Connect with Subscribers Effortlessly </h1>
<p class="sub-header"> Email Marketing Platform for Bloggers & Authors. </p>
</div>
Here is the fiddle to play with.
Hope this helps.

If you remove the margin-top in your .content-container .sub-header rule and add flex-direction: column; align-items: center; to your .content-container the text will center properly
Updated codepen
Stack snippet
* {
box-sizing: border-box;
}
.header-container {
display: flex;
width: 100%;
height: 100px;
align-items: center;
justify-content: space-between;
padding-left: 50px;
padding-right: 75px;
}
.header-container .logo {
width: 250px;
}
.header-container .logo .sb {
max-width: 100%;
}
.header-container .nav-bar-container {
display: flex;
}
.nav-link {
text-decoration: none;
color: #4B4B4B;
font-weight: bold;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
padding: 15px;
}
.content-container {
width: 100%;
height: 300px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
border-style: solid;
margin-top: 15px;
}
.content-container .main-header {
box-sizing: border-box;
font-weight: 600;
font-size: 45px;
height: 75px;
padding-left: 5px;
padding-right: 5px;
color: rgb(51, 51, 51);
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
border-style: solid;
}
.content-container .sub-header {
border-style: solid;
display: flex;
height: 25px;
}
/* Resize window to see its effect */
#media( max-width: 700px) {
.header-container {
flex-direction: column;
}
}
<div class="header-container">
<a class="logo"> <img class="sb" src="logo.png" alt="logo"> </a>
<nav class="nav-bar-container">
<a class="nav-link" href="#"> Tour </a>
<a class="nav-link" href="#"> Pricing </a>
<a class="nav-link" href="#"> Medium </a>
<a class="nav-link" href="#"> Sign Up </a>
<a class="nav-link" href="#"> Login </a>
</nav>
</div>
<div class="content-container">
<h1 class="main-header"> Connect with Subscribers Effortlessly </h1>
<p class="sub-header"> Email Marketing Platform for Bloggers & Authors. </p>
</div>
If you intended to center the .content-container .sub-header w/o take the .content-container .main-header into account, you need to position the .content-container .main-header absolute
With this, it will work with or w/o the flex-direction: column;, which I added in the first solution.
Note though, that on less wider screen you need to adjust both, or else they will overlap
Updated codepen
Added these properties to your .content-container .main-header
position: absolute;
top: 0;
left: 0;
right: 0;
text-align: center;

Related

Keep the button at bottom no matter the length of text [duplicate]

This question already has answers here:
How can I position my div at the bottom of its container?
(25 answers)
Closed 10 months ago.
I want to keep the button at bottom previously which I was making it possible by writing more text to push it to bottom but it wasn't responsive for every device any way I can keep the button in a div at bottom?
:root {
--clr-primary: #651fff;
--clr-gray: #37474f;
--clr-gray-light: #b0bec5;
}
* {
box-sizing: border-box;
font-family: "Open Sans", sans-serif;
margin: 0;
padding: 0;
}
body {
color: var(--clr-gray);
margin: 2rem;
}
.wrapper-grid {
display: grid;
grid-template-columns: repeat(auto-fit, 20rem);
justify-content: center;
}
.container {
overflow: hidden;
box-shadow: 0px 2px 8px 0px var(--clr-gray-light);
background-color: white;
text-align: center;
border-radius: 1rem;
position: relative;
margin: 2rem 0.5rem;
}
.banner-img {
position: absolute;
background-image: url(https://gaito.000webhostapp.com/im/istockphoto-1307289824-640x640.jpg);
height: 10rem;
width: 100%;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.profile-img {
width: 8rem;
clip-path: circle(60px at center);
margin-top: 4.5rem;
height: 8rem;
}
.name {
font-weight: bold;
font-size: 1.5rem;
}
.description {
margin: 1rem 2rem;
font-size: 0.9rem;
}
.btn {
width: 100%;
border: none;
font-size: 1rem;
font-weight: bold;
color: white;
padding: 1rem;
background-color: var(--clr-primary);
}
<div class="wrapper-grid">
<div class="container">
<div class='banner-img'></div>
<img src='https://i.pinimg.com/originals/49/09/f9/4909f9e82c492b1e4d52c2bcd9daaf97.jpg' class="profile-img">
<h1 class="name">Slime</h1>
<p class="description">Slimes also commonly called ooze are common types of</p>
<button class='btn'>Attack this dungeon </button>
</div>
<div class="container">
<div class='banner-img'></div>
<img src='https://static.wikia.nocookie.net/kumo-desu-ga-nani-ka/images/4/4c/Mother_1.png/' alt='profile image' class="profile-img">
<h1 class="name">Gaint spider</h1>
<p class="description">This creature shoots sticky strands of webbing from its abdomen which are most commonly found underground, making their lairs on ceilings or in dark, web-filled crevices.</p>
<button class='btn'>Attack this dungeon </button>
</div>
</div>
Codepen
My solution would be:
.container {
display: flex;
flex-flow: column nowrap; /* Flex everything inside your cards vertically */
}
.description {
flex-grow: 1;
/*
When a card has more space (because another card is taller
with more info) - grow the description
*/
}
Here is a working snippet, click Full page top right to see it working:
:root {
--clr-primary: #651fff;
--clr-gray: #37474f;
--clr-gray-light: #b0bec5;
}
* {
box-sizing: border-box;
font-family: "Open Sans", sans-serif;
margin: 0;
padding: 0;
}
body {
color: var(--clr-gray);
margin: 2rem;
}
.wrapper-grid {
display: grid;
grid-template-columns: repeat(auto-fit, 20rem);
justify-content: center;
}
.container {
overflow: hidden;
display: flex;
flex-flow: column nowrap;
align-items: center;
box-shadow: 0px 2px 8px 0px var(--clr-gray-light);
background-color: white;
text-align: center;
border-radius: 1rem;
position: relative;
margin: 2rem 0.5rem;
}
.banner-img {
position: absolute;
background-image: url(https://gaito.000webhostapp.com/im/istockphoto-1307289824-640x640.jpg);
height: 10rem;
width: 100%;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.profile-img {
width: 8rem;
clip-path: circle(60px at center);
margin-top: 4.5rem;
height: 8rem;
}
.name {
font-weight: bold;
font-size: 1.5rem;
}
.description {
flex-grow: 1;
margin: 1rem 2rem;
font-size: 0.9rem;
}
.btn {
width: 100%;
border: none;
font-size: 1rem;
font-weight: bold;
color: white;
padding: 1rem;
background-color: var(--clr-primary);
}
<div class="wrapper-grid">
<div class="container">
<div class='banner-img'></div>
<img src='https://i.pinimg.com/originals/49/09/f9/4909f9e82c492b1e4d52c2bcd9daaf97.jpg' class="profile-img">
<h1 class="name">Slime</h1>
<p class="description">Slimes also commonly called ooze are common types of</p>
<button class='btn'>Attack this dungeon </button>
</div>
<div class="container">
<div class='banner-img'></div>
<img src='https://static.wikia.nocookie.net/kumo-desu-ga-nani-ka/images/4/4c/Mother_1.png/' alt='profile image' class="profile-img">
<h1 class="name">Gaint spider</h1>
<p class="description">This creature shoots sticky strands of webbing from its abdomen which are most commonly found underground, making their lairs on ceilings or in dark, web-filled crevices.</p>
<button class='btn'>Attack this dungeon </button>
</div>
</div>

onclick stay focused untill click another component from the area

I try to make a chat-app, now i'm trying to implement the cheats and when user click on one chat, styles must change and remain until the user focus it's gonna to another chat
In my example let's assume someone clicked on User 2. The chat changed style and now have the style that should have when someone open that chat. How can i make the style to stay like that until someone open another chat? I didn't find any css propriety or some react tricks.
.userChatBubbles {
display: flex;
flex-direction: column;
align-items: center;
margin-top: 5%;
width: 100%;
height: 100%;
}
.allOfMessage {
width: 100%;
height: 20%;
display: flex;
flex-direction: row;
align-items: center;
margin-left: 13%;
margin-bottom: 1px;
}
.messageBubleNEWMSG {
display: flex;
flex-direction: row;
align-items: center;
width: 90%;
height: 80%;
font-size: 15px;
background: #1d1335;
opacity: 1;
border-radius: 2%;
padding-left: 4%;
margin-right: 10%;
font-family: 'Roboto', sans-serif;
}
.messageBuble {
display: flex;
flex-direction: row;
align-items: center;
width: 90%;
height: 80%;
font-family: 'Roboto', sans-serif;
font-size: 15px;
padding-left: 4%;
margin-right: 10%;
}
.photo {
background: green;
width: 45px;
height: 45px;
margin-top: 2%;
border-radius: 10%;
margin-right: 6%;
}
.messageBubleNEWMSG.personNameAndMsg.personName {
color: green !important;
margin-top: 200%;
}
.messageBuble .personNameAndMsg h3 {
color: #c2b4da;
font-family: 'Roboto', sans-serif;
font-weight: 100;
font-size: 15px;
}
.messageBuble .personNameAndMsg p {
color: #514b64;
margin-top: -8%;
/* margin-top: 1%; */
font-family: 'Roboto', sans-serif;
font-weight: 200;
}
.messageBubleNEWMSG .personNameAndMsg h3 {
color: #e1dbed;
font-family: 'Roboto', sans-serif;
font-weight: 200;
font-size: 15px;
}
.messageBubleNEWMSG .personNameAndMsg p {
color: #736f83;
margin-top: -8%;
font-family: 'Roboto', sans-serif;
font-weight: 500;
/* margin-top: 1%; */
}
.messageTime {
margin-left: auto;
color: #6e6d73;
}
<div class='userChatBubbles'>
<div class='allOfMessage'>
<div class='messageBuble'>
<div class='photo'></div>
<div class='personNameAndMsg'>
<h3>User</h3>
<p> When can we hangout?</p>
</div>
<div class='messageTime'>6m</div>
</div>
</div>
<div class='allOfMessage'>
<div class='messageBubleNEWMSG'>
<div class='photo'></div>
<div class='personNameAndMsg'>
<h3>User 2</h3>
<p> When can we hang out?</p>
</div>
<div class='messageTime'>6m</div>
</div>
</div>
<div class='allOfMessage'>
<div class='messageBuble'>
<div class='photo'></div>
<div class='personNameAndMsg'>
<h3>User 3</h3>
<p> When can we hangout?</p>
</div>
<div class='messageTime'>6m</div>
</div>
</div>
</div>

How do i wrap the buttons?

Here is how my code currently renders, but I want it to look like this
* {
box-sizing: border-box;
}
body {
background: rgb(75, 109, 221);
color: black;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
div {
display: flex;
justify-content: center;
position: relative;
top: 100px;
}
button {
height: 100px;
width: 150px;
background-color: white;
font-size: large;
}
<div className="Home">
<button class="donate-blood-button">Donate Blood</button>
<button class="request-blood-button">Request Blood</button>
</div>
You can do this using flex-direction: column, which basically flips the uses of justify-content and align-items. Then, set align-items: center and justify-content: space-between and set a height for the container (I used 300px).
* {
box-sizing: border-box;
}
body {
background: rgb(75, 109, 221);
color: black;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
div {
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
position: relative;
top: 100px;
height: 300px;
}
button {
height: 100px;
width: 150px;
background-color: white;
font-size: large;
}
<div className="Home">
<button class="donate-blood-button">Donate Blood</button>
<button class="request-blood-button">Request Blood</button>
</div>
Any time you need help with display: flex;, you should test your ideas on the Yoga Layout Playground. It's an extremely powerful tool for positioning elements.
.Home
{
display: inline;
}
.Home
{
display: inline;
}
<div class="Home">
<button class="donate-blood-button">Donate Blood</button>
<button class="request-blood-button">Request Blood</button>
</div>
* {
box-sizing: border-box;
}
body {
background: rgb(75, 109, 221);
color: black;
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
.Home {
margin:13% 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-around;
}
.Home > button {
height: 100px;
width: 150px;
background-color: white;
font-size: large;
margin:50px 0;
}
<div class="Home">
<button class="donate-blood-button">Donate Blood</button>
<button class="request-blood-button">Request Blood</button>
</div>

Why cant center the divs within a flexbox model

I am not able to align center the following divs on the full page width:
header
content
footer
But it does not work! Whats needs to be changed within my CSS to get it working?
body {
margin:0;
padding:0;
}
#wrapper {
display: flex;
flex-direction: column;
}
#header {
height: 50px;
}
#hero-wrapper {
background-image: url('https://unsplash.it/1500?random');
background-size: cover;
background-position: center;
width: 100vw;
height: calc(100vh - 50px);
display: flex;
flex-direction: column;
color:#ffffff;
}
#hero {
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
#hero-text{
font-family:'Roboto';
font-weight:900;
font-size:2em;
text-align:center;
}
#hero-footnote {
font-family: 'Cabin', sans-serif;
font-size: 12px;
padding: 5px 5px 10px 5px;
}
#content {
width: 1024px;
background-color:green;
}
#footer {
font-family: 'Cabin', sans-serif;
weight: 400;
background-color: #ffffff;
/* position: fixed; */
bottom: 0;
left: 0;
width: 100%;
font-size: 0.685em;
color: #1a1717;
padding-bottom: 5px;
}
#footer-info {
width: 1024px;
margin: auto;
padding-left: 3px;
padding-right: 3px;
text-align: left;
line-height: 42px;
}
span#footer-social-icons {
}
span#copyright-info {
font-family: 'Cabin', sans-serif;
weight: 400;
padding-left:10px;
}
span#contact-link {
font-family: 'Source Sans Pro', sans-serif;
weight: 200;
padding-left:10px;
}
<link href="https://fonts.googleapis.com/css2?family=Cabin&family=Source+Sans+Pro:wght#200&family=Roboto:wght#400;500;900&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght#400;500;900&display=swap" rel="stylesheet">
<div id="wrapper">
<div id="header">header</div>
<div id="hero-wrapper">
<div id="hero">
<div id="hero-text">
<span id="hero-header">sell the product</span><br />
<span id="hero-sub">On this page for less</span>
</div>
</div>
<div id="hero-footnote">© Copyright Text</div>
</div>
<div id="content">
<!-- Content-Loop -->
<div class="article">
Here come the Article
</div>
</div>
<div id="footer">
<div id="footer-info">
<span id="footer-social-icons"></span> <span id="copyright-info">Made with love by Y.</span> <span id="contact-link">Contact</span>
</div>
</div>
</div>
You can add align-items: center; to #wrapper.
Normally if you would have flex-direction: row; centering things would be justify-content: center; but as column change the axis so does the alignment properties. They kinda "go with the axis".
One thing though, I would strongly suggest that you don't style with id, use classes for that.
body {
margin:0;
padding:0;
}
#wrapper {
display: flex;
flex-direction: column;
align-items: center;
}
#header {
height: 50px;
}
#hero-wrapper {
background-image: url('https://unsplash.it/1500?random');
background-size: cover;
background-position: center;
width: 100vw;
height: calc(100vh - 50px);
display: flex;
flex-direction: column;
color:#ffffff;
}
#hero {
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
#hero-text{
font-family:'Roboto';
font-weight:900;
font-size:2em;
text-align:center;
}
#hero-footnote {
font-family: 'Cabin', sans-serif;
font-size: 12px;
padding: 5px 5px 10px 5px;
}
#content {
width: 1024px;
background-color:green;
}
#footer {
font-family: 'Cabin', sans-serif;
weight: 400;
background-color: #ffffff;
/* position: fixed; */
bottom: 0;
left: 0;
width: 100%;
font-size: 0.685em;
color: #1a1717;
padding-bottom: 5px;
}
#footer-info {
width: 1024px;
margin: auto;
padding-left: 3px;
padding-right: 3px;
text-align: left;
line-height: 42px;
}
span#footer-social-icons {
}
span#copyright-info {
font-family: 'Cabin', sans-serif;
weight: 400;
padding-left:10px;
}
span#contact-link {
font-family: 'Source Sans Pro', sans-serif;
weight: 200;
padding-left:10px;
}
<link href="https://fonts.googleapis.com/css2?family=Cabin&family=Source+Sans+Pro:wght#200&family=Roboto:wght#400;500;900&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght#400;500;900&display=swap" rel="stylesheet">
<div id="wrapper">
<div id="header">header</div>
<div id="hero-wrapper">
<div id="hero">
<div id="hero-text">
<span id="hero-header">sell the product</span><br />
<span id="hero-sub">On this page for less</span>
</div>
</div>
<div id="hero-footnote">© Copyright Text</div>
</div>
<div id="content">
<!-- Content-Loop -->
<div class="article">
Here come the Article
</div>
</div>
<div id="footer">
<div id="footer-info">
<span id="footer-social-icons"></span> <span id="copyright-info">Made with love by Y.</span> <span id="contact-link">Contact</span>
</div>
</div>
</div>
If you just want to center the text, use text-align: center and the parts?
Or if you are looking to do more centered-content, maybe make your #header, #content, and #footer the following:
display: flex;
flex-direction: column;
align-items: center;
Basically, flex boxes within flex boxes.

Position icon / images inside flexbox

I´m starting my web developer training and i´m having trouble to move (in this case center) icons / images and text inside flexboxes.
Can anyone help?
my html
<div class="thirdcontainer">
<span class="flex3">
<img src="../challenge/img/feature-quality.png">
<img src="../challenge/img/feature-reliability.png">
<img src="../challenge/img/feature-speed.png"> </span>
</div>
My CSS
.thirdcontainer{
width: 1200px;
height: 150px;
background-color: #173493;
margin: 0 auto;
color: white;
font-family: open sans, serif;
font-size: 12px;
align-items: center;
justify-content: center;
}
.thirdcontainer{
width: 1200px;
height: 150px;
background-color: #173493;
margin: 0 auto;
color: white;
font-family: open sans, serif;
font-size: 12px;
align-items: center;
justify-content: center;
}
You forgot to add display: flex; to the containers CSS...
(note: I changed width to max-width for this example to make it fit in the snippet window).
.thirdcontainer{
max-width: 1200px;
height: 150px;
background-color: #173493;
margin: 0 auto;
color: white;
font-family: open sans, serif;
font-size: 12px;
display: flex;
align-items: center;
justify-content: center;
}
<div class="thirdcontainer">
<span class="flex3">
<img src="../challenge/img/feature-quality.png">
<img src="../challenge/img/feature-reliability.png">
<img src="../challenge/img/feature-speed.png"> </span>
</div>