Div boxes vertically inline - javascript

I don't know, how to solve my following css problem, perhaps anybody can help:
I don't know the height of the items, but the width should be 33%. If the max-height of the parent Div box ist reached, the next element should float to the right.
Thanks.

Use Flexbox columns.
https://jsfiddle.net/kirandash/azqjg2kb/
HTML:
<ul>
<li>a</li>
<li>b</li>
<li>c</li>
<li>d</li>
<li>e</li>
<li>f</li>
</ul>
CSS:
ul {
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 306px;
width: 200px;
}
li {
width: 100px;
height: 100px;
background:red;
color: white;
text-align: center;
font-size: 50px;
line-height: 100px;
font-weight: bold;
border: 1px solid white;
list-style: none;
}
I have created an unordered list which is set as a flex container with a column direction, and allowed wrapping.
https://jsfiddle.net/kirandash/azqjg2kb/

You can try CSS columns. They are a little bit tricky to use for anything complex but would do what you are asking for.
Here is a pen but the most important part is outlined below. On the container element, declare the amount of columns, since you want each item to be 33% width, I've put 3 columns. column-gap is the space between each column. After that it's important to put each child element to width: 100% as this is percent width of the column, and display: inline-block.
.container {
max-height: 400px;
columns: 3;
column-gap: 1rem;
}
.box {
width: 100%;
display: inline-block;
}
Read more here
Browser support is here

Hope this helps you. I'm not sure about the structure you have. I just created with dummy structure
.flex-container {
display: flex;
flex-direction: column;
flex-wrap: wrap;
justify-content: flex-start;
align-content: flex-start;
align-items: flex-start;
width: 600px;
max-height: 250px;
}
.flex-item {
order: 0;
flex: 1 1 auto;
align-self: flex-start;
width: 33.33%;
text-align: center;
border: 1px solid #777;
margin-bottom: 10px;
}
<div class="flex-container">
<div class="flex-item"> Element 1<br><br><br><br></div>
<div class="flex-item">Element 2</div>
<div class="flex-item">Element 3</div>
<div class="flex-item">Element 4</div>
<div class="flex-item">Element 5</div>
<div class="flex-item">Element 6</div>
<div class="flex-item">Element 7</div>
<div class="flex-item">Element 8</div>
</div>

.inline{width:100px;background:yellow;white-space:nowrap;display:inline;}
Content forContent forContent for

Related

Flex container: Is there a way stretch the first item's height when container wraps

div {
border: 1px solid #d3d3d3;
}
.flex-container {
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
}
.nowrap {
flex-wrap: nowrap;
}
.item {
height: 100px;
width: 100px;
}
<div class="flex-container">
<div class="flex-container nowrap">
<div class="item">
item 1
</div>
<div class="item">
item 2
</div>
</div>
<div class="item">
item 3
</div>
</div>
Is there a way to make something like this work when the items wrap? Now item-3 wraps exactly below item-1. I want it to wrap below item-2
Fiddle Link: https://jsfiddle.net/kmajqrsx/
You can try achieve this with margin-left: 100px, where 100px is width of the .item and add it to item 3.
*,
::after,
::before {
margin: 0;
padding: 0;
box-sizing: border-box;
}
div {
border: 1px solid #d3d3d3;
}
.flex-container {
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
}
.nowrap {
flex-wrap: nowrap;
}
:root {
--val: 100px;
}
.item {
height: var(--val);
width: var(--val);
}
.flex-container ~ .item {
margin-left: calc(var(--val) + 2px);
}
<div class="flex-container">
<div class="flex-container nowrap">
<div class="item">item 1</div>
<div class="item">item 2</div>
</div>
<div class="item">item 3</div>
</div>
The second solution looks almost the same as MaxiGui, but we create a breakpoint with a media query and set min-content as the width.
*,
::after,
::before {
margin: 0;
padding: 0;
box-sizing: border-box;
}
div {
border: 1px solid #d3d3d3;
}
.flex-container {
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
}
.nowrap {
flex-wrap: nowrap;
}
:root {
--val: 100px;
}
.item {
height: var(--val);
width: var(--val);
}
/* 404px is 100px * 2 + 2px lefr-border + 2px right-border */
#media screen and (max-width: 404px) {
.flex-container {
width: min-content;
background-color: aquamarine;
}
}
.flex-container ~ .item {
margin-left: auto;
}
<div class="flex-container">
<div class="flex-container nowrap">
<div class="item">item 1</div>
<div class="item">item 2</div>
</div>
<div class="item">item 3</div>
</div>
Add margin-left: auto; to last item:
DEMO
div {
border: 1px solid #d3d3d3;
}
.flex-container {
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
}
.nowrap {
flex-wrap: nowrap;
}
.item {
height: 100px;
width: 100px;
}
.flex-container > .item{
margin-left:auto;
}
<div class="flex-container">
<div class="flex-container nowrap">
<div class="item">
item 1
</div>
<div class="item">
item 2
</div>
</div>
<div class="item">
item 3
</div>
</div>

