html - Flexbox 中的行换行居中对齐

标签 html css flexbox

目前我正在尝试使用 flexbox 使我的列居中,但是当我调整浏览器大小时它会粘在左边。我试过证明内容中心和对齐元素中心,但我可能没有把它们放在正确的地方。如果有人知道如何解决这个问题,将不胜感激!谢谢!

.wrap{
	display:flex;
}

main{
	flex:1;
	display:flex;
}

aside, article{
    padding:2em;	
}

aside{
	flex:1;
}

article{
	flex:1;
}

@media (max-width: 800px) {
  main {
    flex-direction: column;
  }
}
<div class="wrap">
    <main>
        <aside>
            <h1>Do You Want to...</h1>
            <ul>
                <li>Rebrand myself online</li>
                <li>Take my current website and make it modern</li>
                <li>Find a way to make information more accessible for customers</li>
                <li> Improve customer service</li>
                <li> Reach a wider range of people</li>
            </ul>
        </aside>
        <article>
            <h1>Do You Want to...</h1>
            <ul>
                <li>Create forms and documents that customers can fill out online</li>
                <li>Start an email list for recurring customers</li>
                <li>Show relatability with my audience</li>
                <li> Have 24/7 online exposure</li>
                <li>Create a map or a way for customers to find my location</li>
            </ul>
        </article>
    </main>
</div>

最佳答案

要在移动 View 中将堆叠的列居中,请在 main 上使用 align-items:center。对于桌面 View ,您可以通过指定 flex: 0 auto 来折叠列。这表示不应允许该元素增长并且应使用自动宽度。然后将 justify-content: center 添加到 main 以使折叠的列居中。

.wrap{
	display:flex;
	
}

main{
	flex:1;
	display:flex;
	justify-content: center;
}

aside, article{

padding:2em;	
}

aside{
	flex:0 auto;
}

article{
	flex:0 auto;
}

@media (max-width: 800px) {
  main {
    flex-direction: column;
    align-items: center;
  }
}
<div class="wrap">
<main>
<aside>
	<h1>Do You Want to...</h1>
    <ul>
  <li>Rebrand myself online</li>
  <li>Take my current website and make it modern</li>
  <li>Find a way to make information more accessible for customers</li>
  <li> Improve customer service</li>
  <li> Reach a wider range of people</li>
</ul>
</aside>
<article>
<h1>Do You Want to...</h1>

<ul>
  <li>Create forms and documents that customers can fill out online</li>
  <li>Start an email list for recurring customers</li>
  <li>Show relatability with my audience</li>
  <li> Have 24/7 online exposure</li>
  <li>Create a map or a way for customers to find my location</li>
</ul>
</article>

</main>
</div>

关于html - Flexbox 中的行换行居中对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42027236/

相关文章:

html - 使用@font-face在网页中多次实现自定义字体

css - 为什么 flexbox 元素图像会根据其初始大小调整不同的大小?

html - 在 flexbox 布局中对齐行 * 和 * 列中的单元格

html - 图库图片的定位问题

html - 在放下元素时动画化 html5 拖放

javascript - 如何让我的页面在不滚动的情况下按比例显示?

javascript - 为什么 .not() 在这种情况下不起作用?

jquery - 简单的 ui 高度以获取完整的浏览器

javascript - .scrollTop() 条件语句在悬停时中断

jquery - 如何使 jquery 在函数后更新 html?