18910140161

JavaScript-如何将“Get Start”按钮居中到标题下面?-堆栈溢出

顺晟科技

2022-10-19 11:32:26

219

我做了这个“开始”按钮。我想把它放在标题“一部电影里的所有记忆”下面。我试着对齐它,但它不起作用。

按钮位于javascript函数下面。id是“button1”,容器的id只是“container”。正如您在CSS文件中看到的,我使用了text-align命令。

此外,我还尝试将内容对齐到中心,并将所有条目对齐到标题下面的中心(使用Javascript函数)。如蒙帮助,不胜感激。


顺晟科技:

主要问题是有立场的文本。若要将按钮定位在文本下方,还必须定位保存该按钮的容器。这反过来也会对齐按钮。

将其添加到样式中:

* {
    margin: 0;
    padding: 0;
}

body{
    height: 100vh;
    width: 50vh;
    background-image: linear-gradient(rgba(0, 0, 0, 0.75), rgba(58, 58, 58, 0.75)), url(4kcamera.jpg);
    background-size: cover;
    background-position: center;
}

nav{
    width: 100%;
    height: 80px;
    position: fixed;
    
}


.logo{
    float: left;
    padding: 0 30px;
    margin-left: 55px;
    margin-right: 30px;
    width: 50px;
    margin-top: 20px;
}

ul li{
    list-style: none;
    display: inline-block;
    line-height: 90px;
    padding: 0 31px;
}

ul{
    margin-left: 840px;
}

ul li a{
    text-decoration: none;
    font-size: 20px;
    font-family: 'Roboto Condensed', sans-serif;
    color: rgb(250, 250, 250);
}

ul li a:hover {
    color: rgb(102, 151, 241);
    transition-duration: 0.5s;
}

#text{
    transform: uppercase;
    color: white;
    font-family: 'Poppins', sans-serif;
    text-align: center;
    margin: 0;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 60px;
}

#button1{
    background-color: rgb(30, 109, 255); 
    border: none;
    border-radius: 15px 50px;
    color: white;
    padding: 35px;
    margin: 22px;
    text-align: center;
    font-size: 13px;
    font-family: 'Poppins', Helvetica;
}

这将按钮定位在文本的底部。

尝试更改按钮的父div以拥有一个类,并将一个添加到CSS中。

所以您可以使用HTML:

* {
    margin: 0;
    padding: 0;
}

body{
    height: 100vh;
    width: 50vh;
    background-image: linear-gradient(rgba(0, 0, 0, 0.75), rgba(58, 58, 58, 0.75)), url(4kcamera.jpg);
    background-size: cover;
    background-position: center;
}

nav{
    width: 100%;
    height: 80px;
    position: fixed;
    
}


.logo{
    float: left;
    padding: 0 30px;
    margin-left: 55px;
    margin-right: 30px;
    width: 50px;
    margin-top: 20px;
}

ul li{
    list-style: none;
    display: inline-block;
    line-height: 90px;
    padding: 0 31px;
}

ul{
    margin-left: 840px;
}

ul li a{
    text-decoration: none;
    font-size: 20px;
    font-family: 'Roboto Condensed', sans-serif;
    color: rgb(250, 250, 250);
}

ul li a:hover {
    color: rgb(102, 151, 241);
    transition-duration: 0.5s;
}

#text{
    transform: uppercase;
    color: white;
    font-family: 'Poppins', sans-serif;
    text-align: center;
    margin: 0;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 60px;
}

#button1{
    background-color: rgb(30, 109, 255); 
    border: none;
    border-radius: 15px 50px;
    color: white;
    padding: 35px;
    margin: 22px;
    text-align: center;
    font-size: 13px;
    font-family: 'Poppins', Helvetica;
}

对于您的CSS:

* {
    margin: 0;
    padding: 0;
}

body{
    height: 100vh;
    width: 50vh;
    background-image: linear-gradient(rgba(0, 0, 0, 0.75), rgba(58, 58, 58, 0.75)), url(4kcamera.jpg);
    background-size: cover;
    background-position: center;
}

nav{
    width: 100%;
    height: 80px;
    position: fixed;
    
}


.logo{
    float: left;
    padding: 0 30px;
    margin-left: 55px;
    margin-right: 30px;
    width: 50px;
    margin-top: 20px;
}

ul li{
    list-style: none;
    display: inline-block;
    line-height: 90px;
    padding: 0 31px;
}

ul{
    margin-left: 840px;
}

ul li a{
    text-decoration: none;
    font-size: 20px;
    font-family: 'Roboto Condensed', sans-serif;
    color: rgb(250, 250, 250);
}

ul li a:hover {
    color: rgb(102, 151, 241);
    transition-duration: 0.5s;
}

#text{
    transform: uppercase;
    color: white;
    font-family: 'Poppins', sans-serif;
    text-align: center;
    margin: 0;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 60px;
}

#button1{
    background-color: rgb(30, 109, 255); 
    border: none;
    border-radius: 15px 50px;
    color: white;
    padding: 35px;
    margin: 22px;
    text-align: center;
    font-size: 13px;
    font-family: 'Poppins', Helvetica;
}

我正在尽最大努力保持更改对您的项目的真实性,而不重组您的HTML。根据您的使用方式,有更好的方法来构造HTML。

我做的第一件事是为文本和按钮创建一个嵌套在里面的容器。然后,我用另一个容器中的按钮移动了文本div和容器div。然后,我把你的很多绝对定位从你的造型上移了出来。然后,我曾经允许使用flexbox样式/居中,因为我发现这是居中的最简单方法。

希望这能有所帮助!

  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航