/    Sign up×
Community /Pin to ProfileBookmark

simple question

if i have a div tag

[code=html]<div id=”content”>
<div id=”img”>
</div>
</div>[/code]

and CSS like this

[code=html]#img {position: absolute; left: 10%; top: 10;}[/code]

Would the img div still be inside the content div? I’m just trying to position my images within my content element.

to post a comment
CSS

17 Comments(s)

Copy linkTweet thisAlerts:
@FangJan 12.2009 — Position absolute is relative to a relatively positioned parent.

If #content is relatively positioned then #img is positioned relative to #content, else it will be positioned relative to the body element.
Copy linkTweet thisAlerts:
@two4god07authorJan 12.2009 — thank you very much!
Copy linkTweet thisAlerts:
@two4god07authorJan 12.2009 — One more simple question pertaining div elements. When I position the main div (the one that contains all the children divs) should I position it on the page using percents or pixels? I was thinking that if I use percents, then people with smaller resolution monitors or larger resolution monitors, it would be on the same part of the page as where it is on mine. I don't know if that's true or not however.
Copy linkTweet thisAlerts:
@FangJan 13.2009 — Nearly all sites either align the content left or centered. Neither of these require you to think in terms of px or &#37;

It's the flexibility of the content area that will determine the use of px, % or em.
Copy linkTweet thisAlerts:
@TheTeenScripterJan 13.2009 — If you use percents, the area will look to be in the same place, but will make your content vary depending on the resolution/size of window. Setting the element in pixels will make the content show up the same regardless of the resolution/size of window. But, the area outside of it will vary depending on the resolution/size of window.
Copy linkTweet thisAlerts:
@two4god07authorJan 13.2009 — yeah, my church page looks really good on a laptop because it is wide screen, but on my mom's computer and my dad's computer, the content is to the right edge.

It also has a big color distortion on three regular sized monitors that I've looked at. The maroon is black and the tan is yellow. I thought it might be because their screens weren't bright enough, but they are the same brightness as mine, and when I increase the resolution, the content aligns correctly, but the colors stay nasty.

anyway.. align it center instead of positioning it


