HTML/CSS: Positioning a background element responsively - html

Here's a rough mockup of what I am trying to create, I have the page laid out but the only piece left that I am struggling with is positioning the background circle in a way that is responsive.
I tried using position absolute with top & left positioning but even small screen changes will completely throw off the layout.
What would you guys recommend as the best way to approach this? Thanks!
code demo: https://jsfiddle.net/e91g8tdj/11/

Try this in your Css :
.yourClassHere{
position: relative;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

Since you haven't provided any demo of your working code so had to create a responsive one from scratch using css only. It might look a bit janky in the snippet below so its better to copy the code in a file and view in a browser.
Let me know in the comments if it breaks.
*,
*::before,
*::after {
box-sizing: border-box;
padding: 0;
margin: 0;
}
.container {
width: 100%;
height: 100vh;
}
.divider1,
.divider2 {
width: 100%;
height: 50vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.divider1 {
background-color: silver;
gap: 2rem;
}
.divider2 {
background-color: gray;
}
.row1,
.row2,
.row3 {
width: 90%;
height: 25%;
background-color: #2a2a2a;
}
.row3 {
width: 40%;
height: 70%;
}
.row1-row2-wrapper, .row3-wrapper {
width: 40%;
height: inherit !important;
display: flex;
justify-content: end;
align-items: center;
position: relative;
}
.row1-row2-wrapper {
flex-direction: column;
justify-content: center;
gap: 2rem;
z-index: 1;
}
.row1-row2-wrapper::before, .row1-row2-wrapper::after, .row3::before, .row3::after {
content: '';
position: absolute;
border: 4px solid #fff;
border-radius: 50%;
}
.row1-row2-wrapper::before {
inset: 0;
z-index: -1;
height: 105%;
width: 100%;
}
.row1-row2-wrapper::after {
width: 2rem;
height: 2rem;
bottom: -18%;
right: 24%;
}
.row3::before {
width: 1.4rem;
height: 1.4rem;
top: 18%;
right: 20%;
}
.row3::after {
width: 1rem;
height: 1rem;
top: 25%;
right: 16%;
}
<div class="container">
<div class="divider1">
<div class="row1-row2-wrapper">
<div class="row1"></div>
<div class="row2"></div>
</div>
</div>
<div class="divider2">
<div class="row3-wrapper">
<div class="row3"></div>
</div>
</div>
</div>

Related

Nesting DIVs and applying CSS to them, I'm new, I'm stuck

I'm trying to make a button that looks like this:
https://postimg.cc/HcG72DCf
and this:
https://postimg.cc/1nkXSpz2
Here is my codepen to try & copy it:
https://codepen.io/assilem4791/pen/KKBoLOm
.buttonContainer {
position: relative;
height: 200px;
width: 500px;
background-color: lightgray;
border-radius: 40px;
align-items: center;
}
.buttonstripes {
position: absolute;
background-color: gray;
height: 5%;
width: 100%;
left: 0;
z-index:1;
}
#stripe1 {
top: 30%;
}
#stripe2 {
top: 40%;
}
#stripe3 {
top: 50%;
}
#stripe4 {
top: 60%;
}
#stripe5 {
top: 70%;
}
.offAir {
postion: absolute;
height: 80%;
width: 70%;
background-color: lightyellow;
border-radius: 40px;
font-size: 50px;
align: center;
z-index: 2;
}
<div class="buttonContainer">
<div class="buttonstripes" id="stripe1"></div>
<div class="buttonstripes" id="stripe2"></div>
<div class="buttonstripes" id="stripe3"></div>
<div class="buttonstripes" id="stripe4"></div>
<div class="buttonstripes" id="stripe5"></div>
<div class="offAir"> <p>OFF AIR</p>
</div>
I'm having trouble with making the stripes sit behind the offAir div.
Any help would be appreciated!
I thought putting a div inside another div with a z-index would work.

Div isn't stretching to 100% height of parent

