IMG creates scroll bars even when width is set as percentage

In a previous discussion about how padding is calculated when using percentages (%), it was mentioned that the padding is determined based on the parent element's width.

Now, I have come across the following CSS assignment:

.img_tutorial_full_width
{
    background-color : #3E3E42;

    margin           : 0;
    border           : 0;
    padding          : 1%;

    width            : 99%;
}

When this CSS is applied to an <img> tag like below:

<img class="img_tutorial_full_width" src="../img/example.jpg"></img>

I would expect the container to stretch across the full width of the page since

margin + border = 0 + padding = 1% + content = 99% = 100%

However, the browser displays scrollbars that grow wider as the window size increases. What could be the issue here?

Answer №1

Typically, images are considered inline elements which ignore height and width properties. To overcome this issue, simply add display: block; or display: inline-block to the .img_tutorial_full_width class and it will function properly.

Additionally, remember that applying padding-top, padding-bottom, margin-top, and margin-bottom will have no effect on inline elements. For a more detailed explanation, refer to this informative article.

Answer №2

When the total width is 100%, applying padding: 1%; will allocate 1% on the left and 1% on the right, adding up to a total of 2%

.img_tutorial_full_width
{
    background-color : #3E3E42;

    margin           : 0;
    border           : 0;
    padding          : 1%;

    width            : 98%;
}

This strategy works as intended because 98% + 2 * 1% = 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

Would like to conceal an element only if it is currently visible

Currently in the process of developing a website, I have successfully implemented a dropdown menu that is triggered when clicking on an "About Me" link. This reveals additional links such as biography and interests. The issue I am facing now is regarding t ...

When a dialog box is displayed, a scrollbar will automatically appear

I've encountered a strange issue while working with Angular dialogs: A vertical scrollbar unexpectedly appears, even though I've set the dialog to have a fixed width. component.ts const dialog = this.dialog.open(DialogComponent, { width: ...

What is the best way to arrange flex-column children from left to right?

I'm looking to arrange flex items from left to right within a flex-column layout. The Current Setup How I Want It to Look This is my code: display:flex; flex-direction: column; align-items:flex-end; justify-content: flex-start; align-content: flex- ...

The Angular ngx-color Color Picker is missing the option to customize the width of the color picker

I am currently utilizing the ngx-color Color Picker: https://www.npmjs.com/package/ngx-color#material This is the section where I am implementing the Color Picker and trying to specify the width <div> <div class="flex-title"> ...

ever-evolving background-image with dynamic CSS styling

Being new to both PHP and Javascript, please excuse any mistakes in my explanation. I have information stored in a PHP array that I bring to my index page using the function below (located in a separate file called articles.php that is included in my index ...

Customizing the CSS shadow for a specific ionic select text color, while ensuring that other ion select instances remain unaffected

I'm encountering an issue while attempting to customize the text color inside an ion select element based on the option selected. Unfortunately, when I make changes to the shadow part in one component, it affects other components as well. I want to ap ...

What is the best way to transfer weather data from a server to an HTML text area box?

Recently, I delved into the world of expressJS to set up a local server (localhost:3000). With the power of JavaScript (specifically in a file named app.js), I was able to send simple messages like "Hello World" to the browser. However, now I find myself f ...

What is the best way to address an issue involving Django Template Language's {{ form }}?

<div class="form-title"> {% if form.errors %} <p>{{ form.errors.error }}</p> {% endif %} <form action="{% url 'login' %}" method="post" class="fs-title"> ...

The unitPngFix plugin ensures that the display of hidden div elements cannot be altered

I have been trying to resolve an issue with PNG files and transparency in IE browsers on my website. I have made progress, but there seems to be a problem specifically with IE6. To display transparent PNG images correctly on my site in IE browsers, I am u ...

The arrows are hidden when using the input type number

On my page, I noticed that the input of type=number is missing its arrows and appears like a regular text input. After doing some investigating, I think it might be due to this: https://i.stack.imgur.com/G1BrV.png My browser of choice is Google Chrome. D ...

Optimal strategies for designing navigation bars on contemporary websites

Struggling to find a solid answer for this dilemma. I have multiple html pages and a single navigation bar that I want to include on all pages for consistency. The idea of duplicating the html code for the navigation bar goes against everything I've l ...

Similar title across various categories, retrieve link, Beautiful Soup in Python

Looking for a way to extract URLs from specific HTML elements: <div class="posts-container col-md-6" <ul class="emb-embassies-list" <a class="entry-title" href="commonlink.com" <ul class="emb-embassies-list" <a class="entry- ...

Modify a particular box-shadow within a list of box-shadows using CSS

Is there a way to override one specific value in the box-shadow list while preserving all other values? For example: div{ box-shadow: 0 0 0 5px #00ff00 , 0 0 0 10px #ff0000 , 0 0 0 15px #0000ff ; } If we only want to change the second value without ...

Issues with the functionality of customisable list items in a list

I am attempting to customize the appearance of specific list items within a ul element. One li element should be displayed in red font, while another li element should have a hyperlink styled with a different color and an underline rollover effect. The thi ...

What is the best way to position multi-line text alongside an image without it wrapping below the image?

I managed to align a single line of text with an image successfully (view fiddle). <div class="profile-details-wrapper"> <img class="profile-picture" src="http://placehold.it/50x50" /> <span class="profile-details"> username &l ...

What can be done to address columns overlapping when sidebar is activated?

Excellent https://i.sstatic.net/gwOI8.png Terrible https://i.sstatic.net/4f637.png The first image labeled as 'Excellent' shows that the card divs do not overlap, but in the second image when the sidebar is active they do. I attempted to cha ...

Place a Button or Link Right Below the Title on the Custom Post Type Editing Page

I am currently working on customizing the backend of a WordPress site for a client by adding multiple custom post types. These custom posts are only meant to be displayed on the front-end through a specific loop on one page, preventing users from accessing ...

Unable to adjust the canvas size or scale using Fabric.js

Is there a way to display a large canvas as a smaller one for better user interaction without compromising the size of the uploaded canvas later on? I'm currently working with Fabric.js and exploring the Canvas CSS trick that involves setting the widt ...

Position the table at the bottom of the page using CSS

I'm experiencing an issue where my HTML is not rendering correctly. I need to move the table with the ID "footer" to the bottom of the page. Here's the structure of the situation: <div id="container"> <table id="footer"></tabl ...

Exploring Javascript parameters with the power of jquery

Is it possible to pass a parameter from PHP into a JavaScript function within HTML? I am currently facing an issue where my code crashes when it reaches a certain condition. This is the code snippet causing the problem: $str="<input type='submit&a ...