Elements are failing to respond to the padding and margin adjustments in my CSS styling

I've been experimenting with creating a visually appealing div that serves as the centerpiece of a website.

Imagine it resembling a standard "news" link. The width set at 80%, an image aligned to the left with a border, a headline detailing the latest news, a paragraph ending with a link for more information, and finishing off with an hr tag before moving on to the next piece of news.

The initial issue arises when attempting to add margin or padding to create space between the text and the image border. No matter what I try, the header and paragraph stubbornly start right where the image border ends. Furthermore, utilizing the hr tag causes it to begin immediately after the paragraph content.

A third obstacle emerges when implementing the display inline-block property; the margin auto functionality ceases to work, resulting in everything being floated to the left.

Below is my code snippet:

div.content1 {
  width: 80%;
  color: white;
  background: #2B2B2B;
  margin-top: 25px;
  box-shadow: 0px 0px 10px 5px #3A3A3A;
  height: auto;
  display: inline-block;
  padding: 15px;
  margin-bottom: 25px;
}

div.content1>img {
  float: left;
  max-height: 100%;
  border: 1px solid black;
  width: 400px;
  height: 200px;
}

.content1>p {
  padding: 15px;
}
<div class="content1">
  <img src="Twitter.png">
  <h1> Welcome! </h1>
  <p>Long followed text... Long followed text... Long followed text... Long followed text... Long followed text... Long followed text... Long followed text... Long followed text... Long followed text... Long followed text... Long followed text... </p>
  <hr>
  <h1> Welcome! </h1>
  <p>Long followed text... Long followed text... Long followed text... Long followed text... Long followed text... Long followed text... Long followed text... Long followed text... Long followed text... Long followed text... Long followed text... </p>
</div>

Answer №1

To address the initial problem, incorporating margin-right should provide a solution. As for the second issue, it is necessary to clear the float assigned to the img tag. Additionally, removing the display:inline-block property is essential for enabling margin:auto functionality. Feel free to test out this revised code.

<div class="content1">
  <img src="Twitter.png">
  <h1> Welcome! </h1>
  <p>Lengthy content follows... Lengthy content follows... Lengthy content follows... Lengthy content follows... Lengthy content follows... Lengthy content follows... Lengthy content follows... Lengthy content follows... Lengthy content follows... Lengthy content follows... Lengthy content follows... </p>
  <div class="clear"></div>
  <hr>
  <h1> Welcome! </h1>
  <p>Lengthy content follows... Lengthy content follows... Lengthy content follows... Lengthy content follows... Lengthy content follows... Lengthy content follows... Lengthy content follows... Lengthy content follows... Lengthy content follows... Lengthy content follows... Lengthy content follows... </p>
</div>

div.content1 {
  width: 80%;
  color: white;
  background: #2B2B2B;
  margin-top: 25px;
  box-shadow: 0px 0px 10px 5px #3A3A3A;
  height: auto;
  margin-left: auto;
  margin-right: auto;
  padding: 15px;
  margin-bottom: 25px;
}

div.content1>img {
  float: left;
  max-height: 100%;
  border: 1px solid black;
  width: 400px;
  height: 200px;
  margin-right: 20px;
  margin-bottom: 20px;
}

.content1>p {}

.clear {
  clear: both;
}

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

Difficulty encountered while setting up jQuery slider

I am struggling to set up a jquery slider (flexslider) It seems like I am overlooking something very fundamental, but for some reason I just can't figure it out. Any assistance would be greatly appreciated. Please check out the link to the test site ...

Achieve uniform display across browsers: Implement Chrome's overflow ellipsis feature on Firefox

I am seeking a CSS solution to achieve the same "overflow: hidden" and "text-overflow: ellipsis" behavior in Firefox as I get in Chrome. In Chrome, when the width of the tag is reduced, the text gets hidden and replaced with an ellipsis. However, in Firefo ...

To close the menu, simply tap on anywhere on the screen

