You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React+Tailwind移动端页面无法全屏及内容溢出问题求助

移动端响应式问题求助

我用React结合Tailwind CSS构建的网站,PC端能全屏显示,但切换到移动端后,页面仅占屏幕80-90%的宽度,同时内容出现溢出,没法实现正常的移动端响应式效果。相关截图及代码如下:

import React, { useState } from 'react';
import { Link } from 'react-router-dom';
import { TiThMenuOutline } from 'react-icons/ti';
import { GiCrossMark } from 'react-icons/gi';
export default function Navbar() {
    const [isOpen, setIsOpen] = useState(false);
    const [rotating, setRotating] = useState(false);
    const [show, setShow] = useState(false);
    const toggleMenu = () => {
        setIsOpen(!isOpen);
    };
    const toggleMain = () => {
        setRotating(true);
        setShow(!show);
        setTimeout(() => {
            setRotating(false);
        }, 500); // This should match the duration of the rotate animation
    };
    return (
        <div className="bg-custom-gradient flex justify-center">
            <nav className=" bg-gradient-to-r from-[#1f353f] via-[#79a8b2] to-[#094c57] shadow-lg border-2 border-white rounded-3xl fixed p-4 w-full font-dongle text-2xl">
                <div className="container mx-auto flex justify-between items-center">
                    <div className="text-white text-4xl font-semibold">
                        Portfolio
                    </div>
                    <div className="relative">
                        <div className="flex items-center justify-between w-full">
                            <div className="overflow-hidden w-full">
                                <div
                                    className={`${
                                        show
                                            ? 'hidden md:flex gap-4 ease-in duration-500'
                                            : 'flex gap-4 mt-[-30px] md:mt-[-30px] ease-in duration-700'
                                    }`}
                                >
                                    <Link
                                        to="/"
                                        className="text-white hover:text-white"
                                    >
                                        Home
                                    </Link>
                                    <Link
                                        to="/about"
                                        className="text-white hover:text-white"
                                    >
                                        About
                                    </Link>
                                    <Link
                                        to="/services"
                                        className="text-white hover:text-white"
                                    >
                                        Services
                                    </Link>
                                    <Link
                                        to="/projects"
                                        className="text-white hover:text-white"
                                    >
                                        Projects
                                    </Link>
                                    <Link
                                        to="/contact"
                                        className="text-white hover:text-white"
                                    >
                                        Contact
                                    </Link>
                                </div>
                            </div>
                            <div>
                                <div className="hidden md:flex ml-4 justify-end">
                                    {show ? (
                                        <button
                                            className={`text-white hover:text-white ${
                                                rotating ? 'animate-rotate' : ''
                                            }`}
                                            onClick={toggleMain}
                                        >
                                            <GiCrossMark />
                                        </button>
                                    ) : (
                                        <button
                                            className={`text-white hover:text-white ${
                                                rotating ? 'animate-rotate' : ''
                                            }`}
                                            onClick={toggleMain}
                                        >
                                            <TiThMenuOutline />
                                        </button>
                                    )}
                                </div>
                            </div>
                        </div>
                    </div>
                    <div className="md:hidden overflow-visible ">
                        <button
                            onClick={toggleMenu}
                            className="text-black focus:outline-none focus:text-black ml-[-90px] overflow-visible "
                        >
                            <TiThMenuOutline />
                        </button>
                    </div>
                </div>
                {isOpen && (
                    <div className="md:hidden mt-2 space-y-2">
                        <Link
                            to="/"
                            className="block text-gray-300 hover:text-white"
                        >
                            Home
                        </Link>
                        <Link
                            to="/about"
                            className="block text-gray-300 hover:text-white"
                        >
                            About
                        </Link>
                        <Link
                            to="/services"
                            className="block text-gray-300 hover:text-white"
                        >
                            Services
                        </Link>
                        <Link
                            to="/projects"
                            className="block text-gray-300 hover:text-white"
                        >
                            Projects
                        </Link>
                        <Link
                            to="/contact"
                            className="block text-gray-300 hover:text-white"
                        >
                            Contact
                        </Link>
                    </div>
                )}
            </nav>
        </div>
    );
}

Home.js代码

import React from 'react';
import Navbar from '../comps/Navbar';
import Footer from '../comps/Footer';
export default function Home() {
    return (
        <div className="bg-custom-gradient min-h-screen flex flex-col font-dongle text-4xl w-full">
            {' '}
            <Navbar />
            <div className="flex-grow mt-32">hello</div>
            <Footer />
        </div>
    );
}
import React from 'react';
import { Link } from 'react-router-dom';
export default function Footer() {
    return (
        <footer className="bg-gray-800 text-gray-300 py-4">
            <div className="container mx-auto text-center">
                <div className="mb-2">
                    <Link
                        to="/"
                        className="text-gray-300 hover:text-white mx-2"
                    >
                        Home
                    </Link>
                    <Link
                        to="/about"
                        className="text-gray-300 hover:text-white mx-2"
                    >
                        About
                    </Link>
                    <Link
                        to="/services"
                        className="text-gray-300 hover:text-white mx-2"
                    >
                        Services
                    </Link>
                    <Link
                        to="/projects"
                        className="text-gray-300 hover:text-white mx-2"
                    >
                        Projects
                    </Link>
                    <Link
                        to="/contact"
                        className="text-gray-300 hover:text-white mx-2"
                    >
                        Contact
                    </Link>
                </div>
                <div className="text-sm">
                    © {new Date().getFullYear()} Portfolio By Mukund. All rights
                    reserved.
                </div>
            </div>
        </footer>
    );
}

问题修复方案

1. 全局样式重置

在项目的全局CSS文件(比如index.css)中添加以下代码,解决基础的宽度和溢出问题:

@tailwind base;
@tailwind components;
@tailwind utilities;

html, body {
  width: 100%;
  overflow-x: hidden;
  margin: 0;
  padding: 0;
}

2. Navbar组件修复

  • 给nav标签添加left-0 right-0,确保fixed定位时完全贴合屏幕两侧:
    <nav className="bg-gradient-to-r from-[#1f353f] via-[#79a8b2] to-[#094c57] shadow-lg border-2 border-white rounded-3xl fixed p-4 w-full left-0 right-0 font-dongle text-2xl">
    
  • 移除移动端菜单按钮的ml-[-90px]负边距,换成ml-auto避免元素溢出容器:
    <button
      onClick={toggleMenu}
      className="text-black focus:outline-none focus:text-black ml-auto overflow-visible"
    >
      <TiThMenuOutline />
    </button>
    
  • 给Navbar的外层容器添加w-full,确保它占满屏幕宽度:
    <div className="bg-custom-gradient flex justify-center w-full">
    

3. Home组件优化

给Home的外层容器添加overflow-x-hidden,防止内容横向溢出:

<div className="bg-custom-gradient min-h-screen flex flex-col font-dongle text-4xl overflow-x-hidden">

4. Footer组件调整

给footer标签添加w-full,确保它在移动端占满屏幕宽度:

<footer className="bg-gray-800 text-gray-300 py-4 w-full">

内容的提问来源于stack exchange,提问作者Mukund Hadiya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 09:44:57