Margin between two boxes in CSS - html

First, I'm a newbie on front-end development. I just want to hear about possible "professional" solutions of my problem from professionals.
Now, firstly check out this fiddle: http://jsfiddle.net/SB7yR/
Here is what I want: create two boxes on each row. I can't do that right here because I want to make margin between two boxes too.
I have solutions for that situation for example create a class like "last" and give it margin-right: 0; then apply it last boxes for each row. But I don't want to do that. It sounds .. hmm.. an amateur solution.
Thanks for advices.

You can add a text-align: justify; to .addresses and remove the margin-right on address_box.
.addresses {
margin-top: 30px;
text-align: justify;
}
also, you should use a class for .addresses #address-box instead of an ID. ID's are supposed to be unique on a page, so only one element is allowed to have a particular ID. Use this instead .addresses .address-box.

Try below code:-
.addresses #address-box {
height: 123px;
width: 298px;
border-color: black;
border-style: solid;
border-width: 1px;
float:left;
margin-right: 20px;
}
#address-box:nth-child(2n) {
margin-right:0;
}

My suggestion would be to make a class for all boxes (assuming each box would be the same size), then float all the boxes left. You would then make the width (in actual physical size or percentage) to be less than the width of the containing div. This would accomplish you having two boxes in each row. Hope this helps.
.box{ float:left; width:48%; margin-right:5px; }
Something like that. Experiment with the margin right amount.

for cross browser compatibility, how about an additional html element:
http://jsfiddle.net/SB7yR/8/
html:
<div id="addresses-wrap">
<div class="addresses">
<div class="overflow">
<div id="address-box"></div>
<div id="address-box"></div>
<div id="address-box"></div>
<div id="address-box"></div>
<div id="address-box"></div>
</div>
</div>
</div>
css:
#addresses-wrap {
width: 620px;
height: auto;
border:1px solid green;
overflow:hidden;
}
.addresses {
width:100%;
float:left;
margin-top: 30px;
}
.addresses .overflow {
width:650px;
overflow:hidden;
}
.addresses #address-box {
height: 123px;
width: 298px;
border:1px solid #000;
float:left;
margin:0 20px 20px 0;
}

You'll need to use a float to make this work. Try replacing your .addresses #address-box definition with the following:
.addresses #address-box {
height: 123px;
width: 288px;
border-color: black;
border-style: solid;
border-width: 1px;
display: inline-block;
margin: 10px;
overflow: hidden;
float: left;
}

Related

Div moves only when zooming out

I did a quick search on stackoverflow and found some ways to solve it but none works.
I have my HTML code like below:
<div id="product_box">
<div id="pro_img"><img src="images.jpg'" width="140px"/></div>
<div id="pro_text">
</div>
</div>
and my CSS:
<style>
#product_box {
border: 1px solid;
border-color: #8dd5f6;
margin-top: 8px;
margin-left: 4px;
margin-right: 4px;
width: 330px;
height: 196px;
float:left;
}
#pro_text{
float:left;
width:189px;
height: 196px;
background-color: #CCC;
}
#pro_img {
float:left;
border-right:1px solid #8dd5f6;
width:140px;
height: 196px;
}
</style>
The #pro_img is to the left and #pro_text is to the right, it works fine at default zoom and large zoom in but the problem is that when I zoom out the pro_text (right div) falls off the container box.
I found someone says that I need box-sizing: border-box; inside of my CSS. I tried it and put it like this:
<style>
#pro_img {
float:left;
border-right:1px solid #8dd5f6;
box-sizing: border-box;
width:140px;
height: 196px;
}
</style>
It won't fall off anymore but the border is invisible as it border the image from inside.
I disable the border-right from #pro_img, the problem's gone but I want a border-right that would separate the image and the text.
Total width needed: 140(img)+1(border)+189(text) = 330px just fit the container box. I tried increase box width to 332px but it won't help.
Thank you.
That is because you are using float:left I cleared that used margin instead see this http://jsfiddle.net/3pmmjLx8/
UPDATED CSS CODE
#pro_text{
margin-left:141px;
width:189px;
height: 196px;
background-color: #CCC;
}

