Comparing inline-block and block display options for displaying images

In this HTML snippet, there is a main div with the id of "main_div" that contains two sub-divs. The first sub-div has an id of "logo" and includes a link to a picture. The second sub-div has an id of "intro" and contains text.

For reference, here is the original code: http://jsfiddle.net/omerbach/y7n2b/

 <div id="main_div">
      <div id="logo" class="headers">
        <a href="http://www.hawkaviation.com" target="_blank">
          <img src="http://www.badcomp.com/wp-content/uploads/2011/03/recommend-hawk.jpg"  alt="Hawk Aviation" />
       </a>
      </div>
      <div id = "intro" class="headers">
        PLACE YOUR ORDER NOW !!! 
      </div>
    </div>

inline-block

You can view the original code for inline-block here: http://jsfiddle.net/omerbach/NneB6/

body {
    background : white;
}
#main_div {
    background:gray;
    border : solid 3px red;    
}
.headers {
    border : solid 1px black;    
    display : inline-block;
}

The author raises some questions about the use of inline and inline-block elements in these examples. They noticed that when using inline, the picture does not stay within its surrounding div, as indicated by the black border. However, when using inline-block, the picture remains inside its containing div.

There seems to be confusion around the purpose of inline-block. The author thought it was meant to make elements inline but maintain their block-level properties like setting width, height, margins, and paddings. Since none of these are specified in the code, they wonder why inline-block should be used in this case.

Lastly, the author is curious about why the second inner element aligns itself to the bottom of the first one by default. They seek clarification on whether this behavior is standard or if there's something specific causing it.

These inquiries reflect the author's quest for understanding and improvement in their coding practices.

Answer №1

A definite yes to the question: is an inline-block element still considered a block? Absolutely. Block elements come with specific heights and widths, making it easy to determine the size of borders around them.

On the contrary, inline elements lack consistent width or height. Consider the strong

  Lorem ipsum dolor <strong> sit amet, consectetur adipisicing elit, sed</strong>

appearing like it's one line high but could break into two lines on a narrow screen:

Lorem ipsum dolor sit amet, consectetur adipisicing
elit, sed

This behavior is not possible with an inline-block. The uncertainty of height and width poses a challenge in determining the dimensions - two lines high? Negative width? The end being to the left of its start!

In essence, it's hard to discern. Even for browsers. With a large element like an img within, an inline div won't expand to fit the entire img; instead, the image will spill over.

To summarize, choosing between a block (inline or other) and a true inline element relies on your circumstances. Understand the distinctions and how to manage them accordingly.

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

Is there a reason why using margin:auto doesn't work for vertically centering items?

I'm having trouble understanding how margin auto functions. In the scenario I'm dealing with, I have a parent div and a child div nested within it, both with specified widths and heights. I attempted to use margin auto in order to center the inne ...

Is animation not functioning for SVG path within a clip path on Firefox?

Creating the HTML for a unique effect: <svg class="svg-defs2" version="1.1" xmlns="http://www.w3.org/2000/svg" height="200" width="640"> <defs> <clipPath id="clipping2"> <!--Adjusting the x-coordinate of start will expand ...

Display or conceal a div depending on whether the user is on a mobile or desktop browser

In order to optimize the display on different devices, I organized my content into two divisions. The left division contains quantity, price, and item name, while the right division includes product names. For mobile browsers, I would like to hide the pro ...

Trouble extracting data with XPath query in Python

Currently, I am attempting to extract specific information from this particular webpage. However, there are three crucial pieces of data that have proven to be quite elusive. Firstly, I am struggling to retrieve the grade, denoted by '5.6' next ...

Is Javascript necessary for submitting a form to a PHP script?

In my current project, I am working on a page to edit information in a database. While I know how to create the form in HTML and the PHP script that runs on the server, I am facing a challenge with JavaScript. My understanding of JavaScript is limited, and ...

