简体   繁体   English

如何将内联元素与50vw的居中div对齐,以便正确调整大小?

[英]How do I align inline element to the side of a centered div with 50vw so that it resizes properly?

I have a div that is set to be 50% of the page's width at all times, and is centered. 我有一个div,它始终设置为页面宽度的50%,并且居中。 I would like to add an element to it's side so that the element touches the div's left edge (on different screen sizes). 我想在它的侧面添加一个元素,以便元素接触div的左边缘(在不同的屏幕尺寸上)。 The element has a fixed max-width. 该元素具有固定的最大宽度。

I've tried: 我试过了:

#element {
  position: absolute;
  align: left;
  padding-left: 3vw;
}

It works on most common screen sizes, but as I go very small or very big the element either overlaps with the div or there is a gap. 它适用于大多数常见的屏幕尺寸,但是当我非常小或非常大时,元素要么与div重叠,要么存在间隙。 What I would like ideally would be for the element to stick to the side of the div. 理想情况下,我希望元素能够坚持div的一面。

Absolute positioning is very problematic for this kind of layout. 绝对定位对于这种布局来说是非常有问题的。 I recommend a flexbox layout. 我推荐一个flexbox布局。

In order to achieve this layout we will need to add a couple divs. 为了实现这种布局,我们需要添加几个div。 One div to act as the 3rd column, I gave it the class ghost because it will contain nothing although the demo here uses a red border so you can see it. 作为第3列的一个div,我给了它类ghost因为它将包含任何内容,虽然这里的演示使用红色边框,所以你可以看到它。

The other div I added wraps the special element you want to stick to the middle div. 我添加的另一个div包含你想要坚持中间div的特殊元素。 This is so the element wrapper ( .element-wrap ) is a flex child, not the element itself. 这是元素包装器( .element-wrap )是一个flex子元素,而不是元素本身。

Next, give the container of these 3 divs display: flex , turning it into a flexbox container: 接下来,给这3个div的容器display: flex ,将其转换为flexbox容器:

section {
  display: flex;
  align-items: center; /* vertical alignment */
  justify-content: center; /* horizontal alignment */
}

The align-items property allows you to vertically align the items inside, while justify-content allows you to align them horizontally. align-items属性允许您垂直对齐内部项目,而justify-content允许您水平对齐它们。

The center div, which I gave a blue background, will of course be in the middle of the layout and maintains its width with width: 50vw; 中心div,我给了蓝色背景,当然将在布局的中间,并保持其宽度width: 50vw; . The 2 divs on the side have a class applied .flex-child which get this CSS: 侧面的2个div有一个类.flex-child获取此CSS:

.flex-child {
  flex-grow: 1;
  flex-basis: calc((100% - 50vw) / 2);
}

flex-grow: 1 allows those side divs to expand with the size of the screen. flex-grow: 1允许那些侧面div随着屏幕的大小而扩展。 The flex-basis rule determines the starting size, where I am using a calc() function to dynamically calculate the width. flex-basis规则确定起始大小,我使用calc()函数动态计算宽度。

Now, the special element with the yellow background needs to be positioned next to the blue background, which is achieved here by turning its wrapper div into a flexbox too and aligning the content to the right: 现在,黄色背景的特殊元素需要定位在蓝色背景旁边,这可以通过将其包装div转换为flexbox并将内容对齐到右边来实现:

.element-wrap {
  display: flex;
  justify-content: flex-end;
}

 * { box-sizing: border-box; } section { display: flex; align-items: center; justify-content: center; /* just for demo */ background: #eee; } .flex-child { flex-grow: 1; flex-basis: calc((100% - 50vw) / 2); /* just for demo */ border: 1px solid red; } .half-screen { width: 50vw; min-height: 300px; background: dodgerblue; } .element-wrap { display: flex; justify-content: flex-end; } .element { /* just for demo */ background: yellow; } 
 <section> <div class="flex-child element-wrap"> <div class="element"> ELEMENT that we are<br> getting to stick to the side<br> of the 50vw div. </div> </div> <div class="half-screen"> 50vw DIV </div> <div class="flex-child ghost"><!-- nothing here --></div> </section> 

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

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