简体   繁体   中英

<amp-fit-text> with <pre> content

<amp-fit-text> is a useful way to fit text in a responsive container. It seems to expect text to wrap however and is preventing vertical overflow. Is it possible to use an <amp-fit-text> tag with <pre> text (or other non-wrap text) to adjust the font-size to avoid the text from overflowing the container horizontally?

I am not exactly sure if this is what you were looking for but I came across this solution while trying to deal with something that sounds similar. I found that the vw unit can adjust font size depending on the viewport's width dynamically. Here is some sample code:

 <!doctype html> <html amp lang="en"> <head> <meta charset="utf-8"> <title>asd</title> <link rel="canonical" href="http://example.ampproject.org/article-metadata.html" /> <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1"> <script type="application/ld+json"> { "@context": "http://schema.org", "@type": "NewsArticle", "headline": "Open-source framework for publishing content", "datePublished": "2015-10-07T12:02:41Z", "image": [ "logo.jpg" ] } </script> <style amp-custom> .container { font-size: 2vw; } </style> <style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript> <script async src="https://cdn.ampproject.org/v0.js"></script> </head> <body> <h1>TEST</h1> <h2>test</h2> <p><span>asd</span></p> <p><span>ads</span></p> <p>body</p> <p><div class="container"><pre> echo 'Voluptua consulatu ad mea. Mea tale accumsan neglegentur cu.' $x = 123; foo(bar); </pre></div></p> </body> </html> 

I hope this helps, let me know if you need me to clarify anything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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