/    Sign up×
Community /Pin to ProfileBookmark

why those divs are not aligned

i have this simple page :

[CODE]<!DOCTYPE html>
<html lang=”en”>
<head>
<title>chapter 1 me!</title>
<meta charset=”UTF-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
<link href=”style.css” rel=”stylesheet” type=”text/css”/>
</head>
<body>

<link rel=”stylesheet” href=”style.css”>
<div class=”upperdiv”>
some upper text
</div>
<div class=”parentdiv”>
<span class=”leftsidediv”>
<p>some in p 1some in p 1some in p 1some in p 1some in p 1some in p 1some in p 1some in p 1
some in p 1some in p 1some in p 1some in p 1some in p 1some in p 1some in p 1
</p>
<p>
some text in p2some text in p2some text in p2some text in p2some text in p2some text in p2
some text in p2some text in p2some text in p2some text in p2some text in p2some text in p2
some text in p2some text in p2some text in p2some text in p2
</p>
</span>
<span class=”rightsidediv”>
<h2>Newsletter</h2>
<p>some text in newsletter p some text in newsletter p some text in newsletter p
some text in newsletter p </p>
</span>
</div>

</body>
</html>[/CODE]

and css :

[CODE]
.leftsidediv
{
display : inline-block;
border : 1px solid black;
background-color: green;
width : 70%;
height : 400px;
margin : 5px;
padding : 5px;
}

.rightsidediv
{
display : inline-block;
border : 1px solid black;
background-color: green;
width : 25%;
height : 400px;
margin : 5px;
padding : 5px;
}

.parentdiv
{
background-color : grey;
border : 5px dashed blue;
}
.upperdiv
{
height : 400px;
border : 1px dashed blue;
}
[/CODE]

the resulting divs are not aligned , the upper margin of one is positioned above the other. so why is that ?

[ATTACH=CONFIG]17277[/ATTACH]

[canned-message]attachments-removed-during-migration[/canned-message]

to post a comment
CSS

5 Comments(s)

Copy linkTweet thisAlerts:
@Kevin2Aug 24.2016 — Start with valid HTML. <p> cannot be a child of <span>, so change those <span> tags to <div>s.

Then add [b]vertical-align: top;[/b] to the styles for those <div>s.
Copy linkTweet thisAlerts:
@jeorge_kabbiauthorAug 25.2016 — thanks i did that and it works. the question then why if i didn't add "vertical-align: top;" the divs are not aligned ? inline-block divs should be "aligned " to each others. no ?
Copy linkTweet thisAlerts:
@Kevin2Aug 25.2016 — One would think that 2 elements with the same height styled inline-block should align vertically by default, but no. It seems that the default is [b]vertical-align: scatter;[/b] ?

As an FYI in your case you could use [b]vertical-align: middle;[/b] or [b]vertical-align: bottom;[/b] and it would accomplish the same thing.
Copy linkTweet thisAlerts:
@cootheadAug 25.2016 — Hi there jeorge_kabbi,

[indent]

the CSS [color=navy][i]"display: flex"[/i][/color] might be a better option here,

especially if you want the left and right containers to

maintain the same height regardless of content. ?

[color=navy]
&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;meta name="viewport" content="width=device-width,height=device-height,initial-scale=1"&gt;
&lt;title&gt;chapter 1 me!&lt;/title&gt;

&lt;link href="style.css" rel="stylesheet" media="screen"&gt;

&lt;style media="screen"&gt;
h1 {
text-align:center;
}

#upper {
margin-bottom: 1em;
border : 0.06em dashed blue;
}

#parent {
display: flex;
padding:1% 0;
background-color : grey;
border : 0.3em dashed blue;
}

#leftside,
#rightside {
width : 71%;
margin : 0 1%;
padding : 1em;
border : 0.06em solid black;
box-sizing: border-box;
background-color: green;
}

#rightside{
width : 26%;
margin : 0 1% 0 0;
}
&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;h1&gt;Chapter One&lt;/h1&gt;

&lt;div id="upper"&gt;some upper text&lt;/div&gt;

&lt;div id="parent"&gt;

&lt;div id="leftside"&gt;
&lt;p&gt;
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur sit amet sem sed libero
bibendum tempus faucibus quis mi. Nulla rhoncus vel ipsum in volutpat. Nam tortor nibh,
posuere ac lorem ut, suscipit tincidunt leo. Duis interdum justo ac justo vehicula consequat.
Curabitur et volutpat nibh. Phasellus rutrum lacus at dolor placerat feugiat. Morbi porta,
sapien nec molestie molestie, odio magna vestibulum lorem, vitae feugiat est leo sit amet
nunc. Curabitur ornare tempor turpis. In nibh sem, porta ac magna sed, pretium commodo
odio. Sed porttitor augue velit, quis placerat diam sodales ac. Curabitur vitae porta ex.
Praesent rutrum ex fringilla tellus tincidunt interdum. Proin molestie lectus consectetur
purus aliquam porttitor. Fusce ac nisi ac magna scelerisque finibus a vitae sem.
&lt;/p&gt;&lt;p&gt;
Donec vehicula diam non leo efficitur, id euismod odio tincidunt. Vivamus auctor viverra
purus vitae lobortis. Sed et libero non diam tempor sagittis. Quisque vel egestas ipsum.
Integer sed elit eu orci blandit commodo in in erat. Donec blandit, mi at gravida varius,
nulla tellus vulputate ex, vitae euismod erat lectus rutrum diam. *** sociis natoque
penatibus et magnis dis parturient montes, nascetur ridiculus mus. Curabitur tristique
varius ullamcorper.
&lt;/p&gt;
&lt;/div&gt;

&lt;div id="rightside"&gt;
&lt;h2&gt;Newsletter&lt;/h2&gt;
&lt;p&gt;
Nam venenatis diam ante, et cursus elit porttitor at. Morbi mattis leo at ex vehicula, non
vestibulum ligula aliquam. Maecenas non nibh sollicitudin, porttitor odio in, elementum arcu.
Donec pulvinar orci enim.
&lt;/p&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;[/color]

[/indent]

[i]coothead[/i]
Copy linkTweet thisAlerts:
@jeorge_kabbiauthorAug 25.2016 — One would think that 2 elements with the same height styled inline-block should align vertically by default, but no. It seems that the default is [b]vertical-align: scatter;[/b] ?

As an FYI in your case you could use [b]vertical-align: middle;[/b] or [b]vertical-align: bottom;[/b] and it would accomplish the same thing.[/QUOTE]


that what bugs me. it seems to me that css is not mature. it is erratic!!

dunno
×

Success!

Help @jeorge_kabbi 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 5.4,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

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