What could be causing the container div to appear smaller than its contents?

I have a question about the code snippet below. The <div> element seems to be smaller than its content. Can anyone explain why this is happening and suggest a solution to fix it?

.switcher {
  border: 1px dashed green;
}

.switcher a {
  padding: 30px;
  background: orange;
}
<div class=switcher>
  <a href=#>first</a>
  <a href=#>second</a>
</div>

Answer â„–1

According to the specifications:

Section 7.8 Inline layout - Within an inline layout, elements are arranged horizontally in a sequence from the top of their containing block. The horizontal margins, borders, and padding between these elements are respected. Various alignment options exist for vertical alignment such as aligning bottoms or tops, or aligning text baselines.

While padding can be added to all sides of an inline element, only left and right padding will impact the adjacent content around it.

The default behavior of the <a> tag is inline. By setting it to inline-block, both horizontal and vertical padding will be taken into account. An inline-block element is positioned as an inline element but behaves like a block element.

.container {
  border: 1px solid blue;
}

.container a {
  padding: 20px;
  background-color: yellow;
  display: inline-block; /*new*/
}
<div class=container>
  <a href=#>item one</a>
  <a href=#>item two</a>
</div>

Answer â„–2

To confine the a elements, you can add either overflow: hidden or overflow: auto to the switcher class.

Answer â„–3

To change <a> tags to display block level, you can use CSS properties like

<div>, <p>, <h1>, <h2>
. By default, <a> tags and span tags are inline elements. They stay inline until you add a property to make them display as block level. You can achieve this by using either display: inline-block or display: block.

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

Split-button dropdowns within tabs implemented with Bootstrap

I'm looking to create a navigation menu that combines both split buttons and dropdown tabs. I've found examples of each individually, but can't figure out how to merge the two: "Tabs with dropdowns" Is it possible to have a tab with a drop ...

conceal the offspring from the guardians, not the offspring

Could you please assist me with this situation... <li id="4331">Region <ul> <li id="4332">Asia</li> <li id="6621">Europe</li> </ul> </li> I have a query when I click on the Reg ...

positioned division towards a subset

Is there a way to align a div with a span? For instance: <div style="some magic">foo bar<div> <p>this is a random text with <span class="stressed">stressed phrase</span> in it</p> The desired output would look like th ...

What is the best way to create shaded areas upon clicking them?

How can I implement shading on areas when they are clicked? Contractor Form (Package One) <area id="39" name ="39" alt="" title="39" href="#" shape="poly" coords="12,204,12,120,138,117,144,72,248,72,252,124,526,125,632,81,668,157,698,149,722,2 ...

Angular.js animation for element movement

I'm currently struggling to create a simple move animation. I am aiming for a similar effect to the one shown in this example (taken from another SO question). So far, I have managed to achieve this using the .animation() method. This is essentiall ...

Dividing the body of md-tabs in Angular material

My current project involves using angular material (https://material.angularjs.org/) I'm curious if there is a method to separate a tabs body from the tab list in angular material. Unfortunately, I couldn't find any information regarding this in ...

Aligning a multi-line list element vertically to the bullet point

When my list breaks into multiple lines on certain screen sizes, I'm facing an issue where the list bullet doesn't align vertically in the middle of the text. I want the bullet to show up next to the second line if there are 3 lines inside the s ...

Unusual encoding in the ajax response

I am trying to use the Ajax function from Jquery to send a string to my JavaScript file, but I am encountering an issue where weird question marks or diamond blocks are appearing before the string that I expect to receive. (function( $ ) { $.f ...

jQuery trigger event when a DropDownList/SELECT item is selected

My current jQuery code filters a list of links on the page by firing when a link with a specified href is clicked. The id of the link determines which content remains visible. Here's an example: <script type="text/javascript"> $(docume ...

Tab Focus discrepancy observed in Mozilla browser

Tab Focus issue with elements on Mozilla Firefox: <div class="editor-field"> <div> <%: Html.TextBox(model => model.AddressLine1, new { maxLength = 30, style = "width:300px", tabinde ...

Validating Password Consistency using Javascript

I'm having trouble with validating the password fields and ensuring they match, even when both input boxes contain the same password. JavaScript var validator = $("#signupform").validate({ rules: { password: { required: true, minle ...

Unable to enable auto-scroll feature in DIV element

My div has an auto-scroll feature that works fine, but I've encountered an issue when using multiple smileys in a single chat post. The auto scroll seems to stop working correctly and only scrolls 2 lines from the bottom. I attempted to fix this with ...

Unusual mobile chrome behavior and erratic modal functionality

It has come to my attention that mobile users on Chrome/Android are experiencing some unusual behavior with modals. The autofocus feature is not working as expected, and the view keeps scrolling to the bottom. This issue specifically occurs with modals th ...

Leveraging the CSS-Element-Queries Library for emulating the functionality of CSS media queries

Recently, I used a nifty CSS-Element-Queries tool to perform basic element manipulations that trigger whenever the window is resized. In simple terms, my goal was to dynamically adjust an element's attribute based on the current width of the window â ...

Issue with Safari causing footer to sporadically appear over overlay

I am encountering a problem with my webpage that has a sticky footer, scrolling content, and an overlay that appears after a few seconds. The issue is specific to Safari on certain devices like iPhone 12-15, where the footer and part of the scrolling conte ...

Learn the technique of coding HTML within inline JavaScript, along with implementing CSS inline styling

I'm looking for a way to incorporate HTML within inline JavaScript, along with CSS inline styles. Can someone provide guidance on how to achieve this? For example, something like the following code snippet: <p><span style="color: #00ff00;"&g ...

In ReactJS, ensure only a single div is active at any given moment

I'm working on a layout with three divs in each row, and there are multiple rows. Only one div can be selected at a time within a row, and selecting a new div will automatically unselect the previously selected one. Here is a simplified version of my ...

What is the best way to resize images to fit in a 200 pixel square box using CSS?

I have a collection of images that all fit within a 400px × 400px box, with one dimension being 400px and the other smaller. I want to resize these images using CSS or jQuery/JavaScript if necessary, to fit into a 200px by 200px box. The goal is to have t ...

The title on my iOS app is off-center towards the left side

My title in the application is not centered on iPhone, but it appears fine when accessed through a browser. How can I fix this issue? Here is my HTML code: <div data-view="Moobile.ScrollView" class="component-testpage-view"> <div data-role="n ...

Eliminating the unwanted line breaks that are automatically inserted when integrating JavaScript with HTML code

Hey everyone, I'm new to web development and could really use some assistance. I am currently working on a website that shows the weather forecast for the next four days, using SimpleWeather.js. In my JavaScript, I want to display the High & Low temp ...