aligning text properly when having background image in div - html

I've got a small problem regarding background-image and some content. Right now, I've got a specific div that contains a background-image. I've got another one with content. They seperate the page into 2 sections. But when I'm sizing my image, it pushes the text all the way to the right. I'd like to have the image (it's smaller on small screens, but grows bigger when screen sizes increases) be independent from my content, so that I can center the text until the image is about to touch it, then make it so that the image is more on the left and the text more to the right. My HTML looks like this:
<div className={styles.indexMain}>
<div className={styles.imageWrapper}>
<div className={styles.heroImage}></div>
</div>
<div className={styles.ctaContent}>
<h1 className={styles.ctaTitle}>
Lorem Ipsum
<span> Lorem</span> Lorem Ipsum.
</h1>
<p className={styles.ctaSubtitle}>
Lorem ipsum some call to action paragraph
</p>
<Link to="/contact" className={styles.ctaButton}>
Contact us
</Link>
</div>
</div>
MY CSS currently looks like this:
.indexMain {
display: flex;
justify-content: space-between;
align-items: stretch;
position: relative;
margin-top: 5rem;
}
.imageWrapper {
flex-direction: column;
display: flex;
justify-content: flex-end;
}
.heroImage {
background-image: url(../images/heroBackgroundMain.png);
min-height: 100vh;
min-width: 50vw;
background-size: contain;
background-repeat: no-repeat;
background-position: bottom left;
opacity: 0.9;
margin-top: 5rem;
z-index: -1;
margin: 0 auto;
}
.ctaContent {
display: flex;
flex-direction: column;
align-items: flex-start;
margin: 0 auto;
margin-left: -2rem;
}
.ctaTitle {
font-weight: 500;
font-size: 2rem;
margin-bottom: 1rem;
text-align: center;
}
.ctaSubtitle {
font-weight: 500;
font-size: 1rem;
margin-bottom: 2rem;
text-align: jusitfy;
}
.ctaButton {
color: white;
padding: 0.75rem 1.5rem;
border-radius: 0.5rem;
text-decoration: none;
background-color: #f07818;
transition: all 300ms ease-in-out;
font-size: 0.9rem;
}
.ctaButton:hover,
.ctaButton:focus {
background-color: #78c0a8;
}
.ctaTitle span {
color: #78c0a8;
font-weight: 900;
display: flex;
align-items: center;
justify-content: center;
font-size: 1.2em;
}
I hope it makes sense. I've been stuck on this for far too long, and I can't seem to figure out why :(

You have two sections, the left section is your background image and the right section is the text.
For bigger screens, your text isn't centered in the section to the right, because you move it to the left with minus margin. Remove margin-left: -2rem; from .ctaContent to fix it.
If you want the image behind the text, you can for example move the code for the background image to .indexMain. The content doesn't get affected by the image this way.
.indexMain {
display: flex;
justify-content: space-between;
align-items: stretch;
position: relative;
margin-top: 5rem;
//background image
background-image: url(https://placekitten.com/600/600);
min-height: 100vh;
min-width: 50vw;
background-size: contain;
background-size: cover;
background-repeat: no-repeat;
background-position: bottom left;
}

In this case, CSS media query plays a vital role in changing size, and shape, enabling or disabling the view of content, etc. You can read about responsive web design media queries on the w3schools website. This is very well explained there.
https://www.w3schools.com/css/css_rwd_mediaqueries.asp

Related

How do I adapt the <main> height to the page when I zoom in and out?

I am learning HTML and CSS and I am trying to replicate the homepage of flickr.com.
I can't get the central part of my page ("Find your inspiration") to stay centered when I zoom in and out. I can't get the <body> content to responsively fill the space between the <header> and the <footer>.
I have spent many hours googling, playing with heights, flex, and trying to reshape the layout of my page, but I can't figure out how to reproduce the desired effect. I think I am messing up the layout (especially with the nesting of my containers) but I can't spot my mistake.
Here are the screenshots of the real flickr.com home page and the screenshot of the clone page I'm trying to build. As you can see, my page doesn't keep the element centered when I zoom out because my block doesn't stretch to fill the space between <header> and <footer>:
My clone home page
Original Flickr home page
html {
background: url(images/8225606733_086c8f3d83_o.jpg)no- repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
height: 100%;
}
header {
min-height: 70px;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0px 20px;
}
main {
min-height: 400px;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.wrapper {
display: block;
text-align: center;
background-color: lightblue;
font-family: Arial, Helvetica, sans-serif;
padding: 0 30px;
}
.wrapper h1 {
font-size: 40px;
font-weight: bold;
margin-bottom: 18px;
}
.wrapper h2 {
font-size: 25px;
font-weight: normal;
margin-bottom: 50px;
}
.wrapper a {
color: black;
font-weight: bold;
background-color: white;
padding: 12px 23px;
border-radius: 3px;
}
footer {
position: fixed;
width: 100%;
bottom: 0px;
}
<header>
HEADER
</header>
<main>
<div class="wrapper">
<h1>Find your inspiration.</h1>
<h2>Join the Flickr community, home to tens of billions of photos and 2 million groups.
</h2>
<div class="start-button">
Start for free
</div>
</div>
</main>
<footer>
FOOTER
</footer>
try this way:
main {
height: calc(100vh - 70px);
width: 100%;
display: flex;
justify-content: center;
align-items: center;
}
You can't center the .wrapper element because you assigned a min-height: 400px to its parent element so it doesn't span for the entire viewport height. Try adding a colored background to the main element and you'll see.

How can I do a responsive absolute positioning?

I'm trying to create a banner that has two buttons on it:
.banner {
width: 100%;
display: flex;
flex-direction: column;
margin-top: -4%;
}
.banner img {
width: 100%;
/*image is 1232x317 by default and defines the size of the banner*/
}
.banner-buttons {
display: flex;
flex-direction: row;
margin-left: 6.2%;
position: absolute;
top: 10%;
}
.banner button {
display: flex;
font-size: 200%;
border: none;
border-radius: 5px;
font-weight: bold;
align-items: center;
padding: 5px 15px;
}
<div class="banner">
<img src="https://picsum.photos/1200/300">
<div class="banner-buttons">
<button>Assistir</button>
<button>Mais Informações</button>
</div>
</div>
but the problem is, the height of the buttons change based on the viewport, destroying the banner, how can I position it without ruining it?
I would personally avoid absolute positioning and use background image to create the layers.
You can set a min height on your banner if you desire.
I would also use em and media queries to reduce the font size when the screen resolution is smaller.
.banner {
background:url(https://picsum.photos/1200/300);
padding:10px;
}
.banner-buttons {
display: flex;
justify-content:center;
align-items: center;
}
.banner button {
font-size: 2em;
border: none;
border-radius: 5px;
font-weight: bold;
padding: 5px 15px;
margin:5px;
}
<div class="banner">
<div class="banner-buttons">
<button>Assistir</button>
<button>Mais Informações</button>
</div>
</div>
Actually what solved for me was adding position: relative to .banner, now the buttons are displayed at the exact same position at every screen size.

Aligning div to top of body

I currently have this design
As you can see, the jumbotron (boostrap v5) is aligned in the center but I want it to be touching the very top of the body. I've tried playing around with margin, display, and align but I can't seem to work it out.
Here is the code so far:
body {display: flex;
align-items: center;
padding-top: 40px;
padding-bottom: 40px;
background-color: #f5f5f5;
}
.jumbotron {
padding: 2rem 1rem;
margin: 0 auto;
width: 100%;
background-color: #e9ecef;
border-radius: .3rem;
}
If I delete the body's align-items:center, the jumbotron aligns at the top but for some reason its height then increases to take up almost all the body. Why might that be? How can I keep the size of the jumbotron as it appears in the image attached but get it to align at the very top?
Thank you!
You can remove the align-items: center; on body and add align-self: flex-start; to your jumbotron. Or just don't use display flex and if the jumbotron is the first item it will be at the top of the page.
body {
display: flex;
padding-top: 40px;
padding-bottom: 40px;
background-color: #f5f5f5;
}
.jumbotron {
align-self: flex-start;
padding: 2rem 1rem;
margin: 0 auto;
width: 100%;
background-color: #e9ecef;
border-radius: .3rem;
}

Horizontally center emoji inside absolute div

I am building a game that places emoji at various coordinates in a webpage. Sometimes, the emoji is bigger than the container it resides in. For example, here is HTML that places a house with font-size 100px inside a 50px container.
div.emoji-container {
position: absolute;
left: 200px;
top: 200px;
width: 50px;
height: 50px;
background-color: red;
}
div.emoji {
position: relative;
font-size: 100px;
line-height: 100px;
}
<div class="emoji-container">
<div class="emoji">🏠</div>
</div>
Here is the fiddle.
As you can see, the emoji is not centered on the red square.
Is there a way that I can use css to horizontally center the emoji within its container, even if the emoji is wider than the container?
In other words, if the width of the emoji is 100px and the container is 50px, then the emoji should protrude from its container by 25px on both sides.
Part of the challenge is that emoji with font size 100px has a different width on Mac, Windows, Andriod, and so on. On Mac, the width is 100px, but on Windows, the width is around 113px.
If there is no css solution, I know that I can use a JavaScript solution.
You can use flexbox for this. For demonstration i have added a transparence to the emoji, to show its center position. On top of the CSS is a custom property which you can change to test different sizes.
https://jsfiddle.net/uvf2h0sj/
Important for you is that both elements (parent and child) using the following CSS to center everything (vertical and horizontal):
display: flex;
justify-content: center;
align-items: center;
:root {
--size: 100px;
}
body {
display: flex;
height: 100vh;
padding: 0;
margin: 0;
justify-content: center;
align-items: center;
}
div.emoji-container {
width: 50px;
height: 50px;
background-color: red;
display: flex;
justify-content: center;
align-items: center;
}
div.emoji {
font-size: var(--size);
line-height: var(--size);
width: var(--size);
height: var(--size);
opacity: 0.5;
display: flex;
justify-content: center;
align-items: center;
}
<div class="emoji-container">
<div class="emoji">🏠</div>
</div>

How to center align image on navbar and for small device move it to the left to fit content?

I have a logo image with some transparency in it. How can I align the logo image to the center on the fixed navbar and then move it slightly to the left on smaller devices so that the content on the right can fit on the navbar?
I have tried background-size: cover but makes the image responsive and the logo gets cut off. I tried background-size: contain but then I lose the background color of the navbar on larger devices.
Is there a way I can move the image to the html to achieve what I need?
Here is my attempt on plnkr:
https://plnkr.co/edit/Uij12vHwFuaeAS91nYUL?p=preview
thanks
UPDATE:
I am trying a different approach and moved the image into the html and try to use the css flexbox approach. Almost there, but need to remove the gap.
HTML:
<nav class="navbar navbar-fixed-top">
<div class="search">
<i class="fa fa-search"></i>
</div>
<a class="brand-logo" href="#/">
<img src="https://i.imgsafe.org/1963cc1736.png" />
</a>
<aside>
<figure class="account-balance">
<span>de 88,980.7740</span>
<i class="fa fa-user"></i>
</figure>
</aside>
</nav>
CSS
nav.navbar {
border: 0;
color: #FFF;
height: 40px;
min-height: 40px;
z-index: 1;
display: 0;
-webkit-box-pack: justify;
-moz-box-pack: justify;
-webkit-justify-content: space-between;
-ms-flex-pack: justify;
justify-content: space-between;
}
.search,.filler,aside,figure {
height: 100%;
}
.search,.filler,aside {
background: #266224;
float: left;
}
.search,aside {
flex-grow: 1;
-webkit-flex: 1;
}
.search .fa-search,figure > span {
display: inline-block;
margin-top: 12px;
}
.search {
padding: 0 12px;
}
.brand-logo img {
height: 40px;
max-width: 100%;
}
figure {
float: right;
}
figure.account-balance {
padding-right: 12px;
}
figure > span {
line-height: 25px;
margin-top: 8px;
padding-right: 5px;
}
Here is the modified version using flexbox:
https://embed.plnkr.co/wSWpIDIoJOiSQYVvwHMU/
This does come close to resolving the problem however, this is not working well on iPhone 6 and old iPad.
There is a slight gap on either side of the anchor tag (maybe half a pixel gap), I have tried the solution mentioned here: css flex layout not working on some iPads but it doesn’t work.
Do you know a way to remove the gap?
Try to write specified styles for different device width using this.
try this:
ul li {
height: 180px;
width: 180px;
background: #f6304c;
display: block;
color: #ffffff;
text-align: center;
margin: 2% 0 0 30%;
padding: 50px 0;
}
for mobile device
#media screen and (min-width: 678px) {
ul li {
height: 180px;
width: 180px;
background: #f6304c;
display: block;
color: #ffffff;
text-align: center;
float:left;
/ margin: 2% 0 0 30%;*/
padding: 50px 0;
}
}
use this
.brand-logo {
background: url(https://i.imgsafe.org/1963cc1736.png) left center no-repeat;
background-size: contain;
background-position: center;
background-color: #266224;
height: 100%;
}
the only thing you have to do is fill the O in your logo with the color because it is transparent XD