I have a dashboard I am setting up that has wrapper blocks with a sprite icon that should be centered vertically and horizontally inside its parent.
I got the blocks to be placed how I want and wrapping how I want using flexbox.
I also have the icon centered horizontally using the text-align property, but for some reason justify-content: center doesn't do anything when I add it to the .view or .sprite-icon elements; adding it to the .views element also doesn't center things how I want and it breaks the padding between the .view blocks.
See my JSFiddle I set up showing the issue here: https://jsfiddle.net/efarley/k0m4nxny/
.views {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-around;
}
.view {
height: 105px;
text-align: center;
width: 105px;
}
.sprite-icon {
background-image: url('http://67.media.tumblr.com/b2336d673e315081b6d657f8258c313d/tumblr_mv98xzSiJu1qhori9o1_500.jpg');
display: inline-block;
height: 35px;
width: 35px;
}
.sprite-icon.one {
background-position: 0 0;
}
.sprite-icon.two {
background-position: 0 35px;
}
.sprite-icon.three {
background-position: 0 70px;
}
<div class='views'>
<div class='view'>
<span class='sprite-icon one'></span>
</div>
<div class='view'>
<span class='sprite-icon two'></span>
</div>
<div class='view'>
<span class='sprite-icon three'></span>
</div>
</div>
You need to make your view a flex container, then add flex centering properties.
.view {
height: 105px;
text-align: center;
width: 105px;
display: flex; /* new */
align-items: center; /* new */
justify-content: center; /* new */
}
revised fiddle
Note that the justify-content property only applies to flex containers. In your question, you were applying it to a flex item. That's why it failed.
Learn more about flex centering:
How to Center Elements Vertically and Horizontally in Flexbox
Methods for Aligning Flex Items
Try these settings:
.sprite-icon {
display: block;
position: relative;
margin: 35px auto;
background-image: url('http://67.media.tumblr.com/b2336d673e315081b6d657f8258c313d/tumblr_mv98xzSiJu1qhori9o1_500.jpg');
height: 35px;
width: 35px;
}
margin 35px auto centers it horzontally (when position is relative) and moves it down 35px, which centers it vertically, but only since display is now block. I erased text-align: center in .view
Fiddle: https://jsfiddle.net/0t4vbzxb/1/
Related
Is it possible to have the content of a div vertically centred?
<div style="padding-left: 15px; vertical-align: middle; align-content: center" >
<h3>Selections</h3>
</div>
I want the text left aligned and in the vertical middle
I gave you an example of vertical alignment using flex rules. In this case, the vertical alignment occurs with the help of an align-item: center. Also, you need space for vertical alignment.
body {
padding: 0;
margin: 0;
box-sizing: border-box;
}
div {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
<div>
<h3>Selections</h3>
</div>
Well it's an easy property you can set. You can set the text-align property. You can read more about this below.
https://www.w3schools.com/cssref/pr_text_text-align.ASP
for vertical centering:
https://www.w3schools.com/css/css_align.asp
You want to use the CSS property text-align. Also remove the inline 'style' you have on your div and use a css file
.center-content {
text-align: center;
width: 100%;
}
<div class="center-content">
hello world
</div>
If you are oke with setting the height of the h3 tag, you can do this:
h3{
height: 4%;
margin-top: 48%;
margin-bottom: 48%;
}
Otherwise do this;
h3{
margin-top: 50%;
margin-bottom: 50%;
}
I'm trying to align the two buttons to be horizontally centered, and evenly distributed vertically on the browser window like the following photo:
#buttonArea {
display: flex;
flex-direction: column;
}
#addButton {
margin: auto;
display: block;
}
#eraseButton {
margin: auto;
display: block;
}
<div id="buttonArea">
<button id="addButton">ADD</button>
<button id="eraseButton">ERASE</button>
</div>
The buttons are horizontally centered, but I don't know how to vertically align them so they are evenly distributed. (e.g. ADD: 33.33%, ERASE: 66.66%)
How can I align the buttons like my photo?
Use justify-content: space-around to distribute equal vertical space around each button and align-items: center to horizontally align the elements.
Just a side note, justify-content applies to the main axis and align-items applies to cross axis. Normally, justify-content applies to the x-axis since the default value for flex-direction is row. However, since we used flex-direction: column, the main axis is now the y-axis.
#buttonArea {
display: flex;
flex-direction: column;
justify-content: space-around;
align-items: center;
width: 100%;
height: 100vh;
border: 1px solid black;
}
<div id="buttonArea">
<button id="addButton">ADD</button>
<button id="eraseButton">ERASE</button>
</div>
HTML
<div id="Area">
<div class="container">
<button id="addButton">ADD</button>
<button id="eraseButton">ERASE</button>
</div>
</div>
CSS
#Area {
width: 100vw;
height: 100vh;
}
.container{
height: 100%;
display: flex;
flex-direction: column;
justify-content: space-around;
align-items: center
}
addButton, eraseButton{
width: 50px;
display: inline-block,
}
What I am doing is
You need to set the width the height of the page so that you can set the items in place you want.
Then will set the container of button to height: 100% because u want to align horizontally centered, and evenly distributed vertically.
Next most important thing is justify-content: space-around; Since you are using flex I hope you know this
You need to add position:relative to the parent div and then for the buttons add position: absolute; and use the top: xx %; property to align it vertically at 33% and 66% of the height.
#buttonArea {
display: flex;
justify-content: center;
align-items: center;
height: -webkit-fill-available;
width: 100%
position:relative;
text-align:centre;
}
#addButton {
display: block;
top: 33.33%;
position:absolute;
}
#eraseButton {
top: 66.66%;
position:absolute;
display: block;
}
<div id="buttonArea">
<button id="addButton">ADD</button>
<button id="eraseButton">ERASE</button>
</div>
Hope this helps. Cheers!
Use position:relative and manage the coordinates accordingly
Im trying to get an image to be in the same row as my text in my ABOUT section.
I've tried to wrap my <div class="portrait"> and <p> into a <div class="wrapper"> with css flex-direction:row
Heres a pen: https://codepen.io/anon/pen/MqRdjb
I think some CSS is being overwritten, or I'm not using my CSS properly.
Thanks for any and all help!!
flex-direction: row has no effect without including display: flex.
Adjust the wrapper css to:
.wrapper {
flex-direction: row;
display: flex;
align-items: center;
justify-content: center;
margin: 0 auto;
}
You can also refactor the margins for the elements inside the wrapper if you want to center them. Any vertical margins can be removed and applied to the wrapper itself.
.portrait{
width:100px;
height:150px;
margin-left: auto;
margin-right: 10px; /* Align your image right */
z-index: 1;
border-radius: 10%;
background-color:#555;
}
.ABOUT p{
font-size:1.2em;
text-align: center;
margin-right: auto; /* align text left */
}
I am trying to center a red box in the middle of the page.
I have set the flex container to 100% in height, and have also set the html,body to 100%, and it still does not align center.
Can anyone please help me understand why its not working? Thanks!
html, body {
height: 100%;
}
.flex-container {
display: flex;
flex-flow: column;
justify-content: center;
height: 100%;
}
.box {
flex: 0 0 100px;
background: red;
width: 100px;
}
<div class='flex-container'>
<div class='box'></div>
</div>
You use justify-content to align flex items on the main axis.
You use align-items to align flex items on the cross axis.
Since your flex container is flex-direction: column:
the main axis is vertical, and
the cross axis is horizontal.
justify-content: center is working fine.
You just need to add align-items: center.
html, body {
height: 100%;
}
.flex-container {
display: flex;
flex-flow: column;
justify-content: center; /* centers flex items vertically, in this case */
align-items: center; /* NEW */ /* centers flex items horizontally, in this case */
height: 100%
}
.box {
flex: 0 0 100px;
background: red;
width: 100px;
}
<div class='flex-container'>
<div class='box'></div>
</div>
Here's a more detailed explanation:
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
You need to add align-items to .flex-container
align-items: center;
See here for an example https://jsfiddle.net/x9gyheo6/1/
I would like to place a button in the middle of my page. What is the best way to center this button properly? steps by steps, please.
<button type="button">Welcome</button>
How can i center this to fit perfectly in the middle of my page? using the margins in CSS?
Thanks!
To centralize horizontally -Without using flexbox
See this fiddle
HTML
<button type="button">Welcome</button>
To make the button centralized, use the following CSS
button{
display: block;
margin:0 auto;
}
To centralize horizontally and vertically -using flexbox
See this fiddle
CSS
body, html {
height: 100%;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
align-items: center;
justify-content: center;
}
button{
display: flex;
margin:0 auto;
align-items:center;
}
To center both vertically and horizontally using margins and positioning, you can do it like this:
button {
height: 50px;
width: 100px;
position: absolute;
margin: -25px 0 0 -50px; //half height and half width
left: 50%;
top: 50%;
}