简体   繁体   English

图片的高度和宽度在CSS中不会改变

[英]Height and width of image won't change in CSS

Trying to alter an image to display alongside another div containing text and have been at it for hours to no avail. 尝试更改图片以使其与包含文本的另一个div一起显示,并且已经花了几个小时无济于事。 The image is supposed to show in line with the other div but refuses to do so: 该图像应显示为与另一个div一致,但拒绝这样做:

图片移动

<div id="leftimg">
            <img src ="SLL/student.jpg"/>
</div>

First attempt: 第一次尝试:

#leftimg {
    max-width: 50%;
    height: inherit;
    left: 0px;
    right: 0px;
}

Second attempt: 第二次尝试:

#leftimg {
    max-width: 100%;
    height: auto;
    left: 0px;
    right: 0px;
}

Third attempt: 第三次尝试:

#leftimg {
    width: 500px;
    height: 500px;
}

It looks like you're selecting the div element in your CSS, instead of the image. 看起来您在CSS中选择了div元素,而不是图像。 If you'd like to apply those rules to the image inside the div you'll need to follow the div's ID with img like this: 如果要将这些规则应用于div中的图像,则需要使用img跟随div的ID,如下所示:

#leftimg img{
  max-width: 50%;
  height: inherit;
  left: 0px;
  right: 0px;
  . . .
}

Here's a full example: 这是一个完整的示例:

 #leftimg img{ max-width: 50%; height: inherit; left: 0px; right: 0px; /* Added to make the text wrap around the image feel free to ignore if that's not how you want it to work */ float:left; padding:10px; } /* Added to clear the float used above - if you leave out my stuff above, then leave this out too */ #leftimg:after{ content:''; clear:both; } 
 <div> <div id="leftimg"> <img src="https://pixabay.com/static/uploads/photo/2015/09/22/15/02/studying-951818_640.jpg"/> </div> <h3>Student Letting</h3> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum tincidunt turpis in magna imperdiet dapibus. Nullam eu neque vel augue rutrum euismod. Sed egestas purus blandit leo rhoncus, sit amet egestas sapien lobortis. Aliquam placerat nec diam id tincidunt. Phasellus in mollis arcu. Aliquam venenatis, est ac pharetra semper, magna libero euismod sem, ac molestie leo ex sed nunc. In ullamcorper orci in dapibus venenatis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Etiam posuere venenatis finibus. Vestibulum orci metus, auctor eu nisi in, suscipit viverra erat. Vivamus purus magna, porta auctor urna eu, venenatis mollis leo.</p> <p>Duis justo magna, hendrerit ut tortor eu, ornare viverra mi. Ut ultrices gravida arcu, nec tincidunt nulla vehicula in. Pellentesque varius nulla ligula, quis convallis felis blandit at. Nam sit amet lobortis dui, et imperdiet orci. Donec malesuada enim nec tellus auctor accumsan. Nam vehicula felis nec dolor facilisis, vel dapibus dui scelerisque. Donec quis nunc venenatis, laoreet est ac, condimentum arcu. Quisque ut luctus felis. Nulla commodo auctor risus a ullamcorper. Curabitur bibendum tincidunt ante a porta. Ut ut orci ut nulla tempor ornare.</p> </div> 

But if you want to have multiple images inserted into a page with this style you really should use a class instead of an ID, like this: 但是,如果您希望以这种样式将多个图像插入页面,则应该使用类而不是ID,如下所示:

 .leftimg img{ /* Notice how we use the class selector instead */ max-width: 50%; height: inherit; left: 0px; right: 0px; /* Added to make the text wrap around the image feel free to ignore if that's not how you want it to work */ float:left; padding:10px; } /* Added to clear the float used above - if you leave out my stuff above, then leave this out too */ .leftimg:after{ /* And again here we use the class selector instead */ content:''; clear:both; } 
 <div> <div class="leftimg"> <!-- Notice id is changed to class --> <img src="https://pixabay.com/static/uploads/photo/2015/09/22/15/02/studying-951818_640.jpg"/> </div> <h3>Student Letting</h3> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum tincidunt turpis in magna imperdiet dapibus. Nullam eu neque vel augue rutrum euismod. Sed egestas purus blandit leo rhoncus, sit amet egestas sapien lobortis. Aliquam placerat nec diam id tincidunt. Phasellus in mollis arcu. Aliquam venenatis, est ac pharetra semper, magna libero euismod sem, ac molestie leo ex sed nunc. In ullamcorper orci in dapibus venenatis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Etiam posuere venenatis finibus. Vestibulum orci metus, auctor eu nisi in, suscipit viverra erat. Vivamus purus magna, porta auctor urna eu, venenatis mollis leo.</p> <p>Duis justo magna, hendrerit ut tortor eu, ornare viverra mi. Ut ultrices gravida arcu, nec tincidunt nulla vehicula in. Pellentesque varius nulla ligula, quis convallis felis blandit at. Nam sit amet lobortis dui, et imperdiet orci. Donec malesuada enim nec tellus auctor accumsan. Nam vehicula felis nec dolor facilisis, vel dapibus dui scelerisque. Donec quis nunc venenatis, laoreet est ac, condimentum arcu. Quisque ut luctus felis. Nulla commodo auctor risus a ullamcorper. Curabitur bibendum tincidunt ante a porta. Ut ut orci ut nulla tempor ornare.</p> </div> 

Doing it with a CSS class would let you have several images on the same page using the same styling, without having to repeat your code. 使用CSS类完成此操作后,您将可以在同一页面上使用相同的样式获得多个图像,而不必重复执行代码。 If you're only going to have one image it may be more beneficial to leave it as an ID though. 如果您只打算拥有一张图像,那么将其保留为ID可能会更有利。 It just depends on how you're going to be using this. 这仅取决于您将如何使用它。

Give the image element an id and set the width/heigth of it: 给图像元素一个id并设置它的宽度/高度:

HMTL: HMTL:

<img id="img" src ="SLL/student.jpg"/>

And in your CSS File 并在您的CSS文件中

#img{
    width: 500px;
    height: 500px;
}

Or set a background to the div 或为div设置背景

#leftimg {
    background-image: url("SLL/student.jpg");
}

You can do it like this: 您可以这样做:

#leftimg img {
    width: 500px;
    height: 500px;
}

Try this 尝试这个

#leftimg img {
    width: 500px;
    height: 400px
}

If you want to change dimensions of your image, change to image itself, not the to image wrapper 如果要更改图像的尺寸,请更改为图像本身,而不是图像包装器

try 尝试

img{

     width:100%;
     display: table-cell; /* to give the img full height */
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM