Struggling to contain the image inside my div element

I'm having trouble with keeping the image inside my div element as it keeps stretching outside of it. I've tried researching for a solution but haven't been successful in finding one. Any help would be greatly appreciated!

Here is the HTML/CSS code snippet...

.navigation {
    padding-top: 70px;
    padding-bottom: 70px;
    position: relative;
    text-transform: uppercase;

}

.container {

}

.brand-text {
    font-family: Georgia, Times, 'Times New Roman', serif;
    font-size: 40px;
    font-weight: 700px;
    color: black;
    -webkit-transition: color 700ms ease;
    text-align: center;

}

.brand-text:hover {
    color: grey;
}

.nav-brand {
    text-decoration:none;
}

.nav-menu {
    text-align: center;
}

.nav-link {
    padding-top: 7px;
    padding-bottom: 7px;
    margin-right: 20px;
    margin-left: 20px;
    text-decoration: none;
    color: black;
    font-family: Georgia, Times, 'Times New Roman', serif;
    -webkit-transition: color 700ms ease;
    position: relative;
}

.nav-link:hover {
    color: grey;
}

a {
    text-decoration: none;
}

div.img {
    margin: 5px;
    float: left;
    width: 180px;
    -webkit-transition: opacity 400ms ease, box-shadow 400ms ease;
    transition: opacity 400ms ease, box-shadow 400ms ease;
    background-color: #fff;
}

div.img:hover {
    background-color: transparent;
    box-shadow: 0 0 50px -14px #000;
    opacity: 0.8;
}

div.img img {
    width: 100%;
    height: auto;
}

div.desc {
    text-align: center;
    color: black;
    padding: 15px;
}

* {
    box-sizing: border-box;
}

@media only screen and (max-width: 700px){
    .reponsive {
        width: 49.999999%;
        margin: 6px;
    }
}

@media only screen and (max-width: 500px){
    .responsive {
        width: 100%
    }
}

.clearfix:after {
    content: "";
    display: table;
}
<!DOCTYPE html>
<html>
<head>
<title>Josh Corbett</title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body>
<div class="navigation">
<div class="container">
<a class="nav-brand" href="#"><h1 class="brand-text">Josh Corbett</h1></a>
<nav class="nav-menu" role="navigation">
<a class="nav-link" href="#">Portfolio</a>
<a class="nav-link" href="#">About</a>
<a class="nav-link" href="#">Contact</a>
<a class="nav-link" href="#">Blog</a>
</nav>
</div>
</div>
<div class="responsive">
<div class="img">
<a target="_blank" href="#">
<img src="http://wallpaperwarrior.com/wp-content/uploads/2016/09/Wallpaper-1-1024x640.png" alt="Mountain" width="300" height="200">
<div class="desc">Beautiful Montain</div>
</a>
</div>
</div>

</body>

Answer №1

attempt

img{
  width: 100%;
  height: auto;
}

this method should ensure your image stays within the boundaries of its container

Answer №2

Don't forget to add the correct ; at the end of your div.img img definition:

div.img img {
    width: 100%;
    height: auto;
}

.navigation {
padding-top: 70px;
padding-bottom: 70px;
position: relative;
text-transform: uppercase;

}

.container {


}

.brand-text {
font-family: Georgia, Times, 'Times New Roman', serif;
font-size: 40px;
font-weight: 700px;
color: black;
-webkit-transition: color 700ms ease;
text-align: center;

}

.brand-text:hover {
color: grey;

}
.nav-brand {
text-decoration:none;
}

.nav-menu {
text-align: center;
}

.nav-link {
padding-top: 7px;
padding-bottom: 7px;
margin-right: 20px;
margin-left: 20px;
text-decoration: none;
color: black;
font-family: Georgia, Times, 'Times New Roman', serif;
-webkit-transition: color 700ms ease;
position: relative;
}

.nav-link:hover {
color: grey;
}

/*Gallery*/

