Я пытаюсь центрировать кнопку в контейнере с жидкостью со 100% высотой по вертикали и дать ей смещение от нижней части экрана. Я в значительной степени там, однако кнопка смещена слева и не правильно отцентрирована. См. пример в моем фрагменте кода.
Я не уверен, что мне здесь не хватает... как мне правильно центрировать кнопку по центру экрана?
В коде моего сайта у меня есть изображение в качестве фона в основном контейнере с жидкостью, поэтому у меня есть размер фона: обложка; и т. д. в моем css.
.main-div {
background-color: pink;
min-height: 100vh;
min-width: auto;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
position: relative;
text-align: center;
}
.centered-button {
position: absolute;
bottom: 3rem;
width: 15rem;
border-radius: 3rem;
border-top-width: 4px;
border-bottom-width: 4px;
border-right-width: 4px;
border-left-width: 4px;
box-shadow: 5px 5px 10px 2px rgba(0,0,0,.8);
transition: all 0.3s ease 0s;
}<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "UTF-8">
<meta http-equiv = "X-UA-Compatible" content = "IE=edge">
<meta name = "viewport" content = "width=device-width, initial-scale=1.0">
<title>Document</title>
<link href = "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel = "stylesheet"
integrity = "sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin = "anonymous">
<link href = "styles.css" rel = "stylesheet"/>
</head>
<body>
<div class = "fluid-container main-div">
<button type = "button" class = "btn btn-primary centered-button">Primary</button>
</div>
</body>
</html>





Вы должны реализовать выравнивание начальной загрузки вместо использования обычного css, попробуйте Flex-контейнеры. Смотрите мой пример ниже:
<button type = "button" class = "d-flex justify-content-center btn btn-primary">Primary</div>
Добавление d-flex justify-content-centerпо горизонтали выровняет ваш элемент. Чтобы выровнять элемент по вертикали, посмотрите на Вертикальное выравнивание
добавлять
left: 50%;
transform: translateX(-50%);
к элементу .centered-button для центрирования.
эта ссылка является полным ответом для центрирования с позицией
.main-div {
background-color: pink;
min-height: 100vh;
min-width: auto;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
position: relative;
text-align: center;
}
.centered-button {
position: absolute;
bottom: 3rem;
width: 15rem;
border-radius: 3rem;
border-top-width: 4px;
border-bottom-width: 4px;
border-right-width: 4px;
border-left-width: 4px;
box-shadow: 5px 5px 10px 2px rgba(0,0,0,.8);
transition: all 0.3s ease 0s;
left: 50%;
transform: translateX(-50%);
}<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "UTF-8">
<meta http-equiv = "X-UA-Compatible" content = "IE=edge">
<meta name = "viewport" content = "width=device-width, initial-scale=1.0">
<title>Document</title>
<link href = "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel = "stylesheet"
integrity = "sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin = "anonymous">
<link href = "styles.css" rel = "stylesheet"/>
</head>
<body>
<div class = "fluid-container main-div">
<button type = "button" class = "btn btn-primary centered-button">Primary</button>
</div>
</body>
</html>и вы можете использовать flex, чтобы сделать это просто.
.main-div {
background-color: pink;
min-height: 100vh;
min-width: auto;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
display: flex;
justify-content: center;
align-items: center;
}
.centered-button {
bottom: 3rem;
width: 15rem;
border-radius: 3rem;
border-top-width: 4px;
border-bottom-width: 4px;
border-right-width: 4px;
border-left-width: 4px;
box-shadow: 5px 5px 10px 2px rgba(0,0,0,.8);
transition: all 0.3s ease 0s;
}<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "UTF-8">
<meta http-equiv = "X-UA-Compatible" content = "IE=edge">
<meta name = "viewport" content = "width=device-width, initial-scale=1.0">
<title>Document</title>
<link href = "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel = "stylesheet"
integrity = "sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin = "anonymous">
<link href = "styles.css" rel = "stylesheet"/>
</head>
<body>
<div class = "fluid-container main-div">
<button type = "button" class = "btn btn-primary centered-button">Primary</button>
</div>
</body>
</html>.main-div {
background-color: pink;
min-height: 100vh;
line-height:100vh;
min-width: auto;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
position: relative;
text-align: center;
}
.centered-button {
vertical-align: middle;
bottom: 3rem;
width: 15rem;
border-radius: 3rem;
border-top-width: 4px;
border-bottom-width: 4px;
border-right-width: 4px;
border-left-width: 4px;
box-shadow: 5px 5px 10px 2px rgba(0,0,0,.8);
transition: all 0.3s ease 0s;
}<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "UTF-8">
<meta http-equiv = "X-UA-Compatible" content = "IE=edge">
<meta name = "viewport" content = "width=device-width, initial-scale=1.0">
<title>Document</title>
<link href = "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel = "stylesheet"
integrity = "sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin = "anonymous">
<link href = "styles.css" rel = "stylesheet"/>
</head>
<body>
<div class = "fluid-container main-div">
<button type = "button" class = "btn btn-primary centered-button">Primary</button>
</div>
</body>
</html>.main-div {
display:flex;
flex-direction:column;
align-items:center;
justify-content:center;
background-color: pink;
min-height: 100vh;
line-height:100vh;
min-width: auto;
position: relative;
}
.centered-button {
bottom: 3rem;
width: 15rem;
border-radius: 3rem;
border-top-width: 4px;
border-bottom-width: 4px;
border-right-width: 4px;
border-left-width: 4px;
box-shadow: 5px 5px 10px 2px rgba(0,0,0,.8);
transition: all 0.3s ease 0s;
}<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "UTF-8">
<meta http-equiv = "X-UA-Compatible" content = "IE=edge">
<meta name = "viewport" content = "width=device-width, initial-scale=1.0">
<title>Document</title>
<link href = "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel = "stylesheet"
integrity = "sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin = "anonymous">
<link href = "styles.css" rel = "stylesheet"/>
</head>
<body>
<div class = "fluid-container main-div">
<button type = "button" class = "btn btn-primary centered-button">Primary</button>
</div>
</body>
</html>Ответы только на код не очень полезны в долгосрочной перспективе. Пожалуйста, рассмотрите редактироватьing, чтобы добавить объяснение того, что вы изменили.
Как сейчас написано, ваш ответ неясен. Пожалуйста, редактировать, чтобы добавить дополнительную информацию, которая поможет другим понять, как это относится к заданному вопросу. Дополнительную информацию о том, как писать хорошие ответы, можно найти в справочном центре.
Существует много кода для поиска двух разных строк. Пожалуйста, редактировать в пояснении.