简体   繁体   English

在div标签内对齐文本

[英]Align text within div tags

I've this below html 我在html下面

str+="<div class='label' date1='"+a+"' date2='"+a1+"' id='mydiv'> Start Date "+a;
str+="End Date "+a1;

Now I want date1 should be aligned left to the page and date2 should be aligned right to the page. 现在,我希望date1应该在页面的左侧对齐,而date2应该在页面的右侧对齐。

How to do this? 这个怎么做?

Regards 问候

Try like this: Demo 尝试这样: 演示

CSS: CSS:

.fl{ float:left; }
.fr{ float:right; }
.label{ clear:both; }

HTML: HTML:

<div class="label" date1="10/5/2016" date2="10/20/2016" id="mydiv">
    <span class="fl">Start Date 10/5/2016</span> 
    <span class="fr">End Date 10/20/2016</span>
</div>

Make it simple with flexbox and pseudo elements . 使用flexboxpseudo elements使其变得简单。

 #mydiv { display: flex; justify-content: space-between; } #mydiv::before { content: "Start Date: " attr(date1); } #mydiv::after { content: "End Date: " attr(date2); } 
 <div class="label" date1="10/5/2016" date2="10/20/2016" id="mydiv"></div> 

So, your code will be like this: 因此,您的代码将如下所示:

str+="<div class='label' date1='"+a+"' date2='"+a1+"' id='mydiv'></div>";

This is gonna save your time 这将节省您的时间

///HTML /// HTML

<div class="label" date1="10/5/2016" date2="10/20/2016" id="mydiv"><div style = 'float:right'>End Date 10/20/2016
</div>
</div>Start Date 10/5/2016></div>

Live demo: https://jsfiddle.net/c6wnsob6/3/ 现场演示: https : //jsfiddle.net/c6wnsob6/3/

wrap the dates in span and add css as below 将日期包装在span并添加css如下

<div class="label" date1="10/5/2016" date2="10/20/2016" id="mydiv">
    <span class="float-left">Start Date 10/5/2016</span>
    <span class="float-right">End Date 10/20/2016</span>
</div>

CSS 的CSS

.float-left{float:left;}
.float-right{float:right;}

try this codepen codepen .. 试试这个codepen codepen ..

I wrapped two dates in two spans and used display: inline-block; 我用两个跨度包装了两个日期并使用display: inline-block; to the span elements to align inline with width and for the first element i used text-align: left; 到span元素以与宽度内联对齐,对于第一个元素,我使用了text-align: left; to start from left and for the second element used text-align: right; 从左边开始,第二个元素使用text-align: right; to start from right. 从正确的开始。

Note: I given max-width: 800px; 注意:我给定的max-width: 800px; to the parent element but you can give your width or you can remove. 到父元素,但您可以指定宽度或删除。

You can put the both dates in span or p tag and make one element float to left and other to right. 您可以将两个日期都放在spanp标签中,并使一个元素向左浮动,其他元素向右浮动。

UPDATED as per your code 根据您的代码更新

 $str .="<div class='label' date1='".a."' date2='".a1."' id='mydiv'><span style='float:left'> Start Date ". a.'</span>';
$str.="<span style='float:right'>End Date ".a1.'</span>';

use float:left and float:right for dates. 使用float:left和float:right表示日期。

 .label { width: 100%; height: auto; max-width: 800px; } .date-1 { width: 50%; text-align: left; float:left } .date-2 { width: 50%; text-align: right; float:right; } 
 <div class="label" date1="10/5/2016" date2="10/20/2016" id="mydiv"><span class="date-1">Start Date 10/5/2016</span><span class="date-2">End Date 10/20/2016</span></div> 

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

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