如何在达到父组件的宽度时将段落移到下一行

huangapple go评论55阅读模式
英文:

How to move paragraph to next line when the width of parent component is reached

问题

我正在尝试在我的项目的移动版本中使用Tailwind CSS创建可以水平滚动的卡片。当达到其父组件的最大宽度时,如何将卡片标题移到下一行。

以下是我的代码:

<section id="pendidikan" class="container mx-auto px-4 sm:mt-12">
    <div class="snap-x flex whitespace-nowrap flex-nowrap overflow-x-scroll hide-scroll-bar gap-4">
        <!-- Program Diploma dan Sarjana Terapan -->
        <div class="w-3/4 bg-secondary group snap-center overflow-hidden sm:w-full">
            <div class="object-cover aspect-square w-full overflow-hidden">
                <img class="aspect-square w-full object-cover group-hover:scale-105 transition-all" src="../assets/diploma.jpg" alt="Program Diploma">
            </div>
            <div class="p-4 flex-col w-full">
                <p class="text-white text-base break-words">Program Diploma dan Pascasarjana</p>
                <a class="text-white font-plusJakarta font-light text-sm" href="#">Baca selengkapnya</a>
            </div>
        </div>
    </div>
</section>

这是结果。正如您所看到的,标题只是溢出其父组件。我尝试使用break-words,但它不起作用。

英文:

I'm trying to make horizontally scrollable card in mobile version of my project using Tailwind CSS. How to move the card title to next line when the max width of it's parent component is reached.

Here's my code:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;section id=&quot;pendidikan&quot; class=&quot;container mx-auto px-4 sm:mt-12&quot;&gt;
        &lt;div class=&quot;snap-x flex whitespace-nowrap flex-nowrap overflow-x-scroll hide-scroll-bar gap-4&quot;&gt;
            &lt;!-- Program Diploma dan Sarjana Terapan --&gt;
            &lt;div class=&quot;w-3/4 bg-secondary group snap-center overflow-hidden sm:w-full&quot;&gt;
                &lt;div class=&quot;object-cover aspect-square w-full overflow-hidden&quot;&gt;
                    &lt;img class=&quot;aspect-square w-full object-cover group-hover:scale-105 transition-all&quot; src=&quot;../assets/diploma.jpg&quot; alt=&quot;Program Diploma&quot;&gt;
                &lt;/div&gt;
                &lt;div class=&quot;p-4 flex-col w-full&quot;&gt;
                    &lt;p class=&quot;text-white text-base break-words&quot;&gt;Program Diploma dan Pascasarjana&lt;/p&gt;
                    &lt;a class=&quot;text-white font-plusJakarta font-light text-sm &quot; href=&quot;#&quot;&gt;Baca selengkapnya&lt;/a&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/section&gt;

<!-- end snippet -->

Here's the result. As you can see the title just overflowing out of it's parent component. I tried to use word break but it wont work.

如何在达到父组件的宽度时将段落移到下一行

答案1

得分: 1

你试过在你的 p 元素上添加 whitespace-normal,以覆盖父级 div 元素上的 whitespace-nowrap 吗?然后在父级 div 上加一个 max-width 以确保样式生效。

英文:

Did you try to add whitespace-normal on your p element to override the whitespace-nowrap from your parent div element? And a max-width on your parent div for good measure.

答案2

得分: 0

尝试添加最大宽度以限制内容,

<html>
    <style>
        .test {
            max-width: 250px;
        }
    </style>
    <section id="pendidikan" class="container mx-auto px-4 sm:mt-12">
        <div class="snap-x flex whitespace-nowrap flex-nowrap overflow-x-scroll hide-scroll-bar gap-4">
            <!-- Program Diploma dan Sarjana Terapan -->
            <div class="w-3/4 bg-secondary group snap-center overflow-hidden sm:w-full">
                <div class="object-cover aspect-square w-full overflow-hidden">
                    <img class="aspect-square w-full object-cover group-hover:scale-105 transition-all" src="./assets/test2.jpg" alt="Program Diploma">
                </div>
                <div class="p-4 flex-col w-full test">
                    <span class="text-white text-base break-words">Program Diploma dan Pascasarjana Program Diploma dan Pascasarjana Program Diploma dan Pascasarjana Program Diploma dan Pascasarjana Program Diploma dan Pascasarjana Program Diploma dan Pascasarjana</span>
                    <a class="text-white font-plusJakarta font-light text-sm" href="#">Baca selengkapnya</a>
                </div>
            </div>
        </div>
    </section>
</html>

注意:代码部分未翻译。

英文:

Try adding maximum width to break the content,

&lt;html&gt;
    &lt;style&gt;
        .test {
            max-width: 250px;
        }
    &lt;/style&gt;
    &lt;section id=&quot;pendidikan&quot; class=&quot;container mx-auto px-4 sm:mt-12&quot;&gt;
        &lt;div class=&quot;snap-x flex whitespace-nowrap flex-nowrap overflow-x-scroll hide-scroll-bar gap-4&quot;&gt;
            &lt;!-- Program Diploma dan Sarjana Terapan --&gt;
            &lt;div class=&quot;w-3/4 bg-secondary group snap-center overflow-hidden sm:w-full&quot;&gt;
                &lt;div class=&quot;object-cover aspect-square w-full overflow-hidden&quot;&gt;
                    &lt;img class=&quot;aspect-square w-full object-cover group-hover:scale-105 transition-all&quot; src=&quot;./assets/test2.jpg&quot; alt=&quot;Program Diploma&quot;&gt;
                &lt;/div&gt;
                &lt;div class=&quot;p-4 flex-col w-full test&quot;&gt;
                    &lt;span class=&quot;text-white text-base break-words&quot;&gt;Program Diploma dan Pascasarjana Program Diploma dan Pascasarjana Program Diploma dan Pascasarjana Program Diploma dan Pascasarjana Program Diploma dan Pascasarjana Program Diploma dan Pascasarjana&lt;/span&gt;
                    &lt;a class=&quot;text-white font-plusJakarta font-light text-sm &quot; href=&quot;#&quot;&gt;Baca selengkapnya&lt;/a&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/section&gt;
&lt;/html&gt;

huangapple
  • 本文由 发表于 2023年4月4日 12:14:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75925471.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定