0

I write a html page containing a header on top, following by a section part and an aside part, and then a footer part, and write some CSS codes for styling. here is the html and CSS code:

body{
    width: 80%;
    margin: 0 auto;
    background-color: peachpuff;
}


header, section, aside, footer, article{
    margin: 10px;
    padding: 10px;
    text-align: center;
    background-color: aliceblue;
    border: 1px solid cadetblue;
    font-size: 2rem;
    width: 100%;
    box-sizing: border-box;
}
header>h1{

    height: 100px;
    font-size: 2.5rem;
    text-shadow: 2px 2px 2px red;
}
article {
    width: 85%;
    margin: 0 auto;
}
section{
    width: 60%;
    height: 300px;
    margin-right:3px;
display: inline-block;
    /*float:left;*/
}

aside{
    width: 37%;
    margin-left:3px;
    height: 300px;
    float: right;
    display: inline-block;
}

footer{
    display: block;
    width: 100%;
    background-color: aliceblue;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="main.css">
</head>
<body>

<header>
    <h1>This is header.</h1>
</header>

<section>
    this is section.
    <article>this is article.</article>
</section>

<aside>aside part.</aside>

<footer>this is footer.</footer>

</body>
</html>

I set width:100% for header and footer, and it should make then to stretch so fill all the body width, as body is their parent node. but I notice that it cause header and footer that go beyond the body width at right side! if I comment that, every thing will be fine, but why it doesn't work as expected?

and second question, I want section and aside to be located next to each other, and by setting float:right; for aside tag they are, but if I set float:left; for section element too, while they still are next to each other, footer become their background too! why that happens?

thanks.

6
  • the margin make's the header push himself out of the body Commented Dec 21, 2018 at 8:33
  • @ Ramon de Vries - but i set box-sizing: border-box; so it margin couldn't cause that, isn't it? Commented Dec 21, 2018 at 8:37
  • try my answer, that might be working for you Commented Dec 21, 2018 at 8:38
  • 1
    The default is width auto because they are block level elements. No need to set width to 100%. Commented Dec 21, 2018 at 8:40
  • @vsync - you're right, but why setting width cause this issue? actually it should has no effect, but it has! Commented Dec 21, 2018 at 8:43

2 Answers 2

3

The margin:10px; will push the header out of the body, remove the margin from the header and it works fine.

body{
    width: 80%;
    margin: 0 auto;
    background-color: peachpuff;
}


header, section, aside, footer, article{
    padding: 10px;
    text-align: center;
    background-color: aliceblue;
    border: 1px solid cadetblue;
    font-size: 2rem;
    width: 100%;
    box-sizing: border-box;
}
header>h1{

    height: 100px;
    font-size: 2.5rem;
    text-shadow: 2px 2px 2px red;
}
article {
    width: 85%;
    margin: 0 auto;
}
section{
    width: 60%;
    height: 300px;
    margin-right:3px;
display: inline-block;
    /*float:left;*/
}

aside{
    width: 37%;
    margin-left:3px;
    height: 300px;
    float: right;
    display: inline-block;
}

footer{
    display: block;
    width: 100%;
    background-color: aliceblue;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="main.css">
</head>
<body>

<header>
    <h1>This is header.</h1>
</header>

<section>
    this is section.
    <article>this is article.</article>
</section>

<aside>aside part.</aside>

<footer>this is footer.</footer>

</body>
</html>

Sign up to request clarification or add additional context in comments.

2 Comments

yes, it works, but i set box-sizing: border-box for all elements, so why still margin change the dimensions of the boxes?
@feelfree, https://www.w3schools.com/css/css3_box-sizing.asp box-sizing will only affect the padding on elements, not the margin
1

body{
    width: 80%;
    margin: 0 auto;
    background-color: peachpuff;
}


header, section, aside, footer, article{
    margin: 10px 0 0 0;
    padding: 10px;
    text-align: center;
    background-color: aliceblue;
    border: 1px solid cadetblue;
    font-size: 2rem;
    width: 100%;
    box-sizing: border-box;
}
header>h1{

    height: 100px;
    font-size: 2.5rem;
    text-shadow: 2px 2px 2px red;
}
article {
    width: 85%;
    margin: 0 auto;
}
section{
    width: 60%;
    height: 300px;
    margin-right:3px;
display: inline-block;
    /*float:left;*/
}

aside{
    width: 37%;
    margin-left:3px;
    height: 300px;
    float: right;
    display: inline-block;
}

footer{
    display: block;
    width: 100%;
    background-color: aliceblue;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="main.css">
</head>
<body>

<header>
    <h1>This is header.</h1>
</header>

<section>
    this is section.
    <article>this is article.</article>
</section>

<aside>aside part.</aside>

<footer>this is footer.</footer>

</body>
</html>

1 Comment

do you want like this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.