Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is Image size reset for me when i add a < a > tag

I have a simple image that has a size attribute added using CSS. I decided to make the image clickable by adding a <a> tag hoping nothing will change. But the whole image has been reset and i cant change the size without removing the <a> tag.

HTML

<a href="#"><header><img src="images/logo.png" alt=""></header></a>

CSS

header img {
    width: 100%;
    height: 50%;
}

Additionally, another React project is having the same issue:


// the css for "catImg"
const useStyles = makeStyles(theme => ({
    catImg: {
        "&:hover": {
            boxShadow: "10px 5px 5px black;",
            backgroundColor: "blue",
        }
    }
}))
.
.
.
<ImageListItem className={classes.catImg}>
                  {/* <a> */}
                   <img
                   src={url}
                   key={cat.token_id} />
                  {/* </a> */}
</ImageListItem>

EDIT:

Putting the a tag outside the ImageListItem makes it work correctly.


// the css for "catImg"
const useStyles = makeStyles(theme => ({
    catImg: {
        "&:hover": {
            boxShadow: "10px 5px 5px black;",
            backgroundColor: "blue",
        }
    }
}))
.
.
.
<a href="something">
<ImageListItem className={classes.catImg}>
                   <img
                   src={url}
                   key={cat.token_id} />
</ImageListItem>
<a/>
like image 229
usernameisnotprogrammedyet Avatar asked Jan 19 '16 20:01

usernameisnotprogrammedyet


People also ask

How to adjust img size HTML?

One of the simplest ways to resize an image in the HTML is using the height and width attributes on the img tag. These values specify the height and width of the image element. The values are set in px i.e. CSS pixels.

How do you change the size of an image in CSS?

Sometimes, it is required to fit an image into a certain given dimension. We can resize the image by specifying the width and height of an image. A common solution is to use the max-width: 100%; and height: auto; so that large images do not exceed the width of their container.

How do you put a picture on a picture in HTML?

How to Insert an Image in HTML. To insert an image in HTML, use the image tag and include a source and alt attribute. Like any other HTML element, you'll add images to the body section of your HTML file. The HTML image element is an “empty element,” meaning it does not have a closing tag.


2 Answers

The quick response is because yow styles said that them images inside yow header are going to have this styles

header img {
  width: 100%;
  height: 50%;
}

The problem is in your html. Html has rules AND one of them rules is that block tags should not be in side of inline tags header tag is a block tag a is inline

If you want to override that rule in your css you need to specify that a tags are block tags as well with

a {
  display: block;
}
like image 200
JS Disciple Avatar answered Nov 10 '22 06:11

JS Disciple


You have to reference the a tag instead of the img

header a {
  width: 100%;
  height: 50%;
  padding: 0;
  margin: 0;
}
header a img {
  width: 100%;
  height: 100%;
}
like image 24
Charlie Cureton Avatar answered Nov 10 '22 06:11

Charlie Cureton