i need to change to layout of the buttons and the counter - css

counter.js
import "./Counter.css";
const Counter = (props) => {
return (
<div className="counter">
<h1>{`Counter ${props.count}`}</h1>
<div className="counter__buttons">
<button onClick={props.incrementCounter}>Increment</button>
<button onClick={props.decrementCounter}>Decrement</button>
</div>
</div>
);
};
Counter.css
.counter {
display: flex;
color: white;
align-items: center;
width: 100%;
height: 100%;
}
.counter > .counter__buttons > button {
color: black;
background-color: grey;
margin: 10px;
padding: 30px;
border: 0;
border-radius: 10px;
}
i want to move the buttons below counter and place the counter and buttons in the center of the page how to change it , display : flex in counter should not be removed

Would something like this work? You can set the flex-direction of a wrapping div to column and set the second div (in your case your buttons) back to flex-direction: row and finally just center it with margin: 0 auto.
<div id="wrap">
<div id="one">1</div>
<div id="two">2
<div id="three">3</div>
<div id="four">4</div>
</div>
</div>
#wrap {
display: flex;
flex-direction: column;
}
#two {
display: flex;
flex-direction: row;
margin: 0 auto;
}

Do you want something like this?
.page {
background: black;
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100vh;
}
.counter {
display: flex;
flex-direction: column; /* add this */
color: white;
align-items: center;
/* width: 100%;*/
/* height: 100%;*/
}
.counter > .counter__buttons > button {
color: black;
background-color: grey;
margin: 10px;
padding: 30px;
border: 0;
border-radius: 10px;
}
<div class="page">
<div class="counter">
<h1>Counter 5</h1>
<div class="counter__buttons">
<button onClick={props.incrementCounter}>Increment</button>
<button onClick={props.decrementCounter}>Decrement</button>
</div>
</div>
</div>
If so, you can make your whole page a flex container and use justify-content and align-items just like you did it for the .counter.
(I did HTML instead of JSX so I could add the snippet easier... don't forget to make changes in your own code)

Related

Vue-Router Flexbox not scrolling properly (or at all)

I have a component with a router-view inside it that can display a flexbox. I've tried several different solutions to flex-boxes not scrolling properly and they all either result in no scrolling, or scrolling, but it doesn't quite reach the bottom (the last element in the v-for ends up getting cut off.)
Here is the template and relevant styles for the router view
<template>
<div class="modal" ref="modal">
<div class="modalcontent" ref="content">
<p class="goback" #click="closeAnimation" ref="goback">←</p>
<div class="title">
<h1>Questions & Answers</h1>
</div>
<div class="routerview">
<router-view :posts="posts" v-slot="{ Component }">
<transition name="slide-in" mode="out-in">
<component :is="Component" />
</transition>
</router-view>
</div>
</div>
</div>
</template>
<style>
.routerview {
position: relative;
width: 80%;
margin: auto;
overflow-y: scroll;
left: 0;
right: 0;
outline: 10px red;
}
</style>
and for the flexbox:
<template>
<div class="container">
<div class="flex-container" ref="flexcontainer">
<div class="question" v-for="post in posts" :key="post._id" #click="this.$router.push(`/forum/question/${post._id}`)">
<h1 class="qTitle">{{ post.title }}</h1>
<p class="qReplyAmt">{{ post.replies.length }} repl<span v-if="!plural(post.replies.length)">y</span><span v-else>ies</span></p>
</div>
</div>
<p>You've reached the end</p>
</div>
</template>
<style scoped>
.question {
width: 98%;
height: fit-content;
min-height: 0;
background-color: whitesmoke;
border-radius: 10px;
padding: 10px 10px 2px 15px;
cursor: pointer;
transition: all 200ms ease-out;
}
.question:hover {
background-color: rgb(255, 107, 107);
color: white;
}
.qTitle {
display: block;
}
.qReplyAmt {
position: relative;
top: -5px;
}
.flex-container {
display: flex;
align-items: center;
flex-direction: column-reverse;
width: 100%;
gap: 15px;
flex: 1;
}
</style>
I've tried:
setting a height on .container, .routerview, .flex-container
setting overflow-y to auto & scroll on .container, .routerview, .flex-container
and a bunch of other answers i've found on how to fix flexboxes not scrolling
I fixed the issue. Here is the updated styles:
.flex-container {
display: flex;
align-items: center;
justify-content: space-between;
flex-direction: column-reverse;
width: 100%;
gap: 15px;
flex: 1;
margin-top: auto;
margin-bottom: 125px;
}
.container {
overflow-y: scroll;
height: 100vh;
}
I added the .container styles to make it actually scroll and added margin-bottom: 125px; to fix the issue with the last item being cut off.

How to control the space between flex items

I'm just doing some learning material on Codeacademy and I'm wanting to know how to control the space between the "locations" text and the three divs below. The assignment is asking me to create a 15px space between them but I don't know how to do that. Currently, there is just a default space that I don't know how is calculated.
html {
text-align: center;
}
.location-container {
background-image: url(https://content.codecademy.com/courses/freelance-1/unit-4/img-locations-background.jpg);
height: 700px;
width: 1200px;
display: flex;
justify-content: center;
align-content: center;
flex-wrap: wrap;
}
#local {
background-color: blueviolet;
}
.location-columns {
display: flex;
justify-content: center;
width: 100%;
gap: 30px;
color: white;
}
.locations {
background-color: black;
width: 300px;
}
<div class="location-container">
<h2 id="local">Locations</h2>
<div class="location-columns">
<div class="locations">
<h3>Downtown</h3>
<h5>384 West 4th St</h5>
<h5> Suite 108</h5>
<h5>Portland, Maine</h5>
</div>
<div class="locations">
<h3>East Baysuide</h3>
<h5>3433 Phisermans Avenue</h5>
<h5>(Northwest Corner)</h5>
<h5>Portland, Maine</h5>
</div>
<div class="locations">
<h3>Oakdale</h3>
<h5>515 Crescent Avenue</h5>
<h5> Second Floor</h5>
<h5>Portland, Maine</h5>
</div>
</div>
Thanks for any insights.
Heading elements come with large upper and lower margins right from the default browser styles.
You can remove those for elements inside .locations, make it a flex container and use row-gap to control vertical spacing:
html {
text-align: center;
}
.location-container {
background-image: url(https://content.codecademy.com/courses/freelance-1/unit-4/img-locations-background.jpg);
height: 700px;
width: 1200px;
display: flex;
justify-content: center;
align-content: center;
flex-wrap: wrap;
}
#local {
background-color: blueviolet;
}
.location-columns {
display: flex;
justify-content: center;
width: 100%;
gap: 30px;
color: white;
}
.locations {
background-color: black;
display: flex;
flex-direction: column;
row-gap: 30px;
padding: 30px;
width: 300px;
}
.locations>* { margin: 0; }
<div class="location-container">
<h2 id="local">Locations</h2>
<div class="location-columns">
<div class="locations">
<h3>Downtown</h3>
<h5>384 West 4th St</h5>
<h5> Suite 108</h5>
<h5>Portland, Maine</h5>
</div>
<div class="locations">
<h3>East Baysuide</h3>
<h5>3433 Phisermans Avenue</h5>
<h5>(Northwest Corner)</h5>
<h5>Portland, Maine</h5>
</div>
<div class="locations">
<h3>Oakdale</h3>
<h5>515 Crescent Avenue</h5>
<h5> Second Floor</h5>
<h5>Portland, Maine</h5>
</div>
</div>

Why flexbox not working responsive design with flex-direction?

i have doing my portfolio but i'm not good with CSS.
I'm using the Flexbox to do the design desktop and mobile but it not working...
It is like this, as i want, using flex-direction: column,:
Code of the div parent:
display: flex;
justify-content:center;
align-items:center;
background-color:#C4C4C4;
min-height: 100vh;
flex-direction: column;
But when i put in responsive, it stay like this:
The elements outside of div parent..
The code is the same, only changes the background-color.
background-color: red;
width:800px;
height:650px;
margin: 30px;
It not stay corrects.
If i dont use the flex-direction: column, it stay like this:
Someone why?
Your main issue was missing max-width: 100%; in the children so the width:800px would not overflow the container parent, take a look at the snippet
section {
display: flex;
justify-content: center;
align-items: center;
background-color: #C4C4C4;
min-height: 100vh;
padding: 15px 30px;
box-sizing: border-box;
}
#media(max-width:800px) {
section {
flex-direction: column;
}
}
div {
max-width: 100%;
width: 800px;
height: 650px;
margin: 15px
}
div:first-of-type {
background-color: red;
}
div:last-of-type {
background-color: blue
}
<section>
<div>red</div>
<div>blue</div>
</section>
max-width not set the width of the children elements.
Make sure you set a width to all of your containers; it looks like you want the gray container to fill the viewport, and the blocks to be evenly distributed.
Here's a working example:
.container {
display: flex;
flex-direction: column;
justify-content: space-evenly;
align-items: center;
text-align: center;
background-color: gray;
width: 100vw;
height: 100vh;
}
.block {
background-color: #C4C4C4;
min-height: 33vh;
width: 90vw;
}
<div class="container">
<div class="block" style="background-color: red">
A
</div>
<div class="block" style="background-color: blue">
B
</div>
</div>

