简体   繁体   English

如何在将项目保留在列中的同时对齐左侧项目?

[英]How can I align items left while keeping them in a column?

I am trying to align items left while keeping them in a column, however, when I put items in a column, it defaults to re-centering the items on the page.我试图在将项目保留在列中的同时对齐左侧的项目,但是,当我将项目放入列中时,默认情况下会重新将页面上的项目居中。

Here's what I have:这是我所拥有的:

HTML HTML

<div className='postHeader'> 
   <div>{post.title}</div>
   <div>{post.author}</div>
</div>

CSS CSS

.postHeader {
  padding-top: 60px;
  padding-left: 25px;
  display: flex;
  justify-content: flex-start;
  flex-direction: column;
}

The alignment in the cross-axis (when you have a column flexbox this is the horizontal axis) is determined by the property align-items .横轴的对齐方式(当你有一个flexbox这是横轴)由属性align-items (the default value is stretch which causes the flex items to extend all the way to the end of the flexbox container) (默认值是stretch ,这会导致 flex 项目一直延伸到 flexbox 容器的末尾)

Set align-items: flex-start - see demo below:设置align-items: flex-start - 请参见下面的演示:

 div { border: 1px solid; } .postHeader { padding-top: 60px; padding-left: 25px; display: flex; justify-content: flex-start; flex-direction: column; align-items: flex-start; /* ADDED */ }
 <div class='postHeader'> <div>{post.title}</div> <div>{post.author}</div> </div>

text-align属性指定元素中文本的水平对齐方式。

text-align:left

Add one more property in your CSS snippet with align-items: flex-start使用align-items: flex-start在 CSS 代码段中再添加一个属性align-items: flex-start

HTML HTML

<div className='postHeader'> 
   <div>{post.title}</div>
   <div>{post.author}</div>
</div>

CSS CSS

.postHeader {
  padding-top: 60px;
  padding-left: 25px;
  display: flex;
  justify-content: flex-start;
  flex-direction: column;
  align-items: flex-start; /* ADDED */
}

如何在<div>同时保持它在右边?</div><div id="text_translate"><p> 我不知道该怎么做,我试过使用 position,但它只是对齐到页面的中间,而不是 div。</p><p> 我试图在中间垂直对齐它。 </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-css lang-css prettyprint-override"> .img1 { float: right; width: 20%; margin-right: 200px; display: inline-block; vertical-align: middle; }.img1 { border-radius: 50%; border: 5px solid white }.container { width: 100%; height: 500px; background-color: black; display: inline-block; }</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;div class="container"&gt; &lt;img class="img1" src="https://via.placeholder.com/500.jpg"&gt; &lt;/div&gt;</pre></div></div><p></p></div> - how can I vertically align this image within the <div> while keeping it to the right?

暂无
暂无

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

相关问题 如何在使div的子元素垂直浮动的同时保持左右浮动? - How can I vertically-align child elements of a div while keeping them floated to the left and right? 如何将两个div对齐到右侧,同时保持它们向左对齐 - How to align two divs to the right while keeping them align to the left 我如何将我的物品左对齐并缩小它们 - How can i align my items left and shrink them 如何在保持 flex 项目左对齐的同时居中 flex-wrap 行? - How can I center flex-wrap rows while keeping the flex items left justified? 将右列与屏幕边缘对齐,同时保持左列边距与站点宽度相同 - Align right column to edge of screen while keeping left column margin the same as site width 如何对齐表单中的项目,使问题位于左侧,而选项位于右侧 - How do I align the items in my form so the questions are on the left while options are on the right 如何垂直居中 li 导航菜单但将其水平对齐到右侧,同时保持徽标图标水平对齐左和 ctrd vert? - How do I vertically center li navigation menu but horizontally align it to the right while keeping logo icon horizontally aligned left and ctrd vert? 如何在页面上将多个div居中放置,并在div降到下一行时将它们左对齐? - How can I center multiple divs on a page, and left-align them when a div drops to next row? 如何在<div>同时保持它在右边?</div><div id="text_translate"><p> 我不知道该怎么做,我试过使用 position,但它只是对齐到页面的中间,而不是 div。</p><p> 我试图在中间垂直对齐它。 </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-css lang-css prettyprint-override"> .img1 { float: right; width: 20%; margin-right: 200px; display: inline-block; vertical-align: middle; }.img1 { border-radius: 50%; border: 5px solid white }.container { width: 100%; height: 500px; background-color: black; display: inline-block; }</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;div class="container"&gt; &lt;img class="img1" src="https://via.placeholder.com/500.jpg"&gt; &lt;/div&gt;</pre></div></div><p></p></div> - how can I vertically align this image within the <div> while keeping it to the right? 无法左对齐侧边栏链接使它们居中 - Unable to left align sidebar links keeping them centered
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM