子div在父div中的位置及其所引起的问题

问题描述 投票:0回答:2

我想将h1和按钮放置在底部底部:40%左侧:20%,即悬停在父div(id =“ head)的底部左下方。使用display:abolute;和定位标签使它们移到外面当屏幕尺寸改变时,屏幕是否存在。除了positiong方法还是没有使用flex或grid,还有其他方法吗,因为当我将显示更改为flex / grid时,我的另一个div受到影响。谢谢。

*{
    margin: 0;
    padding: 0;
}


body{
    background: #888;
    color: #fff;
    line-height: 1.5;
    font-family: 'Work Sans', sans-serif;
    overflow: hidden;
}

.button{
    font-size: 1.5rem;
    text-decoration: none;
    color: #fff;
    border: #fff 3px solid;
    padding: 1.3rem 2.3rem;
    border-radius: 10px;
    margin-top: 2.2rem;
    margin-left: 3rem;
    transition: all 0.6s ease;
}

.button:hover{
    font-size: 1.7rem;
    border: black 3px solid;
}


#head{
    position: relative;
    /* background-image: url(/images/snow-capped-mountains-under-the-cloudy-skies-1054289.jpg); */
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
    height: 100vh;
    overflow: hidden;
}

#head .show-top {
    width: 90%;
    margin: auto;
    text-align: right;
    padding: 1.5rem;
    font-size: 1.2rem;
}


.show-top li{
    display: inline;
    padding: 0 1rem;
}

.show-top a{
    text-decoration: none;
    color: #333;
}


#head .showcase{
    position: absolute;
    top: 65%;
    left: 20%;
    transform: translate(-50%, -50%);
}

#head .showcase h1{
    font-weight: bold;
    font-size: 5rem;
    /* padding: 2rem; */
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" type="text/css" href="style.css">
    <link href="https://fonts.googleapis.com/css2?family=Work+Sans&display=swap" rel="stylesheet">
    <title>Document</title>
</head>

<body>
    <header id="head" style="border: violet 5px solid;">
        <div class="show-top">
            <ul>
                <li><a href="#">ABOUT</a></li>
                <li><a href="#">PROGRAMS</a></li>
                <li><a href="#">SPECIALS</a></li>
            </ul>
        </div>

        <div class="showcase" style="border: turquoise 5px solid;">
            <h1>Take a step</h1>
            <a href="#" class="button">Enter</a>
        </div>

    </header>
</body>

</html>
html css position css-position
2个回答
0
投票

不确定,您最终想要的结果是什么。但这对我有效:

将父级设为绝对值,并相对于其父级设置showcase。如果您调整窗口大小,则尺寸/位置比率保持不变。

*{
    margin: 0;
    padding: 0;
}


body{
    background: #888;
    color: #fff;
    line-height: 1.5;
    font-family: 'Work Sans', sans-serif;
    overflow: hidden;
}

.button{
    font-size: 1.5rem;
    text-decoration: none;
    color: #fff;
    border: #fff 3px solid;
    padding: 1.3rem 2.3rem;
    border-radius: 10px;
    margin-top: 2.2rem;
    margin-left: 3rem;
    transition: all 0.6s ease;
}

.button:hover{
    font-size: 1.7rem;
    border: black 3px solid;
}


#head{
    position: absolute;
    /* background-image: url(/images/snow-capped-mountains-under-the-cloudy-skies-1054289.jpg); */
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
    height: 100vh;
    width: 100%;
    overflow: hidden;
}

#head .show-top {
    width: 90%;
    margin: auto;
    text-align: right;
    padding: 1.5rem;
    font-size: 1.2rem;
}


.show-top li{
    display: inline;
    padding: 0 1rem;
}

.show-top a{
    text-decoration: none;
    color: #333;
}


.showcase{
    position: relative;
    top: 40%;
    left: 20%;
    width: 50vw;    
}

.showcase h1{
    font-weight: bold;
    font-size: 2rem;
    width: 20vw;
    /* padding: 2rem; */
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" type="text/css" href="style.css">
    <link href="https://fonts.googleapis.com/css2?family=Work+Sans&display=swap" rel="stylesheet">
    <title>Document</title>
</head>

<body>
    <header id="head" style="border: violet 5px solid;">
        <div class="show-top">
            <ul>
                <li><a href="#">ABOUT</a></li>
                <li><a href="#">PROGRAMS</a></li>
                <li><a href="#">SPECIALS</a></li>
            </ul>
        </div>

        <div class="showcase" style="border: turquoise 5px solid;">
            <h1>Take a step</h1>
            <a href="#" class="button">Enter</a>
        </div>

    </header>
</body>

</html>

0
投票

一种解决方案使用边距:

*{
  margin: 0;
  padding: 0;
}


body{
  background: #888;
  color: #fff;
  line-height: 1.5;
  font-family: 'Work Sans', sans-serif;
  overflow: hidden;
}

.button{
  font-size: 1.5rem;
  text-decoration: none;
  color: #fff;
  border: #fff 3px solid;
  padding: 1.3rem 2.3rem;
  border-radius: 10px;
  margin-top: 2.2rem;
  margin-left: 3rem;
  transition: all 0.6s ease;
}

.button:hover{
  font-size: 1.7rem;
  border: black 3px solid;
}


#head{
  position: relative;
  /* background-image: url(/images/snow-capped-mountains-under-the-cloudy-skies-1054289.jpg); */
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  height: 100vh;
  overflow: hidden;
}

#head .show-top {
  width: 90%;
  margin: auto;
  text-align: right;
  padding: 1.5rem;
  font-size: 1.2rem;
}


.show-top li{
  display: inline;
  padding: 0 1rem;
}

.show-top a{
  text-decoration: none;
  color: #333;
}


#head .showcase{
  margin-top: 20%;
  margin-left: 40%;
  transform: translate(-50%, -50%);

}

#head .showcase h1{
  font-weight: bold;
  font-size: 5rem;
  /* padding: 2rem; */
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" type="text/css" href="style.css">
    <link href="https://fonts.googleapis.com/css2?family=Work+Sans&display=swap" rel="stylesheet">
    <title>Document</title>
</head>

<body>
    <header id="head" style="border: violet 5px solid;">
        <div class="show-top">
            <ul>
                <li><a href="#">ABOUT</a></li>
                <li><a href="#">PROGRAMS</a></li>
                <li><a href="#">SPECIALS</a></li>
            </ul>
        </div>

        <div class="showcase" style="border: turquoise 5px solid;">
            <h1>Take a step</h1>
            <a href="#" class="button">Enter</a>
        </div>

    </header>
</body>

</html>
© www.soinside.com 2019 - 2024. All rights reserved.