I'm designing my CSS layout, but can't get the div to stretch to 100% of the height of the parent.
I have a menu bar that takes up the top 13.714vh of the screen. Then I have a main div that I want to take up the remainder of the screen height which I did with height: 100%. bottom-container takes up the bottom 38.2% of the vertical space available in main, and I want speech-bubble to take up the remaining 61.8% of the vertical space in main.
For some reason though, there's a huge white container in the middle of the screen, and speech-bubble isn't taking up the remaining space because of it. Can anyone help me figure out what's going on?
Is there a problem with my HTML or did I make an error in the CSS?
Here's the code pen:
https://codepen.io/TheNomadicAspie/pen/NWjKwxE
body {
margin: 0;
}
.menu-bar {
height: 13.714vh;
width: 100vw;
background: darkblue;
top: 0%;
}
.main {
background: black;
grid-template-rows: 61.8% 100%;
height: 100%;
width: 100%;
padding-left: 1.5%;
padding-right: 1.5%;
padding-top: 1.5%;
padding-right: 1.5%;
}
.speech-bubble {
grid-row: 1;
position: relative;
background: orange;
height: 97%;
width: 97%;
border-radius: 4em;
}
.speech-bubble:after {
content: '';
position: absolute;
bottom: 0;
left: 50%;
width: 0;
height: 0;
border: 4em solid transparent;
border-top-color: white;
border-bottom: 0;
margin-left: -4em;
margin-bottom: -4em;
}
.email-container {
visibility: hidden;
}
.question-text {
visibility: hidden;
}
.bottom-container {
grid-row: 2;
position: fixed;
background: green;
height: 38.2%;
width: 100vw;
bottom: 0%;
left: 0%;
}
<div id="menu_bar" , class="menu-bar"></div>
<div id="main" , class="main">
<div id="speech_bubble" , class="speech-bubble">
<div id="email_container" class="email-container">
<label for="email">Enter your email:</label>
<input type="email" id="email" name="email">
<button id="submit_email_btn" class="btn">Submit</button>
</div>
<div id="question_text" class="question-text">Question</div>
</div>
</div>
<div id="bottom_container" , class="bottom-container">
</div>
</div>
</div>
Do you want anything like this? screenshot.
If so, making your .menu-bar as position: relative and modifying your .main class styles as follows will work:
.main {
position: absolute;
background: black;
left: 0;
right: 0;
height: 50%;
}
Also, you may add margin: auto in your speech-bubble class to align it to center.
Your main tag is not taking full height as your html and body tags are not taking the full height.
Always remember that block elements can stretch maximum to their's parent's height, hence you need to give html and body tag height of 100%.
I have added the additional css below.
html, body { height: 100%;}
I think you want thing like this
* {
margin: 0;
box-sizing: border-box;
}
body {
display: flex;
flex-direction: column;
height: 100vh;
}
.menu-bar {
height: 13.714vh;
background-color: tomato;
color: #fff
}
.main {
background: black;
padding: 1.5%;
flex: 1
}
.speech-bubble {
background-color: orange;
border-radius: 4em;
height: 95%;
position: relative;
display: flex;
flex-direction: column;
}
.speech-bubble:after {
content: '';
position: absolute;
bottom: 0;
left: 50%;
width: 0;
height: 0;
border: 4em solid transparent;
border-top-color: white;
border-bottom: 0;
margin-left: -4em;
margin-bottom: -4em;
}
.email-container {
align-items: center;
justify-content: center;
flex: 1;
display: flex;
}
.question-text {
height: 50px;
position: relative;
text-align: center
}
.bottom-container {
height: 70px;
background-color: lightseagreen;
}

Responsive image in a div responsive?

I'm building my first actually decent website basically and I'm creating a background atm with some css and an image and I want to do it just as it is in the picture
.
(accomplished already) but it's not responsive probably because of position: absolute property and I want to make it properly responsive.
Here is the HTML code I am using for the background
.bg {
background: #9359C7;
color: white;
display: grid;
text-align: center;
height: 764px;
width: 1280px;
}
.content {
position: relative;
}
.content img {
width: auto;
height: auto;
position: absolute;
right: -178px;
bottom: 0;
}
<div class="bg">
<div class="content">
<h1>Lol</h1>
<img src="https://www.pikpng.com/pngl/m/69-698658_yami-ygi-y-gi-yu-gi-oh.png" alt="Yugi">
</div>
</div>
EDIT: I put my whole site on this patebin since idk it wouldn't let me upload it on here the snippet I was given did not work for me at least.
This should do the trick:
body {
font-family: sans-serif;
margin: 0;
padding: 0;
}
.hero {
position: relative;
overflow-x: hidden;
}
header {
position: relative;
background: black;
padding: 40px 0;
z-index: 5;
}
.content {
position: relative;
z-index: 5;
color: white;
text-align: center;
width: 70%;
height: 100vh;
max-height: 768px;
background: #9359C7;
padding: 20px;
}
img {
width: auto;
height: 98%;
position: absolute;
right: 30%;
bottom: 0;
transform: translateX(50%);
padding-top: 80px;
z-index: 10;
}
h1 {
margin: 0;
}
<div class="hero">
<header></header>
<div class="content">
<h1>Lol</h1>
</div>
<img src="https://ms.yugipedia.com//c/c4/YamiYugi-DULI.png" alt="Yugi">
</div>

Why my Element is going outside the header element

