keeping a div centered within a BG image in CSS - html

The issue im having is I'm struggling to keep my riot games text fluid, when i resize my browser it seems like it's stuck where it's at. either the left side or the right, usually the right side has more space from the end of the S to the side of the window.
// The showcase is just the container
// containing the background image.
.showcase {
position: relative;
width: 100%;
height: 35.76em;
text-align: center;
background: url(https://www.riotgames.com/darkroom/original/d6120739b8bf25995d5c43e69b9ce85d:bf411966145dd8b6b0f224e372aa27e5/annie.jpg);
background-repeat: no-repeat;
background-position: center;
background-size: cover;
}
// Info Text is The Main Text
// I'm trying to keep centered and fluid **
.info-text {
color:white;
position: absolute;
text-transform: uppercase;
letter-spacing: 1px;
font-family: "Montserrat", sans-serif;
font-size: 6.5em;
font-weight: bold;
margin: 0;
top: 15%;
left: 25%;
}
// I just ended up adding in the flex-box properties hoping to fix it,
// but im not too sure if it made things worse or actually helped out.
#media only screen and (max-width: 768px) {
.grid {
display: flex;
flex-direction: row;
flex-wrap: wrap;
align-content: center;
}
.info-text {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
align-content: center;
height: 25vh;
margin: 0;
text-transform: uppercase;
letter-spacing: 1px;
font-family: "Montserrat", sans-serif;
font-size: 3.5em;
font-weight: bold;
top: 38%;
left: 10%;
margin: 0;
}
<section class="showcase">
<div class="welcome-headline">
<h6>Welcome</h6>
</div>
<div class="info-text">
<p>
Riot Games
</p>
</div>
</section>

You can use flexbox for that with this properties on the container (background image in your case):
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
align-items: center;
justify-content: center;
Example:
.showcase {
position: relative;
width: 100%;
height: 35.76em;
text-align: center;
background: url(https://www.riotgames.com/darkroom/original/d6120739b8bf25995d5c43e69b9ce85d:bf411966145dd8b6b0f224e372aa27e5/annie.jpg);
background-repeat: no-repeat;
background-position: center;
background-size: cover;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
align-items: center;
justify-content: center;
}
.info-text {
color: white;
position: absolute;
text-transform: uppercase;
letter-spacing: 1px;
font-family: "Montserrat", sans-serif;
font-size: 6.5em;
font-weight: bold;
}
#media only screen and (max-width: 768px) {
.info-text {
font-size: 3.5em;
}
}
<section class="showcase">
<div class="welcome-headline">
<h6>Welcome</h6>
</div>
<div class="info-text">
<p>
Riot Games
</p>
</div>
</section>

Related

How do you put h3 tag on top of h6 tag to fit in within the container?

* {
margin: 0;
padding: 0;
overflow: auto;
font-family: Arial, helvetica, Sans-serif;
}
body {
background-color: aliceblue;
}
.container {
margin: 50%;
width: 324px;
height: 495px;
border: 10px solid white;
border-radius: 20px;
/* Setup */
display: flex;
justify-content: center;
align-items: center;
background-color: white;
}
.child {
display: flex;
justify-content: center;
align-items: center;
text-align: center;
margin-top: 70%;
}
.container .child2 {
display: flex;
justify-content: center;
align-items: center;
text-align: center;
margin-top: 100%;
}
<div class="container">
<h3 class="child">
Improve your front-end skills by building front-end projects
</h3>
<h6 class="child2">
Scan the QR code to visit Front-end Mentor and take your coding skills to
the next level
</h6>
</div>
In the code snippet above h3 text is below h6 text , although i would want h6 text to be nicely fit under h3 text in the container
This becomes a problem. Should I remove h6 heading to paragraph txt so that it will perfectly under h3 tag?
Need some advice
h3 tag and h6 are side by side because you are using flexbox and by default flexbox elements are aligned in row
what you can do is to bring h6 under h3 is you can use another div container and make it flex and change its direction to column flex-direction: column
here is the updated code
* {
margin:0;
padding:0;
overflow: auto;
font-family: Arial, helvetica , Sans-serif;
}
body {
background-color: aliceblue;
}
.container {
margin: 50%;
width: 324px;
height: 495px;
border: 10px solid white;
border-radius: 20px;
/* Setup */
display: flex;
justify-content: center;
align-items: center;
background-color: white;
}
.content{
margin-top: 70%;
display:flex;
flex-direction:column;
}
.child {
display: flex;
justify-content: center;
align-items: center;
text-align: center;
}
.container .child2 {
display: flex;
justify-content: center;
align-items: center;
text-align: center;
margin-top:10%;
/* margin-top: 100%; */
}
<div class="container">
<div class="content">
<h3 class="child"> Improve your front-end skills by building front-end projects
</h3>
<h6 class="child2"> Scan the QR code to visit Front-end Mentor and take your coding skills to the next level</h6>
</div>
</div>

Why is flex-direction: row rendering items in a column?

Hope someone can shed light on this question of mine.
I have a flex container, which has 2 flex containers with flex-direction: column.
However, it all displays in 1 column.
Initially, it displayed in 2 columns but did not start at the same height, and now it is in one column only.
Advise here will be appreciated.
<!-- ***** finding container ***** -->
section.finding-container {
top: 180px;
display: flex;
flex-direction: row;
position: absolute;
justify-content: space-between;
align-items: center;
/* height: 100% */
/* margin-right: 215px; */
}
.find-agent {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
margin-top: 350px;
margin-bottom: auto;
margin-left: 50px;
}
.find-agent img,
h1,
p,
button {
display: block;
margin-left: auto;
margin-right: auto;
}
.find-agent h1 {
font-weight: 550;
color: #1E95EE;
text-align: center;
margin-top: 12px;
margin-bottom: 0;
}
.find-agent p {
color: #3A3C3E;
font-weight: 350;
width: 445px;
height: 71px;
text-align: center;
opacity: 1;
}
.try {
border: 2px solid #1E95EE;
border-radius: 5px;
opacity: 1;
color: #1E95EE;
font-size: 18px;
font-weight: Regular;
height: 50px;
width: 143px;
text-align: center;
vertical-align: middle;
text-decoration: none;
justify-content: center;
}
.agent-profiles {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
margin-top: 0;
margin-bottom: auto;
}
.agent-profiles h2,
img {
display: block;
margin-left: auto;
margin-right: auto;
}
.agent-profiles h2 {
font-weight: 350;
color: #1E95EE;
text-align: center;
width: 182px;
height: 44px;
letter-spacing: 0;
opacity: 1;
}
```
<!-- ***** finding container ***** -->
<section class="finding-container">
<div class="find-agent">
<img src="./images/search.svg" alt="search">
<h1>Find the perfect agent</h1>
<p>
No more browsing through thousands of applications. Each week, we'll send you a fresh batch of hand-picked, personally-vetted candidates.
</p>
<button type="button" class="try">Try today</button>
</div>
<div class="agent-profiles">
<h2>
Selections from the Overpass Marketplace
</h2>
<img src="./images/profiles.svg" alt="profiles">
</div>
</section>
I have a flex container, which has 2 flex containers with flex-direction: column.
You've got an invalid comment in your CSS, which is breaking your first line of code:
<!-- ***** finding container ***** -->
section.finding-container {
top: 180px;
display: flex;
flex-direction: row;
position: absolute;
justify-content:space-between;
align-items: center;
/* height: 100% */
/* margin-right: 215px; */
}
That's HTML commenting syntax. In CSS, it's invalid, so the following section.finding-container selector is seen as an error and ignored. The container then falls back to its default layout model, display: block, which stacks child elements vertically. (You can see the proper CSS commenting syntax at the bottom of your rule.)
More details about CSS commenting syntax and error handling:
Why are some of my CSS rules not working?
Initially, it displayed in 2 columns but did not start at the same height ...
You've got all sorts of margins and alignment properties (such as justify-content) set on the items and container, so they're rendering at different heights.
section.finding-container {
/* top: 180px; */
display: flex;
flex-direction: row;
position: absolute;
justify-content: space-between;
/* align-items: center; */
border: 2px dashed red; /* for illustration purposes */
padding: 10px; /* for illustration purposes */
}
.find-agent {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
/* margin-top: 350px; */
/* margin-bottom: auto; */
/* margin-left: 50px; */
border: 1px solid green; /* for illustration purposes */
}
.find-agent img,
h1,
p,
button {
display: block;
margin-left: auto;
margin-right: auto;
}
.find-agent h1 {
font-weight: 550;
color: #1E95EE;
text-align: center;
margin-top: 12px;
margin-bottom: 0;
}
.find-agent p {
color: #3A3C3E;
font-weight: 350;
width: 445px;
height: 71px;
text-align: center;
opacity: 1;
}
.try {
border: 2px solid #1E95EE;
border-radius: 5px;
opacity: 1;
color: #1E95EE;
font-size: 18px;
font-weight: Regular;
height: 50px;
width: 143px;
text-align: center;
vertical-align: middle;
text-decoration: none;
justify-content: center;
}
.agent-profiles {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
margin-top: 0;
/* margin-bottom: auto; */
border: 1px solid black; /* for illustration purposes */
}
.agent-profiles h2,
img {
display: block;
margin-left: auto;
margin-right: auto;
}
.agent-profiles h2 {
font-weight: 350;
color: #1E95EE;
text-align: center;
width: 182px;
height: 44px;
letter-spacing: 0;
opacity: 1;
}
<section class="finding-container">
<div class="find-agent">
<img src="./images/search.svg" alt="search">
<h1>Find the perfect agent</h1>
<p>
No more browsing through thousands of applications.
Each week, we'll send you a fresh batch of hand-picked,
personally-vetted candidates.
</p>
<button type="button" class="try">Try today</button>
</div>
<div class="agent-profiles">
<h2>
Selections from the
Overpass Marketplace
</h2>
<img src="./images/profiles.svg" alt="profiles">
</div>
</section>

Justify-content: space-between works incorrect

i have the following code
header {
width: 100%;
height: 65px;
background: #fff;
font-size: 11px;
font-weight: bold;
text-transform: uppercase;
display: flex;
justify-content: space-between;
align-items: center;
color: #252c3a;
}
#menu {
width: 100%;
display: flex;
margin-left: 28%;
}
#menu div {
width: 100px;
height: 65px;
display: flex;
align-items: center;
justify-content: center;
position: relative;
}
<header>
<div id="logo">
<img src="img/header/logo.png" alt="Logo">
</div>
<div id="menu">
<div>Home</div>
<div>Club-Life</div>
<div>Training</div>
<div>Instructors</div>
<div>Contact</div>
</div>
Chrome inspect
The width of the other blocks is 100%, but the header width gets bigger than the block below. I use justify-content: space-between.
Remove width & margin
Add flex-wrap on the header
header {
width: 100%;
height: 65px;
background: #fff;
font-size: 11px;
font-weight: bold;
text-transform: uppercase;
display: flex;
justify-content: space-between;
align-items: center;
color: #252c3a;
flex-wrap:wrap;
}
#menu {
display: flex;
flex-wrap:wrap;
}
There are few things we need to do:
In header:
1. Remove width
In #main:
1. Remove width
2. Remove margin
In #menu div:
1. Remove everything and just add margin left
Demo: https://codepen.io/Bibeva/pen/povddJr
Final code:
header {
height: 65px;
background: #fff;
font-size: 11px;
font-weight: bold;
text-transform: uppercase;
display: flex;
justify-content: space-between;
align-items: center;
color: #252c3a;
}
#menu {
display: flex;
}
#menu div {
margin: 0 0 0 30px;
}
#menu {
display: flex;
}
that's all , Can you try this ?

How do I flex 1 item of a flex div all the way right when its inside another flex div?

I seem to be havin some issues with the bottom portion of this blog card for my hobby website.
As you can see im trying to go for the standard author name on the left side with the like symbol on the right side.
For whatever reason, I can't flex the heart all the way to the right. I made the parent container 100% width. I've tried all the align-self. But I can't seem to get it to work and have come to the pros for help.
.container {
width: 20%;
display: flex;
flex-direction: column;
margin-left: 10px;
position: relative;
margin: 10rem auto;
background-color: white;
box-shadow: 0 1.5rem 4rem rgba(0, 0, 0, .25);
align-items: flex-start
}
.picture {
width: 100%;
height: 25rem;
background-image: url(./images/adventure.jpg);
overflow: hidden;
background-size: cover;
background-position: center bottom;
padding-bottom: 2rem;
}
.container__p {
font-size: 1rem;
font-weight: 700;
align-self: center;
color: #777;
letter-spacing: 1px;
text-transform: uppercase;
padding-bottom: .5rem;
padding-left: .5rem;
padding-right: .5rem;
padding-top: 1rem;
border-bottom: 1px solid lightgrey;
}
.container__header {
font-size: 3rem;
text-align: center;
padding: 1rem;
align-self: center;
font-weight: 600;
}
.container__blogsummary {
font-size: 1.3rem;
padding-top: 5rem;
padding: 1rem;
text-align: center;
align-self: center;
color: #777;
}
/*Problem CSS below*/
.icon-basic-heart {
font-size: 2rem;
color: red;
align-self: flex-end;
}
.test {
display: inline-flex;
align-items: flex-start;
}
<div class='container'>
<div class='picture'></div>
<p class='container__p'><span>Trending<span></p>
<h1 class='container__header'>Survivng the Amazon Forest</h1>
<i class='icon-basic-world'></i>
<p class='container__blogsummary'>
A matchbox, cantene of water and pocket knife. How I survived the Amazon rainforest for 3 days with just the bare necessaties and how you can too!
</p>
<!-- this is the problem div below -->
<div class='test'>
<p class='container_author'>James</p>
<i class='icon-basic-heart'></i>
</div>
</div>
It's probably a fundamental mistake as I am learning flexbox throuh creating this on my own. Or maybe I havent had enough coffe today. Thanks in advance!
.icon-basic-heart {
float: left;
}
.container_author {
float: right;
}
.test {
width: 100%;
}
You overcomplicate things. There is no need in flex for only 2 objects - flex doesnt fit in here.
Change your .test CSS to this:
.test {
display: inline-flex;
align-items: flex-start;
// Add the following
justify-content: space-between;
width: 100%;
}

How to align this logo at the top of the page using flexbox? [duplicate]

This question already has answers here:
Center and bottom-align flex items
(3 answers)
Closed 5 years ago.
Hi,
I want the logo 'Abyk Deco' to stay at the top of the page, but it's now centered along with the two heading text. How can I align this at the top using flexbox? I've tried this code.
<body>
<div class="banner">
<div class="logo">
<h1>Abyk<span id="red">Deco<span></h1>
</div>
<div class="heading">
<h1>ABYK DECO</h1>
<h4>We make your dream home come true!</h4>
</div>
</div>
The CSS:
.banner{
height: 100%;
background-image: url(../img/1.jpeg);
background-size: cover;
background-position: center top;
background-repeat: no-repeat;
background-attachment: fixed;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.logo{
color: #fff;
font-size: 2em;
font-family: 'Oswald', sans-serif;
align-items: flex-start;
justify-content: center;
}
.logo a{
color: #fff;
}
#red{
color: red;
}
.heading{
font-family: 'PT Sans', sans-serif;
color: #fff;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
.heading h1{
font-size: 4.5em;
}
.heading h4{
font-size: 2em;
}
How can I align the logo to the top while the heading texts stay at the center? I prefer to use flexbox to solve this problem. Thank You.
.container{
width: 100vw;
height: 100vh;
display: flex;
flex-direction: column;
align-items:center;
justify-content: flex-start;
background-color: gray;
}
.logo{
width: 200px;
height: 200px;
background-color: red;
}
<div class="container">
<div class="logo">
<p>Text something </p>
<p>Something else </p>
<p>What else?</p>
</div>
</div>
Try to use this.
The main trick here to be able put the logo at the top and heading in the center of the viewport, is to use the ::after pseudo element to match the logo and then add this rule and make them equal high by setting flex-grow to 1 and flex-basis to 0
.logo, .heading, .banner::after {
content: ''; /* for the pseudo to render */
flex: 1 1 0; /* grow/shrink equal based on zero content */
}
Stack snippet
html, body {
height: 100%;
margin: 0;
}
.banner {
height: 100%;
background-image: url(http://placehold.it/300x100/555);
background-size: cover;
background-position: center top;
background-repeat: no-repeat;
background-attachment: fixed;
display: flex;
flex-direction: column;
align-items: center;
}
.logo {
color: #fff;
font-size: 2em;
font-family: 'Oswald', sans-serif;
}
.logo a {
color: #fff;
}
#red {
color: red;
}
.heading {
font-family: 'PT Sans', sans-serif;
color: #fff;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.heading h1 {
font-size: 4.5em;
margin: 0; /* remove default margin */
}
.heading h4 {
font-size: 2em;
margin: 0; /* remove default margin */
}
.logo, .heading, .banner::after {
content: ''; /* for the pseudo to render */
flex: 1 1 0; /* grow/shrink equal based on zero content */
}
<div class="banner">
<div class="logo">
<h1>Abyk<span id="red">Deco</span></h1>
</div>
<div class="heading">
<h1>ABYK DECO</h1>
<h4>We make your dream home come true!</h4>
</div>
</div>
If the heading should be in the center of the space left (between the end of the logo and page bottom), use auto margin on the heading
html, body {
height: 100%;
margin: 0;
}
.banner {
height: 100%;
background-image: url(http://placehold.it/300x100/555);
background-size: cover;
background-position: center top;
background-repeat: no-repeat;
background-attachment: fixed;
display: flex;
flex-direction: column;
align-items: center;
}
.logo {
color: #fff;
font-size: 2em;
font-family: 'Oswald', sans-serif;
}
.logo h1 {
margin-bottom: 0; /* remove default margin */
}
.logo a {
color: #fff;
}
#red {
color: red;
}
.heading {
font-family: 'PT Sans', sans-serif;
color: #fff;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
margin: auto 0;
}
.heading h1 {
font-size: 4.5em;
margin: 0; /* remove default margin */
}
.heading h4 {
font-size: 2em;
margin: 0; /* remove default margin */
}
<div class="banner">
<div class="logo">
<h1>Abyk<span id="red">Deco</span></h1>
</div>
<div class="heading">
<h1>ABYK DECO</h1>
<h4>We make your dream home come true!</h4>
</div>
</div>
Change align-items to flex-start
.banner{
height: 100%;
background-image: url(../img/1.jpeg);
background-size: cover;
background-position: center top;
background-repeat: no-repeat;
background-attachment: fixed;
display: flex;
flex-direction: column;
align-items: flex-start;
justify-content: center;
}
Remove the following styles from banner class would do.
justify-content: center;
align-items: center;
Add the following styles to logo class
align-items: center;
justify-content: center;
display: flex;
body {
margin: 0
}
.banner {
height: 100%;
background-image: url('https://images.designtrends.com/wp-content/uploads/2015/11/06084012/Dark-Brown-Bricks-Backgroud.jpg');
background-size: cover;
background-position: center top;
background-repeat: no-repeat;
background-attachment: fixed;
display: flex;
flex-direction: column;
}
.logo {
color: #fff;
font-size: 2em;
font-family: 'Oswald', sans-serif;
align-items: center;
justify-content: center;
display: flex;
}
.logo a {
color: #fff;
}
#red {
color: red;
}
.heading {
font-family: 'PT Sans', sans-serif;
color: #fff;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
.heading h1 {
font-size: 4.5em;
}
.heading h4 {
font-size: 2em;
}
<div class="banner">
<div class="logo">
<h1>Abyk<span id="red">Deco<span></h1>
</div>
<div class="heading">
<h1>ABYK DECO</h1>
<h4>We make your dream home come true!</h4>
</div>
</div>
Try to add flex-grow: 1; to the .heading, align-self: center; to the .logo and remove margin-top from the .logo H1 (optional)
HTML:
<div class="banner">
<div class="logo">
<h1>Abyk<span id="red">Deco<span></h1>
</div>
<div class="heading">
<h1>ABYK DECO</h1>
<h4>We make your dream home come true!</h4>
</div>
CSS:
.banner{
height: 100%;
min-height: 100vh;
background-image: url(../img/1.jpeg);
background-size: cover;
background-position: center top;
background-repeat: no-repeat;
background-attachment: fixed;
display: flex;
flex-direction: column;
}
.logo {
color: #fff;
font-size: 2em;
font-family: 'Oswald', sans-serif;
align-self: center; /*added*/
}
.logo a{
color: #fff;
}
.logo H1 { /*added*/
margin-top: 0;
}
#red{
color: red;
}
.heading {
font-family: 'PT Sans', sans-serif;
color: #fff;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
flex-grow: 1; /*added*/
}
.heading h1{
font-size: 4.5em;
}
.heading h4{
font-size: 2em;
}
Code exmaple on JSFiddle
The trick is to add margin: 0 auto auto auto; to both your .logo and .heading
p.s. I have minimize the font size so that you can see the preview properly in the code snippet or just check out the preview on full screen
html{
height:100%;
}
body{
background:grey;
height:100%;
}
.banner{
height: 100%;
background-image: url(../img/1.jpeg);
background-size: cover;
background-position: center top;
background-repeat: no-repeat;
background-attachment: fixed;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.logo{
color: #fff;
font-size: .9em;
font-family: 'Oswald', sans-serif;
margin: 0 auto auto auto;
}
.logo a{
color: #fff;
}
#red{
color: red;
}
.heading{
font-family: 'PT Sans', sans-serif;
color: #fff;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
margin: 0 auto auto auto;
}
.heading h1{
font-size: 2em;
margin: 0;
}
.heading h4{
font-size: 1.3em;
}
.logo h1 {
margin: 0;
}
.heading h1 {
margin-bottom: 0;
}
<div class="banner">
<div class="logo">
<h1>Abyk<span id="red">Deco<span></h1>
</div>
<div class="heading">
<h1>ABYK DECO</h1>
<h4>We make your dream home come true!</h4>
</div>
</div>