Solved

CSS margin-right example

Posted on 2010-11-29
10
411 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:gudii9
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 18

Accepted Solution

by:
Sudaraka Wijesinghe earned 150 total points
ID: 34236360
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
 
LVL 44

Assisted Solution

by:scrathcyboy
scrathcyboy earned 200 total points
ID: 34245303
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
 
LVL 8

Assisted Solution

by:Zado
Zado earned 150 total points
ID: 34249602
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
 
LVL 8

Expert Comment

by:Zado
ID: 34317457
Please grant the points and close the question, thanks :-)
0
 
LVL 8

Expert Comment

by:Zado
ID: 34384854
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 7

Author Comment

by:gudii9
ID: 34461959
>>>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
 
LVL 44

Expert Comment

by:scrathcyboy
ID: 34462228
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
 
LVL 7

Author Comment

by:gudii9
ID: 34661209
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
 
LVL 18

Expert Comment

by:Sudaraka Wijesinghe
ID: 34661305
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
 
LVL 44

Expert Comment

by:scrathcyboy
ID: 34669924
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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
In this tutorial viewers will learn how to style different bullet points for unordered lists in CSS. Begin with a normal unordered list; the default bullet point is a solid circle: In the CSS, create a defined class of unordered list by typing "ul.S…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now