Shift attention to the subsequent division after a correct radio option or text input has been submitted

I need to enhance the user experience on my form. When a user interacts with specific elements like hitting enter in a text input or clicking a radio button, I want to automatically focus on the next "formItem" division. My form structure is organized as ...

I'm curious about how I can apply @media queries given that certain phones possess higher resolution than computers

There seems to be a common recommendation to utilize @media queries for adjusting CSS based on whether the user is on mobile or not. However, I'm a bit confused because my phone has a width of 1440p while my computer has 1920p. Should I consider apply ...

Is it possible that images appear normal locally, but become stretched once deployed? (CSS/HTML only)

I designed a straightforward webpage using only HTML and CSS. Everything looks great when I view it on my local machine. However, once I deployed the site on Droppages, a static-page hosting service integrated with Dropbox, all my images became stretched ...

"Creating eye-catching popup images in just a few simple steps

<div className="Image popup"> <Modal isOpen={modalIsOpen} onRequestClose={closeModal} contentLabel="Image popup" > <img src="../img/navratri-1.png" alt="Image popup" /> <b ...

Vue.js component unable to validate HTML input patterns

Attempting to create HTML forms with the 'pattern' input attribute has been an interesting challenge for me. When implementing this through Vue.js components, I encountered some strange behavior. Check out this fiddle to see it in action. Vue.co ...

Select a specific date range from a form and export CSV file containing MySQL data within that range

I have successfully implemented a database CSV export functionality, but now I am looking to allow the user to specify a date range for the export process. Below is the HTML section that handles the date selection (date format: mm/dd/yyyy): <form act ...

What is the best way to expand the header, content, and footer colors to cover the entire width of a page while keeping everything centered within a fixed width

I managed to get the header and main content area to stretch across the full page, while keeping the content centered. But now I am facing an issue with the footer not stretching like the header. How can I make the footer stretch as well? HTML: <body ...

What is the best way to choose content that has been clicked on?

How do I display only clicked content? Currently, I have a system where clicking a button shows the data in the nav tag. The code is written in views.py. class TopView(TemplateView): model = Data template_name = 'index.html' def get ...

Guide to inserting an HTML file into an article's content

After downloading an extension from freefrontedit and uploading it to my server in the directory /accordeon, I successfully accessed it by pointing my browser to . However, I am now faced with the challenge of loading the index.html into my Joomla article ...

The footer is not displaying at the bottom of the page in Firefox

While the footer design is functioning well in both Chrome and IE, it seems to be encountering issues with Firefox. div.footer{ width: 100%; padding: 0px; margin-top: 200px; font-size: 0.6em; line-height: 1.2em; clear: both; po ...

Issue with fixed sidebar on brief content

It's interesting how the sticky widget performs differently on long articles compared to short ones on my website. Here is an example of a long article where the sticky widget works well without any lag: . Now, let's take a look at a shorter art ...

Looking to target an element using a cssSelector. What is the best way to achieve this?

Below are the CSS Selector codes I am using: driver.findElement(By.cssSelector("button[class='btn-link'][data-sugg-technik='append_numbers']")).click(); driver.findElement(By.cssSelector("button[class='btn-link'][data-sugg- ...

Display a specific section depending on the user's input by utilizing either ng-if or ng-show

I have a scenario where I need to display one of two sections based on user input. If the user selects 'Daily' in the first 'type' input, I want section 1 to appear (Enter start date and hour). For any other type selection, I want secti ...

Updating content with Ajax in Laravel

Hey, I'm currently facing an issue with my update functionality. Below is the blade code snippet: <div class="form-group floating-label" id="role_colaboration1"> <select name="{{$role}}[]" id="role" class="form-control"> ...

What is the difference between using min-height in vh versus % for the body tag

I'm trying to understand a code where the min-height of the body element is set to 100vh after previously defining the height of the html element as 100%. Why is there a need for this specific sequence? body { position: relative; overflow: hidd ...