简体   繁体   English

中心 alignment 使用 flex-box 没有空格

[英]Center alignment using flex-box without empty spaces

Hi everyone , i'm currently working on a note project, i'm trying to center my notes using flex box without any useless spaces , take a look at the photos you will understand what i mean.大家好,我目前正在做一个笔记项目,我正在尝试使用 flex box 将我的笔记居中,没有任何无用的空间,看看照片你就会明白我的意思。

在此处输入图像描述

bottom space is just fine , the problem is the space that is caused by flex box on the right side , i want the yellow box to resize and fit but stay centered as well , the yellow box is a ul tag and the red boxes are li tags.底部空间很好,问题是由右侧的弹性框引起的空间,我希望黄色框调整大小并适合但也保持居中,黄色框是 ul 标签,红色框是 li标签。

here is the css styles: ( container includes the whole page except the header )这是 css styles:(容器包括除 header 之外的整个页面

.container {
  width: 100%;
  display: flex;
  height: 100vh;
  align-items: flex-start;
  justify-content: center;
  position: relative;
}

ul {
  align-content: flex-start;
  margin: 40px auto;
  max-width: 1400px;
  width: fit-content;
  height: 80vh;
  display: flex;
  justify-content: flex-start;
  align-items: flex-start;
  flex-wrap: wrap;
  overflow-y: scroll;
  margin-top: 100px;
  border: 3px solid yellow;
}

ul li {
  border: 2px solid red;
  list-style-type: none;
  font-size: 20px;
  margin: 5px;
  background: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5));
  backdrop-filter: blur(10px);
  width: 330px;
  height: 200px;
  display: flex;
  overflow: hidden;
  justify-content: space-between;
  align-items: flex-start;
  flex-direction: column;
  padding: 20px;
  border-radius: 5px;
  position: relative;
  transition: height 0.3s;
}

here is the HTML codes : ( sorry it's not clean )这是HTML 代码:(对不起,它不干净)

<title>Dashboard</title>

<div class="container">
  <ul>
    <li class="item">
      <div class="priority"></div><span class="title c-title"> title </span>
      <span class="content c-content"> Content </span>
      <span class="date"> date </span>

      <div class="item_buttons">
        <a href=""><button class="detail faint_color"><img class="edit-icon" src="/icons/invisible.svg" alt="see icon"></button></button></a>
        <a href=""><button class="detail faint_color"><img class="edit-icon" src="/icons/edit.svg" alt="edit icon"></button></button></a>
        <a href=""><button class="deleteButton detail faint_color"><img class="edit-icon" src="/icons/trash.svg" alt="delete icon"></button></a>
      </div>
    </li>
  </ul>
</div>

<div class="addButton">
  <img class="addButtonImage" src="/add.svg" alt="add new note" />
  <div class="smallMenu hide">
    <a href="/modify/folders">
      <button class="folders_button"><img src="/icons/folder.svg" alt="folder icon"></button>
    </a>
    <a href="/note/add">
      <button class="addNote_button"><img src="/icons/pen.svg" alt="pen icon"></button>
    </a>
  </div>
  </div>

As I said in the comment, I don't think you can solve this using pure HTML/CSS, but you could calculate how many elements can fit at given time inside your .container and change width of your ul .正如我在评论中所说,我认为您无法使用纯 HTML/CSS 解决此问题,但您可以计算在给定时间在您的.container中可以容纳多少元素并更改ulwidth

Check this code below:检查下面的代码:

 function totalWidth(element) { style = window.getComputedStyle(element) return parseFloat(style.marginLeft) + parseFloat(style.marginLeft) + parseFloat(style.width) } function resizeContainer() { const container = document.querySelector(".container") const element = document.querySelector(".container ul") const children = document.querySelector(".container ul li") const childrenWidth = totalWidth(children) const amountFits = Math.floor(Math.min(container.offsetWidth, 1400) / childrenWidth) element.style.width = `${childrenWidth * amountFits}px` } resizeContainer() window.addEventListener("resize", resizeContainer)
 .container { width: 100%; display: flex; height: 100vh; align-items: flex-start; justify-content: center; position: relative; } ul { align-content: flex-start; margin: 40px auto; max-width: 1400px; width: fit-content; height: 80vh; display: flex; justify-content: flex-start; align-items: flex-start; flex-wrap: wrap; overflow-y: scroll; margin-top: 100px; border: 3px solid yellow; padding: 0; } ul li { border: 2px solid red; list-style-type: none; font-size: 20px; margin: 5px; background: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)); backdrop-filter: blur(10px); width: 330px; height: 200px; display: flex; overflow: hidden; justify-content: space-between; align-items: flex-start; flex-direction: column; padding: 20px; border-radius: 5px; position: relative; transition: height 0.3s; box-sizing: border-box; }
 <div class="container"> <ul> <li>test</li><li>test</li> <li>test</li><li>test</li> </ul> </div>

