Why do I cannot center elements if i scale them - html

I am using this to center things in CSS:
.testclass {
display: flex;
justify-content: center;
}
but when i want to scale elements using width and height, it doesn't work and my elements are not centered.
Like this:
.testclass {
display: flex;
justify-content: center;
width: 200px;
height: 200px;
}
What's the problem?

This looks like the expected behavior.
Remember that in this case justify-content: center; centers what is inside the container - not the container itself.
EDIT:
I added margin: 0 auto; to center the container.
#container1 {
display: flex;
justify-content: center;
border: 1px solid red;
}
#container1 > div {
border: 1px solid blue;
background: yellow;
}
#container2 {
display: flex;
justify-content: center;
border: 1px solid red;
width: 200px;
height: 200px;
margin: 0 auto;
}
#container2 > div {
border: 1px solid blue;
background: yellow;
}
<div id="container1">
<div>test 1</div>
</div>
<div id="container2">
<div>test 2</div>
</div>

display: flex; and justify-content: center;
works for parent elements. That is, child elements of that particular parent will be centered, not the parent.
To center .testclassHTML
<div class="parent">
<div class="testclass"></div>
</div>
CSS
.parent {
background-color: red;
display: flex;
justify-content: center;
}
.testclass {
background-color: green;
width: 200px;
height: 200px;
}

