I have structure like:
<style>
#main{
max-width: 500px;
margin: 0 auto;
overflow: hidden;
border: 1px solid red;
}
#container{
margin-right: -50px;
}
.block{
display: inline-block;
width: 100px;
height: 100px;
border: 1px solid grey;
margin-right: 30px;
}
</style>
<div id="main">
<div id="container">
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
</div>
</div>
If I have wide width it looks like
http://i.stack.imgur.com/3I1yM.png
It's ok.
But if I use narrow it sucks
I need that internal bloks is aligned to center like this
http://i.stack.imgur.com/5GXMJ.png
Hi what you need here is the property text-align:center:
#container{
margin-right: -50px;
text-align:center;
}
The demo http://jsfiddle.net/u5HHc/
What about this?
http://jsfiddle.net/ALR8P/3/
Remove margin from blocks, user text-align justify and add word spacing if you want some fixed space for your blocks on the last row.
#container {
letter-spacing: 10px; //word-spacing: 10px; //to space blocks on last row
text-align: justify; //to align everything to the borders on the first lines
padding: 0px 10px; //to separate the blocks from the borders a little
}
.block {
margin-right: 0px;
}
It looks wrong when width is too small and only one block fits on each row but maybe there you need some media query to make your css responsive to fix that on that special case.
EDIT: letter-spacing works better crossbrowser http://jsfiddle.net/ALR8P/5/, you may want letter-spacing: 30px; since your margin was 30px
Related
I have a flow chart that was originally an image which ought to be simple enough to translate into CSS, but as I have little skill in CSS div manipulation I was hoping the wizards of Stack Overflow could help.
I'm trying to replicate the above image in CSS. It doesn't have to look exactly the same, but I'd like to keep the basic layout the same.
I've taken a stab at the second and third columns just to see if I could get that part figured out, but I can't seem to get the second item in the second column to line up with the first item in the second column.
If someone could help me with just that portion, I would be eternally grateful.
.RoleContainerTop {
border: 1px black solid;
margin: auto;
text-align: center;
width: 100px;
margin: 20px;
float: left;
}
.RoleContainerMiddle {
border: 1px black solid;
margin: auto;
text-align: center;
width: 100px;
margin-top: 75px;
float: left;
}
.RoleContainerBottom {
border: 1px black solid;
margin: auto;
text-align: center;
width: 100px;
margin-top: 150px;
float: left;
}
.RoleContainer p {
text-align: center
}
<div>
<div>
<div class="RoleContainerTop">
<p>
Abracadabra
</p>
</div>
<div class="RoleContainerMiddle">
<p>
Shazam
</p>
</div>
<div class="RoleContainerBottom">
<p>
Alakazam
</p>
</div>
</div>
</div>
I would do something like this. The key is to create your columns of variable width, and from there it's pretty simple. I chose percentage width but you could do it however you like.
I would also advise you to consolidate your css a bit :). You're repeating alot of code that is shared between like elements.
CSS:
* {
box-sizing: border-box;
}
.column-25 {
width: 25%;
padding: 10px;
float: left;
}
.column-25:last-child {
float: right;
}
.block {
width: 100%;
height: 60px;
margin-bottom: 20px;
border: 1px solid #000;
}
.block.center {
margin-top: 40px;
}
HTML:
<div class="container">
<div class="column-25">
<div class="block"></div>
</div>
<div class="column-25">
<div class="block"></div>
<div class="block"></div>
</div>
<div class="column-25">
<div class="block center"></div>
</div>
<div class="column-25">
<div class="block"></div>
<div class="block"></div>
</div>
</div>
From here, you could look into absolute positioned elements with some :before/:after wizardry to create the arrows if you'd like.
jsfiddle demo
All right so I have a bit of an odd request to try and fulfill. I have a page that has a listing of divs, inside of which are an image and a link. Each row has 3 divs max. Sometimes, though, the last row may have just 1 or 2 divs. What I need to try and do is set things up so that if the last row only has 1 or 2 divs, then those divs need to be centered horizontally within their row.
Is this something that is possible? Would I need to use JavaScript/jQuery to count the number of divs in the final row? Or could I do this all with just CSS?
Here is how the divs are setup right now:
<div class="About-Categories clearfix">
<div class="About-Category clearfix">
<img src="[image url]" class="image">
[link text]
</div>
<!--Additional About-Category divs go here as needed-->
</div>
There could be any number of About-Category divs, but the css is setup to ensure that there are only 3 About-Category divs within the width of the page (until a screen size of 460px is reached).
CSS as it is now:
.About-Categories{
text-align: center;
}
.About-Category{
float: left;
height: auto;
margin-left: 0%;
margin-top: 0px;
display:inline-block;
padding: 30px;
width: 33%;
&:nth-of-type(1){
clear: both;
}
}
#wrap {
width: 600px;
text-align: center;
}
.tile {
width: 198px;
height: 100px;
border: 1px solid #000;
display: inline-block;
margin: 0;
padding: 0;
}
<div id="wrap">
<div class="tile"></div><div class="tile"></div><div class="tile"></div><div class="tile"></div><div class="tile"></div>
</div>
If you display the divs as inline-block, they'll respect the text-align attribute. In combination with a wrapper sized to fit three divs, you should get the result you're looking for.
Example:
#wrap {
width: 600px;
text-align: center;
}
.tile {
width: 198px;
height: 100px;
border: 1px solid #000;
display: inline-block;
vertical-align: top;
margin: 0;
padding: 0;
}
<div id="wrap">
<div class="tile"></div><div class="tile"></div><div class="tile"></div><div class="tile"></div><div class="tile"></div>
</div>
I'm even finding it difficult to phrase my question right so bear with me here please.
I have one div that serves as the main container of my page. Inside that div I would like to have five other divs which have equal size and equal margins. However when I calculate everything right, the fifth div always jumps to the next line.
I hope this makes sense. This is my code:
CSS and HTML as follows
#content {
position: absolute;
width: 1000px;
height: 500px;
left: 50%;
top: 50%;
margin-left: -500px;
margin-top: -250px;
border: 2px solid #f9423a;
border-radius: 10px;
background-color: #3eb1c8;
overflow: hidden;
}
.bookmark {
display: inline-block;
width: 15%;
height: 20%;
margin-left: 2%;
margin-right: 2%;
margin-top: 2.5%;
border: 1px solid #f9423a;
border-radius: 10px;
background-color: #f9423a;
}
<div id="content">
<div class="bookmark"></div>
<div class="bookmark"></div>
<div class="bookmark"></div>
<div class="bookmark"></div>
<div class="bookmark"></div>
<div class="bookmark"></div>
<div class="bookmark"></div>
<div class="bookmark"></div>
<div class="bookmark"></div>
<div class="bookmark"></div>
<div class="bookmark"></div>
<div class="bookmark"></div>
<div class="bookmark"></div>
<div class="bookmark"></div>
<div class="bookmark"></div>
</div>
Note that I'm just working with color-filled divs to see if it's working.
As you can see it almost works, the online thing that bothers me is that there's a bit more margin on the right than there is on the left. I would like to have equal margins between the sides and the outer elements, and between the inner elements of course.
I hope someone understands my question!
This is because you are using: display: inline-block which reads the white space between your divs on your HTML code as a literal white space, like putting a space between words, that breaks the layout.
Try changing your sintax like this:
<div>content</div><div>
content2</div><div>
content3</div><div>
content4</div><div>
content5</div>
Then, for the CSS you could use calc(); to add borders, that would ruin your layout too.
Like this:
div {
width: calc(20% - 4px);//20*5 = 100 - 2px on each side each time
border: 2px solid black;
}
JS Fiddle
body {
margin: 0 0 0 0;
}
div {
text-align: center;
display: inline-block;
width: calc(20% - 4px);
/*20*5 = 100 - 2px on each side each time*/
border: 2px solid black;
background-color: red;
}
<div>content1</div><div>
content2</div><div>
content3</div><div>
content4</div><div>
content5</div>
check this example, if this is what you wanted
Here i have removed the right margin and increased the bookmark div with to 17%
.bookmark {
display: inline-block;
width: 17%;
height: 20%;
margin-left: 2%;
margin-top: 2.5%;
border: 1px solid #f9423a;
border-radius: 10px;
background-color: #f9423a;
}
https://jsfiddle.net/0gkfp7zr/
Aramil's answer is good and correct correct. There is no "nice" way to deal with this. Different people have different methods, but they are all a bit hackish. The way I prefer to do it is with comments as follows:
<div class="bookmark"></div><!--
--><div class="bookmark"></div><!--
--><div class="bookmark"></div><!--
--><div class="bookmark"></div><!--
--><div class="bookmark"></div>
Basically you don't want any white space between one closing div and the next opening div. Sometimes if my content is short enough I will put them all together on one line as you see below, but it makes it much harder to read.
<div class="bookmark"></div><div class="bookmark"></div><div class="bookmark"></div><div class="bookmark"></div><div class="bookmark"></div>
add this to .bookmark
float:left and the add .8 to the width of bookmark, your computation is not equal because in 1 bookmark div you have 15% plus the margin-left and right which is 4%, total of 1 div is 19 x 5 = 95 so I added .8 to fill the remaining white spaces. 19.8 x 5 = 99
.bookmark {
display: inline-block;
width: 15.8%;
height: 20%;
margin-left: 2%;
margin-right: 2%;
margin-top: 2.5%;
border: 1px solid #f9423a;
border-radius: 10px;
background-color: #f9423a;
float: left;
}
.block {
display: table-cell;
margin: 5px auto;
background-color: grey;
width: 100px;
height: 100px;
}
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="col-md-6">
<div class="block"></div>
</div>
<div class="col-md-6">
<div class="block"></div>
</div>
It looks fine when I look in the large screen, but when I resize the window and they are stacked, then the two grey blocks will become one larger rectangular grey block. So how could I place spacing between them?
try to add margin to get this fixed..
css
.block{
margin:5px auto;
}
I have a lot of blocks, and I want to order them in a table-like way. The problem is, that because of the margin-right of the last one, they don't really fit in the way I want them to. See this example. I want to have 4 blocks on one line, how can I achieve that?
CSS:
.box {
width: 500px;
height: 10000px;
}
.block {
float: left;
width: 100px;
height: 100px;
margin-right: 30px;
background-color: #0f0;
margin-bottom: 50px;
}
Use last-child, a CSS selector, like so: http://jsfiddle.net/QwGV5/1/
.block:last-child {margin-right: 0;}
For compatibility: http://caniuse.com/css-sel3
div:last-child {
margin-right: 0;
}
But this will only work in IE9+ (maybe 8, can't remember)
Otherwise, put a class on the last one.
Here's an update to your fiddle http://jsfiddle.net/QwGV5/2/
I think for what you want to do though, you'd actually want it on every 4th div. So you can use nth-child
.box div:nth-child(4n) {
margin-right: 0;
}
Eaxmple: http://jsfiddle.net/QwGV5/4/
You can make a new class for the last block in each row (e.g. .last) and make margin-right:0 for that class.
HTML:
<div class="box">
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block last"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block last"></div>
</div>
CSS:
.last { margin-right: 0; }
JS Fiddle Example