Mind the fact that I added box-sizing: border-box;请注意我添加了box-sizing: border-box; to your ul li so it's a bit easier to calculate the actual size of the element.给你的ul li这样计算元素的实际大小会更容易一些。 Naturally you could ignore this and change my totalWidth method to calculate it the way you'd like.当然,您可以忽略它并更改我的totalWidth方法以按照您喜欢的方式计算它。

I don't think you can do it differently but I'd like to be proven wrong.我不认为你可以做不同的事情,但我想被证明是错误的。

There's a number of solutions for this one, depending which way you want to go,这个有很多解决方案,具体取决于您想要 go 的方式,

If the notes don't have to be exactly 330px wide, you can achieve removing the gaps by replacing:如果注释不必正好是 330px 宽,您可以通过替换来消除间隙:

width: 330px;

with

flex: 200px;

This takes advantage of the fluid responsiveness of flexbox.这利用了 flexbox 的流体响应能力。

One issue with this flex solution is the 'leftover' element upon wrapping: eg with three elements across, the fourth will wrap and grow to the full width of the parent:这个 flex 解决方案的一个问题是包装时的“剩余”元素:例如,三个元素横跨,第四个将包装并增长到父级的整个宽度:

CSS flex-grow 导致最后一个元素伸展

To get around this with flex, you would need to either:要使用 flex 解决这个问题,您需要:

  1. change flex to something like flex: 0 1 50%;flex更改为flex: 0 1 50%; with percentage values and flex-grow disabled, which means you would need @media queries to give the lists the fluidity they had before禁用百分比值和 flex-grow,这意味着您将需要@media查询来为列表提供它们之前的流动性
  2. use a JavaScript solution to dynamically set the width of the last element based on the other lists (like @SnoopFrog's answer)使用 JavaScript 解决方案根据其他列表动态设置最后一个元素的宽度(如@SnoopFrog 的回答)

If you do need the notes to be 330px wide, I would set the parent <ul> to have a set size twice that of the lists width: 785px;如果你确实需要注释为 330px 宽,我会将父<ul>的设置大小设置为列表width: 785px; and ensure that justify-content: center;并确保justify-content: center;


Alternatively, turning to CSS grid or a simple inline-block method may fit your needs but both would need @media queries for responsivity.或者,转向 CSS grid或简单的inline-block方法可能会满足您的需求,但两者都需要@media查询才能响应。

 * { /* just for reset and for remove all margins and paddings */ box-sizing: border-box; margin: 0; padding: 0; } ul { display: flex; /*justify-content: flex-start; don't need this, is start by default align-items: flex-start;*/ /* magical words that they can align internal box(li) */ justify-content: center; align-items: center; margin: 40px auto; /*auto <--> both part is ok*/ max-width: 1400px; /* just for see the center(you can remove this width static of 25rem */ width: 25rem; /* width: fit-content; */ height: 80vh; overflow-y: scroll; margin-top: 100px; border: 3px solid yellow; } ul li { display: flex; justify-content: space-between; align-items: flex-start; flex-direction: column; border: 2px solid red; list-style-type: none; background: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)); backdrop-filter: blur(10px); width: min(100%, 330px); /*make it responsive, when shrink down, under 330px, is take 100% of width avaiable, and not overflowing*/ height: 200px; margin: 10px auto; /*in every elements, you can use margin auto for both parts*/ padding: 20px; border-radius: 5px; transition: all 0.3s; /*transition:height 0.3 --> not height, but all inside:*/ /* overflow; hidden, don't need: that not should be overflow */ /* position; relative: Is by default, like height,auto*/ } /* if you want to style your buttons, write this code all times */ /* remove deafult aspect of a btn and others, beacuse every browser take a different style*/ button: input; a { appearance: none; -webkit-appearance: none; -moz-appearance: none; }
 <title>Dashboard</title> <ul> <div class="wrapper"> <li class="item"> <div class="priority"></div> <span class="title c-title"> title </span> <span class="content c-content"> Content </span> <span class="date"> date </span> <div class="item_buttons"> <a href=""> <button class="detail faint_color"> <img class="edit-icon" src="/icons/invisible.svg" alt="see icon"> </button> </a> <a href=""> <button class="detail faint_color"> <img class="edit-icon" src="/icons/edit.svg" alt="edit icon"> </button> </a> <a href=""> <button class="deleteButton detail faint_color"> <img class="edit-icon" src="/icons/trash.svg" alt="delete icon"> </button> </a> </div> </li> </div> </ul> <div class="addButton"> <img class="addButtonImage" src="/add.svg" alt="add new note" /> <div class="smallMenu hide"> <a href="/modify/folders"> <button class="folders_button"> <img src="/icons/folder.svg" alt="folder icon"> </button> </a> <a href="/note/add"> <button class="addNote_button"> <img src="/icons/pen.svg" alt="pen icon"> </button> </a> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM