Я пытаюсь центрировать изображение в div, но никакие методы не работают. Я пытался использовать margin: auto;
и многие другие популярные методы, но они не работают.
<style>
/* Add a black background color to the top navigation */
.topnav {
background-color: #1F363D;
overflow: hidden;
}
/* Style the links inside the navigation bar */
.topnav a {
float: left;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
/* Change the color of links on hover */
.topnav a:hover {
background-color: #BBBBBB;
color: #1F363D;
}
/* Add a color to the active/current link */
.topnav a.active {
background-color: #808782;
color: white;
}
body{
margin: 0px;
}
a{
font-family: monospace;
}
h1 {
font-family: monospace;
}
.header {
text-align: center;
background-color: #000088;
color: white;
padding: 5px;
}
img{
width: 100px;
height: 100px;
border-style: solid;
border-color: black;
border-radius: 10%;
border-weight: 1px;
vertical-align: middle;
}
.grid-holder {
display: grid;
grid-template-columns: 100px 100px 100px 100px;
grid-templete-rows: 100px 100px 100px 100px;
gap: 20px;
height: 100%;
width: fit-content;
margin: auto;
pading: 5px;
border-style: solid;
border-width: 1px;
border-color: black;
border-radius: 10px;
}
#mastermind-holder {
grid-row: 1 / span 1;
grid-column: 1 / span 1;
}
#simon-holder {
grid-row: 1 / span 1;
grid-column: 2 / span 1;
}
.grid-item{
text-align: center;
padding: 5px;
border-style: solid;
border-color: black;
border-radius: 10%;
border-weight: 1px;
width: 100px;
}
.grid-item a {
border-style: solid;
border-color: black;
border-radius: 10%;
border-weight: 1px;
}
</style>
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<meta name = "viewport" content = "width=device-width">
<title>Portfolio - Projects</title>
<link href = "style.css" rel = "stylesheet" type = "text/css" />
</head>
<body>
<div class = "header">
<h1>Portfolio</h1>
</div>
<div class = "topnav">
<a href = "/index.html">Home</a>
<a class = "active" href = "/projects.html">Projects</a>
<a href = "#contact">Contact</a>
<a href = "#about">About</a>
</div>
<div class = "grid-holder">
<div class = "grid-item" id = "mastermind-holder">
<div class = "image-holder">
<img src = "/mastermind-icon.png">
</div>
<a>Mastermind</a>
</div>
<div class = "grid-item" id = "simon-holder">
<img href = "">
<a>Simon</a>
</div>
</div
<script src = "script.js"></script>
</body>
</html>
@Stoff, я вижу, у вас есть редактирование, ожидающее утверждения, почему бы не включить это в свое редактирование?
@Stoff, спасибо за это. Редактор, который я использую, не очень полезен, когда дело доходит до этого.
@tb_03, короткие предложения, используйте инструмент разработчика вместо редактора, у редактора нет сильной функциональности для поиска ошибок
@James Вы должны знать, что это поле предназначено не для обсуждения, а для ответа на ваш вопрос - OP может быть полезно знать, что они не работают. Следовательно, я упомянул об этом, чтобы он знал о них — вместо того, чтобы, возможно, использовать тот же код где-то еще. Не знаю, почему это нужно объяснять.
Вы можете использовать display:flex
в родительском контейнере изображения, который является image-holder
, и использовать align-item:center
, чтобы он мог выравниваться по вертикали.
Я немного отредактировал вашу ширину для img, чтобы лучше показать цель.
/* Add a black background color to the top navigation */
.topnav {
background-color: #1F363D;
overflow: hidden;
}
/* Style the links inside the navigation bar */
.topnav a {
float: left;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
/* Change the color of links on hover */
.topnav a:hover {
background-color: #BBBBBB;
color: #1F363D;
}
/* Add a color to the active/current link */
.topnav a.active {
background-color: #808782;
color: white;
}
body{
margin: 0px;
}
a{
font-family: monospace;
}
h1 {
font-family: monospace;
}
.header {
text-align: center;
background-color: #000088;
color: white;
padding: 5px;
}
img{
max-width: 100px;
max-height: 100px;
padding:30px;
border-style: solid;
border-color: black;
border-radius: 10%;
border-weight: 1px;
vertical-align: middle;
}
.image-holder{
display: flex;
align-items: center;
}
.grid-holder {
display: grid;
grid-template-columns: 100px 100px 100px 100px;
grid-templete-rows: 100px 100px 100px 100px;
gap: 20px;
height: 100%;
width: fit-content;
margin: auto;
pading: 5px;
border-style: solid;
border-width: 1px;
border-color: black;
border-radius: 10px;
}
#mastermind-holder {
grid-row: 1 / span 1;
grid-column: 1 / span 1;
}
#simon-holder {
grid-row: 1 / span 1;
grid-column: 2 / span 1;
}
.grid-item{
text-align: center;
padding: 5px;
border-style: solid;
border-color: black;
border-radius: 10%;
border-weight: 1px;
width: 100px;
}
.grid-item a {
border-style: solid;
border-color: black;
border-radius: 10%;
border-weight: 1px;
}
<div class = "header">
<h1>Portfolio</h1>
</div>
<div class = "topnav">
<a href = "/index.html">Home</a>
<a class = "active" href = "/projects.html">Projects</a>
<a href = "#contact">Contact</a>
<a href = "#about">About</a>
</div>
<div class = "grid-holder">
<div class = "grid-item" id = "mastermind-holder">
<div class = "image-holder">
<img src = "/mastermind-icon.png">
</div>
<a>Mastermind</a>
</div>
<div class = "grid-item" id = "simon-holder">
<img href = "">
<a>Simon</a>
</div>
display:grid
and place-item:center
on parent element as well.
/* Add a black background color to the top navigation */
.topnav {
background-color: #1F363D;
overflow: hidden;
}
/* Style the links inside the navigation bar */
.topnav a {
float: left;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
/* Change the color of links on hover */
.topnav a:hover {
background-color: #BBBBBB;
color: #1F363D;
}
/* Add a color to the active/current link */
.topnav a.active {
background-color: #808782;
color: white;
}
body{
margin: 0px;
}
a{
font-family: monospace;
}
h1 {
font-family: monospace;
}
.header {
text-align: center;
background-color: #000088;
color: white;
padding: 5px;
}
img{
max-width: 100px;
max-height: 100px;
padding:30px;
border-style: solid;
border-color: black;
border-radius: 10%;
border-weight: 1px;
vertical-align: middle;
}
.image-holder{
display: grid;
place-content: center;
}
.grid-holder {
display: grid;
grid-template-columns: 100px 100px 100px 100px;
grid-templete-rows: 100px 100px 100px 100px;
gap: 20px;
height: 100%;
width: fit-content;
margin: auto;
pading: 5px;
border-style: solid;
border-width: 1px;
border-color: black;
border-radius: 10px;
}
#mastermind-holder {
grid-row: 1 / span 1;
grid-column: 1 / span 1;
}
#simon-holder {
grid-row: 1 / span 1;
grid-column: 2 / span 1;
}
.grid-item{
text-align: center;
padding: 5px;
border-style: solid;
border-color: black;
border-radius: 10%;
border-weight: 1px;
width: 100px;
}
.grid-item a {
border-style: solid;
border-color: black;
border-radius: 10%;
border-weight: 1px;
}
<div class = "header">
<h1>Portfolio</h1>
</div>
<div class = "topnav">
<a href = "/index.html">Home</a>
<a class = "active" href = "/projects.html">Projects</a>
<a href = "#contact">Contact</a>
<a href = "#about">About</a>
</div>
<div class = "grid-holder">
<div class = "grid-item" id = "mastermind-holder">
<div class = "image-holder">
<img src = "/mastermind-icon.png">
</div>
<a>Mastermind</a>
</div>
<div class = "grid-item" id = "simon-holder">
<img href = "">
<a>Simon</a>
</div>
Обратите внимание, несколько строк в вашем css написаны с ошибками. вес границы = ширина границы? прокладка = прокладка? сетка-шаблон-строки = сетка-шаблон-строки? Что касается изображений, какое изображение вы пытаетесь центрировать, в чем?