/    Sign up×
Community /Pin to ProfileBookmark

How to put div next to centered div

If I have a div in the center of a page with the css

#div
{
width: 100px;
margin: 0px auto;
}

How do I get a div to the left of it(div 2) like in the image: [url]http://i45.tinypic.com/34osvw3.png[/url]
that will never move from the left of it.

Thanks alot!

to post a comment
CSS

6 Comments(s)

Copy linkTweet thisAlerts:
@waxdocFeb 16.2013 — Floats are kinda like position:absolute in that floated elements are partially taken out of normal HTML doc flow.

Using width: 30% for all, I found that if one element is float: left and another is float: right, I could add an element margin: 0 auto AFTER the floated elements and it would ignore them and be centered. Putting the centered element before the floated ones, did not work.
Copy linkTweet thisAlerts:
@safinnauthorFeb 16.2013 — so the main div in the middle would have the float also?
Copy linkTweet thisAlerts:
@waxdocFeb 16.2013 — NO. Do NOT float div with margin: auto; that would take it partially out of normal HTML flow.

I saw this on a post nd tried it. My explanation is a guess,

... add an element margin: 0 auto AFTER the floated elements and it would ignore them and be centered.
[/quote]

but it seems logical. The position of the centered element AFTER the floated ones is important,
Copy linkTweet thisAlerts:
@cootheadFeb 17.2013 — Hi there safinn,

try it like this...
[color=navy]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="language" content="english">
<meta http-equiv="Content-Style-Type" content="text/css">

<title>centered box with left appendage</title>

<style type="text/css">
#box {
position:relative;
width:100px;
height:100px;
margin:auto;
background-color:#f88613;
}
#hangeron {
position:absolute;
width:25px;
height:100%;
left:-25px;
background-color:#0000eb;
}
#center {
width:6px;
height:6px;
margin:6px auto 0;
border-radius:3px;
background-color:#000;
}
</style>

</head>
<body>

<div id="box">
<div id="hangeron"></div>
</div>

<div id="center"></div>

</body>
</html>[/color]


[i]coothead[/i]
Copy linkTweet thisAlerts:
@safinnauthorFeb 17.2013 — Thanks a lot coothead! Worked great. Pretty simple, disappointed I didn't think of doing it like that.
Copy linkTweet thisAlerts:
@cootheadFeb 17.2013 — Hi there safinn,

when you know the answer, then, of course, the question will no longer look difficult. ?


[i]coothed[/i]
×

Success!

Help @safinn 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.17,
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,
)...