This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
Closed 2 years ago.
I'm using a flexbox and it is putting items next to each other and not centering vertically. I got the code to center from http://howtocenterincss.com. Below is my code and what I see. A solution would be appreciated.
<div style="display:flex;justify-content:center;align-items:center;">
<h1>heading</h1>
<p>content</p>
</div>
heading
content
you need to add flex-direction property
element {
display: flex;
flex-direction: column;
}
You have to add flex-direction in your container. One more important concept, you can't align your items vertically center unless you set some height to your container so also add some height in your container div as per your requirement. Also, make sure to use all browser prefixes for cross-browser compatibilities. You can add this code as an internal style or as an external stylesheet.
<style>
div{
min-height:100vh;
display:-webkit-box;
display:-webkit-flex;
display:-ms-flexbox;
display:flex;
-webkit-flex-wrap:wrap;
-ms-flex-wrap:wrap;
flex-wrap:wrap;
-webkit-box-pack:center;
-webkit-justify-content:center;
-ms-flex-pack:center;
justify-content:center;
-webkit-box-align:center;
-webkit-align-items:center;
-ms-flex-align:center;
align-items:center;
-webkit-box-orient:vertical;
-webkit-box-direction:normal;
-webkit-flex-direction:column;
-ms-flex-direction:column;
flex-direction:column;
}
</style>
These codes will center all the child elements, horizontally and vertically.
Related
I'm trying to do a front of an HTML website by using vue.js,
but I wasn't able to center an image using css.
I wrote all of my code in the App.vue file :
<template>
<div id="container3">
<img id="teamBackground" src="./assets/bourg_palette_rounded.png" alt="Bourg palette in background" width="360" height="170"/>
</div>
</template>
<style>
<!-- team -->
#container3 img{
display:block;
margin:0 auto;
}
</style>
I tried the text-align and the display-block + margin: 0 auto properties but it didn't change neither the placement of the image or the placement of other elements
Have you tried using display:flex; together with justify-content:center;?
You can also try out using position:absolute;
You can read more about image-centering methods here: https://www.freecodecamp.org/news/how-to-center-an-image-in-css/
#container3 {
display: flex;
flex-direction: column;
justify-content: center;
}
you can put those css codes to the parent div
#container3 {
display: flex;
width:100%;
height:500px;
justify-content: center;
align-items: center;
}
Flexbox is the most suitable solution for your problem.
The parent (#container3) should be a flex container. For horizontal centering we use justify-content: center & for vertical centering we use align-items: center.
Note: We must provide height to the parent for child to align in vertical direction. Also the above example is for default flex-direction ( row). For more details refer to the page https://www.digitalocean.com/community/tutorials/css-centering-using-flexbox
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
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;
}
This question already has answers here:
CSS-only masonry layout
(4 answers)
Closed 4 years ago.
This is my problem:
I have different DIVs with all the same widths but different heights.
On a large viewport these DIVs should be arranged as a grid with two columns.
The margin between the DIVs should be equal (vertically and horizontally).
Since the DIVs should be displayed in one column with the correct order on mobile it is not possible to have its own parent elements for each column.
Here is an image of what I want to achieve:
Is there any way to solve this with pure html/css?
The only solution I found so far is to use some kind of masonry javascript. But I feel like there must be a better solution...
What I've tried so far:
Using float/inline-block: I get perfect rows but 4 always starts at the same height as 3. So the margins are not equal. (See: https://codepen.io/OsmaGiliath/pen/vaPqro)
// EXAMPLE I
.parent {
width:230px;
}
.children {
display:inline-block;
width:100px;
}
Flexbox: Same (See: https://codepen.io/OsmaGiliath/pen/ajMgjR)
// EXAMPLE II
.parent {
display:flex;
flex-wrap: wrap;
}
.children {
flex:none;
}
Vertical flexbox: Works – but only with a fixed height on the parent element which is not possible in my example since this would limit the elements in the growth (See: https://codepen.io/OsmaGiliath/pen/ZjPdVx)
// EXAMPLE III
.parent {
display:flex;
flex-wrap: wrap;
flex-direction:column;
}
.children {
flex:none;
}
You can add columns that will warp up in one column if there is no enough width. This will allow you to display it as one column on mobiles. See working example here: https://codepen.io/anon/pen/BPEaXQ . You can see it working by changing the width of parent "grid" element to simulate mobiles.
<div class="grid">
<div class="column">
<div class="element higher">1</div>
<div class="element">2</div>
</div>
<div class="column">
<div class="element">3</div>
<div class="element">4</div>
</div>
</div>
.grid {
display:flex;
flex-wrap:wrap;
flex-direction:row;
margin:0 auto;
width:230px;
border:1px solid blue;
}
.column {
display: flex;
flex-direction: column;
}
.element {
width:100px;
height:140px;
margin:5px;
background: red;
}
.higher {
height:160px;
}
I finally found a solution thanks to the comment by #tobias-k.
For Desktop:
Using columnt-count: 2 on the parent element
Change the order of the 2nd and 3rd element
For Mobile:
Position the elements in a column using flexbox
Use flexbox's order to swap back the 2nd and 3rd element
https://codepen.io/OsmaGiliath/pen/vaMYPY
Thank you for all the quick responses!
I am having trouble moving one line of text below another. I have a feeling its something to do with the flexbox vertical align. I have also tried using display: block but to no avail. Please help, thank you.
The problem is the first section here:
https://jsfiddle.net/sqeeux47/
HTML
<section id="section1">
<h1 id="section1heading">Welcome to our multicultural society.</h1>
<h4 id="section1paragraph">Come worship with us.</h4>
</section>
CSS
#section1 {
background-image: url(" http://i300.photobucket.com/...");
background-repeat:no-repeat;
background-size:100%;
background-position:center;
width:100%;
height:606px;
text-align:center;
display:flex;
align-items:center;
justify-content:center;
}
Add one line to the parent container:
#section1 { flex-direction: column; }
OR
#section1 { flex-wrap: wrap; }
When you create a flex container (by declaring display: flex on an element), several default rules go into effect. Two of these rules are flex-direction: row and flex-wrap: nowrap.
This means that flex items will align horizontally on a single line, which is the issue you are facing.
In order to alter this behavior, you could change the flex-direction or allow flex items to wrap.