Text positioning for heading. learning flexbox [duplicate] - html

This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
Closed 2 years ago.
I'm trying to put the H2 above the H4.

you can use display: block;
h2, h3 {
display: block;
}
or you can use flex
<div class="box">
<h2>test</h2>
<h3>test</h3>
</div>
.box {
flex-direction: column;
}

Try to add this:
.mission {
/* ... */
flex-direction: column;
}

Explanation
Flexbox modal is unidirectional. When you define the parent element as flex, the direct children follow the main-axis or cross-axis as per the flex-direction. Therefore float, clear, vertical-align and display properties have no effect on flex items.
Solution
Add flex-direction to column as you want the content flow from top-to-bottom
.mission {
flex-direction: column;
}

Related

Positioning button [duplicate]

This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
Closed 4 months ago.
I was centering the elements of my page with:
display: block;
margin-left: auto;
margin-right: auto;
but when I try to do this with one div that has two buttons they stay in the left corner, why? and how I place them in the center.
Option 1
If both the buttons are inside the div container you also need to specify the width of the div container, because by default div covers the complete width.
div{
max-width:10rem;
margin :0px auto;
}
<div>
<button>Button1</button>
<button>Button2</button>
</div>
Option 2
You can also flex the div container to center the buttons
div{
display:flex;
align-items:center;
justify-content:center;
}
<div>
<button>Button1</button>
<button>Button2</button>
</div>
Option 3
You can also use the simple text align center property on the div container so it will center the buttons
div{
text-align:center;
}
<div>
<button>Button1</button>
<button>Button2</button>
</div>
because buttons are inline elements.
Not sure about the context but you can use this centering pattern (both horizontal and vertical) with Flexbox as well:
.container {
display: flex;
align-items: center;
justify-content: center;
}
Positioning is very easy with flexbox. Please try following properties on your div
display: flex;
justify-content: center;
align-items: center;
Justify content will place content centrally along horizontal axis and align items will place content centrally along vertical axis (for flex direction row which is default)
The div css:
text-align: center

Stuck. How do I center and space out these 3 texts? [duplicate]

This question already has answers here:
Align 3 unequal blocks left, center and right
(4 answers)
Closed 8 months ago.
Trying to left, center, right the 3 pieces of text? Trying to use CSS and I'm stuck.
Thanks.
you could use flex and set justify-content to space-between
#text-container {
display: flex;
justify-content: space-between;
}
<div id="text-container">
<p>Left</p>
<p>Middle</p>
<p>Right</p>
</div>
You could use the flex property to solve this. You would have a parent div which contains all 3 of your children items. Depending on where the item is in your HTML code, the content will justify to its respective side. So if it's the first, it will be justified to the left. If it's the second, it will be justified to the middle. And if it's the third, it will be justified to the right. Here is how you would do it;
#bar {
display: flex;
}
#bar>* {
flex: 1;
display: flex;
}
#bar> :nth-child(1) {
justify-content: flex-start;
}
#bar> :nth-child(2) {
justify-content: center;
}
#bar> :nth-child(3) {
justify-content: flex-end;
}
<div id="bar">
<p>Left</p>
<p>Middle</p>
<p>Right</p>
</div>
I agree with the other responses. Check out this really helpful/visual website on flexboxes
You want to set the parent div as display: flex and depending on what you want to do, you can set
justify-content: flex-start for left,
justify-content: flex-end to right, and
justify-content: center to center.

How to use flex to make one align on the right and another on the left - CSS? [duplicate]

This question already has answers here:
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
(6 answers)
Closed 1 year ago.
I would like to have two items aligned horizontally, but I don't know how to use flex to make them responsively separated:
<div class="container">
<div class="item-left">Left</div>
<div class="item-right">Right</div>
</div>
.container {
display: flex
}
The purpose is that no matter how I change the width of screen, they are still be separated (one on the left and another on the right). I can use grid and justify-self to achieve this, but how would I use flex to get this expected result?
Thank you!
By using justify-content: space-between:
.container {
display: flex;
justify-content: space-between;
}
<div class="container">
<div class="item-left">Left</div>
<div class="item-right">Right</div>
</div>
.container {
display: flex;
justify-content: space-between;
}
<div class="container">
<div class="item-left">Left</div>
<div class="item-right">Right</div>
</div>
Try using justify-content: space-between; for your container. It will evenly put space between your elements.
See MDN Web Docs.
It will probably break once the screen is too small for both elements to fit next to each other so you will probably have to have at least one media query that removes display: flex; or that changes the width of the two elements.

To remove space between a inline element like <span>, Is flexbox the only and better option or can we use something else? [duplicate]

This question already has answers here:
How to remove the space between inline/inline-block elements?
(41 answers)
Closed 1 year ago.
For Ex:
The below flexbox code will remove the space between two span. But is there any other better option?
#remove_space {
display: flex;
flex-direction: row;
align-content: flex-start;
}
span {
padding: 0px;
margin: 0px;
}
<section id="remove_space">
<span>Hello</span>
<span>All</span>
</section>
You can try this:
<section id="remove_space">
<span>Hello</span><span>All</span>
</section>
or
span {
float:left;
}
I hope it will help you
I don't see other solutions than with flexbox to properly fix your problem.
For your code, try using classes instead of ids, this can avoid some problems if you have more than one "space remove" block in your page.
You can also remove this part, because flex-direction's default value is already set to row, and the default value for align-content is stretch, wich keep the height and width constraints :
flex-direction: row;
align-content: flex-start;

How to make a flex item not fill the height of the flex container? [duplicate]

This question already has answers here:
How to disable equal height columns in Flexbox?
(4 answers)
Closed 5 years ago.
As you can see in the code below, the left div inside the flex container stretches to meet the height of the right div. Is there an attribute I can set to make its height the minimum required for holding its content (like usual height: auto divs outside flex containers)?
#a {
display: flex;
}
#a > div {
background-color: red;
padding: 5px;
margin: 2px;
}
#b {
height: auto;
}
<div id="a">
<div id="b">left</div>
<div>right<br>right<br>right<br>right<br>right<br></div>
</div>
The align-items, or respectively align-content attribute controls this behaviour.
align-items defines the items' positioning perpendicularly to flex-direction.
The default flex-direction is row, therfore vertical placement can be controlled with align-items.
There is also the align-self attribute to control the alignment on a per item basis.
#a {
display:flex;
align-items:flex-start;
align-content:flex-start;
}
#a > div {
background-color:red;
padding:5px;
margin:2px;
}
#a > #c {
align-self:stretch;
}
<div id="a">
<div id="b">left</div>
<div id="c">middle</div>
<div>right<br>right<br>right<br>right<br>right<br></div>
</div>
css-tricks has an excellent article on the topic. I recommend reading it a couple of times.
When you create a flex container various default flex rules come into play.
Two of these default rules are flex-direction: row and align-items: stretch. This means that flex items will automatically align in a single row, and each item will fill the height of the container.
If you don't want flex items to stretch – i.e., like you wrote:
make its height the minimum required for holding its content
... then simply override the default with align-items: flex-start.
#a {
display: flex;
align-items: flex-start; /* NEW */
}
#a > div {
background-color: red;
padding: 5px;
margin: 2px;
}
#b {
height: auto;
}
<div id="a">
<div id="b">left</div>
<div>
right<br>right<br>right<br>right<br>right<br>
</div>
</div>
Here's an illustration from the flexbox spec that highlights the five values for align-items and how they position flex items within the container. As mentioned before, stretch is the default value.
Source: W3C