HTML - Putting text in some field

I'm trying to create a single sentence in some kind of a field I created, and every time I just make the font bigger it pops out of the field, and I gotta lower the font-size to put it inside again.
Can I make the font size bigger and keep it in the field at the same time?
My code:
HTML:
<div id="wrapper">
<h1 style=""> Nothing Created Yet </h1>
</div>
CSS:
#wrapper {
width: 1000px;
height: 120px;
margin-top: 100px;
border: 5px solid gray;
border-radius:500px;
margin-left: auto;
margin-right: auto;
font-family: Arial;
font-size:40px;
background-color: #F0EEF3;
border-color:red;
}
What I get:
You firstly need to remove the browser-default margin styling on your h1 element:
#wrapper h1 {
margin: 0;
}
Then you should ideally give your #wrapper element a line-height equal to its height:
#wrapper {
...
height: 120px;
line-height: 120px;
}
JSFiddle demo.
try this DEMO
#wrapper {
width: 1000px;
height: 120px;
margin-top: 100px;
border: 5px solid gray;
border-radius:500px;
margin-left: auto;
margin-right: auto;
font-family: Arial;
font-size:40px;
line-height:10px;
background-color: #F0EEF3;
border-color:red;
text-align:center;
}
The reason why this happens is you set fixed width and height for the DIV and when you increase the font size, it could no longer fit inside the DIV. So, the answer is it is impossible in fixed size DIV like this.
or do
-added class to the header and put the margin to 0 and center the text
(jsfiddle.net/6GRGH/)

CSS positioning - two elements next to each other

Ok, I know this question has been here at least hundred of times but this positioning is driving me crazy - can someone help me?
I have a portlet page (its basically html) with a table and a div tag. I would like to position them next to each other (table on the left, div on the right). Here are parts of my html:
<div id="page>
<table id="logtable">
...
</table>
<div id="divMessage>
...
</div>
</div>
...and CSS:
#page {
width: 1200px;
margin: 0px auto -1px auto;
padding: 15px;
}
#logtable {
width: 800px;
float: left;
}
#divMessage {
width: 350px;
position:relative;
right:-5px;
top: -20px;
}
I have tried various positions - absolute, fixed, float etc, but I cant seem to get it right... Thanks for any help!
You could use...
float: left;
on your div 'logtable'
I would advise using DIVs to do you alignment of content so wrap the table in a DIV.
I also prefer to use inline-block over float left and gives more predictable results.
so...
<div id="page">
<div id="divTable" class="InsideContent">
<table id="logtable">
Left
</table>
</div>
<div id="divMessage" class="InsideContent">
Right
</div>
</div>
#page {
width: 1200px;
margin: 0px auto -1px auto;
padding: 15px;
}
.InsideContent{
display:inline-block;
}
}
#divTable {
width: 800px;
}
#divMessage {
width: 350px;
}
Code needs tidying up but you get the idea...
JSFiddle http://jsfiddle.net/3N53d/
Use float:left on the element which should be on the left and float:right on the right one. Keep in mind that if the sum of their widths exceeds the available space in the parent element they will be split into two lines anyway.
Here you go , no need of right:-5px;
#divMessage {
width: 350px;
position: relative;
top: -20px;
float: left;
}
I see that you forgot closing the quotes at <div id="page>, this might cause some problems, but basically you have to use:
float: left;
for the last div.
I have created this JSFiddle for you to see if this fits your needs.
You can just use display: inline-* to put them side by side in a row
#logtable {
width: 800px;
display: inline-table;
}
#divMessage {
width: 350px;
display: inline-block;
}
JSFiddle
Just try this.
Fiddle
CSS
#logtable {
width: 500px;
float: left;
background:red;
}
#divMessage {
width: 350px;
position:relative;
float:left;
background:blue;
}
try this:
<table id="logtable">
<tr>
<td>
table area
</td>
</tr>
</table>
<div id="divMessage">
div area
</div>
</div>
#page {
width: 800px;
margin: 0px auto -1px auto;
padding: 15px;
border:red solid 1px;
height:170px;
}
#logtable {
width: 400px;
height:150px;
float: left;
border: blue dashed 1px;
}
#divMessage {
width: 350px;
height:150px;
right:-5px;
top: -20px;
border: green dashed 1px;
float:right;
}
here is a smaple
In simple we can do like this:
table#logtable, div.divMessage{
display:inline-block;
}
Or
table#logtable, div.divMessage{
float:left;
width:50%;
}

