CSS margin-right example

Hi,

I was running following example.

<html>
<head>

<style type="text/css">
body
{
background:#ffffff url('img_tree.png') no-repeat right top;
margin-right:2000px;
}
</style>

</head>

<body>
<h1>Hello World!</h1>
<p>Now the background image is only show once, and positioned away from the text.</p>
<p>In this example we have also added a margin on the right side, so the background image will never disturb the text.</p>
</body>

</html>

Open in new window


I have not understood the output and als what "margin:right" is doing. Is it is putting right margin for all h1 tag, p tag, and body tag, image tag etc . Please advise. any ideas, links, resources, sample code highly appreciated. thanks in advance
LVL 7
gudii9Asked:
Who is Participating?
 
Sudaraka WijesingheConnect With a Mentor Web Application ProgrammerCommented:
See here: http://www.w3schools.com/css/css_margin.asp
margin-right is for setting the distance between the right side border of an element and the next element.

In the above example margin-right is set on the html body tag. Margin settings in CSS are not inherited by child elements, so this will not affect any other tags in the HTML document, how ever you can set margins on them separately.
0
 
scrathcyboyConnect With a Mentor Commented:
You should not be using margin-right as the person is doing in the sample you chose.

He is using margin-right: 2000 px to basically shove the background color out of view completely.

That is BAD programming practice -- suggest you do NOT follow it.

A typical use of margin-right would be --

TD  { margin-left: 2px; margin-right: 20 px }

This will push all the contents of the table cells to the left, and is used when you have a series of numbers in the table cells that you want to appear closer to the middle of the cell, but still be right justified, as in a list of percents --

|     30%            |
|     50%            |

and so on.
0
 
ZadoConnect With a Mentor Commented:
First of all 'margin-right:2000px;' is too big, I think you wanted use 'margin-right:200px;'
Anyway, if you want to use margin-left or margin-right, you need to specify width of the actual element and/or parent element, example:
<style type="text/css">
#div7
{
width:400px;
margin-right:200px;
}
</style>

Open in new window


let's say you have this html code:
<body>
  <div id="div7">
   <!-- your code here -->
  </div>
</body>

Open in new window


So div will be moved 200px from right. I could give you another 10 examples, you can use margin-right with combination of other properties to get required effect. I recommend this tutorial to understand better how margin works:

http://www.w3schools.com/CSS/css_margin.asp ,and then this one:
http://www.w3.org/TR/CSS2/box.html
 
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
ZadoCommented:
Please grant the points and close the question, thanks :-)
0
 
ZadoCommented:
To be clear here: I'm not asking for grant the points to me, but generally.
If you have a problem with any suggestion from experts or nothing works with your expectation, please let us know, so we will continue work on solution for you, thanks.
0
 
gudii9Author Commented:
>>>In the above example margin-right is set on the html body tag. Margin settings in CSS are not inherited by child elements

can we give style name as body which is same as name of the tag(<body>). Which are child elements here and how inheritance concept applies to them. please advise
0
 
scrathcyboyCommented:
By that he simply means that if you set in CSS --

BODY   { margin-right: 5px; }

that it only applies to the body tag, not other elements under it.  You can set those separately.

"can we give style name as body which is same as name of the tag(<body>)"

Yes, that is what I did in the CSS line above.

Child elements are any other element UNDER the body tag, which means all other CSS elements in the document.

 BTW, you should not be using 2000 px as a right margin for the BODY, it throws the entire page off the screen.  Offsets like this are bad programming practice, recommend not to use them.
0
 
gudii9Author Commented:
here
body
{
background:#ffffff url('img_tree.png') no-repeat right top;
margin-right:2000px;
}

the above styles get applied to all body child elements right like

<body>
<h1>Hello World!</h1>
<p>Now the background image is only show once, and positioned away from the text.</p>
<p>In this example we have also added a margin on the right side, so the background image will never disturb the text.</p>
</body>


here child elements of body aree h1, p and again p.


i think this code is not proper right

<html>
<head>
<style type="text/css">
body
{
background:#ffffff url('img_tree.png') no-repeat right top;
margin-right:2000px;
}
</style>

</head>
where  body close tag is not there above still style, head close tags are there. please advise
0
 
Sudaraka WijesingheWeb Application ProgrammerCommented:
In CSS background and margin styles are not inherited by the child elements, unless you explicitly specify it.

body *{background:inherit; margin:inherit;}

Open in new window

0
 
scrathcyboyCommented:
as I said above, you MUST get rid of the -- margin-right: 2000 px

It will throw your entire page off the browser window.  It will neverl work with 2000 px.   Make it 20 px.
0
All Courses

From novice to tech pro — start learning today.