简体   繁体   English

在段落的浮动右侧部分向左对齐文本

[英]Align Text left of the floating right part of paragraph

I need to give some floated right text same start position.我需要给一些浮动的右文本相同的开始 position。 I have multiple p and every p is divided in two span.我有多个 p 并且每个 p 都分为两个跨度。 The initial span are floated left and seconds are floated right.初始跨度向左浮动,秒数向右浮动。

Some HTML and Image to be more clear about.一些 HTML 和图像要更清楚。

 .card { max-width: 20rem; border: 1px solid #004d99; border-radius: 20px; }.card-header { background-color: #fff; border-bottom: 1px solid #004d99; border-top-left-radius: 20px; border-top-right-radius: 20px; }.card-header input { border: 2px #004d99 solid; }.card-header span { color: #90ee90; }.card-header span i { margin-left: 3%; }.card-body { margin-bottom: 0px; margin-top: 0px; }.card-body p span:first-child { float: left; }.card-body p span:last-child { float: right; text-align: left; }
 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous"> <div class="card col-md-3 text-dark bg-light m-3"> <div class="card-header"> <input class="form-check-input chk" type="checkbox" value="" id="flexCheckDefault"> Stato: Pubblicato <span> &#11044 </span><i class="far fa-file-alt"></i>11 <i class="fas fa-eye"></i>&nbsp;112 </div> <div class="card-body"> <p> <span >Nome:</span> <span >Lotto Prova</span> </p> <p> <span>Data Pubblicazione:</span> <span>10/12/2021</span> </p> <p> <span>Data di Scadenza:</span> <span>25/12/2021</span> </p> <p> <span>Accesso:</span> <span>Link</span> </p> </div> </div>

What I have:我有的:

错误对齐

What I need:我需要的:

右对齐

Thanks to who will answer!谢谢谁来回答!

Actually, this type of content is a typical example where you can use a table, and its default settings will do exactly what you are asking for.实际上,这种类型的内容是您可以使用表格的典型示例,其默认设置将完全符合您的要求。 So I modified your code accordingly, using CSS-table display parameters (but you could as well use actual HTML table/tr/td/th tags, then you wouldn't need that CSS and it would semantically be better).所以我相应地修改了你的代码,使用 CSS 表display参数(但你也可以使用实际的 HTML 表/tr/td/th 标签,那么你就不需要 CSS 并且它在语义上会更好)。

Note: I erased your inline-styles for the spans, and I also erased the md-3 class from the card, since otherwise the content simply wouldn't fit into the card line-per-line, but cause breaks due to the limited width, at least here in the snippet (different on wide screens).注意:我删除了跨度的内联样式,并且我还从卡中删除了md-3 class,因为否则内容根本不适合每行的卡行,但由于有限而导致中断宽度,至少在片段中(在宽屏幕上不同)。

 .card { border: 1px solid #004d99; border-radius: 20px; width: 300px; }.card-header { background-color: #fff; border-bottom: 1px solid #004d99; border-top-left-radius: 20px; border-top-right-radius: 20px; }.card-header input { border: 2px #004d99 solid; }.card-header span { color: #90ee90; }.card-header span i { margin-left: 3%; }.card-body { margin-bottom: 0px; margin-top: 0px; display: table; }.card-body > p { display: table-row; }.card-body > p > span { display: table-cell; }
 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous"> <div class="card text-dark bg-light m-3"> <div class="card-header"> <input class="form-check-input chk" type="checkbox" value="" id="flexCheckDefault"> Stato: Pubblicato <span> &#11044 </span><i class="far fa-file-alt"></i>11 <i class="fas fa-eye"></i>&nbsp;112 </div> <div class="card-body"> <p> <span>Nome:</span> <span>Lotto Prova</span> </p> <p> <span>Data Pubblicazione:</span> <span>10/12/2021</span> </p> <p> <span>Data di Scadenza:</span> <span>25/12/2021</span> </p> <p> <span>Accesso:</span> <span>Link</span> </p> </div> </div>

You can accomplish this with flexbox and simplify the HTML in the process.您可以使用 flexbox 完成此操作,并在此过程中简化 HTML。

Remove the inline style from the span elements and setup the CSS to make the flex children take up exactly half the space.span元素中删除内联样式并设置 CSS 以使 flex 子项恰好占据一半空间。 This will give you the layout you're looking for.这将为您提供您正在寻找的布局。

 .card-body>p { display: flex; flex-flow: row nowrap; }.card-body>p>* { flex: 1; }
 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous"> <div class="card col-md-3 text-dark bg-light m-3" style="max-width: 20rem; border: 1px solid #004d99; border-radius: 20px;"> <div class="card-header" style="background-color: #fff; border-bottom: 1px solid #004d99; border-top-left-radius: 20px; border-top-right-radius: 20px;"> <input style="border: 2px #004d99 solid;" class="form-check-input chk" type="checkbox" value="" id="flexCheckDefault"> Stato: Pubblicato <span style="color: #90ee90;"> &#11044 </span><i class="far fa-file-alt" style="margin-left: 3%;"></i>11 <i class="fas fa-eye" style="margin-left: 3%;"></i>&nbsp;112 </div> <div class="card-body"> <p><span>Nome:</span><span>Lotto Prova</span></p> <p><span>Data Pubblicazione:</span><span>10/12/2021</span></p> <p><span>Data di Scadenza:</span><span>25/12/2021</span></p> <p><span>Accesso:</span><span>Link</span></p> </div> </div>

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

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