I need to create a centered grid of squares with text inside it. With jQuery help user will be able to add or remove squares (in row and column).
Basically the code would be:
<style>
div.square{
padding: 5px;
margin: 5px;
border: 1px solid green;
/*display: inline-table; /* IE fail*/
/*display: inline-block; /* IE fail*/
float: left;
}
div.row{
margin: 0 auto;
}
</style>
<div style="width:500px; border:1px solid red; margin: 0 auto">
<div class="row">
<div class="square">1</div>
<div class="square">2</div>
<div class="square">3</div>
</div>
<div style="clear:both"></div>
<div class="row">
<div class="square">1</div>
<div class="square">2</div>
<div class="square">3</div>
</div>
<div style="clear:both"></div>
<div class="row">
<div class="square">1</div>
<div class="square">2</div>
<div class="square">3</div>
</div>
<div style="clear:both"></div>
</div>
What is the best way to do it?
Are you looking for something like this:
http://jsbin.com/etunuk/1/
Floated elements shrink wrap, so they need a width attached.
To center floated elements you need to add their width + paddings + margins + borders and assign that width to the wrapper, in this case .row and center .row.
If you remove the border, you need to remove the width, 1px * 6 from the row width and so on.
Related
Very silly question, but i can't understand...
In a row of small div elements some of my divs have different margin-right, even if i define same margin-right for all row items. I wonder what exactly fires this kind of action.
P.S. Thanks for reading, sorry for my bad english
.row{
display: flex;
}
.row__item{
width: 50px;
height: 20px;
margin-right: 1px;
background: red;
}
<div class="row">
<div class="row__item"></div>
<div class="row__item"></div>
<div class="row__item"></div>
<div class="row__item"></div>
<div class="row__item"></div>
<div class="row__item"></div>
</div>
margin is outside of element that's so normal for it to Not take background
Remove margin-right to get rid of that space
If you want your content to be spaced from each other and yet take the background, use padding instead
Padding box is a part of your element (breathing space between content-box and border-box)
And if you want the padding Not to add up with your element's (50px) width, the answer is box-sizing: border-box;
So if this wasn't what you mean and what you need, let me know
.row{
display: flex;
}
.row__item{
width: 50px;
height: 20px;
background: red;
}
<div class="row">
<div class="row__item"></div>
<div class="row__item"></div>
<div class="row__item"></div>
<div class="row__item"></div>
<div class="row__item"></div>
<div class="row__item"></div>
</div>
This question already has answers here:
Align inline-block DIVs to top of container element
(5 answers)
Closed 4 years ago.
I see this.
I have a div with inside 20 inline-block div.
I centered with "text-align:center".
A javascript code write some text to first div.
The first div is above than others.
How can I center divs without happening this?
.teszt
{
border:1px solid rgba(255,0,0,0.5);
width:200px;
min-height:100px;
margin:20px;
display:inline-block;
border-radius:5px;
box-shadow: 5px 5px 5px rgba(0,0,0,0.5);
background:rgba(50,0,0,0.5);
color:white;
}
<div style="text-align:center">
<div class="teszt" id="testmcserverinfo">Here's a text generated by javascript</div>
<div class="teszt">Teszt2</div>
<div class="teszt">Teszt3</div>
<div class="teszt">Teszt4</div>
<div class="teszt">Teszt5</div>
<div class="teszt">Teszt6</div>
<div class="teszt">Teszt7</div>
<div class="teszt">Teszt8</div>
<div class="teszt">Teszt9</div>
<div class="teszt">Teszt10</div>
<div class="teszt">Teszt11</div>
<div class="teszt">Teszt12</div>
<div class="teszt">Teszt13</div>
<div class="teszt">Teszt14</div>
<div class="teszt">Teszt15</div>
<div class="teszt">Teszt16</div>
<div class="teszt">Teszt17</div>
<div class="teszt">Teszt18</div>
<div class="teszt">Teszt19</div>
<div class="teszt">Teszt20</div>
</div>
(sorry for my bad english)
Add a vertical-align value to your elements.
.teszt {
vertical-align: top; // or middle
}
Add overflow:auto; to the div. The reason for this is the box model. The text is overflowing the div and occupying the full width of the viewport. Limiting the text by not letting it overflow the div stops that.
Just add a line to your class.
overflow : hidden;
That div going above will be come to right position.
I think the following looks a lot cleaner and it uses the css grid.
.teszt
{
border:1px solid rgba(255,0,0,0.5);
min-height:100px;
border-radius:5px;
box-shadow: 5px 5px 5px rgba(0,0,0,0.5);
background:rgba(50,0,0,0.5);
color:white;
}
.row {
text-align: center;
display: grid;
grid-template-columns: repeat(5, 200px);
grid-column-gap: 20px;
grid-row-gap: 20px;
}
<div class="row">
<div class="teszt" id="testmcserverinfo">Here's a text generated by javascript</div>
<div class="teszt">Teszt2</div>
<div class="teszt">Teszt3</div>
<div class="teszt">Teszt4</div>
<div class="teszt">Teszt5</div>
<div class="teszt">Teszt6</div>
<div class="teszt">Teszt7</div>
<div class="teszt">Teszt8</div>
<div class="teszt">Teszt9</div>
<div class="teszt">Teszt10</div>
<div class="teszt">Teszt11</div>
<div class="teszt">Teszt12</div>
<div class="teszt">Teszt13</div>
<div class="teszt">Teszt14</div>
<div class="teszt">Teszt15</div>
<div class="teszt">Teszt16</div>
<div class="teszt">Teszt17</div>
<div class="teszt">Teszt18</div>
<div class="teszt">Teszt19</div>
<div class="teszt">Teszt20</div>
</div>
You can play around with the grid-template-columns: repeat(5, 200px); to adjust the amount of columns.
CODE
<body align="left" style="background-color: white;">
<div style="background-color:#1e836c;height:100vh;width:1vw;"></div>
<div align="center" style="background-color: pink;height:100vh;width:1vw;"></div>
<div align="right" style="background-color: red;height:100vh;width:1vw;"></div>
</body>
How can I implement bootstrap's grid system in the code above by keeping the same width and size of the divs?
I want to make the left and right divs very smalll and the center one bit bigger
You can achieve that using flexbox
.parent {
display: flex;
height: 200px
}
.child {
flex: 1;
margin: 0 5px;
border: red solid
}
.parent > div:nth-of-type(2) {
flex: 3 /* this will be 3x times bigger than the other child */
}
<div class="parent">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>
I can achieve this by bootstrap grid pattern by giving col-md-1 for
left and right and col-md-8 for the center, but i wanna make the left
and right too small as possible
Using bootstrap, you can use col-xs-2 (or col-xs-1) for smaller childs and col-xs-8(col-xs-10) for middle one child
I used col-xs-2 +col-xs-10 for the demo (having margins)
[class^="col"] div {
height: 200px;
border: red solid
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row">
<div class="col-xs-2">
<div></div>
</div>
<div class="col-xs-8">
<div></div>
</div>
<div class="col-xs-2">
<div></div>
</div>
</div>
</div>
I just want the width of that red line only.. Thats the tricky part
So that's not divs, that's a border property
using bootstrap grid and pseudo elements::before/::after here you go:
.col-xs-10 {
height: 200px;
border: red solid
}
.col-xs-10::before,
.col-xs-10::after {
border-left: red solid;
position: absolute;
content: '';
top: -3px;
height: 200px
}
.col-xs-10::before {
left: -20px
}
.col-xs-10::after {
right: -20px
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row">
<div class="col-xs-10 col-xs-offset-1">
</div>
</div>
</div>
I don't understand very well, but I think that's it:
#div-01{background-color: pink;height:100vh;width:1vw;float: left;}
#div-02{background-color: red;height:100vh;width:1vw;float: right;}
<div id="div-01"></div>
<div id="div-02"></div>
if you want other thing please explain in detail
UPDATE
doing so you have the main div in the center and the side floating.
#div-center{border: solid blue;height:2000px;width:65%;display: block;margin: 0 auto;}
#div-right{border: solid pink;height:200px;width:15%;position: fixed;right: 0px;}
#div-left{border: solid red;height:200px;width:15%;position: fixed;left: 0px;}
<div id="div-right"></div>
<div id="div-left"></div>
<div id="div-center"></div>
I have the following HTML:
<div style="width:300px;background:yellow;">
<div style="float:left;border:solid 2px red;"><!--Img Div-->
<img src="https://cdn2.iconfinder.com/data/icons/despicable-me-2-minions/128/Dancing-minion-icon.png">
</div>
<div style="border:solid 2px lime;float:left;"><!--Text Div-->
Banana!
</div>
<div style="clear:both;"></div>
</div>
I need to set the width of the Text Div such that it occupies the remaining width. I know this can be done by using width style attribute as width:164px;. What I needed to know is: Can this be done without setting the width manually using other css properties?
1) Remove float:left from the text div
2) Set overflow:hidden (or auto) on the text div
Updated fiddle
This creates a new block formatting context which causes the text div to fill the remaining width
Try this
<div style="width:300px;background:yellow;">
<div style="float:left;border:solid 2px red;"><!--Img Div-->
<img src="https://cdn2.iconfinder.com/data/icons/despicable-me-2-minions/128/Dancing-minion-icon.png">
</div>
<div style="border:solid 2px lime;display:block; text-align: center; overflow: hidden;"><!--Text Div-->
Banana!
</div>
<div style="clear:both;"></div>
</div>
I added display:block; text-align: center; overflow: hidden; and removed the float: left from the text div
use display:table for parent and display:table-cell for the one which you want the width to fill
Note : moved your styles outside
.top {
width: 300px;
background: yellow;
display: table;
}
.inside {
float: left;
border: solid 2px red;
}
.txt {
border: solid 2px lime;
display: table-cell;
width: 100%;
vertical-align: top;
}
.clear {
clear: both;
}
<div class="top">
<div class="inside">
<!--Img Div-->
<img src="https://cdn2.iconfinder.com/data/icons/despicable-me-2-minions/128/Dancing-minion-icon.png" />
</div>
<div class="txt">
<!--Text Div-->Banana!</div>
<div class="clear"></div>
</div>
My URL http://www.ilandeistudio.com/store/
Below the main slider I have some images and categories..How can I center the div "Nelson" and lock it into place so they stay put while zooming/out?
I tried using padding-left and margin-left but they increase the space in between the categories as well. I just want to slide that entire group into the center...
Thanks!
Put those items in a container div and give that div a width and margin: 0 auto;
HTML:
<div class="nelsonContainer">
<div class=nelson>
<a href="http://www.ilandeistudio.com/store/index.php?route=product/category&path=49">
<img src="http://www.ilandeistudio.com/store/image/cache/data/illandei-120x120.png" />
OUTDOOR
</a>
</div>
<div class=nelson>
<a href="http://www.ilandeistudio.com/store/index.php?route=product/category&path=52">
<img src="http://www.ilandeistudio.com/store/image/cache/no_image-120x120.jpg" />
LIVING
</a>
</div>
/* etc. */
</div>
CSS:
.nelsonContainer {
width: 960px;
margin: 0 auto;
}
Wrap them all in a bigger div, say let's call it nelson-wrapper, and then add automatic left and right margins to it.
HTML:
<div id="nelson-wrapper">
<div class="nelson">...</div>
<div class="nelson">...</div>
<div class="nelson">...</div>
<div class="nelson">...</div>
<div class="nelson">...</div>
</div>
CSS:
#nelson-wrapper{
margin: 0 auto; /* Shorthand for 0 top/bottom, auto left/right margins */
}
EDIT:
Actually, since your divs are blocks, not inline, the above will NOT work, although the following (which I found at: http://www.impressivewebs.com/center-multiple-divs/) will work:
HTML:
<div id="nelson-wrapper">
<div class="nelson">
<div class="nelson">
<div class="nelson">
<div class="nelson">
<div class="nelson">
<div class="nelson">
</div>
CSS:
#nelson-wrapper {
text-align: center;
}
.nelson {
display: inline-block;
}
Note: This will not work correctly in Internet Explorer 8-. To get them to behave, unfortunately, the only workaround is either an Internet-Explorer-only hack, or class. The hack works as such:
CSS:
.nelson {
*display: inline;
*margin: 0 20px 0 20px;
}