Yabusa
Yabusa

Reputation: 579

Trying to center an image 33% of the total viewport width

I have the image as display: block;, text-align: center;, width: 33%; and still not centering or acting like 33%. I also tried width: 33vw (33% viewport width), still no luck.

I've tried creating a container of 100%, then creating a div which the image lies, still didn't solve it. I have a strong feeling it's something so silly, but I've been looking at the code too long I can't spot it. I just want the logo to change in size so someone on a mobile can see the full logo, instead of having to scroll to the right.

a {
    text-decoration: none;
    color: black;
}

nav {
    position: sticky;
    position: -webkit-sticky;
    top: 0;
}

.nav {
    position: sticky;
    left: 0;
    background-color: rgba(255,255,255,.8);
    border-radius: 0px;
    border: none;
    width: 100%;
    padding: 10px 0;
    flex-direction: row;
    display: flex;
    align-items: center;
    justify-content: space-evenly;
}

.item {
    color: black;
    font-weight: bold;
    text-transform: capitalize;
    width: 25%;
    text-align: center;

}

.submenu {
    display: none;
    flex-wrap: wrap;
    align-items: center;
    align-text: center;
    position: absolute;
    padding-top: 107px;
    padding: 10px;
    font-size: small;
    left: 0;
    right: 0;
    text-transform: capitalize;
    z-index: 1;
    background-color: #2F4F4F;
    color: white;
    justify-content: left;
}

.submenu li {
    margin-left: 6%;
    width: 19%;
    padding: 5px;

}

.item.has-children:hover .submenu {
    display: flex;
    align-items: center;
    flex-direction: row;
    justify-content: left;
    flex-wrap: wrap;
    flex: 1 1 calc(25% - 80px);
    color: black;
    background-color: rgba(255,255,255,.8);
}

ul {
    list-style: none;
    padding: 0;
}

.logo {
    top: 0;
    display: block;
    width: 33%;
    text-align: center;
    margin-top: 50px;
    margin-bottom: 25px;
}
<head>
<meta charset="UTF-8" content="width=device-width, initial-scale= 1">
<title>Graphic Design</title>

<link href="../CSS/Navigation.css" rel="stylesheet" type="text/css">
<link href="../CSS/Printing.css" rel="stylesheet" type="text/css">

</head>

    <a class="logo" href="index.html">
        <img src="../Images/Navigation/Intak Logo 40px High.png" alt="Home"/>
    </a>

<nav>...

It should be centered with a width of 33%, but it's not reacting to width commands or centering.

Upvotes: 2

Views: 1239

Answers (2)

Michael Almeida
Michael Almeida

Reputation: 132

You must apply the style in the image.

Try something like this:

.logo {
    top: 0;
    display: block;
    margin-top: 50px;
    margin-bottom: 25px;
}

.logo img {
  display: block;
  margin: auto;
  width: 33%;
}

For a image be centralized it should have the attributes: display block, margin auto and some width.

Upvotes: 3

hungerstar
hungerstar

Reputation: 21685

This is probably the most basic solution to your problem.

.logo {
  display: block;
  margin-right: auto;
  margin-left: auto;
  width: 33.33333vw;
}
<img class="logo" src="https://via.placeholder.com/600x600/fc0">

With a link.

.logo {
  display: block;
  margin-right: auto;
  margin-left: auto;
  width: 33.33333vw;
}

.logo img {
  display: block;
  height: auto;
  max-width: 100%;
}
<a class="logo" href="#">
  <img src="https://via.placeholder.com/600x600/fc0">
</a>

Upvotes: 5

Related Questions