Home > Web Front-end > HTML Tutorial > How to put div2 in the middle of div1_html/css_WEB-ITnose

How to put div2 in the middle of div1_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 12:24:46
Original
1215 people have browsed it


How to make div2 in the horizontal middle of div1


Reply to discussion (solution)

You can use js to dynamically calculate the width and height of the two and then divide by 2 to set The padding of div1

<style>div{margin:0;padding:0}#div1{position:relative;height:200px;width:200px;background-color:red}#div2{position:absolute;height:100px;width:100px;background-color:yellow;top:50%;margin-top:-50px;left:50%;margin-left:-50px}</style><div id="div1">	<div id="div2">	</div></div>
Copy after login
Copy after login
Copy after login

<style>div{margin:0;padding:0}#div1{position:relative;height:200px;width:200px;background-color:red}#div2{position:absolute;height:100px;width:100px;background-color:yellow;top:50%;margin-top:-50px;left:50%;margin-left:-50px}</style><div id="div1">	<div id="div2">	</div></div>
Copy after login
Copy after login
Copy after login


The width of my div1 extends with the span of the browser

The extension is To extend, you can just remove the width attribute of div1
As long as the width of div2 is fixed.


<style>div{margin:0;padding:0}#div1{position:relative;height:200px;width:200px;background-color:red}#div2{position:absolute;height:100px;width:100px;background-color:yellow;top:50%;margin-top:-50px;left:50%;margin-left:-50px}</style><div id="div1">	<div id="div2">	</div></div>
Copy after login
Copy after login
Copy after login


My div1 width extends with the span of the browser

1111

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template