欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

关于HTML中父子级外边距合并的问题

程序员文章站 2022-03-26 17:57:12
父子级外边距合并的解决方式解决方式:1解决方法2解决方法3解决方法4下面展示一些代码,看看出现的结果。 Document</t...</div> <div class="content"> <p> <br></p> <div class="toc"> <h3> 父子级外边距合并的解决方式 </h3> <ul><ul> <li> <a href="#1_41">解决方式:1</a> </li> <li> <a href="#2_50">解决方法2</a> </li> <li> <a href="#3_61">解决方法3</a> </li> <li> <a href="#4_68">解决方法4</a> </li> <li> <a href="#_84">方法总结</a> </li> </ul></ul> </div> <br> 下面展示一些代码,看看出现的结果。 <p> <br></p> <pre><code class="prism language-javascript"><span class="token operator"><</span><span class="token operator">!</span><span class="token constant">DOCTYPE</span> html<span class="token operator">></span> <span class="token operator"><</span>html lang<span class="token operator">=</span><span class="token string">"en"</span><span class="token operator">></span> <span class="token operator"><</span>head<span class="token operator">></span> <span class="token operator"><</span>meta charset<span class="token operator">=</span><span class="token string">"UTF-8"</span><span class="token operator">></span> <span class="token operator"><</span>meta name<span class="token operator">=</span><span class="token string">"viewport"</span> content<span class="token operator">=</span><span class="token string">"width=device-width, initial-scale=1.0"</span><span class="token operator">></span> <span class="token operator"><</span>title<span class="token operator">></span>Document<span class="token operator"><</span><span class="token operator">/</span>title<span class="token operator">></span> <span class="token operator"><</span>style<span class="token operator">></span> <span class="token operator">*</span><span class="token punctuation">{</span> margin<span class="token punctuation">:</span> <span class="token number">0</span> auto<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">.</span>outer<span class="token punctuation">{</span> width<span class="token punctuation">:</span> <span class="token number">200</span>px<span class="token punctuation">;</span> height<span class="token punctuation">:</span> <span class="token number">200</span>px<span class="token punctuation">;</span> background<span class="token operator">-</span>color<span class="token punctuation">:</span> red<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">.</span>outer<span class="token operator">></span>div<span class="token punctuation">{</span> width<span class="token punctuation">:</span> <span class="token number">50</span>px<span class="token punctuation">;</span> height<span class="token punctuation">:</span> <span class="token number">50</span>px<span class="token punctuation">;</span> background<span class="token operator">-</span>color<span class="token punctuation">:</span> blue<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token operator"><</span><span class="token operator">/</span>style<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>head<span class="token operator">></span> <span class="token operator"><</span>body<span class="token operator">></span> <span class="token operator"><</span>div <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">"outer"</span><span class="token operator">></span> <span class="token operator"><</span>div<span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>body<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>html<span class="token operator">></span> </code></pre> <p> 所得结果如下图<img onerror="this.src='/statics/superweb999/images/image_error.jpg'"src="/default/index/img?u=aHR0cDovL2ltYWdlczUuMTBxaWFud2FuLmNvbS8xMHFpYW53YW4vMjAyMDA4MDgvYl8wXzIwMjAwODA4MTkyOTAyMjUwOS5wbmc=" alt="关于HTML中父子级外边距合并的问题" title="关于HTML中父子级外边距合并的问题"><br> 接下来使蓝色区域与红色区域的上边框产生一定的间距,添加如下代码,看看结果。 </p> <pre><code class="prism language-javascript"><span class="token punctuation">.</span>outer<span class="token operator">></span>div<span class="token punctuation">{</span> margin<span class="token operator">-</span>top<span class="token punctuation">:</span><span class="token number">100</span>px<span class="token punctuation">;</span> <span class="token punctuation">}</span> </code></pre> <p> 所得结果如下图,蓝色区域与红色区域的上边框并没有产生间距<br><img onerror="this.src='/statics/superweb999/images/image_error.jpg'"src="/default/index/img?u=aHR0cDovL2ltYWdlczUuMTBxaWFud2FuLmNvbS8xMHFpYW53YW4vMjAyMDA4MDgvYl8wXzIwMjAwODA4MTkyOTAzMTQ0OC5qcGc=" alt="关于HTML中父子级外边距合并的问题" title="关于HTML中父子级外边距合并的问题"></p> <h2> <a id="1_41" target="_blank" rel="nofollow" href="/default/index/url?u=aHR0cHM6Ly93d3cuMTBxaWFud2FuLmNvbQ=="></a>解决方式:1 </h2> <pre><code class="prism language-javascript"><span class="token punctuation">.</span>outer<span class="token punctuation">{</span> border<span class="token punctuation">:</span><span class="token number">1</span>px solid red<span class="token punctuation">;</span> <span class="token punctuation">}</span> </code></pre> <p> 蓝色区域与红色区域的上边框分离了<br><img onerror="this.src='/statics/superweb999/images/image_error.jpg'"src="/default/index/img?u=aHR0cDovL2ltYWdlczUuMTBxaWFud2FuLmNvbS8xMHFpYW53YW4vMjAyMDA4MDgvYl8wXzIwMjAwODA4MTkyOTAzMzY0Ni5qcGc=" alt="关于HTML中父子级外边距合并的问题" title="关于HTML中父子级外边距合并的问题"></p> <h2> <a id="2_50" target="_blank" rel="nofollow" href="/default/index/url?u=aHR0cHM6Ly93d3cuMTBxaWFud2FuLmNvbQ=="></a>解决方法2 </h2> <pre><code class="prism language-javascript"><span class="token punctuation">.</span>outer<span class="token operator">></span>div<span class="token punctuation">{</span> margin<span class="token operator">-</span>left<span class="token punctuation">:</span><span class="token number">100</span>px<span class="token punctuation">;</span> <span class="token comment">/*可自行设置距离*/</span> float<span class="token punctuation">:</span>left<span class="token punctuation">;</span> <span class="token punctuation">}</span> </code></pre> <p> 所得结果如下图<br><img onerror="this.src='/statics/superweb999/images/image_error.jpg'"src="/default/index/img?u=aHR0cDovL2ltYWdlczUuMTBxaWFud2FuLmNvbS8xMHFpYW53YW4vMjAyMDA4MDgvYl8wXzIwMjAwODA4MTkyOTA0MTM1OS5qcGc=" alt="关于HTML中父子级外边距合并的问题" title="关于HTML中父子级外边距合并的问题"></p> <h2> <a id="3_61" target="_blank" rel="nofollow" href="/default/index/url?u=aHR0cHM6Ly93d3cuMTBxaWFud2FuLmNvbQ=="></a>解决方法3 </h2> <pre><code class="prism language-javascript"><span class="token punctuation">.</span>outer<span class="token punctuation">{</span> overflow<span class="token punctuation">:</span>hidden<span class="token punctuation">;</span> <span class="token punctuation">}</span> </code></pre> <p> <img onerror="this.src='/statics/superweb999/images/image_error.jpg'"src="/default/index/img?u=aHR0cDovL2ltYWdlczUuMTBxaWFud2FuLmNvbS8xMHFpYW53YW4vMjAyMDA4MDgvYl8wXzIwMjAwODA4MTkyOTA0MjMwMS5qcGc=" alt="关于HTML中父子级外边距合并的问题" title="关于HTML中父子级外边距合并的问题"></p> <h2> <a id="4_68" target="_blank" rel="nofollow" href="/default/index/url?u=aHR0cHM6Ly93d3cuMTBxaWFud2FuLmNvbQ=="></a>解决方法4 </h2> <p> 将本应该设置给子元素的外边距设置给父元素的内边距 </p> <pre><code class="prism language-javascript"><span class="token operator">*</span><span class="token punctuation">{</span>box<span class="token operator">-</span>sizing<span class="token punctuation">:</span>border<span class="token operator">-</span>box<span class="token punctuation">;</span><span class="token punctuation">}</span> <span class="token comment">/*将盒子类型设置为边框盒子*/</span> <span class="token punctuation">.</span>outer<span class="token punctuation">{</span> padding<span class="token operator">-</span>top<span class="token punctuation">:</span><span class="token number">100</span>px<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">.</span>outer<span class="token operator">></span>div<span class="token punctuation">{</span> <span class="token comment">/*margin-left:100px;*/</span> <span class="token punctuation">}</span> </code></pre> <p> 也能得到下图<br><img onerror="this.src='/statics/superweb999/images/image_error.jpg'"src="/default/index/img?u=aHR0cDovL2ltYWdlczUuMTBxaWFud2FuLmNvbS8xMHFpYW53YW4vMjAyMDA4MDgvYl8wXzIwMjAwODA4MTkyOTA1MzUyNy5qcGc=" alt="关于HTML中父子级外边距合并的问题" title="关于HTML中父子级外边距合并的问题"><br> 还有一些方法,等着你去探索!!! </p> <h2> <a id="_84" target="_blank" rel="nofollow" href="/default/index/url?u=aHR0cHM6Ly93d3cuMTBxaWFud2FuLmNvbQ=="></a>方法总结 </h2> <p> 1.给父元素添加边框属性<br> 2.给父元素/子元素添加浮动属性<br> 3.给父级元素设置overflow:hidden属性<br> 4.将本应该设置给子元素的外边距设置给父元素的内边距 </p> <p> 本文地址:https://blog.csdn.net/song_dehao/article/details/107864307 </p> </div> <div class="tags"> 相关标签: <a href="/t/html/" target="_blank"> html </a> <a href="/t/css/" target="_blank"> css </a> <a href="/t/html5/" target="_blank"> html5 </a> </div> <div class="info-pre-next"> <p> 上一篇: <a href="/article/341094.html"> 小刘同学的第一百零一篇博文 </a> </p> <p> 下一篇: <a href="/article/341096.html"> 浅谈工程化\浅谈模块化\浅谈模块化\浅谈组件化\浅谈规范化 </a> </p> </div> <div class="wz_tuijian"> <p> 推荐阅读 </p> <ul> <li> <a href="/article/1561045.html" target="_blank" title="关于css中margin的值和垂直外边距重叠问题"> <h2> 关于css中margin的值和垂直外边距重叠问题 </h2> </a> </li> <li> <a href="/article/1556371.html" target="_blank" title="关于HTML中父子级外边距合并的问题"> <h2> 关于HTML中父子级外边距合并的问题 </h2> </a> </li> <li> <a href="/article/341095.html" target="_blank" title="关于HTML中父子级外边距合并的问题"> <h2> 关于HTML中父子级外边距合并的问题 </h2> </a> </li> <li> <a href="/article/320622.html" target="_blank" title="请教关于CSS中padding内边距的问题_html/css_WEB-ITnose"> <h2> 请教关于CSS中padding内边距的问题_html/css_WEB-ITnose </h2> </a> </li> <li> <a href="/article/243527.html" target="_blank" title="请教关于CSS中padding内边距的问题_html/css_WEB-ITnose"> <h2> 请教关于CSS中padding内边距的问题_html/css_WEB-ITnose </h2> </a> </li> <li> <a href="/article/142573.html" target="_blank" title="关于css中margin的值和垂直外边距重叠问题"> <h2> 关于css中margin的值和垂直外边距重叠问题 </h2> </a> </li> </ul> </div> </article> </div> </main> <footer><div class="box"><div class="ft_nav"><div class="ft_about"><p>关于网站</p><ul><li><a href="/sitemap.xml" target="_blank" title="网站地图">网站地图</a></li><li><a href="/list/2/" title="最新程序员文章站">最新程序员文章站</a></li></ul></div><div class="ft_contact"><ul><li>本站所有数据收集于网络如有侵犯到您的权益,请联系我们进行下架处理。</li><li class="email_show"></li></ul></div></div><div class="copyright"><div class="cr_left"><p> 备案号:<a href="https://beian.miit.gov.cn/#/Integrated/index" target="_blank">粤ICP备20058927号</a></p><p>© Copyright © 2020-2022 www.superweb999.com 程序员文章站. </p></div></div></div></footer> </body> </html>