If you want full center (horizontal vertical) you can use this code:
.container {
position: relative;
width: 100%;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
<div class="container">
<div class="testclass">Content</div>
</div>

Related

Is there a way to get a <div> in the same line of another <div>?

So I have 2 <div> tags in my body, and I want them both to be in one line. However, it automatically makes a line break. Is there a way to fix this?
.firstDiv {
display: flex;
justify-content: center;
align-items: center;
width: 100px;
height: 150px;
border-radius: 30%;
background-color: grey;
}
.secondDiv {
display: flex;
justify-content: center;
align-items: center;
width: 100px;
height: 150px;
border-radius: 30%;
background-color: grey;
margin-left: 10px;
}
.mainDiv {
display:flex;
flex-direction: row;
}
<div class="mainDiv">
<div class="firstDiv">
First Div
</div>
<div class="secondDiv">
Second Div
</div>
</div>
Use display inline block
.content {
width: 100px;
height: 100px;
display: inline-block;
background-color: black;
}
<div class="content">Content</div>
<div class="content">Content</div>

Div with vertical and horizontal text inside

I need to reproduce this div:
It simply has a circle (an emoji for semplicity) on the top, and two texts on the bottom.
I try this but it doesn't work as I expect
.container {
width: 70px;
height: 400px;
background-color: lightgray;
display: flex;
flex-wrap: wrap;
align-content: space-between;
justify-content: center;
}
.circle {
}
.bottom-container {
display: flex;
flex-direction: column;
}
.artist {
border: 1px solid red;
writing-mode: vertical-rl;
transform: rotate(180deg);
}
.number {
border: 1px solid lime;
}
<div class="container">
<div class="circle">🔴</div>
<div class="bottom-container">
<div class="artist">Artist - Never forget my love</div>
<div class="number">03</div>
</div>
</div>
What's the problem?
You almost had it. Added flex-direction: column and switch your align content and justify content on your container div.
.container {
width: 70px;
height: 400px;
background-color: lightgray;
display: flex;
flex-wrap: wrap;
flex-direction: column;
align-content: center;
justify-content: space-between;
}
.circle {
}
.bottom-container {
display: flex;
flex-direction: column;
}
.artist {
border: 1px solid red;
writing-mode: vertical-rl;
transform: rotate(180deg);
}
.number {
border: 1px solid lime;
}
<div class="container">
<div class="circle">🔴</div>
<div class="bottom-container">
<div class="artist">Artist - Never forget my love</div>
<div class="number">03</div>
</div>
</div>
The circle div should take full space. Adding width and text-align to the circle class did the job:
.circle {
width: 100%;
text-align: center;
}

How do I create space between elements in flexbox?

I'm trying to create a layout for this website using flexbox but the justify-content property doesn't seem to be working. Ultimately, I want it to look like this:
I have the grid laid out correctly but can't get space between elements at this point. This is what I've got right now:
How do I align my content correctly? Here is my codepen: https://codepen.io/caseycling/pen/poNXRXZ
.service-container {
display: flex;
justify-content: center;
}
.img {
margin: .5rem;
border: 1px solid grey;
min-width: 100px;
}
.container>div {
border: 1px solid grey;
min-height: 100px;
min-width: 100px;
text-align: center;
}
.container {
display: flex;
flex-wrap: wrap;
justify-content: space-around; //Not working
}
.service {
flex: 40%;
margin: .5rem;
}
<div class='service-container'>
<div class='img'>IMG</div>
<div class='container'>
<div class='service'>Service</div>
<div class='service'>Service</div>
<div class='service'>Service</div>
<div class='service'>Service</div>
</div>
</div>
I think it's because you attach space-around in wrong place. Now you have it on container which is only the left rectangle. This property will affect everything inside but not outside. You should add this prop to the highest wrapper which in this case is service-container
.service-container {
display: flex;
justify-content: space-around; // working here
}
Your primary flex container has justify-content: center applied.
You don't want to center the items, it seems. You want them separated like in your image.
So use justify-content: space-between.
But if you still want the layout centered, set a width to the container and use the margin property.
.service-container {
display: flex;
justify-content: space-between;
width: 800px;
margin: 0 auto;
}
.img {
margin: .5rem;
border: 1px solid grey;
min-width: 100px;
}
.container>div {
border: 1px solid grey;
min-height: 100px;
min-width: 100px;
text-align: center;
}
.container {
display: flex;
flex-wrap: wrap;
justify-content: space-around; //Not working
}
.service {
flex: 40%;
margin: .5rem;
}
<div class='service-container'>
<div class='img'>IMG</div>
<div class='container'>
<div class='service'>Service</div>
<div class='service'>Service</div>
<div class='service'>Service</div>
<div class='service'>Service</div>
</div>
</div>
Add margin-right to .img
.img {
margin: .5rem;
border: 1px solid grey;
min-width: 100px;
margin-right: 100px; // Increase space between image and servies
}

Flex Box Behavior

I cannot understand WHY I am not getting this:
.container {
width: 600px;
height: 400px;
border: 3px solid green;
}
.cg-panel {
display: flex;
flex-flow: column;
align-items: stretch;
justify-content: center;
}
.cg-panel .content {
flex: 1;
background-color: tomato;
}
<div class="container">
<div class="cg-panel">
<div class="content"></div>
</div>
</div>
I, for the life of me, cannot understand why the content panel does not vertically stretch the entire container. What is the purpose of "flex:1" if it isn't going to work? Am I not reading the documentation correctly?
There's nothing in your CSS that is expanding the height of .cg-panel to fit its parent .container.
Adding height: 100%; to .cg-panel fixes this:
.container {
width: 600px;
height: 400px;
border: 3px solid green;
}
.cg-panel {
display: flex;
flex-flow: column;
align-items: stretch;
justify-content: center;
height: 100%; /* add this */
}
.cg-panel .content {
flex: 1;
background-color: tomato;
}
<div class="container">
<div class="cg-panel">
<div class="content"></div>
</div>
</div>

Center child divs inside parent div

I have a parent div that must stay at 100% with 3 child divs inside. I need to center the 3 child divs, but don't know how.
.parent {
width: 100%;
border: 1px solid blue;
}
.child {
float: left;
border: 1px solid red;
margin: 2px;
}
<div class="parent">
<div class="child">child1</div>
<div class="child">child2 - center us child divs! :)</div>
<div class="child">child3</div>
<div style="clear: both"></div>
</div>
Here is a JSFIddle:
http://jsfiddle.net/qdHH3/2/
Try using display: inline-block and text-align: center
.parent {
width: 100%;
border: 1px solid blue;
text-align: center;
}
.child {
display: inline-block;
border: 1px solid red;
margin: 2px;
}
jsFiddle: http://jsfiddle.net/qdHH3/3/
Flex solution: set justify-content: center; to the parent element:
.parent {
display: flex;
justify-content: center;
/* align-items: center; /* To align vertically, if needed */
}
.parent, .child { border: 1px solid #000; }
<div class="parent">
<div class="child">child1</div>
<div class="child">child2 - center us child divs! :)</div>
<div class="child">child3</div>
</div>
.child {
$box-size: 100%;
width: $box-size;
height: $box-size;
display: flex;
justify-content: center;
align-items: center;
}