Why doesn't this text-align:justified work?

2019-09-08 14:51发布

问题:

The problem I am facing is that inside a div having text-align:justified;, there is a bunch of a elements, on which the text-align:justified is Not working. So I wrote an SSCCE to demonstrate the problem, but to my surprise and frustration, it does work in the SSCCE.

I have used all my brain power to make the SSCCE as similar to the original code as possible, and they are like equivalent at this point. So I am posting the screenshots of both the pages, and also the code.

PART OF ORIGINAL WEBSITE:

The following mark-up is from the .php file, and this <section> has a <div> which has position:relative; display:table; and a few sibling <section> elements, each of which has position:relative. These are children of a <div> which also has position:relative, which is a child of <body> which doesn't have position or display property set, but has height:100%.

<section class="body-wrapper full-width">
    <div class="everything-wrapper fixed-width">
        <p class="desc-para">Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
        <div class="links-wrapper">
            <?php //$num=1;
                foreach ($links as $a_link) {
                    echo '<a class="a-link" href="' . $a_link->the_url . '" target="_blank">';
                        echo '<div class="image-replace the-div" style="background-image:url(pix/colours.png); background-repeat:no-repeat; background-position:center center;"  >';
                            echo 'just a link';
                        echo '</div>';
                        echo '<p class="caption">Those angel eyes</p>';
                    echo '</a>';
                }
            ?>
        </div><!-- .links-wrapper -->
    </div><!-- .everything-wrapper -->
</section>

CSS: I copied and pasted the CSS to the SSCCE, so you can find it below in the code-editor; I am not posting it here because that would make the question longer for no good reason.


SSCCE:

article, aside, figure, footer, header, hgroup, nav, section, details, summary {
    display: block;
}


section.body-wrapper {
	position:relative;
	background-color:teal;
	width:100%;
	min-width:960px;
}

div.fixed-width {
	width:960px;
	margin-left:auto;
	margin-right:auto;
	padding:0px 10px;
}

.everything-wrapper p.desc-para {
	font-size:18px;
	width:460px;
	margin:0 auto;
	padding:40px 0px 27px;
}

div.links-wrapper {
	background-color:black;
	padding: 20px 0px 50px;
	text-align:justify;
}
div.links-wrapper:after{
  content: "";
  width: 100%;
  display: inline-block;
}

.a-link {
	display:inline-block;
	text-decoration:none;
}

div.the-div {
	font: 0px/0 a;
	border: coral 2px solid;
	height:140px;
	width:140px;
	transition: all 0.3s ease 0s
}

.the-div:hover {
	background-color:grey;
	border: coral 6px solid;
}

.caption {
	font-family:sans-serif;
	font-size:14px;
	padding-top:20px;
	text-align:center;
	color:white;
	font-weight:100;
	transition: all 0.3s ease 0s;
}
.caption:hover {
	color:cyan;
	font-weight:200;
}
<section class="body-wrapper">



	<div class="everything-wrapper fixed-width">
		<p class="desc-para">Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>

		<div class="links-wrapper">

			<a class="a-link" href="#">
				<div class="the-div" style="background-image:url(http://www.monitoryourheart.com/wcm/groups/mdtcom_sg/@mdt/@crdm/documents/images/icon-mri-50px.gif); background-repeat:no-repeat; background-position:center center;">Bulb</div>
				<p class="caption">Strangler Wrangler</p>
			</a>

			<a class="a-link" href="#">
				<div class="the-div" style="background-image:url(http://www.monitoryourheart.com/wcm/groups/mdtcom_sg/@mdt/@crdm/documents/images/icon-mri-50px.gif); background-repeat:no-repeat; background-position:center center;">Bulb</div>
				<p class="caption">Strangler Wrangler</p>
			</a>

			<a class="a-link" href="#">
				<div class="the-div" style="background-image:url(http://www.monitoryourheart.com/wcm/groups/mdtcom_sg/@mdt/@crdm/documents/images/icon-mri-50px.gif); background-repeat:no-repeat; background-position:center center;">Bulb</div>
				<p class="caption">Strangler Wrangler</p>
			</a>

			<a class="a-link" href="#">
				<div class="the-div" style="background-image:url(http://www.monitoryourheart.com/wcm/groups/mdtcom_sg/@mdt/@crdm/documents/images/icon-mri-50px.gif); background-repeat:no-repeat; background-position:center center;">Bulb</div>
				<p class="caption">Strangler Wrangler</p>
			</a>

			<a class="a-link" href="#">
				<div class="the-div" style="background-image:url(http://www.monitoryourheart.com/wcm/groups/mdtcom_sg/@mdt/@crdm/documents/images/icon-mri-50px.gif); background-repeat:no-repeat; background-position:center center;">Bulb</div>
				<p class="caption">Strangler Wrangler</p>
			</a>

			<a class="a-link" href="#">
				<div class="the-div" style="background-image:url(http://www.monitoryourheart.com/wcm/groups/mdtcom_sg/@mdt/@crdm/documents/images/icon-mri-50px.gif); background-repeat:no-repeat; background-position:center center;">Bulb</div>
				<p class="caption">Strangler Wrangler</p>
			</a>

		</div> <!-- .links-wrapper -->

	</div> <!-- .everything-wrapper -->



</section><!-- .body-wrapper -->

回答1:

Probably, the problem is that in your live code you have no white space between the a elements, while in the SSCCE there are some. text-align:justify works by widening the white spaces, so if there are no white spaces it can't happen