Flex item same width as children

I'm trying to make a container element, a flex item, the same width as it's span text child. As you can see by inspecting the result from the snippet, although the container .calc-text-container takes up more space than the child element does, even though the flex-grow-property is set to 0.
Is there a way to achieve the desired result?
.calc {
width: 230px;
background: green;
height: 100px;
display: flex;
justify-content: center;
flex-direction: column;
}
.calc a {
display: flex;
justify-content: center;
}
.calc-icon-container {
margin-right: 1em;
}
.calc-text-container {
flex: 0 0 0;
background: rgba(20,200,0,0.8);
}
<div class="calc">
<a href="#">
<div class="calc-icon-container"><span class="calc-icon">🔫</span></div>
<div class="calc-text-container"><span class="calc-text">To do something megagiga</span></div>
</a>
</div>
Just flex: 0 0 0; will do the job.
.calc {
width: 230px;
background: green;
height: 100px;
display: flex;
justify-content: center;
flex-direction: column;
}
.calc a {
display: flex;
justify-content: center;
}
.calc-icon-container {
margin-right: 1em;
}
.calc-text-container {
flex: 0 0 0; // or just flex: 0;
background: rgba(20,200,0,0.8);
}
<div class="calc">
<a href="#">
<div class="calc-icon-container"><span class="calc-icon">🔫</span></div>
<div class="calc-text-container"><span class="calc-text">Reallylongword Short shortandlong</span></div>
</a>
</div>

