Most of the things I create I need centered, but able to scale if necessary, so I use a table layout to help center content as I've done for years. However, wit the introduction of flex I believe this has been made more efficient ( less html ). I've done some research but haven't gotten to a proper cross-browser solution in the time alloted.
So very simply, I need to replace the following code:
<style>
.app-frame {position:fixed;top:0;left:0;width:100%;height:100%;}
.app-frame td {vertical-align:middle;text-align:center;}
.app-content {display:inline-block;border:1px solid gainsboro;max-width:600px;position:relative;}
</style>
<table class="app-frame">
<tr>
<td>
<div class="app-content">
<!--app-->
<div class="slide" id="slide1">
<h1>Step 1: Name your character.</h1>
<p class=info>You can change this at any time after creation.</p>
<label>Character's Name</label>
<input name="charname" />
</div>
<!--/app-->
</div>
</td>
</tr>
</table>
with a solution that allows me to use divs, like so:
<div class="app-frame">
<div class="app-content"> ... </div>
</div>
These are the settings (for the container element) with which you can center all included elements vertically and horizontally using flex:
.slide {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.slide {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
<div class="app-frame">
<div class="app-content">
<div class="slide" id="slide1">
<h1>Step 1: Name your character.</h1>
<p class=info>You can change this at any time after creation.</p>
<label>Character's Name</label>
<input name="charname" />
</div>
</div>
</div>
This is how to horizontally center a div with css flexbox:
.app-frame{
display:flex;
justify-content:center;
align-items:center;
width:100%
}
<div class="app-frame">
<div class="app-content">Centered DIV</div>
</div>
It's more flexible as you can change your layout without touching the HTML.
Related
Example Picture Don't worry about the look of it. I'm trying to align the input and the anchor horizontally with the logo using flexbox. Example attached. The logo will be on the right and the input with the anchor will be on the left but all horizontal.
<div class="small-12 cell footer-logo-container">
<a href="#">
LOGO
</a>
<div class="footer-email">
Sign Up for the Rock River Report Here
<input class="footer-input" type="email" placeholder="email">
<a href="#" class="link">
<div class="link-text">
SUBSCRIBE
</div>
</a>
</div>
Check https://css-tricks.com/snippets/css/a-guide-to-flexbox/. Useful website for most of your CSS. If I understood what you wrote correctly you could use this code:
.footer-logo-container {
display: flex;
flex-direction: row-reverse;
justify-content: space-between;
}
.footer-email {
display: flex;
}
<div class="small-12 cell footer-logo-container">
<a href="#">
LOGO
</a>
<div class="footer-email">
Sign Up for the Rock River Report Here
<input class="footer-input" type="email" placeholder="email">
<a href="#" class="link">
<div class="link-text">
SUBSCRIBE
</div>
</a>
</div>
use the following css code, it should work fine:
.footer-logo-container{
display: flex;
flex-direction: row-revers;
align-items: center;
justify-content: space-between;
}
In flexbox there is three important concepts: flex-direction, align-items and justify-contents.
flex-direction establishes the main-axis, thus defining the direction flex items are placed in the flex container.
In your case you need a flex container which should have display: flex style and you need to set align-items: center and justify-content: space-between.
You can change the order of your elements in html based on your design.
CSS:
.footer-logo-container{
display: flex;
align-items: center;
justify-content: space-between;
}
<div class="small-12 cell footer-logo-container">
LOGO
<div class="footer-email">
<p>Sign Up for the Rock River Report Here</p>
<input class="footer-input" type="email" placeholder="email">
<a href="#" class="link">
<div class="link-text">SUBSCRIBE</div>
</a>
</div>
</div>
CSS -
.footer-logo-container{
display: flex;
flex-direction: row-revers;
align-items: center;
justify-content: space-between;
}
.footer-email > p {
display: inline-block;
}
.footer-email > input {
display: inline-block;
}
.footer-email > a {
display: inline-block;
}
jsFiddle
I am making a site that will have three cards and to create them I'm using flex box. I am using justify-content: space-between which works perfectly when the three columns are all on the same row, as their margins are perfect with the container, and the space in-between them is great.
However, when the columns wrap, the column that is now on the new row is at the same left margin as the first, which is expected with space-between, but in my scenario, space-around's wrap behavior would look much better, as I have and odd number of cards.
Is there a way for me to get space-between's outer margin alignment with space-around's wrapping behavior?
Here is a codepen that gives a quick example of what I have now.
https://codepen.io/anon/pen/xdrpEo
.flex-row {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: spacing-between;
}
.file-links-flex-container {
max-width: 400px;
flex-grow: 1;
background-color: red;
}
.container-content {
justify-content: flex-start;
padding: 0 30px;
max-height: 600px;
}
<div class="flex-row space-around">
<div class="file-links-flex-container col">
<div class="container-header">
<h5>A</h5>
</div>
<div class="container-content">
<p>I AM TEST CONTENT!</P>
</div>
</div>
<div class="file-links-flex-container col">
<div class="container-header">
<h5>B</h5>
</div>
<div class="container-content">
<p>I AM TEST CONTENT!</P>
</div>
</div>
<div class="file-links-flex-container col">
<div class="container-header">
<h5>C</h5>
</div>
<div class="container-content">
<p>I AM TEST CONTENT!</P>
</div>
</div>
</div>
Try adding margin: 0 auto, You get that.
.file-links-flex-container{
margin: 0 auto;
}
I think this one helped you. :)
Even you can solve this one using justify-content:center; but margin will be the good one.
I'm trying to center two div Horizontally, and align one at the Middle of the parent container, and the second one at the Bottom of the parent container. I'm using Foundation 6, with the Flexbox version.
Html
<section id="hero">
<div class="row align-middle align-center">
<h1>Welcome</h1>
</div>
<div class="row align-bottom align-center">
<p>Some text</p>
</div>
</section>
Css
#hero {
height: 100vh;
display: flex;
flex-wrap: wrap;
}
The problem is that every div in the flex container (#hero) appear on the same line.
Thank's for helping !
Pretty sure you have to add the class column to the sub element of row like so:
id="hero">
<div class="row align-middle align-center">
<h1 class="column">Welcome</h1>
</div>
<div class="row align-bottom align-center">
<p class="column">Some text</p>
</div>
</section>
I finally found how to align vertically 3 divs using a Flexbox. Foundation wasn't really in the problem.
I made a Js Fiddle for those who have the same question:
.wrapper {
display: flex;
flex-direction: column;
height:90vh;
}
.center {
width: 100%;
flex-grow: 1;
align-items: center;
display:flex;
}
.a {
width: 100%;
}
<div class="wrapper">
<div class="a">Header</div>
<div class="center">Body</div>
<div class="a">Footer</div>
</div>
JSFiddle: https://jsfiddle.net/ek38ff5r/20/
I'm using a flex container to place a button so that it abuts an input on its right border. I can't get the input to be horizontally centered. How to center it, without eliminating the display: flex?
.flexContainer {
display: flex;
}
<div style="text-align:center">
<div>Enter the search term</div>
<div style="text-align: center">
<div class="flexContainer">
<input id="abc" />
<button id="search" name="search" type="submit">go</button>
</div>
</div>
</div>
Here's the fiddle: https://jsfiddle.net/wpd3b0kt/1/
Just add justify-content: to the CSS:
.flexContainer {
display: flex;
justify-content: center;
}
fiddle update
I have 2 threads in an imageboard stacked on top of each other, each thread has some text and an image file.
I want images to be always on right of the thread, instead of being on top of reply button.
When I float image to right by float: right; the images keep stacking on top of each other instead and form a branch like structure
How do I force images with post-image class to not break <div> structure of following thread and stay on right of screen?
<div class="you" id="thread_27" data-board="b" align="Right">
<div class="files">
<div class="file">
<p class="fileinfo">File: 1454704253855.jpg <span class="unimportant">(78.69 KB, 1024x768, <span class="postfilename">soft-color-background.jpg</span>)</span>
</p>
<img class="post-image" src="/b/thumb/1454704253855.png" style="width: 255px; height: 192px; max-width: 98%;" alt="">
</div>
</div>
<div class="post op" id="op_27">
<p class="intro">
<input class="delete" name="delete_27" id="delete_27" type="checkbox">
<label for="delete_27"><span class="name">Anonymous (You)</span>
<time data-local="true" datetime="2016-02-05T20:30:54Z">02/05/16 (Fri) 22:30:54</time>
</label> <a class="post_no" id="post_no_27" onclick="highlightReply(27)" href="/b/res/27.html#27">No.</a><a class="post_no" onclick="citeReply(27)" href="/b/res/27.html#q27">27</a>[Reply]</p>
<div class="body">xxxxxx2</div>
</div>
<br class="clear">
<hr>
</div>
<div class="you" id="thread_26" data-board="b" align="Right">
<div class="files">
<div class="file">
<p class="fileinfo">File: 1454704190918.jpg <span class="unimportant">(157.33 KB, 1024x768, <span class="postfilename" title="mac-style-presentation-background.jpg">mac-style-presentation-bac….jpg</span>)</span>
</p>
<img class="post-image" src="/b/thumb/1454704190918.png" style="width: 255px; height: 192px; max-width: 98%;" alt="">
</div>
</div>
<div class="post op" id="op_26">
<p class="intro">
<input class="delete" name="delete_26" id="delete_26" type="checkbox">
<label for="delete_26"><span class="name">Anonymous (You)</span>
<time data-local="true" datetime="2016-02-05T20:29:51Z">02/05/16 (Fri) 22:29:51</time>
</label> <a class="post_no" id="post_no_26" onclick="highlightReply(26)" href="/b/res/26.html#26">No.</a><a class="post_no" onclick="citeReply(26)" href="/b/res/26.html#q26">26</a>[Reply]</p>
<div class="body">xxxxx</div>
</div>
<br class="clear">
<hr>
</div>
when I add CSS
.post-image
{
float: right;
}
it breaks everything.
jsfiddle before
js fiddle after floating post-image to the right
This happens because of float left or right. clear controls the float behavior hence, the ideal way is to make all the float properties inside a container usually div. Sometimes, browser renders it differently so whenever coming to a new container or sibling, ideal way is to clear the float(if you don't need the floating property).
In your code you could probably
Add this:
.clear{
clear:both;
}
to better understand. Also don't forget to play with it to put it ideally where it is required.
If you can use flexbox (no way to old browsers) try something like:
.row{
display: flex;
flex-direction: row;
flex-wrap: nowrap;
justify-content: flex-start;
align-content: stretch;
align-items: flex-start;
}
.col1{
position: relative;
flex: 1 1 100%;
align-self: stretch;
overflow: hidden;
}
.col2{
position: relative;
display: flex;
flex-direction: column;
flex: 0 1 45px;
align-self: stretch;
}
on a html strukture like
<div class="row">
<div class="col1">
</div>
<div class="col2">
</div>
</div>
Add one more div with "clear: both" styling will fix your problem.
<div class="file">
your code here...
<div style="clear:both"></div>
</div>
jsfiddle