iT邦幫忙

2025 iThome 鐵人賽

DAY 15
0
Modern Web

HTML&CSS30天修煉系列 第 15

day15Html overflow

  • 分享至 

  • xImage
  •  

overflow,這個屬性負責如何處理溢出的內容
這是一般的情況,內容直接超出其包裹的容器繼續顯示,這是因為物件的預設值overflow:visible
https://ithelp.ithome.com.tw/upload/images/20250929/20178696xJR7Ii6Qdx.png
overflow:hidden隱藏溢出內容

    <div style="overflow: hidden;">
        Lorem ipsum dolor, sit amet consectetur adipisicing elit. Vitae, laborum facere? Rem iusto voluptatibus tempora, id magni laudantium aliquid molestias ipsum ipsam quo dicta. Quod eveniet consectetur nihil dignissimos adipisci?
    </div>

原先超出容器的部分會被隱藏看不見
https://ithelp.ithome.com.tw/upload/images/20250929/20178696xPzMWtD1RR.png
不僅限文字,圖片也能使用
https://ithelp.ithome.com.tw/upload/images/20250929/20178696349Zh6roHE.png
overflow:scroll給容器滾動條,但就算沒有溢出也會顯示滾動條的樣式,文字因為寬度會被限制在容器內所以沒有橫向的滾動條
https://ithelp.ithome.com.tw/upload/images/20250929/20178696j4aUBHvjW0.png
https://ithelp.ithome.com.tw/upload/images/20250929/20178696GsPng1zbe7.png
https://ithelp.ithome.com.tw/upload/images/20250929/20178696xEIe2szZoQ.png
overflow:auto給容器滾動條,但跟scroll不一樣的是,如果內容沒有溢出容器的話,就不會顯示滾動條的樣式

    <div style="overflow:auto;">
        <p>Lorem ipsum dolor sit amet</p>
    </div>

https://ithelp.ithome.com.tw/upload/images/20250929/20178696T1Rakd1yTq.png
https://ithelp.ithome.com.tw/upload/images/20250929/20178696k9UX4COI6c.png
overflow也可以分別設定
overflow-y給容器的y軸設定overflow屬性
overflow-x給容器的x軸設定overflow屬性

    <div style="overflow-y: hidden;overflow-x: scroll;">

縱向超出的部分,因為overflow-y:hidden被隱藏,overflow-x:scroll給了容器橫向滾動條
https://ithelp.ithome.com.tw/upload/images/20250929/20178696HeRBfCJAuh.png

overflow和position的關係
https://ithelp.ithome.com.tw/upload/images/20250929/20178696O7chfeEORj.png
hidden對fixed無用

   <div style="overflow:hidden">
        
            <p style="position: fixed;top: 2%;"></p>
       
    </div>
    <div style="overflow:auto">

            <p style="position:relative;top:50px ;left: 100px;"></p>

    </div>

相對定位可以照常移動
https://ithelp.ithome.com.tw/upload/images/20250929/20178696IrRziZqU3q.png

    <div style="overflow:auto">

            <p style="position:absolute;top:0px ;left: 100px;"></p>

    </div>

overflow對以body為基準的絕對定位沒有影響
https://ithelp.ithome.com.tw/upload/images/20250929/20178696ikFNkx6ahN.png

   <div style="overflow:auto;position: relative;">

            <p style="position:absolute;top:0px ;left: 100px;"></p>

    </div>

但是以相對定位為基準的話,則overflow可以生效
https://ithelp.ithome.com.tw/upload/images/20250929/20178696s4o6rdOvTH.png


上一篇
day14Html背景
系列文
HTML&CSS30天修煉15
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言