a {
text-decoration: none;

}

div.img {
margin: 5px;
float: left;
width: 180px;
-webkit-transition: opacity 400ms ease, box-shadow 400ms ease;
transition: opacity 400ms ease, box-shadow 400ms ease;
background-color: #fff;
}

div.img:hover {
background-color: transparent;
box-shadow: 0 0 50px -14px #000;
opacity: 0.8;

}

div.img img {
width: 100%;
height: auto;
}

div.desc {
text-align: center;
color: black;
padding: 15px;
}

* {
box-sizing: border-box;
}

@media only screen and (max-width: 700px){
.reponsive {
width: 49.999999%;
margin: 6px;
}
}

@media only screen and (max-width: 500px){
.responsive {
width: 100%
}

}

.clearfix:after {
content: "";
display: table;
}
<!DOCTYPE html>
<html>
<head>
<title>Josh Corbett</title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body>
<div class="navigation">
<div class="container">
<a class="nav-brand" href="#"><h1 class="brand-text">Josh Corbett</h1></a>
<nav class="nav-menu" role="navigation">
<a class="nav-link" href="#">Portfolio</a>
<a class="nav-link" href="#">About</a>
<a class="nav-link" href="#">Contact</a>
<a class="nav-link" href="#">Blog</a>
</nav>
</div>
</div>
<div class="responsive">
<div class="img">
<a target="_blank" href="#">
<img src="http://wallpaperwarrior.com/wp-content/uploads/2016/09/Wallpaper-1-1024x640.png" alt="Mountain" width="300" height="200">
<div class="desc">Beautiful Montain</div>
</a>
</div>
</div>

</body>

Answer №3

you are explicitly defining the image width and height as "300" and "200"

Since you have already set the image width in your CSS, Update this:

<img src="http://wallpaperwarrior.com/wp-content/uploads/2016/09/Wallpaper-1-1024x640.png" alt="Mountain" width="300" height="200">

To this

<img src="http://wallpaperwarrior.com/wp-content/uploads/2016/09/Wallpaper-1-1024x640.png" alt="Mountain">

(remove the width and height attributes)

Answer №4

You forgot to include a semi-colon at the end of the image width property.

div.img img {
    width: 100%;
    height: auto;
}

Answer №5

If you're unsure if this is what you're searching for, consider adjusting the size properties of the picture elements to 100%.

Here's how you can make the change:

<img src="http://wallpaperwarrior.com/wp-content/uploads/2016/09/Wallpaper-1-1024x640.png" alt="Mountain" width="300" height="200">

Change it to:

<img src="http://wallpaperwarrior.com/wp-content/uploads/2016/09/Wallpaper-1-1024x640.png" alt="Mountain" width="100%" height="100%">

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The column flex-direction property seems to be malfunctioning

Can anyone help me with styling a ul tag using display: flex? I'm trying to order it by column with flex-direction: column;, but for some reason, it's not working. Here is the CSS code for the container: #nav li.four_columns ul.sub-menu { widt ...

Display additional content button, dynamic div identification

I've created a small script that includes a "show more" button at the end of displaying 10 entries. Here is the code: <div id="more<?=$lastid;?>"> <a onclick="showmore(<?=$lastid;?>);">More</a> </div> And here ...

Using the onclick attribute as a unique identifier for a button

I am currently facing a challenge with a form that does not have an ID Here is the code snippet in question: <button class="btn btn-primary" onclick="showModal()" type="button">Browse Data</button> Unfortunately, I don't have any contro ...

Encountering an issue where an error message indicates that a variable previously declared is now undefined

Currently, I am working on developing a small test application to enhance my coding skills. However, I have encountered a roadblock while attempting to display dummy information from a mongodb database that I have set up. I have tried various solutions bu ...

Ways to implement CSS styling to every input element excluding inputs located within list items of a specific class