[code=html]{margin-left: auto; margin-right: auto; width: (haven't checked yet);}[/code]

correct?
Copy linkTweet thisAlerts:
@TheTeenScripterJan 13.2009 — yeah, my church page looks really good on a laptop because it is wide screen, but on my mom's computer and my dad's computer, the content is to the right edge.

It also has a big color distortion on three regular sized monitors that I've looked at. The maroon is black and the tan is yellow. I thought it might be because their screens weren't bright enough, but they are the same brightness as mine, and when I increase the resolution, the content aligns correctly, but the colors stay nasty.

anyway.. align it center instead of positioning it


[code=html]{margin-left: auto; margin-right: auto; width: (haven't checked yet);}[/code]

correct?[/QUOTE]


There are multiple ways of centering....

<center></center>

<p align="center"></p>

{ text-align:center; }

there are probably more examples that I can't think of right now...
Copy linkTweet thisAlerts:
@two4god07authorJan 13.2009 — I was looking online and it was talking about aligning elements center with the <center> tags is becoming outdated.

I know about the text-align, but that won't work with my entire div tag, just the text inside.
Copy linkTweet thisAlerts:
@CharlesJan 13.2009 — I was looking online and it was talking about aligning elements center with the <center> tags is becoming outdated.[/QUOTE]It was depricated back in 1998. Depricatred means that the issuing standards organization, W3C, asked us to stop using it.

You can center blocks with [font=monospace]margin:auto[/font] but you're going to screw up your positioning. But those big blocks of color on either side looked kinid of icky anyway. Just let you page flow.
Copy linkTweet thisAlerts:
@two4god07authorJan 13.2009 — Those two blocks are supposed to be a deep maroon, but on a regular monitor they show up as a dirty dark brown almost black.

I don't want to change the layout, because we visited the church Sunday night, and they expressed deeply how much they liked the webpage. I just wanted to change how it was viewed on different sized monitors. I don't want content to be lost off the side of the monitor.
Copy linkTweet thisAlerts:
@CharlesJan 13.2009 — &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;title&gt;Giant Says&lt;/title&gt;
&lt;style type="text/css"&gt;
body {
border-left:solid 10em #a00;
border-right:solid 10em #a00;
margin:0;
padding:0;
}
#skip {display:none}
#navigation {
margin:0;
padding:0;
list-style-type:none;
width:5em;
position:absolute;
top:1px;
left:11em;
}
#content {
margin-left:5em;
padding:1em;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p id="skip"&gt;&lt;a href="#content"&gt;Skip to content&lt;/a&gt;&lt;/p&gt;
&lt;ul id="navigation"&gt;
&lt;li&gt;Fee&lt;/li&gt;
&lt;li&gt;Fie&lt;/li&gt;
&lt;li&gt;Foe&lt;/li&gt;
&lt;li&gt;Fum&lt;/li&gt;
&lt;/ul&gt;
&lt;div id="content"&gt;
&lt;h1&gt;Giant Says&lt;/h1&gt;
&lt;p&gt;Lorem ipsum dolor sit amet consectetuer hendrerit et eros Vivamus non. Eu dolor accumsan ut Ut Curabitur Vivamus In porttitor iaculis orci. Aenean feugiat in nibh elit amet magna dictumst sollicitudin ac orci. Ante Ut ut lacinia at scelerisque quis sit leo neque tempus. Id venenatis Curabitur ipsum elit scelerisque Nulla orci ligula Vivamus condimentum. In et amet vel arcu orci id rhoncus.&lt;/p&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
[i]or[/i]&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;title&gt;Giant Says&lt;/title&gt;
&lt;style type="text/css"&gt;
body {
background-color:#a00;
margin:0 10em;
padding:0;
}
#skip {display:none}
#navigation {
margin:0;
padding:0;
list-style-type:none;
width:5em;
position:absolute;
top:1px;
left:11em;
}
#content {
background-color:#fff;
padding:1em;
padding-left:5em;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p id="skip"&gt;&lt;a href="#content"&gt;Skip to content&lt;/a&gt;&lt;/p&gt;
&lt;ul id="navigation"&gt;
&lt;li&gt;Fee&lt;/li&gt;
&lt;li&gt;Fie&lt;/li&gt;
&lt;li&gt;Foe&lt;/li&gt;
&lt;li&gt;Fum&lt;/li&gt;
&lt;/ul&gt;
&lt;div id="content"&gt;
&lt;h1&gt;Giant Says&lt;/h1&gt;
&lt;p&gt;Lorem ipsum dolor sit amet consectetuer hendrerit et eros Vivamus non. Eu dolor accumsan ut Ut Curabitur Vivamus In porttitor iaculis orci. Aenean feugiat in nibh elit amet magna dictumst sollicitudin ac orci. Ante Ut ut lacinia at scelerisque quis sit leo neque tempus. Id venenatis Curabitur ipsum elit scelerisque Nulla orci ligula Vivamus condimentum. In et amet vel arcu orci id rhoncus.&lt;/p&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@TheTeenScripterJan 13.2009 — as far as I know, the text-align css attribute works on anything. not just text. also, if it didnt i couldnt imagine how it would look if you added that attribute to images and text...
Copy linkTweet thisAlerts:
@two4god07authorJan 13.2009 — Teen, Yes is does center anything that is content, but it doesn't center entire sections like... if i set a div size to 100.. and i use a text-align on it

that 100X100 block would still be left aligned, but the content inside would be centered
Copy linkTweet thisAlerts:
@TheTeenScripterJan 13.2009 — i actually just tested that out on a div, set to 630x630 pixels, and it seems to center it
Copy linkTweet thisAlerts:
@two4god07authorJan 13.2009 — really? cuz it never did it on mine. Maybe my doctype wasn't right at the time
Copy linkTweet thisAlerts:
@TheTeenScripterJan 13.2009 — I came up with a really weird way of (vertically) centering content, that I have barely managed to get working in both Internet Explorer and Firefox. Using the text-align and vertical-align css, I was able to have the data perfectly centered both horizontally and vertically (without using tables)...

I have tested the text-align method with a div also (you have to make sure the content you want centered is inside the tag with the text-align css attribute and that the width is limited)

Here is the weird code that works (try getting it to work another way):

[code=html]<div Style="display:inline-block; position:relative; width:630px; height:630px; background:gray; text-align:center;">
<span style="display:inline-block; vertical-align:middle; height:100&#37;"></span>

<span Style="display:inline-block; background:black; color:white; width:250px; vertical-align:middle; text-align:center;">
This is a test.<br>This works in a really weird way.
</span>
</div>[/code]
Copy linkTweet thisAlerts:
@two4god07authorJan 20.2009 — I got my site centered by adding a div tag around all my divs.

I set the position of that div to relative and removed most of the positioning of the child divs. So now, when I position the one div, it moves them all without distorting any of the content.

I had to remove margins from the body element though because for some reason, surrounding all divs with one caused a margin to appear. It wouldnt position top: 0px. It all turned out though. I just have to upload it to the server now.
×

Success!

Help @two4god07 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...