Align element to the second column

I have the following layout:
#container {
display: flex;
flex-wrap: wrap;
}
#a {
flex-basis: 100%;
height: 100px;
}
#b {
flex-grow: 1;
height: 150px;
}
#c {
flex-grow: 2;
height: 100px;
}
/* Less relevant styles */
#a, #b, #c {
font-family: sans-serif;
font-size: 3em;
color: white;
text-shadow: 1px 1px 3px grey;
display: flex;
justify-content: center;
align-items: center;
}
#a {
background-color: #FFC300;
}
#b {
background-color: #FF5733;
}
#c {
background-color: #C70039;
}
<div id="container">
<div id="a">A</div>
<div id="b">B</div>
<div id="c">C</div>
</div>
How can I change the A element so that instead of taking up all the horizontal space, it's aligned to the C element? Basically I want to achieve something like that:
Note that I don't want to set something like margin-left: 33% on the A element, because I don't know if the B element will always take up one third of the horizontal space. I want the A element to remain aligned to C even if the width of B or C changes.
If possible, I want to achieve this using flexbox, but I'm open to workarounds.
Wrapping nested flexboxes and justify-content:flex-end is one option.
#container {
display: flex;
}
[class^="wrap"] {
display: flex;
flex-direction: column;
justify-content: flex-end;
}
/* Less relevant styles */
#a,
#b,
#c {
height: 100px;
font-family: sans-serif;
font-size: 3em;
color: white;
text-shadow: 1px 1px 3px grey;
display: flex;
justify-content: center;
align-items: center;
}
#a {
background-color: #FFC300;
}
#b {
background-color: #FF5733;
}
#c {
background-color: #C70039;
}
.wrap-left {
flex: 1;
}
.wrap-right {
flex: 2;
}
<div id="container">
<div class="wrap-left">
<div id="b">B</div>
</div>
<div class="wrap-right">
<div id="a">A</div>
<div id="c">C</div>
</div>
</div>
This sort of layout is standard for a table element (especially because of the variable width of each column). Something to consider, if possible.
Basically, you need to force "A" and "C" into the same column so they can track each other's width.
One method would be to force "B" to consume all space in the first column. This can be done by nesting "B" in a container with 100% height, then aligning "B" to the bottom half.
#container {
display: flex;
flex-flow: column wrap;
height: 200px;
}
nested-container {
flex-basis: 100%;
display: flex;
align-items: flex-end;
}
#b {
flex: 1;
background-color: #FF5733;
}
#a {
background-color: #FFC300;
}
#c {
background-color: #C70039;
}
#a, #b, #c
{
height: 100px;
font-family: sans-serif;
font-size: 3em;
color: white;
text-shadow: 1px 1px 3px grey;
display: flex;
justify-content: center;
align-items: center;
}
<div id="container">
<nested-container>
<div id="b">B</div>
</nested-container>
<div id="a">A</div>
<div id="c">C</div>
</div>
You can group your boxes this way:
<div id="container">
<div class="first-col">
<div id="empty"></div>
<div id="b">B</div>
</div>
<div class="second-col">
<div id="a">A</div>
<div id="c">C</div>
</div>
</div>
and style their width togehter by column, but their color and content by id

Resources