Center remaining bootstrap columns [duplicate]

How to center div horizontally, and vertically within the container using flexbox. In below example, I want each number below each other (in rows), which are centered horizontally.
.flex-container {
padding: 0;
margin: 0;
list-style: none;
display: flex;
align-items: center;
justify-content: center;
}
row {
width: 100%;
}
.flex-item {
background: tomato;
padding: 5px;
width: 200px;
height: 150px;
margin: 10px;
line-height: 150px;
color: white;
font-weight: bold;
font-size: 3em;
text-align: center;
}
<div class="flex-container">
<div class="row">
<span class="flex-item">1</span>
</div>
<div class="row">
<span class="flex-item">2</span>
</div>
<div class="row">
<span class="flex-item">3</span>
</div>
<div class="row">
<span class="flex-item">4</span>
</div>
</div>
http://codepen.io/anon/pen/zLxBo
I think you want something like the following.
html, body {
height: 100%;
}
body {
margin: 0;
}
.flex-container {
height: 100%;
padding: 0;
margin: 0;
display: flex;
align-items: center;
justify-content: center;
}
.row {
width: auto;
border: 1px solid blue;
}
.flex-item {
background-color: tomato;
padding: 5px;
width: 20px;
height: 20px;
margin: 10px;
line-height: 20px;
color: white;
font-weight: bold;
font-size: 2em;
text-align: center;
}
<div class="flex-container">
<div class="row">
<div class="flex-item">1</div>
<div class="flex-item">2</div>
<div class="flex-item">3</div>
<div class="flex-item">4</div>
</div>
</div>
See demo at: http://jsfiddle.net/audetwebdesign/tFscL/
Your .flex-item elements should be block level (div instead of span) if you want the height and top/bottom padding to work properly.
Also, on .row, set the width to auto instead of 100%.
Your .flex-container properties are fine.
If you want the .row to be centered vertically in the view port, assign 100% height to html and body, and also zero out the body margins.
Note that .flex-container needs a height to see the vertical alignment effect, otherwise, the container computes the minimum height needed to enclose the content, which is less than the view port height in this example.
Footnote:
The flex-flow, flex-direction, flex-wrap properties could have made this design easier to implement. I think that the .row container is not needed unless you want to add some styling around the elements (background image, borders and so on).
A useful resource is: http://demo.agektmr.com/flexbox/
How to Center Elements Vertically and Horizontally in Flexbox
Below are two general centering solutions.
One for vertically-aligned flex items (flex-direction: column) and the other for horizontally-aligned flex items (flex-direction: row).
In both cases the height of the centered divs can be variable, undefined, unknown, whatever. The height of the centered divs doesn't matter.
Here's the HTML for both:
<div id="container"><!-- flex container -->
<div class="box" id="bluebox"><!-- flex item -->
<p>DIV #1</p>
</div>
<div class="box" id="redbox"><!-- flex item -->
<p>DIV #2</p>
</div>
</div>
CSS (excluding decorative styles)
When flex items are stacked vertically:
#container {
display: flex; /* establish flex container */
flex-direction: column; /* make main axis vertical */
justify-content: center; /* center items vertically, in this case */
align-items: center; /* center items horizontally, in this case */
height: 300px;
}
.box {
width: 300px;
margin: 5px;
text-align: center; /* will center text in <p>, which is not a flex item */
}
DEMO
When flex items are stacked horizontally:
Adjust the flex-direction rule from the code above.
#container {
display: flex;
flex-direction: row; /* make main axis horizontal (default setting) */
justify-content: center; /* center items horizontally, in this case */
align-items: center; /* center items vertically, in this case */
height: 300px;
}
DEMO
Centering the content of the flex items
The scope of a flex formatting context is limited to a parent-child relationship. Descendants of a flex container beyond the children do not participate in flex layout and will ignore flex properties. Essentially, flex properties are not inheritable beyond the children.
Hence, you will always need to apply display: flex or display: inline-flex to a parent element in order to apply flex properties to the child.
In order to vertically and/or horizontally center text or other content contained in a flex item, make the item a (nested) flex container, and repeat the centering rules.
.box {
display: flex;
justify-content: center;
align-items: center; /* for single line flex container */
align-content: center; /* for multi-line flex container */
}
More details here: How to vertically align text inside a flexbox?
Alternatively, you can apply margin: auto to the content element of the flex item.
p { margin: auto; }
Learn about flex auto margins here: Methods for Aligning Flex Items (see box#56).
Centering multiple lines of flex items
When a flex container has multiple lines (due to wrapping) the align-content property will be necessary for cross-axis alignment.
From the spec:
8.4. Packing Flex Lines: the align-content
property
The align-content property aligns a flex container’s lines within the
flex container when there is extra space in the cross-axis, similar to
how justify-content aligns individual items within the main-axis.
Note, this property has no effect on a single-line flex container.
More details here: How does flex-wrap work with align-self, align-items and align-content?
Browser support
Flexbox is supported by all major browsers, except IE < 10. Some recent browser versions, such as Safari 8 and IE10, require vendor prefixes. For a quick way to add prefixes use Autoprefixer. More details in this answer.
Centering solution for older browsers
For an alternative centering solution using CSS table and positioning properties see this answer: https://stackoverflow.com/a/31977476/3597276
Add
.container {
display: flex;
justify-content: center;
align-items: center;
}
to the container element of whatever you want to center. Documentation:
justify-content and
align-items.
You can make use of
display: flex;
align-items: center;
justify-content: center;
on your parent component
Don't forgot to use important browsers specific attributes:
align-items: center; -->
-webkit-box-align: center;
-moz-box-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
justify-content: center; -->
-webkit-box-pack: center;
-moz-box-pack: center;
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;
You could read this two links for better understanding flex:
http://css-tricks.com/almanac/properties/j/justify-content/ and
http://ptb2.me/flexbox/
Good Luck.
Use this:
html, body {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
}
for some HTML markup like this:
<html>
<body>
<main>
<button> abc </button>
<p> something </p>
</main>
</body>
</html>
html, body {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
}
<html>
<body>
<main>
<button> abc </button>
<p> something </p>
</main>
</body>
</html>
1 - Set CSS on parent div to display: flex;
2 - Set CSS on parent div to flex-direction: column; Note that this will make all content within that div line up top to bottom. This will work best if the parent div only contains the child and nothing else.
3 - Set CSS on parent div to justify-content: center;
Here is an example of what the CSS will look like:
.parentDivClass {
display: flex;
flex-direction: column;
justify-content: center;
}
diplay: flex; for it's container and margin:auto; for it's item works perfect.
NOTE: You have to setup the width and height to see the effect.
#container{
width: 100%; /*width needs to be setup*/
height: 150px; /*height needs to be setup*/
display: flex;
}
.item{
margin: auto; /*These will make the item in center*/
background-color: #CCC;
}
<div id="container">
<div class="item">CENTER</div>
</div>
margin: auto works "perfectly" with flexbox i.e. it allows to center item vertically and horizontally.
html, body {
height: 100%;
max-height: 100%;
}
.flex-container {
display: flex;
height: 100%;
background-color: green;
}
.container {
display: flex;
margin: auto;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS</title>
</head>
<body>
<div class="flex-container">
<div class="container">
<div class="row">
<span class="flex-item">1</span>
</div>
<div class="row">
<span class="flex-item">2</span>
</div>
<div class="row">
<span class="flex-item">3</span>
</div>
<div class="row">
<span class="flex-item">4</span>
</div>
</div>
</div>
</body>
</html>
If you need to center a text in a link this will do the trick:
div {
display: flex;
width: 200px;
height: 80px;
background-color: yellow;
}
a {
display: flex;
align-items: center;
justify-content: center;
text-align: center; /* only important for multiple lines */
padding: 0 20px;
background-color: silver;
border: 2px solid blue;
}
<div>
text
text with two lines
</div>
RESULT:
CODE
HTML:
<div class="flex-container">
<div class="rows">
<div class="row">
<span class="flex-item">1</span>
</div>
<div class="row">
<span class="flex-item">2</span>
</div>
<div class="row">
<span class="flex-item">3</span>
</div>
<div class="row">
<span class="flex-item">4</span>
</div>
</div>
</div>
CSS:
html, body {
height: 100%;
}
.flex-container {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
}
.rows {
display: flex;
flex-direction: column;
}
where flex-container div is used to center vertically and horizontally your rows div, and rows div is used to group your "items" and ordering them in a column based one.
You can add flex-direction:column to flex-container
.flex-container {
flex-direction: column;
}
Add display:inline-block to flex-item
.flex-item {
display: inline-block;
}
because you added width and height has no effect on this element since it has a display of inline. Try adding display:inline-block or display:block. Learn more about width and height.
Also add to row class( you are given row{} not taken as style)
.row{
width:100%;
margin:0 auto;
text-align:center;
}
Working Demo in Row :
.flex-container {
padding: 0;
margin: 0;
list-style: none;
display: flex;
align-items: center;
justify-content:center;
flex-direction:column;
}
.row{
width:100%;
margin:0 auto;
text-align:center;
}
.flex-item {
background: tomato;
padding: 5px;
width: 200px;
height: 150px;
margin: 10px;
line-height: 150px;
color: white;
font-weight: bold;
font-size: 3em;
text-align: center;
display: inline-block;
}
<div class="flex-container">
<div class="row">
<span class="flex-item">1</span>
</div>
<div class="row">
<span class="flex-item">2</span>
</div>
<div class="row">
<span class="flex-item">3</span>
</div>
<div class="row">
<span class="flex-item">4</span>
</div>
</div>
Working Demo in Column :
.flex-container {
padding: 0;
margin: 0;
width: 100%;
list-style: none;
display: flex;
align-items: center;
}
.row {
width: 100%;
}
.flex-item {
background: tomato;
padding: 5px;
width: 200px;
height: 150px;
margin: 10px;
line-height: 150px;
color: white;
font-weight: bold;
font-size: 3em;
text-align: center;
display: inline-block;
}
<div class="flex-container">
<div class="row">
<span class="flex-item">1</span>
</div>
<div class="row">
<span class="flex-item">2</span>
</div>
<div class="row">
<span class="flex-item">3</span>
</div>
<div class="row">
<span class="flex-item">4</span>
</div>
</div>
Hope this will help.
.flex-container {
padding: 0;
margin: 0;
list-style: none;
display: flex;
align-items: center;
justify-content: center;
}
row {
width: 100%;
}
.flex-item {
background: tomato;
padding: 5px;
width: 200px;
height: 150px;
margin: 10px;
line-height: 150px;
color: white;
font-weight: bold;
font-size: 3em;
text-align: center;
}
Using CSS+
<div class="EXTENDER">
<div class="PADDER-CENTER">
<div contentEditable="true">Edit this text...</div>
</div>
</div>
take a look HERE

List item will go second column automatic and container space will also increase [duplicate]

This question already has answers here:
When flexbox items wrap in column mode, container does not grow its width
(9 answers)
Closed 4 years ago.
I'm trying to build navigation which will be vertical. Navigation's outer width will be fixed and when the item will be increased then other items will go to the next column.
I tried it with flexbox and added flex-wrap: wrap; in the container. But cant design this.
I tried with these codes.
.container {
display: flex;
flex-direction: column;
height: 320px;
width: 80px;
flex-wrap: wrap;
background: red;
padding: 10px
}
.item {
padding: 20px 30px;
background: #efefef;
display: inline-block;
margin: 0 5px 5px 0;
width: 20px
}
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<div class="item">7</div>
</div>
Instead of width: 80px; give width: auto or do not provide width at all. When the number of items increases touches to the bottom of the container it will be added to the next column.
You do not have control over the container with here. You should make it flexible with auto width.
Learn more here.
.container {
display: flex;
flex-direction: column;
height: 320px;
flex-wrap: wrap;
background: red;
padding: 10px;
width: auto;
}
.item {
padding: 20px 30px;
background: #efefef;
display: block;
margin: 0 5px 5px 0;
width: 20px;
}
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<div class="item">7</div>
<div class="item">8</div>
</div>

Modal not displaying in center of page with flexbox [duplicate]

How to center div horizontally, and vertically within the container using flexbox. In below example, I want each number below each other (in rows), which are centered horizontally.
.flex-container {
padding: 0;
margin: 0;
list-style: none;
display: flex;
align-items: center;
justify-content: center;
}
row {
width: 100%;
}
.flex-item {
background: tomato;
padding: 5px;
width: 200px;
height: 150px;
margin: 10px;
line-height: 150px;
color: white;
font-weight: bold;
font-size: 3em;
text-align: center;
}
<div class="flex-container">
<div class="row">
<span class="flex-item">1</span>
</div>
<div class="row">
<span class="flex-item">2</span>
</div>
<div class="row">
<span class="flex-item">3</span>
</div>
<div class="row">
<span class="flex-item">4</span>
</div>
</div>
http://codepen.io/anon/pen/zLxBo
I think you want something like the following.
html, body {
height: 100%;
}
body {
margin: 0;
}
.flex-container {
height: 100%;
padding: 0;
margin: 0;
display: flex;
align-items: center;
justify-content: center;
}
.row {
width: auto;
border: 1px solid blue;
}
.flex-item {
background-color: tomato;
padding: 5px;
width: 20px;
height: 20px;
margin: 10px;
line-height: 20px;
color: white;
font-weight: bold;
font-size: 2em;
text-align: center;
}
<div class="flex-container">
<div class="row">
<div class="flex-item">1</div>
<div class="flex-item">2</div>
<div class="flex-item">3</div>
<div class="flex-item">4</div>
</div>
</div>
See demo at: http://jsfiddle.net/audetwebdesign/tFscL/
Your .flex-item elements should be block level (div instead of span) if you want the height and top/bottom padding to work properly.
Also, on .row, set the width to auto instead of 100%.
Your .flex-container properties are fine.
If you want the .row to be centered vertically in the view port, assign 100% height to html and body, and also zero out the body margins.
Note that .flex-container needs a height to see the vertical alignment effect, otherwise, the container computes the minimum height needed to enclose the content, which is less than the view port height in this example.
Footnote:
The flex-flow, flex-direction, flex-wrap properties could have made this design easier to implement. I think that the .row container is not needed unless you want to add some styling around the elements (background image, borders and so on).
A useful resource is: http://demo.agektmr.com/flexbox/
How to Center Elements Vertically and Horizontally in Flexbox
Below are two general centering solutions.
One for vertically-aligned flex items (flex-direction: column) and the other for horizontally-aligned flex items (flex-direction: row).
In both cases the height of the centered divs can be variable, undefined, unknown, whatever. The height of the centered divs doesn't matter.
Here's the HTML for both:
<div id="container"><!-- flex container -->
<div class="box" id="bluebox"><!-- flex item -->
<p>DIV #1</p>
</div>
<div class="box" id="redbox"><!-- flex item -->
<p>DIV #2</p>
</div>
</div>
CSS (excluding decorative styles)
When flex items are stacked vertically:
#container {
display: flex; /* establish flex container */
flex-direction: column; /* make main axis vertical */
justify-content: center; /* center items vertically, in this case */
align-items: center; /* center items horizontally, in this case */
height: 300px;
}
.box {
width: 300px;
margin: 5px;
text-align: center; /* will center text in <p>, which is not a flex item */
}
DEMO
When flex items are stacked horizontally:
Adjust the flex-direction rule from the code above.
#container {
display: flex;
flex-direction: row; /* make main axis horizontal (default setting) */
justify-content: center; /* center items horizontally, in this case */
align-items: center; /* center items vertically, in this case */
height: 300px;
}
DEMO
Centering the content of the flex items
The scope of a flex formatting context is limited to a parent-child relationship. Descendants of a flex container beyond the children do not participate in flex layout and will ignore flex properties. Essentially, flex properties are not inheritable beyond the children.
Hence, you will always need to apply display: flex or display: inline-flex to a parent element in order to apply flex properties to the child.
In order to vertically and/or horizontally center text or other content contained in a flex item, make the item a (nested) flex container, and repeat the centering rules.
.box {
display: flex;
justify-content: center;
align-items: center; /* for single line flex container */
align-content: center; /* for multi-line flex container */
}
More details here: How to vertically align text inside a flexbox?
Alternatively, you can apply margin: auto to the content element of the flex item.
p { margin: auto; }
Learn about flex auto margins here: Methods for Aligning Flex Items (see box#56).
Centering multiple lines of flex items
When a flex container has multiple lines (due to wrapping) the align-content property will be necessary for cross-axis alignment.
From the spec:
8.4. Packing Flex Lines: the align-content
property
The align-content property aligns a flex container’s lines within the
flex container when there is extra space in the cross-axis, similar to
how justify-content aligns individual items within the main-axis.
Note, this property has no effect on a single-line flex container.
More details here: How does flex-wrap work with align-self, align-items and align-content?
Browser support
Flexbox is supported by all major browsers, except IE < 10. Some recent browser versions, such as Safari 8 and IE10, require vendor prefixes. For a quick way to add prefixes use Autoprefixer. More details in this answer.
Centering solution for older browsers
For an alternative centering solution using CSS table and positioning properties see this answer: https://stackoverflow.com/a/31977476/3597276
Add
.container {
display: flex;
justify-content: center;
align-items: center;
}
to the container element of whatever you want to center. Documentation:
justify-content and
align-items.
You can make use of
display: flex;
align-items: center;
justify-content: center;
on your parent component
Don't forgot to use important browsers specific attributes:
align-items: center; -->
-webkit-box-align: center;
-moz-box-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
justify-content: center; -->
-webkit-box-pack: center;
-moz-box-pack: center;
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;
You could read this two links for better understanding flex:
http://css-tricks.com/almanac/properties/j/justify-content/ and
http://ptb2.me/flexbox/
Good Luck.
Use this:
html, body {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
}
for some HTML markup like this:
<html>
<body>
<main>
<button> abc </button>
<p> something </p>
</main>
</body>
</html>
html, body {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
}
<html>
<body>
<main>
<button> abc </button>
<p> something </p>
</main>
</body>
</html>
1 - Set CSS on parent div to display: flex;
2 - Set CSS on parent div to flex-direction: column; Note that this will make all content within that div line up top to bottom. This will work best if the parent div only contains the child and nothing else.
3 - Set CSS on parent div to justify-content: center;
Here is an example of what the CSS will look like:
.parentDivClass {
display: flex;
flex-direction: column;
justify-content: center;
}
diplay: flex; for it's container and margin:auto; for it's item works perfect.
NOTE: You have to setup the width and height to see the effect.
#container{
width: 100%; /*width needs to be setup*/
height: 150px; /*height needs to be setup*/
display: flex;
}
.item{
margin: auto; /*These will make the item in center*/
background-color: #CCC;
}
<div id="container">
<div class="item">CENTER</div>
</div>
margin: auto works "perfectly" with flexbox i.e. it allows to center item vertically and horizontally.
html, body {
height: 100%;
max-height: 100%;
}
.flex-container {
display: flex;
height: 100%;
background-color: green;
}
.container {
display: flex;
margin: auto;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS</title>
</head>
<body>
<div class="flex-container">
<div class="container">
<div class="row">
<span class="flex-item">1</span>
</div>
<div class="row">
<span class="flex-item">2</span>
</div>
<div class="row">
<span class="flex-item">3</span>
</div>
<div class="row">
<span class="flex-item">4</span>
</div>
</div>
</div>
</body>
</html>
If you need to center a text in a link this will do the trick:
div {
display: flex;
width: 200px;
height: 80px;
background-color: yellow;
}
a {
display: flex;
align-items: center;
justify-content: center;
text-align: center; /* only important for multiple lines */
padding: 0 20px;
background-color: silver;
border: 2px solid blue;
}
<div>
text
text with two lines
</div>
RESULT:
CODE
HTML:
<div class="flex-container">
<div class="rows">
<div class="row">
<span class="flex-item">1</span>
</div>
<div class="row">
<span class="flex-item">2</span>
</div>
<div class="row">
<span class="flex-item">3</span>
</div>
<div class="row">
<span class="flex-item">4</span>
</div>
</div>
</div>
CSS:
html, body {
height: 100%;
}
.flex-container {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
}
.rows {
display: flex;
flex-direction: column;
}
where flex-container div is used to center vertically and horizontally your rows div, and rows div is used to group your "items" and ordering them in a column based one.
You can add flex-direction:column to flex-container
.flex-container {
flex-direction: column;
}
Add display:inline-block to flex-item
.flex-item {
display: inline-block;
}
because you added width and height has no effect on this element since it has a display of inline. Try adding display:inline-block or display:block. Learn more about width and height.
Also add to row class( you are given row{} not taken as style)
.row{
width:100%;
margin:0 auto;
text-align:center;
}
Working Demo in Row :
.flex-container {
padding: 0;
margin: 0;
list-style: none;
display: flex;
align-items: center;
justify-content:center;
flex-direction:column;
}
.row{
width:100%;
margin:0 auto;
text-align:center;
}
.flex-item {
background: tomato;
padding: 5px;
width: 200px;
height: 150px;
margin: 10px;
line-height: 150px;
color: white;
font-weight: bold;
font-size: 3em;
text-align: center;
display: inline-block;
}
<div class="flex-container">
<div class="row">
<span class="flex-item">1</span>
</div>
<div class="row">
<span class="flex-item">2</span>
</div>
<div class="row">
<span class="flex-item">3</span>
</div>
<div class="row">
<span class="flex-item">4</span>
</div>
</div>
Working Demo in Column :
.flex-container {
padding: 0;
margin: 0;
width: 100%;
list-style: none;
display: flex;
align-items: center;
}
.row {
width: 100%;
}
.flex-item {
background: tomato;
padding: 5px;
width: 200px;
height: 150px;
margin: 10px;
line-height: 150px;
color: white;
font-weight: bold;
font-size: 3em;
text-align: center;
display: inline-block;
}
<div class="flex-container">
<div class="row">
<span class="flex-item">1</span>
</div>
<div class="row">
<span class="flex-item">2</span>
</div>
<div class="row">
<span class="flex-item">3</span>
</div>
<div class="row">
<span class="flex-item">4</span>
</div>
</div>
Hope this will help.
.flex-container {
padding: 0;
margin: 0;
list-style: none;
display: flex;
align-items: center;
justify-content: center;
}
row {
width: 100%;
}
.flex-item {
background: tomato;
padding: 5px;
width: 200px;
height: 150px;
margin: 10px;
line-height: 150px;
color: white;
font-weight: bold;
font-size: 3em;
text-align: center;
}
Using CSS+
<div class="EXTENDER">
<div class="PADDER-CENTER">
<div contentEditable="true">Edit this text...</div>
</div>
</div>
take a look HERE

make div change height dynamically

I have a page with 3 rows. The first row is a fixed height of 150px and the last row is a fixed height of 120px. I need the middle row to adjust according to the height of the window so that all three rows are visible and there are no scroll bars visible. The middle row must adjust dynamically so that even after loading if you move the browser window to another screen that is smaller the middle row must adjust accordingly. Secondly the middle row must have it's content aligned vertically middle and horizontally center. Any help is appreciated.
CSS for the height:
.first-row {
height: 150px;
}
.middle-row {
height: calc(100vh - 150px - 120px);
}
.last-row {
height: 120px;
}
Run the code snippet in full page mode (!). Use css calc function to automatically calculate the height of middle container.
body {
margin: 0;
}
.top {
background-color: #f0f0f0;
height: 150px;
}
.bottom {
background-color: #ddd;
height: 120px;
}
.middle {
background-color: teal;
display: table-cell;
height: calc(100vh - 270px);
text-align: center;
vertical-align: middle;
width: 100vw;
}
<div class="top">TOP</div>
<div class="middle">MIDDLE</div>
<div class="bottom">BOTTOM</div>
As suggested, try using flexbox:
<style>
.container {
display: flex;
flex-flow: column wrap;
align-items: stretch;
height: 100%;
}
.row{}
.row.row1{height: 150px;}
.row.row2{flex: 1 1 auto;}
.row.row3{height: 120px;}
</style>
<div class="container">
<div class="row row1">First row</div>
<div class="row row2">Middle row</div>
<div class="row row3">Final row</div>
</div>
Don't forget to add your vendor prefixes when using this.
I use flexboxes for these things: A Complete Guide to Flexbox
.container {
display: flex;
flex: 1 1 auto;
flex-direction: column;
}
.header {
height: 50px;
background-color: green;
flex: 0 0;
}
.footer {
height: 30px;
background-color: red;
flex: 0 0;
}
.content {
background-color: blue;
flex: 1 1 auto;
min-height: 50px;
}
<div class="container">
<div class="header"></div>
<div class="content"></div>
<div class="footer"></div>
<div>
Personally the cleanest solution is using flexbox:
.container {
display: flex;
flex-direction: column;
}
.dynamic {
flex: 1 1 auto;
}
http://codepen.io/kevinfarrugia/pen/wzOqGo?editors=1100#0

Categories