CSS negative margins, what am I doing wrong?

I'm trying to achieve a 1 column flexible / 1 column fixed layout. 'col-a' should be flexible, taking up 100% - 110px, 'col-b' should be fixed and aligned right.
I' trying to use negative margins but having little luck.
<div class="cont">
<div class="col-a">
Be flexible
</div>
<div class="col-b">
Be fixed
</div>
</div>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
.cont {
background-color: #00f;
padding: 10px;
overflow:hidden;
}
.col-a {
background-color: #0ff;
padding-right: 110px;
margin-right: -110px;
float: left;
}
.col-b {
background-color: #ff0;
width: 110px;
float: left;
}
Can it be done using just this mark-up?
/*Answer found */
Here is the solution
.cont {
background-color: #00f;
overflow:hidden;
padding: 10px;
}
.col-a {
width: 100%;
background-color: #0ff;
margin-right: -110px;
float: left;
}
.col-b {
background-color: #ff0;
width: 110px;
float: right;
}
I wouldn't use a negative margin for this.
This is how I would set it up.
Set your column parent container to position relative.
Set your column A to have a padding-right of 110px (to make space for Column B)
Set your column B to be absolutely positioned to the top, right with a fixed width of 110px.
This will allow your Column A to expand 100% horizontally, while leaving space on the right for Column B.
Here's an example of what I outlined above: http://jsfiddle.net/NPn8d/
How about something like this, then.
<style type="text/css">
.cont{position:relative;}
.col-a{
border:1px solid #0000ff;
width:auto;
margin:0,110,0,0;
}
.col-b{
border:1px solid #ff0000;
width:110px;
float:right;
top:0;
position:absolute;
margin:0,0,0,-110
}
</style>
<div class="cont">
<div class="col-a">Be flexible</div>
<div class="col-b">Be fixed</div>
</div>

CSS Multiple grouped elements with margins

Take this HTML:
<div>
<div class="block">Hello</div>
<div class="block">Hello</div>
<div class="block">Hello</div>
<div class="block">Hello</div>
</div>
With the companion CSS:
div.block
{
float: left;
width: 100px;
height: 100px;
margin: 1px;
background: red;
}
The result of this is four blocks, which have between them 2 pixels of space (1px from the right margin of the left block and 1px from the left margin of the right block).
Is there a way that I can achieve a similar effect to border-collapse? ie. I want there to be only one pixel of margin between adjacent blocks.
This is a basic example of often more complex situations that I run into, and I don't want to get around it by by anything similar to only setting margin-left to 1 pixel etc.
There are multiple ways to this
One of them is
div.block
{
float: left;
width: 100px;
height: 100px;
margin: 1px 1px 1px 0;
background: red;
}
div.block:last-child {
margin: 1px 0 1px 0;
}
Another is
div.block+div.block { margin-left: 1px; }
You can check the demo of both way here
How about using the CSS selector :first-child and :last-child to alter the first and last <div>?
div.block
{
float: left;
width: 100px;
height: 100px;
margin: 2px 1px 2px 0;
background: red;
}
div.block:first-child {
margin-left: 2px;
}
div.block:last-child {
margin-right: 2px;
}
If you can alter the markup itself, then I guess we can have a cross browser compatible solution:
<div class="block"> <div class="block_2"></div> </div>
and then apply the css like:
div.block{float: left; width: 100px; height: 100px; }
div.block_2{width:99px; height:100px; background-color:red}
Assign a class for last block called 'last'.
The set margin-right of every block to 1px.
Set margin-right of block that has last class to 0.
.block.last { margin-right: 0px; }
Pseudo selectors like forst-child and last-child are not well supported so I think this is the best option you have.