I'm new to CSS , can anyone help me with the layout. Below is the layout I have
I want the green button to be center and in the bottom of its container div (the white area) and I'm using flexbox. Below is my code:
//html
<div className="box-layout">
<div className="box-layout__box">
<h1 className="box-layout__title">Expensify</h1>
<p>It's time to get your expenses under control</p>
<button className="button" onClick={startLogin}>Login with Google</button>
<button className="button_anon" onClick={startLoginAnonymously}>Try it</button>
</div>
</div>
and css
.box-layout {
background: url('/images/bg.jpg');
background-size: cover;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
width: 100vw;
}
.box-layout__box{
background: fade-out(white, .15);
border-radius: 3px;
padding: $l-size $m-size;
text-align: center;
width: 25rem;
}
.button {
background: $blue;
border: none;
color: white;
font-weight: 300;
font-size: $font-size-large;
padding: $s-size;
}
.button_anon {
background: green;
border: none;
color: white;
font-weight: 300;
font-size: 1.5rem;
padding: 0.8rem;
}
I belive code below should do what you want
.box-layout__box{
background: fade-out(white, .15);
border-radius: 3px;
padding: $l-size $m-size;
text-align: center;
width: 25rem;
flex-direction: column;
justify-content: center;
}
There is pretty nice article about flexbox and how to use it
Try the code <br>right above the green button. It seems that it should be already centered. Otherwise wrap it around a div, and align the div to center.
Related
I am trying to fix an svg icon in the bottom right corner of my hero banner (using flexbox). I am struggling to pin it into the correct position and also need to be able to adjust its position relative to the text and button (its a responsive website and I need to adjust based on screen size). I tried to adjust with the css margin property (left and right), but it does not work well.
.hero {
display: flex;
flex-direction: column;
justify-content: space-between;
height: 100vh;
padding: 1em;
box-sizing: border-box;
color: #333333;
background: url(https://images.unsplash.com/photo-1500417148159-68083bd7333a) center center no-repeat;
background-size: cover;
}
.hero-title {
max-width: 17em;
margin: 0;
font-size: 8vh;
font-weight: 100;
line-height: .9;
padding-left: 93px;
padding-top: 150px;
text-transform: none;
color: white;
}
.hero-subtitle {
max-width: 23em;
margin: 0;
font-size: 2vh;
font-weight: 100;
line-height: 1.3;
padding-left: 100px;
padding-bottom: 100px;
padding-top: 60px;
color: white;
}
.hero-footer {
display: flex;
margin-bottom: 2.5em;
}
/* button */
.button-primary {
color: red;
background-color: transparent;
padding: 8px 25px;
margin-left: 100px;
margin-bottom: 350px;
text-decoration: none;
border: .1em solid red;
font-size: 12px;
}
.button-primary:hover {
color: #ffffff;
background-color: #333333;
border: .1em solid #ffffff;
}
#iconheader {
display: flex;
flex-direction: column;
align-items: flex-end;
}
#myicon {
text-decoration: none;
font-size: 5vh;
font-weight: bold;
background: url(../images/test_icon.svg) 50% 50% no-repeat;
color: white;
}
<section class="hero">
<header id="header">
</header>
<header class="hero-header">
<h1 class="hero-title">Wonderful Day<br>Amazing Forum<br>Great Friends</h1>
</header>
<header class="hero-header">
<h2 class="hero-subtitle">Stackoverflow is the #1 forum among developers. Just ask anyone. </h2>
</header>
<footer class="hero-footer">
<a class="button-primary" href="#">Learn More</a>
<div id="iconheader">
<a id="myicon" href="#">Icon</a>
</div>
</footer>
</section>
Any help or suggestions would be greatly, greatly appreciated! Thank you.
Set position absolute and zindex high
.hero {
position: relative;
}
#iconheader {
position: absolute;
right: 0;
bottom: 0;
z-index:99999
}
for set a svg icon bottom in right corner use this css rules instead of yours:
#iconheader {
position: absolute;
right: 0;
bottom: 0;
}
in this way, the svg is always there
I was wondering if there is an alternative method to achieving the results shown in the attached picture. I used absolute positioning to get .resume-icon-container to sit flush with .resume-container.
Every time I tried to add padding or height/width to .resume-icon-container it would undesirably resize .resume-container. I experimented with overflow: auto and the z-index but the only way I could achieve the results I want is with absolute positioning and adding margin-left to position it then padding and font-size to make it flush with .resume-container. I was browsing similar questions as well and someone said to add box-sizing: border-box but I already declared that setting in my CSS reset under the * selector.
I would prefer to stay away from absolute positioning for responsive purposes, so I was wondering if there is another way to achieve what I want.
This is the desired result:
.resume-container {
display: flex;
flex-direction: row;
background: rgba(144, 144, 144, 0.3);
margin-top: 20px;
border-radius: 20px;
padding: 20px;
width: 350px;
margin-left: auto;
margin-right: auto;
align-items: center;
justify-content: space-between;
}
.resume-container h1 {
color: #fff;
font-size: 25px;
}
.resume-icon-container {
background: rgba(196, 196, 196, 0.3);
padding: 20px;
position: absolute;
margin-left: 268px;
border-radius: 20px;
font-size: 10px;
}
.resume-icon-container i {
color: #fff;
}
<div class="resume-container">
<h1>Download Resume</h1>
<div class="resume-icon-container">
<i class="fa-solid fa-file fa-3x"></i>
</div>
</div>
Remove the absolute positioning and padding and use margin-left on your h1.
.resume-container {
display: flex;
flex-direction: row;
background: rgba(144, 144, 144, 0.3);
margin-top: 20px;
border-radius: 20px;
width: 350px;
margin-left: auto;
margin-right: auto;
align-items: center;
justify-content: space-between;
}
.resume-container h1 {
color: #fff;
font-size: 25px;
margin-left: 1em;
}
.resume-icon-container {
background: rgba(196, 196, 196, 0.3);
padding: 20px;
border-radius: 20px;
}
.resume-icon-container i {
color: #fff;
width: 100%;
}
<script src="https://kit.fontawesome.com/6140596fcb.js" crossorigin="anonymous"></script>
<div class="resume-container">
<h1>Download Resume</h1>
<div class="resume-icon-container">
<i class="fa-solid fa-file fa-3x"></i>
</div>
</div>
The way you are tuning those margins and paddings, you will always be looking at something different depending on the size of the screen. You need to use relative positioning so that the items appear on the screen the same regardless of the number of pixels. It can also get confusing to mix margin with padding. Margin will push the element from its nearest element while padding will push elements inside that element away from the left,top,etc.
I like to start by creating a container for each element so that we can design each new div element like its own page.
Consider the following code:
<div id="View">
<div id="OptionBlock">
<div id="Options1">
<div id="AddDocument" class="options">Add New Document<div id="DocumentIcon"></div></div>
<div id="AddTemplate" class="options">Add New Template<div id="TemplateIcon"></div></div>
</div>
<div id="Options2">
<div id="ChangeSignature" class="options">Change Your Signature<div id="SignatureIcon"></div></div>
<div id="Settings" class="options">Settings and Subscription<div id="SettingsIcon"></div></div>
</div>
</div>
</div>
#View {
width: 100%;
height: 60%;
}
#OptionBlock {
width: 100%;
}
#Options1 {
width: 100%;
float: left;
}
#Options2 {
width: 100%;
float: left;
}
#AddDocument {
float: left;
padding: 5px;
width: 25%;
height: 38%;
margin-left: 24%;
margin-right: 2%;
margin-top: 2%;
text-align: center;
border: 2px solid black;
border-radius: 25px;
background-color: #ffffff;
font-size: x-large;
font-weight: bold;
}
#AddTemplate {
float: left;
padding: 5px;
width: 25%;
height: 38%;
margin-top: 2%;
margin-right: 24%;
text-align: center;
border: 2px solid black;
border-radius: 25px;
background-color: #ffffff;
font-size: x-large;
font-weight: bold;
}
Notice how I treat the outer boxes as large containers, defining all the total width and height we need, then leaving it to the css for particular elements showing content to position themselves within that container on the screen. The width and left and right margins of elements #AddDocument and #AddTemplate add up to 100% width so that the entire box it is placed in is accounted for.
Preview CSS Placements (this renders dead center at the top of the webpage)
It's just a matter of playing with the css.
For this kind of "trial and error" problem you should use CodePen or similar. It'll make your life much easier.
.resume-container {
display: flex;
flex-direction: row;
background: rgba(144, 144, 144, 0.3);
margin-top: 20px;
border-radius: 20px;
/* padding: 20px;*/
width: 350px;
margin-left: auto;
margin-right: auto;
align-items: center;
justify-content: space-between;
}
.resume-container h1 {
color: #fff;
font-size: 25px;
padding: 20px;
margin: 0;
}
.resume-icon-container {
background: rgba(196, 196, 196, 0.3);
padding: 20px;
float: right;
/*margin-left: 268px;*/
border-radius: 20px;
/*font-size: 10px;*/
height: 100%;
}
.resume-icon-container i {
color: #fff;
}
.bi {
font-size: 2rem;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons#1.8.1/font/bootstrap-icons.css" rel="stylesheet" />
<div class="resume-container">
<h1>Download Resume</h1>
<div class="resume-icon-container">
<i class="fa-solid fa-file fa-3x"></i>
<i class="bi bi-file-earmark-text"></i>
</div>
</div>
This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
Closed 12 months ago.
I know this is a generic question, but I looked everywhere and couldn't find anything, here's my code :
<a href='/Customers' class='centre'>Start</a>
I tried encasing this in a div tag too but couldn't get that to work. I also want a small grey box around it as a background.
my css code:
a {
width: 100%;
height: 250px;
display: inline-block;
}
.centre {
text-align: center;
display: inline-block;
font-size: 20px;
font-family: 'Roboto', sans-serif;
background-color: grey;
}
Use flex. Also, use from 4 code lines specified for aligned vertically:
*{
box-sizing: border-box;
}
body{
margin: 0;
}
div{
width: 100%;
height: 200px;
background-color: grey;
display: flex;
align-items: center;
justify-content: center;
position: absolute; /*here*/
top: 50%; /*here*/
transform: translateY(-50%); /*here*/
margin: 0; /*here*/
}
.centre {
text-align: center;
line-height: 150px; /*for aligning of text vertically in anchor tag*/
background-color: red;
width: 90%;
display: inline-block;
font-size: 20px;
font-family: 'Roboto', sans-serif;
text-decoration: none;
color: white;
}
<div>
<a href='/Customers' class='centre'>Start</a>
</div>
For more knowing about flex, see the first comment.
With your given code snippet you can do it like below.
Just change display: inline-block to display: flex and add those attirbutes:
justify-content: center;
align-items: center;
.linkContainer {
display: flex;
justify-content: center;
width: 100%;
}
.button {
border-radius: 5px;
text-align: center;
width: 75px;
padding: 5px 10px;
background-color: grey;
}
.button a {
color: white;
font-size: 20px;
font-family: 'Roboto', sans-serif;
text-decoration: none;
}
<div class="linkContainer">
<div class="button">Start</div>
</div>
I did edit this code. Your link is now a "button", so it might be easier for you to understand.
But I'd recommend you to learn a bit on your own: https://developer.mozilla.org/en-US/docs/Learn/Getting_started_with_the_web/HTML_basics
I need to move form input element from left to the center of div element.
I tried to solve this problem by margin: auto and position: relative.
The second way works, but there is a small problem. When I change the size
of the browser window, the input element changes its position. How to avoid it
by solving the first problem at the same time?
Screenshot
#stretch5 {
display: flex;
justify-content: center;
align-items: center;
height: 30em;
background-image: url(img/newsletter.jpg);
background-size: cover;
background-repeat: no-repeat;
background-position: 50% 50%;
}
.frame {
position: relative;
display: inline-block;
height: 25em;
top: 50px;
bottom: 100px;
margin: auto;
width: 100%;
border: 5px solid #fff;
margin-left: auto;
margin-right: auto;
transform: scale(0.8975, 0.8975) translate(-68.5808px, -31.2925px);;
}
.frame h2 {
font-size: 1.4em;
}
.frame input[type=submit] {
padding: 5px 15px;
background: #f7e0cd;
text-transform: uppercase;
border: 0 none;
display: inline-block;
margin: auto;
outline: 0;
cursor: pointer;
width: 2em;
}
<div id="stretch5" class="newsletter-opt-in">
<div class="frame">
<h2>Join our<span class="vip">VIP list</span></h2>
<p class="vip-list-paragraph">For the exclusive tips, free resources and best tips</p>
<form action="#">
<input type="submit" value="SUBSCRIBE">
</form>
</div>
</div>
Try in .frame input to change the element display to flex;. So the button is displayed in the middle of the page even if the page is reduced
.frame input[type=submit] {
padding: 5px 15px;
background: #f7e0cd;
text-transform: uppercase;
border: 0 none;
display: flex;
margin: auto;
outline: 0;
cursor: pointer;
width: 2em;
}
I hope it will solves your problem
Just apply text-align:center to the form.
#stretch5 {
display: flex;
justify-content: center;
align-items: center;
height: 30em;
background-image: url(img/newsletter.jpg);
background-size: cover;
background-repeat: no-repeat;
background-position: 50% 50%;
}
.frame {
height: 25em;
border: 5px solid #fff;
border: 1px solid red;
display: flex;
flex-direction: column;
}
.frame h2 {
font-size: 1.4em;
}
form {
text-align: center;
}
.frame input[type=submit] {
padding: 5px 15px;
background: #f7e0cd;
text-transform: uppercase;
outline: 0;
cursor: pointer;
}
<div id="stretch5" class="newsletter-opt-in">
<div class="frame">
<h2>Join our<span class="vip">VIP list</span></h2>
<p class="vip-list-paragraph">For the exclusive tips, free resources and best tips</p>
<form action="#">
<input type="submit" value="SUBSCRIBE">
</form>
</div>
</div>
This question already has an answer here:
Why is a flex item limited to parent size?
(1 answer)
Closed 3 years ago.
I'd like to make circle buttons. In this snippet, when the screen is narrow so that both buttons don't fit completely, button A begins to squash, while button B is still a circle (what I want). Button B is wrapped with a div, button A is not.
Two questions:
a) Why simply wrapping button B with a div makes it behave differently?
b) How, if possible, can I get the desired behaviour (button B) without the extra div?
.counter {
display: flex;
margin-top: 10pt;
background-color: #444444;
justify-content: space-around;
align-items: center;
border-radius: 60pt;
width: 100%;
padding: 5pt;
}
button {
outline: none;
}
.btn {
background-color: #222222;
border-radius: 50%;
border: 1pt solid white;
width: 50pt;
height: 50pt;
display: inline-block;
}
.btn-text {
color: white;
font-size: 14pt;
text-align: center;
}
.btn:active {
background-color: #444444;
}
<div class="counter">
<button class="btn"><span class="btn-text">A</span></button>
<div class="btn-div">
<button class="btn"><span class="btn-text">B</span></button>
</div>
</div>
https://jsfiddle.net/njto340f/3/
It is because the width is adjusting with the container, making it compress too.
You must set min-width and min-height to make sure that the width wouldn't go below your desired width and prevent it from shrinking
.counter {
display: flex;
margin-top: 10pt;
background-color: #444444;
justify-content: space-around;
align-items: center;
border-radius: 60pt;
width: 100%;
padding: 5pt;
}
button {
outline: none;
}
.btn {
background-color: #222222;
border-radius: 50%;
border: 1pt solid white;
min-width: 50pt;
min-height: 50pt;
display: inline-block;
}
.btn-text {
color: white;
font-size: 14pt;
text-align: center;
}
.btn:active {
background-color: #444444;
}
<div class="counter">
<button class="btn"><span class="btn-text">A</span></button>
<div class="btn-div">
<button class="btn"><span class="btn-text">B</span></button>
</div>
</div>
Check this fiddle
Source: min/max-width vs width