Solved

CSS margin-right example

Posted on 2010-11-29
10
420 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

615 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