Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
182 views
in Technique[技术] by (71.8m points)

html - margin problem in css while using position element

I am pretty new to web development so I am facing a margin issue which I think I might be due to position element in css,I'm not sure though .Here in code I have posted below is just a code for practice purpose on position element in css. Here's my html code:

<!DOCTYPE html>

<head>
    <title>Position Demo</title>
    <link rel="stylesheet" type="text/css" href="style1.css">
</head>

<body>
    <header>
        <span class="title-text">Position Demo</span>
    </header>
    <div class="container-1"></div>
    <div class="container-2"></div>
</body>

And here's my css code:

html {
    font-size: 62.5%;
}

*,
html,
body {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
}

.title-text {
    font-size: 3rem;
    text-align: center;
    display: inline-block;
}

.container-1 {    <!-- this container has right margin even though I have set margin to 0--> 
    width: 10rem;
    height: 10rem;
    position: relative;
    top: 30%;
    left: 0;
    margin-right: 0;
    background-color: rgb(218, 173, 173);
}

.container-2 {   <!-- this container has right margin even though I have set margin to 0-->
    width: 10rem;
    height: 10rem;
    position: relative;
    top: 30%;
    left: 30%;
    margin-right: 0;
    background-color: rgb(149, 218, 183);
}
question from:https://stackoverflow.com/questions/65603135/margin-problem-in-css-while-using-position-element

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What you are facing in inspect mode, is not margin.

enter image description here

Just to make sure:

enter image description here

Each element, without changing the display property of the parent element, is placed below it's sibling element. I mean elements are displayed in the page based on their place in your html.
That's why browser shows that yellowish line right of the boxes, it means that this line is taken.
I suggest you set the display property for each section in html.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...