I currently have the following code:
<div id="container">
<div class="card">1</div>
<div class="card">2</div>
<div class="card">3</div>
<div class="card">4</div>
</div>
<style>
#container{
width:100%;
height:100%;
overflow-y:auto;
position:absolute;
left:0; top:10px;
padding:10px;
}
.card{
width:100px; height:100px; margin:10px;
float:left;
}
</style>
I am trying to vertically and horizontally align the div boxes so that the more boxes that appear, it still stays both vertically and horizontally centred in the container. For example:
Example of what it would look like with 4 cards which fit in the container..
Example of what it would look like with 12 cards which overflow in the container..
Example of what it would look like with cards that dont fit in the container..
DEMO
HTML
<div class="container">
<div class="container-wrapper">
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
</div>
</div>
CSS
.container {
position: relative;
display:table;
background: red;
width:100%;
height: 100%; /* auto is default, you can have ur height here */
}
.container-wrapper {
display: table-cell;
margin:auto;
text-align:center;
font-size:0;
width:90%;
height:90%;
vertical-align:middle;
}
.card {
display: inline-block;
height:100px;
width:100px;
border: 1px solid #ddd;
background: #eee;
margin:10px;
}
DEMO 2 with some height of the container
Try Flexbox DEMO
#container {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
min-height: 100vh;
margin: 0 auto;
flex-wrap: wrap;
box-sizing: border-box;
}
.inner {
display: flex;
flex-direction: row;
align-items: center;
justify-content: flex-start;
max-width: 750px;
margin: 0 auto;
flex-wrap: wrap;
}
.card{
width:100px;
height:100px;
margin:10px;
border: 1px solid black;
}
<div id="container">
<div class="inner">
<div class="card">1</div>
<div class="card">2</div>
<div class="card">3</div>
<div class="card">4</div>
<div class="card">1</div>
<div class="card">2</div>
<div class="card">3</div>
<div class="card">4</div>
<div class="card">3</div>
<div class="card">4</div>
</div>
</div>
You may not be able to use external libraries for your project, but there is still much to learn from them in my opinion.
Your situation is a basic example of how a grid system can be useful. In deed, this problem has been solved and equated many times before by such systems.
Normally I would suggest Twitter Bootstrap 3, but since this framework is somewhat complex, I think it would be easier to read something more lightweight, like 960 grid system. Here are two links that can give you a brief introduction into the library:
http://sixrevisions.com/web_design/the-960-grid-system-made-easy/
http://webdesign.tutsplus.com/articles/using-the-960-grid-system-as-a-design-framework--webdesign-2036
Once you understand this, IMO, you have no other choice than to dive into the framework and see how it is done. It will be messy.
I also believe you will have to use JavaScript. Can you use jQuery?
Either way, when adding a new card, detect it using JavaSctipt, and then change the DOM based on that.
Hope I helped.
Related
i am making a porfolio website and using a (HTML,CSS,JS) logo that i need to place them at the centre of the element. i have used the text-align: center but this is not working.
i am new into the development.
i have tried text-align : center; but this is not working.
Based on this you can do as follow
<html>
<body>
<style>
.logo {
border: 1px solid black;
width: 200px;
}
#logo-container {
border: 1px dashed red;
display: flex;
justify-content: center;
text-align: center;
}
</style>
<div id="logo-container">
<div class='logo'>Logo 1</div>
<div class='logo'>Logo 2</div>
<div class='logo'>Logo 3</div>
<div class='logo'>Logo 4</div>
<div class='logo'>Logo 5</div>
</div>
</body>
</html>
You could use flex for this purpose, warp the logos inside a parent div.
the align then vertically with align-items: center; and horizontally with justify-content:space-around;
like this :
body{
padding:0;
margin:0;
height:100vh;
width:100vw;
display:grid;
place-items:center;
}
.parent{
background-color:#6cba70;
display:flex;
width:400px;
height:200px;
justify-content:space-around;
align-items: center;
}
<div class="parent">
<div>HTML</div>
<div>CSS</div>
<div>JS</div>
</div>
Text-align is for text, it sounds like you are taling about some form of image elements, that will not work.
Hard to give you an exact solution without an example, but I would use flex. You could try that out: https://www.w3schools.com/cssref/css3_pr_flex.asp
I am relatively new to flexbox and I find it awesome.
I am currently trying to align a text to the top right of a card section, but the problem I encounter occurs because I don't want it to stick to the right of the screen, but to the rightmost card - So if I have a single card, it wont go to the end of the screen, and also if I have more than the screen can hold (I also flex wrap it) it would still stick to the rightmost card.
I simplified the code for the example (enter on fullscreen and play with the width of the screen by shrinking/expanding devtools(F12))
.spacer {
padding-top: 300px;
}
.card {
height: 200px;
width: 200px;
background-color: blue;
margin-right: 16px;
margin-bottom: 16px;
}
.container {
display: flex;
flex-wrap: wrap;
}
.text-container {
display: flex;
justify-content: flex-end;
}
<div class="spacer"></div>
<div>
<div class="text-container">
<div> wow </div>
</div>
<div class="container">
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
</div>
</div>
the card amount is not fixed as in the example, it is an *ngFor a changing list
I heard a thing from a colleague about fxFlexOffset, but couldn't understand how to use it, and if it is helpful in my example.
Anyone have any Idea how to do it except of media queries, And if it is impossible to do it without media queries, is there a simple one (the cards sizes are fixed)?
Thanks!
If you want an easy CSS solution without media query and without JS I can only thing about a hacky way to achieve this:
.card {
height: 200px;
width: 200px;
background-color: blue;
margin-right: 16px;
position:relative;
text-align:right
}
.card:before {
content:"WOW";
font-size:20px;
background:#fff;
position:absolute;
right:0;
left:-1000%;
top:-25px;
z-index:0;
}
.card:after {
content:"";
position:absolute;
bottom:0;
background:#fff;
left:0;
right:0;
height:20px;
z-index:1;
}
.container {
margin-top:50px;
display: flex;
flex-wrap: wrap;
}
.text-container {
display: flex;
justify-content: flex-end;
}
<div class="container">
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
</div>
I have struggled to align content-div elements of a wrapper-div using only CSS with the following restriction.
The wrapper div can have a row which allows multiple content-div elements be on the row
At most 4 content-div elements can exist on a row of the wrapper div.
Content-div elements of the last row must expand to fill the row. (e.g if 3 content-div elements exist on the last row, then the width of each content-div should be 33.3%)
One and only one content-element always is selected, and the selected element should be bottom-left conner of the wrapper-div element.
To handle this, I have tried the following css.
.wrapper{
width:100%;
}
.content{
max-width:100%;
min-width:25%;
background-color:white;
float:right;
}
.content.selected{
position:absolute;
top:100%;
left:0;
float:left;
background-color:yellow;
}
I thought that the "float:right; float:left position:absolute; top:100%; left:0;" option can handle the restriction 1 and restriction 4, the "min-width:25%" option can handle the restriction 2 and the "max-width:100%" option can handle the restriction 3. However, only a few restriction were satisfied through the CSS.
I have setup jsFiddle example:
https://jsfiddle.net/6qyc5kLw/2/
I would help in this regard.
This image is what I want to do.
ever considered display:flex? its HUGE!
.wrapper{
width:100%;
position:relative;
display: flex;
flex-flow:row wrap;
align-items: stretch;
}
.content{
min-width:25%;
background-color:white;
//float:right;
flex:1;
order:1;
}
.content.selected{
//position:absolute;
//top:100%;
//left:0;
//float:left;
background-color:yellow;
order:-1;
}
The new flexbox possibilities are most certainly what you are looking for. See below snippet or https://jsfiddle.net/6qyc5kLw/3/ for an updated demo with some basic flexbox properties. An additional one would be
flex-order (to reverse the order of elements in first row)
.wrapper{
width:100%;
position:relative;
display: flex;
justify-content: space-between;
flex-wrap: wrap;
}
.content{
flex-basis: 25%;
background-color:white;
border: 1px solid #ccc;
flex-grow: 1;
box-sizing: border-box;
}
.content.selected{
align-self: flex-end;
background-color:yellow;
}
<body>
<div class="wrapper">
<div class="content">
1
</div>
<div class="content">
2
</div>
<div class="content">
3
</div>
<div class="content">
4
</div>
<div class="content selected">
5
</div>
<div class="content">
6
</div>
<div class="content">
7
</div>
</div>
</body>
You can use display: flex
.wrapper{
width:100%;
display: flex;
flex-wrap: wrap;
justify-content: space-around;
}
.content{
background:#fff;
box-flex: 1;
min-width:25%;
flex: 1;
margin: auto;
}
.content.selected{
background-color:yellow;
}
<div class="wrapper">
<div class="content">1</div>
<div class="content">2</div>
<div class="content">3</div>
<div class="content">4</div>
<div class="content selected">5</div>
<div class="content">6</div>
<div class="content">7</div>
</div>
Here is exactly what you want in example picture:
.wrapper{
width:100%;
position:relative;
}
.content{
max-width:100%;
min-width:25%;
background-color:white;
float:right;
border: 1px solid black;
box-sizing: border-box;
}
.content.selected{
background-color:yellow;
float: left;
width: 33.33%;
}
.content:nth-child(6) {
float: right;
width: 33.33%;
}
.content:nth-child(7) {
float: left;
width: 33.33%;
}
<body>
<div class="wrapper">
<div class="content">
1
</div>
<div class="content">
2
</div>
<div class="content">
3
</div>
<div class="content">
4
</div>
<div class="content selected">
5
</div>
<div class="content">
6
</div>
<div class="content">
7
</div>
</div>
</body>
I have a few divs aligned horizontally.
How do I make the spacing between them automatic so that if I resize the screen or add another div, there will be equal spacing between divs.
Example when screen width is 600px:
Example when screen width is 330px:
Hopefully my explanation is good enough.
Thanks for any help!
Flexbox can do that https://jsfiddle.net/2Lzo9vfc/210/
HTML
<div class="content">
<div class="box">Box</div>
<div class="box">Box</div>
<div class="box">Box</div>
</div>
CSS
.content {
display: -webkit-flex;
display: flex;
-webkit-justify-content: space-around;
justify-content: space-around;
width: 100%;
}
.box {
background: black;
padding: 25px;
color: white;
}
Here you can find a solution with flexbox:
.container {
display:flex;
justify-content:space-between;
}
.item {
background:#000;
height:50px;
width:120px;
}
<div class="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
More information about using flexbox you can find here: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
You can use flexboxes, just appy these proprieties to the container of divs:
.container {
display: flex;
justify-content: space-between;
}
You may use inline-block + text-align:justify; for older browser generating an extra last invisible line with :after, or flex + justify-content:space-betwween;
.ib {
text-align:justify;
}
.ib:after {
content:'';
display:inline-block;
width:99%;
}
.flex {
display:flex;
justify-content:space-between;
}
.d100 {
width:100px;
height:2em;
background:blue;
display:inline-block;
}
<div class="ib">
<div class="d100"></div>
<div class="d100"></div>
<div class="d100"></div>
</div>
<div class="flex">
<div class="d100"></div>
<div class="d100"></div>
<div class="d100"></div>
</div>
Use the flex declaration - see here: http://www.w3schools.com/cssref/css3_pr_flex.asp
You can use flexboxes, this solustion is for IE 10+ and latest other browsers.
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Option 1: Add a border thats the same color as background
Option 2: add a background colored div and set display to inline
I am new to bootstrap and trying.
I need a pattern as follows
How ca we split the div's vertically with two equal divs ?
In horizondal we can do that by col-md-6.
Thanks in advance
Bootstrap mainly focuses on WIDTH, thus to my knowledge there are
no special classes to make two div's of equal height.
You can do it by specifying height:/* value in px */; in the <div>'s styling !
<div class="container"> /*Grid Layout*/
<div class="row testdiv">/*row cuts of 15px margin of left&right*/
</div>
<div class="row testdiv">
</div>
</div>
Now the CSS :
.testdiv{
height:400px;/*or some other value*/
}
A class selector is used to affect the styling of both div's at the same time !
add two div as follows
<div class="container-fluid mainbg">
<div class="row">
<div class="col-md-12 div1">.col-md-12</div>
</div>
<div class="row">
<div class="col-md-12 div2">.col-md-12</div>
</div>
</div>
Some css code to include
.mainbg{
background:grey;
padding:10px; }
.div1{
background:red; }
.div1, .div2{
height:100px;
line-height:100px;
font-size:50px; }
.div2{
text-align:center;
}
You could use css tables to achieve equal-height rows
Bootply
Markup
<div class="container-fluid">
<div class="row">
<div class="one">DIV1</div>
</div>
<div class="row">
<div class="two">DIV2</div>
</div>
</div>
(Relevant) CSS
.container-fluid
{
height: 200px; /* whatever you need */
display:table;
width:100%;
}
.one,.two
{
display:table-cell;
width:100%;
vertical-align: middle;
}
.row {
display:table-row;
}
Depending on your browser support you could always use CSS3 Flexbox
I did a quick demo here of what you're after.
http://codepen.io/tom-maton/pen/LCbIx
.container {
align-content: stretch;
background-color: gray;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-ms-flex-flow: row wrap;
-webkit-flex-flow: row wrap;
flex-flow: row wrap;
height: 350px;
margin: 20px auto;
padding: 15px;
width: 750px;
}
All the flexbox settings are set on the containing element and it does the rest from there on in.
A good article about FlexBox can be found here http://css-tricks.com/snippets/css/a-guide-to-flexbox/