html – 为什么flex给出hr元素宽度为0?

html – 为什么flex给出hr元素宽度为0?,第1张

概述当我尝试将hr元素放入带有display:flex的容器中时,它会消失. (如果我删除display:flex,它会重新出现,所以我确信这是原因.) 我在blog post上读到,flex导致hr的宽度为零,我通过给它宽度100%确认(再次出现),但没有给出解释. 出于好奇(我只是将hr放在flex容器外面来解决问题),为什么会出现这种情况?使用flexbox会导致其他随机元素消失吗? 我不认为我 当我尝试将hr元素放入带有display:flex的容器中时,它会消失. (如果我删除display:flex,它会重新出现,所以我确信这是原因.)

我在blog post上读到,flex导致hr的宽度为零,我通过给它宽度100%确认(再次出现),但没有给出解释.

出于好奇(我只是将hr放在flex容器外面来解决问题),为什么会出现这种情况?使用flexBox会导致其他随机元素消失吗?

我不认为我的代码中的其他内容导致了这一点,但在这里是我的完整代码以防万一:

* {  /* Makes wIDth and height include padding,border */  Box-sizing: border-Box;}body {  Font-family: "Quicksand",sans-serif;  margin: 0;}h1,h2,h3,h4,h5,h6 {  color: #2d3c49;  text-transform: uppercase;}h1 {  Font-weight: 200;  /* browsers typically display h1 as 2em */  Font-size: 2.6em;  margin-bottom: 0;}/* Adds a bit of space above subTitle (set h1 bottom margin to 0) */header h4 {  margin-top: 7px;}/* top content */header {  display: flex;  max-wIDth: 1000px;  margin: 0 auto;  margin-top: 1em;  /* Vertically centers logo  */  align-items: center;}/* logo img */.logo {  height: 90px;  wIDth: auto;  margin-right: auto;}/* Only subTitle isn't aligned all the way to the right; this fixes it. Todo: figure out why doesn't apply to h1 text */.header-text {  display: flex;  justify-content: flex-end;}hr {  background-color: #7d97ad;  max-wIDth: 1000px;  margin-bottom: 1em;  border: none;  height: 3px;}.main-image {  max-wIDth: 1000px;  height: auto;}/* ApplIEs to content within <main> element (excludes header,footer) */.container {  display: flex;  flex-direction: column;  align-items: center;  justify-content: center;  max-wIDth: 1000px;  margin: 0 auto;}/* ApplIEs to project section (including header text) */.container-projects {  display: flex;  /* Parent container needs this for flex-item to take full wIDth in row */  flex-wrap: wrap;  flex-direction: row;  justify-content: space-between;  margin: 2em 0;}.portfolio-header {  /* Puts header in its own row without removing from container with row flex direction (setting parent container to wrap also required) */  wIDth: 100%;  text-align: left;  color: #7d97ad;}/* div containing single project's Title,image,text *//* Todo: add declarations */.project {  wIDth: 300px;  height: auto;}.project p,h3 {  text-align: center;}/* Images cropped with 3:2 ratio,scaled resolution down to 600 x 400 */.project-image {  wIDth: 300px;  height: auto;}footer {  text-align: center;  margin-top: 1em;  background-color: #ccc;  color: white;  padding: 2em;  Font-size: 1.1em;}/* Remove default 1em margin-top */footer p {  margin-top: 0;}/* ApplIEs to Font Awesome social icons */.fab {  margin: 0 0.5em;  color: white;}/* Changes social icon color to dark grey on hover */.fab:hover {  color: #2d3c49;}/* Media querIEs (breakpoints correspond to bootstrap breakpoints). 1rem = 16px *//* Small devices (landscape phones) */@media screen and (max-wIDth: 767px) {  h1 {    Font-size: 2em;  }  h2 {    Font-size: 1.5em;  }  h3 {    Font-size: 1.3em;  }  h4 {    Font-size: 1.1em;  }  /* Doesn't seem to be doing anything Todo: find out why   */  .portfolio-header {    margin-bottom: 1em;  }  /* Todo: make slightly wIDer  */  .container-projects {    margin: 1.5em 0 0 0;  }  header {    margin: 0 1em 0 0;  }  header,.container,footer {    max-wIDth: 100%;  }  /* Must specify max-wIDth for img even though parent .container has the same declaration because max-wIDth isn't inherited */  .container img {    max-wIDth: 100%;  }  .project {    /* Centers projects (aligned left otherwise) */    margin: 0 auto;  }  /* Aligns portfolio header text flush left of portfolio rows */  .portfolio-header {    wIDth: 300px;    margin: 10px auto;  }  .logo {    height: 50px;    wIDth: auto;  }}/* tablets */@media screen and (max-wIDth: 991px) {  h1 {    Font-size: 1.7rem;  }}/* Small laptops */@media screen and (max-wIDth: 1199px) {  h1 {    Font-size: 2rem;  }}@H_403_18@  
<header>  <img src="https://image.ibb.co/jVeP4S/udacity_logo.png" alt="Udacity logo" >  <!-- header is set to display: flex,and it only works on direct children. Without the div,its default row alignment puts header text sIDe by sIDe,but insIDe a div,it works on the div as a block and since header text items are no longer direct children,it has no effect on them -->  <div>    <h1>NatalIE Cardot</h1>    <h4 >Front-End Web Developer</h4>  </div></header><!-- Todo: put insIDe main? But makes it disappear --><main >  <hr>  <img src="https://image.ibb.co/cTcuM7/using_laptop_large.jpg" alt="Woman using laptop at home during the day" >  <section >    <h2 >Featured Work</h2>    <div >      <img  src="https://image.ibb.co/hv4c8n/santorini_small.jpg" alt="VIEw from island of Santorini on a sunny day">      <h3>Project No. 1</h3>      <p>Lorem ipsum dolor sit amet,consectetur adipiscing elit.</p>    </div>    <div >      <img  src="https://image.ibb.co/c9sKM7/coast_small.jpg" alt="distant vIEw of a rugged island with a sailboat nearby">      <h3>Project No. 2</h3>      <p>Lorem ipsum dolor sit amet,consectetur adipiscing elit.</p>    </div>    <div >      <img  src="https://image.ibb.co/eO9oES/mediterranean_small.jpg" alt="Bird's eye vIEw of a rocky beach with clear turquoise waters">      <h3>Project No. 3</h3>      <p>Lorem ipsum dolor sit amet,consectetur adipiscing elit.</p>    </div>  </section>  <!-- End of container-project --></main><footer>  <p>Created by me</p>  <!-- "fab" prefix new in Font Awesome version 5 (released 3/18) -->  <a href="https://github.com/natalIEcardot" target="_blank"><i ></i></a>  <a href="https://codepen.io/natalIEcardot/"><i ></i></a>  <a href="https://twitter.com/natalIEcardot/"><i ></i></a>  <a href="https://www.linkedin.com/in/natalIE-cardot/" target="_blank"><i ></i></a></footer>@H_403_18@解决方法 hr元素具有浏览器设置的默认样式.例如,以下是Chrome的功能:  

在d性容器中,自动边距消耗边缘方向上的所有可用空间.

所以-webkit-margin-start:auto(在LTR中,相当于margin-left:auto)和-webkit-margin-end:auto(在LTR中,相当于margin-right:auto),消耗所有可用空间hr元素的左侧和右侧,将其压缩为宽度:0(因为内部没有内容).

您可以使用宽度:100%覆盖这些自动边距.但是简单地覆盖保证金可能更有效:自动保证金:0.

但即便如此,在这种情况下,一旦删除自动边距,对齐项目:中心将从父级开始.

这会覆盖默认的align-items:stretch并且左/右自动边距正在执行相同的 *** 作.同样,hr被压缩到宽度:0.

因此,对您的hr规则进行以下两项调整:

hr {  margin: 0;  align-self: stretch;}@H_403_18@  
* {  /* Makes wIDth and height include padding,h6 {  color: #2d3c49;  text-transform: uppercase;}h1 {  Font-weight: 200;  /* browsers typically display h1 as 2em */  Font-size: 2.6em;  margin-bottom: 0;}/* Adds a bit of space above subTitle (set h1 bottom margin to 0) */header h4 {  margin-top: 7px;}/* top content */header {  display: flex;  max-wIDth: 1000px;  margin: 0 auto;  margin-top: 1em;  /* Vertically centers logo  */  align-items: center;}/* logo img */.logo {  height: 90px;  wIDth: auto;  margin-right: auto;}/* Only subTitle isn't aligned all the way to the right; this fixes it. Todo: figure out why doesn't apply to h1 text */.header-text {  display: flex;  justify-content: flex-end;}hr {  background-color: #7d97ad;  max-wIDth: 1000px;  /* margin-bottom: 1em; */  border: none;  height: 3px;  margin: 0 0 1em 0;    /* NEW */  align-self: stretch;  /* NEW */}.main-image {  max-wIDth: 1000px;  height: auto;}/* ApplIEs to content within <main> element (excludes header,consectetur adipiscing elit.</p>    </div>  </section>  <!-- End of container-project --></main><footer>  <p>Created by me</p>  <!-- "fab" prefix new in Font Awesome version 5 (released 3/18) -->  <a href="https://github.com/natalIEcardot" target="_blank"><i ></i></a>  <a href="https://codepen.io/natalIEcardot/"><i ></i></a>  <a href="https://twitter.com/natalIEcardot/"><i ></i></a>  <a href="https://www.linkedin.com/in/natalIE-cardot/" target="_blank"><i ></i></a></footer>@H_403_18@  

请注意,d性自动边距优先于关键字对齐属性(例如justify-content和align-items).如果设置了自动边距,则首先应用自动边距,消耗线上的所有可用空间. align-items排在第二位,但没有剩余空间,所以它无法做任何事情.

这就是为什么在上面的代码中,align-items在删除自动边距之前不会执行任何 *** 作.

07002

Prior to alignment via justify-content and align-self,any
positive free space is distributed to auto margins in that
dimension.

If free space is distributed to auto margins,the alignment
propertIEs will have no effect in that dimension because the margins
will have stolen all free space left over after flexing.

有关flex auto margin的更多详细信息:

> In CSS Flexbox,why are there no “justify-items” and “justify-self” properties?

总结

以上是内存溢出为你收集整理的html – 为什么flex给出hr元素宽度为0?全部内容,希望文章能够帮你解决html – 为什么flex给出hr元素宽度为0?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1039226.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存