html - 如何在调整页面大小时防止 div 移动?

标签 html css resize

有谁知道如何防止“看直播”和“骰子游戏”那样移动吗?

screen recording

HTML:

<!doctype html>
<html>

        <head>
                <meta charset="UTC-8">
                <title>RLESSON85</title>
                <link href="main.css" rel="stylesheet">
        </head>

        <body>

                        <h1 class="index"><a href="index.html">HOME</a></h1>
                        <h1 id="indextitle">RLEESON85</h1>
                        <h1 id="stream"><a href="http://www.twitch.tv/rleeson85">WATCH THE STREAM</a></div>
                        <h1 id="dice"><a href="dice.html">DICE GAME</a></h1>

        </body>

</html>

CSS:

body {
        background: #333;
}

footer {
}

.index {
        font-family: bebas neue;
        font-size: 33px;
        font-style: inherit;
        font-weight: inherit;
        color: white;
        letter-spacing: 3px;
        float: left;
        margin-left: 30px;
        background: transparent;
        height: 60px;
        width: 140px;
        line-height: 60px;
        text-align: center;
        border-style: solid;
        border-width: 2px;
        border-radius: 6px;
        transition: all 0.3s ease 0s;
}
#indextitle {
        font-family: bebas neue;
        font-size: 190px;
        font-style: inherit;
        font-weight: inherit;
        color: white;
        letter-spacing: 5px;
        position: absolute;
        top: 30%;
        left: 50%;
        transform: translateX(-50%) translatey(-50%);
}

#dicetitle {
        font-family: bebas neue;
        font-size: 160px;
        font-style: inherit;
        font-weight: inherit;
        color: white;
        letter-spacing: 5px;
        position: absolute;
        top: 30%;
        left: 50%;
        transform: translateX(-50%) translatey(-50%);
}

#stream {
        font-family: bebas neue;
        font-size: 33px;
        font-style: inherit;
        font-weight: inherit;
        color: white;
        letter-spacing: 3px;
        position: absolute;
        top: 56%;
        left: 50%;
        transform: translateX(-50%) translatey(-50%);
        background: transparent;
        height: 60px;
        width: 320px;
        line-height: 60px;
        text-align: center;
        border: 0;
        border-style: solid;
        border-width: 2px;
        border-radius: 6px;
        transition: all 0.3s ease 0s;
}

#dice {
        font-family: bebas neue;
        font-size: 33px;
        font-style: inherit;
        font-weight: inherit;
        color: white;
        letter-spacing: 3px;
        position: absolute;
        top: 66%;
        left: 50%;
        transform: translateX(-50%) translatey(-50%);
        background: transparent;
        height: 60px;
        width: 200px;
        line-height: 60px;
        text-align: center;
        border: 0;
        border-style: solid;
        border-width: 2px;
        border-radius: 6px;
        transition: all 0.3s ease 0s;
}

.index:hover {
        font-family: bebas neue;
        font-size: 33px;
        font-style: inherit;
        font-weight: inherit;
        color: white;
        letter-spacing: 3px;
        float: left;
        margin-left: 40px;
        background: #777;
        height: 60px;
        width: 120px;
        line-height: 60px;
        text-align: center;
        border-style: solid;
        border-width: 2px;
        border-radius: 6px;
        transition: all 0.3s ease 0s;
}


#stream:hover {
        font-family: bebas neue;
        font-size: 33px;
        font-style: inherit;
        font-weight: inherit;
        color: white;
        letter-spacing: 3px;
        position: absolute;
        top: 56%;
        left: 50%;
        transform: translateX(-50%) translatey(-50%);
        background: #777;
        height: 60px;
        width: 300px;
        line-height: 60px;
        text-align: center;
        border: 0;
        border-style: solid;
        border-width: 2px;
        border-radius: 6px;
        transition: all 0.3s ease 0s;
}


#dice:hover {
        font-family: bebas neue;
        font-size: 33px;
        font-style: inherit;
        font-weight: inherit;
        color: white;
        letter-spacing: 3px;
        position: absolute;
        top: 66%;
        left: 50%;
        transform: translateX(-50%) translatey(-50%);
        background: #777;
        height: 60px;
        width: 180px;
        line-height: 60px;
        text-align: center;
        border: 0;
        border-style: solid;
        border-width: 2px;
        border-radius: 6px;
        transition: all 0.3s ease 0s;
}

a:link {
    text-decoration: none;
    color: white;
}

a:visited {
    text-decoration: none;
    color: white;
}

最佳答案

不要单独定位每个元素,而是定位整个 block 。

另外,你不应该使用 <h1>对于一切。

    <head>
            <meta charset="UTC-8">
            <title>RLESSON85</title>
            <link href="main.css" rel="stylesheet">
    </head>

    <body>

            <div class="index"><a href="index.html">HOME</a></div>
            <div class="maingroup">
                    <h1 id="indextitle">RLEESON85</h1>
                    <div id="stream"><a href="http://www.twitch.tv/rleeson85">WATCH THE STREAM</a></div>
                    <div id="dice"><a href="dice.html">DICE GAME</a></div>
            </div>
    </body>

CSS

.maingroup {
    position: absolute;
    top: 50%;
    height: 450px;
    margin-top: -225px;
    text-align: center;
}

#stream, #dice {
    margin: 0 auto 20px;
}

See the JSFiddle

关于html - 如何在调整页面大小时防止 div 移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28534328/

相关文章:

javascript - 如何编写鼠标悬停时更改的函数?

javascript - 如何清除某些按钮按下时的输入值?

html - CSS 对齐 Logo 与文本高度相同

iphone - 如何在iPhone中重绘基于UIBezierpath的textview文本内容大小

javascript - 如何使用 JavaScript 中的 DOM 操作函数将 <map> 元素添加到文档中?

html - ember-bootstrap : page background rendered half gray, 的问题通常应该是全白的

css - 我如何根据浏览器窗口的宽度动态调整大小

jquery - 我该如何解决这个问题 - 设置两个列表的样式

html - 如何防止 Ajax 元素换行?

javascript - 使导航悬停下划线响应浏览器调整大小