input.not(li.tagit-new >input) { background-color: lightgrey; } <ul id="tagAdministrator" style="width: 505px" class="tagit ui-widget ui-widget-content ui-corner-all"> <li class="tagit-new" style="box-shadow: none;"> <input ty ...

Modify the base URL with JavaScript

Is it possible to dynamically change the href using JavaScript? I attempted to make this change with the code below in my HTML file, but unfortunately, it didn't work: <base href="/" /> <script type="text/javascript"> function setbasehr ...

Ways to toggle the visibility of an element based on the condition of two other elements

I'm facing a challenging UI use-case that I just can't crack. I have 2 non-nested divs that are causing me trouble (not nesting is crucial here as it would have made things much simpler). Here is the structure of the divs: <div class="a"> ...

Using JQuery AJAX to transfer unstructured list elements to an ASP.net MVC controller action

Need help passing list item values to controller action method as model, but the model is returning null. Below is the JQuery code I have attempted: Here is my HTML: <ul class="to_do" id="Emaillist"> <li class="alert" value="2">example list ...

What is the best way to position the top line next to the border?

I've been attempting to create a falling line effect on the left side of the box's border, but I can't seem to figure out what's causing the issue. Below is the code snippet: @import url('https://fonts.googleapis.com/css ...

CSS directives are not compatible with Internet Explorer 8

I implemented a registration form with CSS rules that highlight error fields with a red border when users submit incorrect or empty data. However, this functionality is not working in Internet Explorer. The red border appears correctly in Firefox and Safar ...

What could be causing my JavaScript source to fail to load in an HTML document?

Currently, I am in the process of creating a basic offline dinosaur game using HTML, JS, and CSS that is well-known to everyone. I have been diligently working on it and have successfully made everything function for a few hours. However, out of nowhere, m ...

Transforming DOM elements into Objects

Here are some values that I have: <input name="Document[0][category]" value="12" type="text"> <input name="Document[0][filename]" value="abca.png" type="text" > I am looking for a way to convert them into an object using JavaScript or jQuer ...

Using Bootstrap classes within a specified class declaration

Can you nest style classes within a single class? For example: .my-upper-class{ .hidden-md, .hidden-sm, .hidden-lg} ...

Learn how to apply formatting to all textboxes on an ASP.NET website using CSS without having to set the CSS class property for each individual textbox

I am currently developing a website using Asp.Net. I am looking for a way to customize the font, color, and background color of all the textboxes on my site using CSS. However, I would like to avoid having to assign a "cssclass" property to each individua ...

Is there a way to adjust the padding temporarily?

Important Note: Despite the misleading title of my question, it doesn't accurately reflect my actual query (sort of). Below is the code snippet in question: .one{ background-color: gray; } .two{ padding: 20px; } <div class = "one"> < ...

Challenges with jQuery Image Sliders

Currently, I am learning from a tutorial on creating a custom jQuery content slider and you can find the tutorial here. Everything is working smoothly in terms of creating the image slider. However, I am facing an issue where I want to have the 'Next ...

How to Align <ul> to the Right in Bootstrap 4

Looking to align a list to the right in the header section, here is the code snippet: <div class="collapse navbar-collapse" id="app-header-nav"> <ul class="navbar-nav mr-auto mt-2 mt-lg-0"> <li class="nav-item"> <a class=" ...

Retrieve the total count of tables within a specific div element

If I have an unspecified amount of tables within a div, how can I determine the total number of tables using either plain JavaScript or jQuery? ...

A guide to customizing the appearance of Textfield labels in Material-UI using React

Can someone assist me with changing the label color in Material-UI from grey to black? I am new to Material-UI and struggling to figure it out. Below is the code snippet: import React from "react"; import ReactDOM from "react-dom"; import { TextField, Bu ...

Tips for starting the debugging process with POST and GET requests in Django and the Python Shell

I've encountered an issue with my code where the database is not updating and a new record is not being created. I suspect that there may be a problem with how I am handling the POST data retrieval in my script. Can anyone provide guidance on where to ...