2

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.

Yabusa
  • 579
  • 1
  • 6
  • 15
  • 1
    Try adding `margin: 0 auto` to the `.logo` class before the margins you already have... also `top: 0;` is not doing anything since `.logo` is a static element – IvanS95 Apr 22 '19 at 20:20
  • I would suggest you learn the box model: https://developer.mozilla.org/en-US/docs/Learn/CSS/Introduction_to_CSS/Box_model – Julian Espinosa Apr 22 '19 at 20:23
  • Possible duplicate of [How to horizontally center a
    ?](https://stackoverflow.com/questions/114543/how-to-horizontally-center-a-div)
    – IvanS95 Apr 22 '19 at 20:25
  • @IvanS95 flexbox? there is nothing related to flexbox... I am providing a link for the box model, not flexbox. – Julian Espinosa Apr 22 '19 at 20:25
  • My bad, read that wrong :) – IvanS95 Apr 22 '19 at 20:26
  • You don't seem to have a body element. That may be your problem. It might not know of what it should be 33%. try putting some type of container around it. – Patrick Mcvay Apr 22 '19 at 20:28

2 Answers2

5

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>
hungerstar
  • 21,206
  • 6
  • 50
  • 59
  • 1
    Did you implement the second selector `.logo img`? Without it, the image will overflow the ``. – hungerstar Apr 22 '19 at 20:27
  • I had tried it before your edit, it works now! thanks – Yabusa Apr 22 '19 at 20:31
  • The only issue now, is I wanted to make it 70vw, but when it becomes 100% of the image size, it stops being centered – Yabusa Apr 22 '19 at 20:33
  • 1
    The styles for `.logo img` are standard responsive image styles. The `max-width: 100%` declaration prevents the image from expanding beyond its native resolution. Which means if the container element is `500px` wide and the image is `400px` wide, the image will not expand beyond `500px`. When an image expands beyond its native resolution, it will become pixelated. This is one of the primary reasons for `max-width: 100%;`. To fix your issue you can either supply a larger image or remove `max-width: 100%` and replace it with `width: 100%;`, though your image may become pixelated. – hungerstar Apr 22 '19 at 21:09
3

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.