Is there a way to modify a script so that it closes the menu when clicking on any part of the screen, not just on an 'li' element? $(function() { $('.drop-down-input').click(function() { $('.drop-down-input.selected').rem ...

JavaScript code for iframe auto resizing is not functioning properly on Firefox browser

I have implemented a script to automatically resize the height and width of an iframe based on its content. <script language="JavaScript"> function autoResize(id){ var newheight; var newwidth; if(document.getElementById){ newh ...

The addition of one-page navigation in Angular 7 seems to be experiencing some glitches

I recently added a new menu option to my Angular 7 project, but it's not functioning correctly. I used Bootstrap 4 and followed this example from here. Can anyone help me figure out the correct way to implement this? Check out the sample on StackBlit ...

Rows in the table are distinguished by varying colors

Is it achievable to replicate this design solely using table CSS style? I am able to assign different colors to even and odd rows, but my goal is to achieve something like this (just the colors): ...

Tips on displaying hyperlinks within a text area in an Angular application

In my Angular application, I am facing an issue with displaying hyperlinks within a text area box for dynamic content. The hyperlinks are not recognized and therefore cannot be clicked. Can someone please advise on how to properly display hyperlinks with ...

What is the best method for resizing an SVG according to the size of my website's window?

I am attempting to implement a scalable settings icon SVG file that adjusts based on the window width. My approach involved creating a JavaScript function that alters the element's dimensions according to the window size, but unfortunately, this metho ...

What are the steps for populating a table cell with data using AJAX, MySQL, and PHP in the given situation?

The challenge is to create an HTML table with two columns where the first column gets its data from PHP and MySQL. The goal is to turn the values in the first column into clickable links that, when clicked, trigger an AJAX call to fetch information from th ...

An alternative to Firebug for Internet Explorer

Can anyone suggest a suitable replacement for Firebug that is compatible with IE 7 and 8? I need a tool that allows me to edit CSS/HTML in real-time, debug JavaScript code, and visualize the positions of elements on web pages. Appreciate any recommendati ...

Adding data from PHP into a designated div element

update: After some trial and error, I managed to find a solution using jQuery and append() methods. From my PHP code, I created an array containing all the necessary information that I wanted to insert into the tab divs. I then encoded this array into JSON ...

``A problem with the background image of the left panel in Framework 7

After setting a background image for the side panel and blurring it using CSS, I encountered an issue where the text and icons within the side panel also became blurred. Despite attempting to isolate the background image in a separate class, the problem ...

What causes Bootstrap to cut off text in buttons?

My button text is getting truncated for some unknown reason. Here is the HTML code: <input class="btn btn-default" value="Lorem ipsum dolor sit amet, consectetur adipiscing elit."/> And this is how it appears: https://i.sstatic.net/Sr6Z0.png I hav ...

Modify the placeholder color for a disabled Input element to match the overall Mui theme design

I am struggling to change the color of the placeholder text in a disabled input field within my app's theme. Despite attempting to target the MuiFormControl, MuiInputBase, and MuiInput components in my theme.tsx file on CodeSandbox, I have not been su ...

Exploring the latest MUI Styles in Scoping Version 5

We are currently in the process of transitioning our legacy application to React and MUI. To ensure that there is no styling conflict between the old and new parts of the application, we have implemented scoped styles by combining an id selector with a des ...

My code seems to be malfunctioning

I'm attempting to conceal the chatname div. Once hidden, I want to position the chatid at the bottom, which is why the value 64px is utilized. However, I encountered an error stating The function toggle3(); has not been defined. <div id="chat ...

Issue with displaying hamburger menu items when using data-toggle in Angular

I've been diving into learning Angular and Bootstrap lately. I wanted to merge some existing HTML content into an Angular component, but ran into issues with its functionality. When I attempt to toggle the hamburger menu icon, nothing happens, and the ...

Find the elements of <a> tags specifically without the attribute href

Currently, I am extracting the class of all <a> elements in an HTML document of a webpage using VB.net from a WinForm: Dim htmlLinks As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("a") For Each link As HtmlElement In htmlLi ...

I'm puzzled as to why a scroll bar materializes

I recently encountered an interesting issue while working on my responsive web design project. Upon resizing the browser to a width of 615px or less, a horizontal scroll bar unexpectedly appeared. It's puzzling as to which element is causing this prob ...

Troubleshooting Problems with display:table and display:table-cell Alignment in Internet Explorer Versions 7 and 9

I have been struggling with the layout of a website that contains multiple columns. After extensive work, I believed I had everything properly aligned. Adobe BrowserLab seemed to confirm this, aside from various issues in IE6 and IE7 which I decided to ove ...