如何让垂直菜单与div保持间距,适配xl屏幕布局?
问题
页面需根据屏幕尺寸自适应,但在xl屏幕下显示异常:fixed定位的垂直菜单占据屏幕空间,导致包含CONTACT按钮的div显示在菜单下方,本该位于菜单右侧,同时要保留两者的尺寸。

原代码:
<div id="main_menu" class="fixed top-0 left-0 h-screen w-32 m-0 flex flex-col bg-gray-900 text-white invisible xl:visible 2xl:visible"> <div class="text-7xl relative flex items-center justify-center text-center mt-2"> C </div> <div class="mt-4 relative flex items-center justify-center text-center"> <p>TESTING</p> </div> <div class="mt-40"> <div class="sidebad"> <i><p>TESTING 1</p></i> </div> <div class="sidebad"> <i><p>TESTING 2</p></i> </div> <div class="sidebad"> <i><p>TESTING 3</p></i> </div> <div class="sidebad"> <i><p>TESTING 4</p></i> </div> <div class="sidebad"> <i><p>TESTING 5</p></i> </div> </div> </div> <div id="main_body" class="container mx-auto"> <div class="grid grid-cols-2 mt-40"> <div class="border border-indigo-600"> <div class="text-white lg:text-7xl font-Martel font-extrabold p-10"> <p>HELLO,</p> <p>THIS IS,</p> <p>A WEB </p> </div> <div class="mt-20 p-10"> <button class="btn-contact">CONTACT !</button> </div> </div> </div> <div class="py-96"> <div class="p-10"> <p class="text-blue-500 text-7xl font-Martel font-extrabold">ABOUT</p> <br> <div class="bg-[url('../public/images/work_2.png')]"> </div> <p class="text-black">It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p> </div> <div class="p-10 grid grid-cols-1 sm:grid-cols-1 md:grid-cols-1 lg:grid-cols-2 xl:grid-cols-3 gap-5"> <!--Card 1--> <div class="rounded overflow-hidden shadow-lg bg-white"> <img class="w-full" src="/mountain.jpg" alt="Mountain"> <div class="px-6 py-4"> <div class="font-bold text-xl mb-2">Mountain</div> <p class="text-gray-700 text-base"> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptatibus quia, nulla! Maiores et perferendis eaque, exercitationem praesentium nihil. </p> </div> <div class="px-6 pt-4 pb-2"> <span class="inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2 mb-2">#photography</span> <span class="inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2 mb-2">#travel</span> <span class="inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2 mb-2">#winter</span> </div> </div> <!--Card 2--> <div class="rounded overflow-hidden shadow-lg bg-white"> <img class="w-full" src="/river.jpg" alt="River"> <div class="px-6 py-4"> <div class="font-bold text-xl mb-2">River</div> <p class="text-gray-700 text-base"> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptatibus quia, nulla! Maiores et perferendis eaque, exercitationem praesentium nihil. </p> </div> <div class="px-6 pt-4 pb-2"> <span class="inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2 mb-2">#photography</span> <span class="inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2 mb-2">#travel</span> <span class="inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2 mb-2">#summer</span> </div> </div> <!--Card 3--> <div class="rounded overflow-hidden shadow-lg bg-white"> <img class="w-full" src="/forest.jpg" alt="Forest"> <div class="px-6 py-4"> <div class="font-bold text-xl mb-2">Forest</div> <p class="text-gray-700 text-base"> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptatibus quia, nulla! Maiores et perferendis eaque, exercitationem praesentium nihil. </p> </div> <div class="px-6 pt-4 pb-2"> <span class="inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2 mb-2">#photography</span> <span class="inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2 mb-2">#travel</span> <span class="inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2 mb-2">#fall</span> </div> </div> </div> </div> </div>
解决方法
fixed定位元素会脱离文档流,不占据正常布局空间,导致主体内容默认从屏幕左侧排列,被显示的菜单遮挡。只需给主体内容在xl及以上屏幕添加左侧偏移,抵消菜单宽度即可:
- 直接修改
main_body的class,添加xl:ml-32(菜单宽度为w-32,对应左边距设为32):
<div id="main_body" class="container mx-auto xl:ml-32">
- 若希望主体内容保持
container的自适应最大宽度,同时避开菜单,也可以嵌套一层结构:
<div class="w-full xl:pl-32"> <div id="main_body" class="container mx-auto"> <!-- 原有主体内容 --> </div> </div>
以上两种方式都能让xl屏幕下,主体内容显示在菜单右侧,同时保留两者的尺寸,实现自适应布局。
内容的提问来源于stack exchange,提问作者Kvothe_0077
相关产品推荐
相关产品推荐

