简体   繁体   English

水平对齐文本行?

[英]Horizontally justify rows of text?

I would like to have the links in each row div to be justified, and to be centered on the page, so it looks cleaner (assume below is centered on the page): 我想使每行div中的链接合理,并使其位于页面的中心,因此它看起来更干净(假定下面位于页面的中心):

Chrysanthemum.jpg     Desert.jpg     Hydrangeas.jpg
Jellyfish.jpg         Koala.jpg      Lighthouse.jpg
Penguins.jpg          test (1).jpg     test (2).jpg
test (3).jpg          test (4).jpg       Tulips.jpg

It doesn't have to look exactly like that, as the image names are of variable length, I'd just like to make it look better than it does now: 它不必看起来完全一样,因为图像名称的长度是可变的,我只是想使其比现在更好:

html: 的HTML:

<div id='image-list'>
        <div class="row">
            <a class="image-link" href="/static/images/Chrysanthemum.jpg"> Chrysanthemum.jpg </a>
            <a class="image-link" href="/static/images/Desert.jpg"> Desert.jpg </a>
            <a class="image-link" href="/static/images/Hydrangeas.jpg"> Hydrangeas.jpg </a>
        </div>
        <div class="row">
            <a class="image-link" href="/static/images/Jellyfish.jpg"> Jellyfish.jpg </a>
            <a class="image-link" href="/static/images/Koala.jpg"> Koala.jpg </a>
            <a class="image-link" href="/static/images/Lighthouse.jpg"> Lighthouse.jpg </a>
        </div>
        <div class="row">
            <a class="image-link" href="/static/images/Penguins.jpg"> Penguins.jpg </a>
            <a class="image-link" href="/static/images/test%20%281%29.jpg"> test (1).jpg </a>
            <a class="image-link" href="/static/images/test%20%282%29.jpg"> test (2).jpg </a>
        </div>
        <div class="row">
            <a class="image-link" href="/static/images/test%20%283%29.jpg"> test (3).jpg </a>
            <a class="image-link" href="/static/images/test%20%284%29.jpg"> test (4).jpg </a> 
            <a class="image-link" href="/static/images/Tulips.jpg"> Tulips.jpg </a>                
        </div>            
    </div>

css: CSS:

#image-list{
    display:  flex;
    justify-content: space-around;
}

current output, kind of works but puts all my rows on the same "line": 当前输出,工作正常,但是将所有行都放在同一行上: 在此处输入图片说明

I've also tried this css: 我也尝试过此CSS:

#image-list.row{
    display:  flex;
    justify-content: space-around;
}

But that just pushes everything to the left, with no seeming justification: screenshot here 但这只是将所有内容推向左侧,似乎没有任何理由: 此处为屏幕截图

What am I overlooking or doing wrong? 我忽略了什么或做错了什么? Do I have to maybe set a div to be a specific width, then justify to that width somehow? 我是否必须将div设置为特定宽度,然后以某种方式证明该宽度合理?

It looks like you are displaying tabular data, so why not use a <table> ? 看起来您正在显示表格数据,那么为什么不使用<table>呢?

If you want some sort of responsive layout, then you can use display: table and display: table-cell for desktop, then revert to display: block at smaller breakpoints with media queries. 如果需要某种响应式布局,则可以使用display: tabledisplay: table-cell用于桌面,然后还原为display: block在较小的断点处进行媒体查询。

Otherwise, defining a set width for each link will line them up nicely eg .image-link { width: 33.33%; } 否则,为每个链接定义一个设置的宽度将使它们很好地.image-link { width: 33.33%; }例如.image-link { width: 33.33%; } .image-link { width: 33.33%; }

Also, to enable flex items to wrap rather than display on one line, you need to use #image-list.row { flex-wrap: wrap; } 另外,要使Flex项目可以换行而不是显示在一行上,您需要使用#image-list.row { flex-wrap: wrap; } #image-list.row { flex-wrap: wrap; }

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

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