There is a div with a class of favourite which is not aligning in the header section.
Yes this is a react project but it is edited like a normal project for this question.
<div className='header'>
<img class='profilePhoto' src='./userIcon'></img>
<div class='brand-logo'>
</div>
<div class='favourite'>Favourite</div> <!-- This Favourite is going outisde div.header-->
</div>
CSS File:
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
.header {
width: 100vw;
height: 5rem;
background: #2475b0;
position: fixed;
}
.profilePhoto {
position: absolute;
margin-left: 32px;
vertical-align: middle;
top: 50%;
transform: translateY(-50%);
}
.brand-logo {
display: flex;
height: 100%;
justify-content: center;
align-items: center;
}
.favourite {
display: inline-block;
position: relative;
right: 3rem;
}
Brand Logo CSS
.icon-box {
background: #eaf0f1;
width: 19rem;
height: 75%;
border-radius: 1rem;
text-align: center;
vertical-align: bottom;
}
.brand-name {
display: flex;
height: 100%;
justify-content: center;
align-items: center;
margin-top: 0.3rem;
font-family: 'Luckiest Guy', cursive;
font-size: 2rem;
}
Image Preview:
Any tip will be helpful.. Thank you in advance
Try this piece of code in favourite class.
.favourite {
text-align: right;
position: relative;
right: 3rem;
margin-top: -45px;
}
Adjust margin-top as your need.
Increase the height of header div
.header {
height: 10rem;
}
You don't need to give position for make your logo in center. Just remove fix height from header and try using padding. Hope will help you.
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
.header {
width: 100vw;
padding: 20px;
background: #2475b0;
position: fixed;
}
<div class='header'>
<div class='brand-logo'>
<img class='profilePhoto' src='/userIcon'>
</div>
<div class='favourite'>Favourite</div> <!-- This Favourite is going outisde div.header-->
</div>
add display: inline-block to 3 classes:
.profilePhoto, .brand-logo, .favourite {
display: inline-block;
}
and remove position attribute in class favourite. One more, i think you should use 'class' attribute, not 'className' like :
<div className='header'>
Try this just add height and padding.
.header {
width: 100vw;
height: 10rem;
background: #2475b0;
padding-bottom:10px;
position: fixed;
}
Thanks: https://stackoverflow.com/users/2875348/ravibagul91
Guys this is happening because .brand-logo is taking 100% height!
When I removed:
height: 100%
then code is working fine...
I just simply removed flexbox and used position property to align my .brand-logo
.brand-logo{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
.header {
width: 100vw;
height: 5rem;
background: #2475b0;
position: fixed;
}
.profilePhoto {
position: absolute;
margin-left: 32px;
vertical-align: middle;
top: 50%;
transform: translateY(-50%);
}
.brand-logo {
display: flex;
height: 100%;
justify-content: center;
align-items: center;
}
.favourite {
display: flex;
position: fixed;
top: 50px;
left: 10px;
}
<div class='header'>
<img class='profilePhoto' src='./userIcon'>
<div class='favourite'>Favourite</div>
<div class='brand-logo'></div>
</div>

Scroll does't work properly when aligning in the center of screen (using transform)

I'm trying to align a div in the center of the screen.
The code works great until I make the screen small, then the scrolling doesn't work properly and it cuts off the top of my centered component (when the centered component height becomes equal or less than screen size)
Any idea About a solution ?!
I'd be happy to use any other approach (without transform) but with none of the approaches I've accomplished to put the div in the center of screen)
.LoginContainer {
background-color: $base;
position: fixed;
top: 0px;
right: 0px;
bottom: 0px;
left: 0px;
width: 100%;
height: 100%;
overflow: scroll;
}
.LoginBox {
margin: 1rem;
padding: 5rem;
max-width: 30rem;
width: 60%;
position: relative;
left: 50%;
top: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
background-color: white;
}
<div class="LoginContainer">
<div class="LoginBox">
TEST
</div>
</div>
codesandbox.io/embed/suspicious-bird-hzood
I've also recreated the issue here. As you can see when the screen size has less height the white component gets clipped.
Image of the screen when the top part is clipped
Image of the regular screen
Would simply centering child components not work:
.LoginContainer {
background-color: $base;
...
display: flex;
justify-content: center;
align-items: center;
}
If you want the parent to be scrollable and the LoginComponent to be scrolled:
.LoginContainer {
background-color: $base;
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
overflow: auto;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.LoginBox {
padding: 15rem 5rem;
position: relative;
background-color: yellow;
}
<div class="LoginContainer">
<div class="LoginBox">
TEST
</div>
</div>
If you want the LoginComponent to not exceed the boundaries of the parent and make is scrollable:
.LoginContainer {
background-color: $base;
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.LoginBox {
padding: 15rem 5rem;
position: relative;
background-color: yellow;
max-height: 100%;
overflow: auto;
}
<div class="LoginContainer">
<div class="LoginBox">
TEST
</div>
</div>