Simple problem with CSS styling

I'm having some trouble with my new website design. I am working on adding a section to the sidebar, but can't seem to get the text aligned correctly next to the image. I've attempted to float the text left, but it's not displaying as desired. Currently, the only CSS I have in place is that the sidebar is 300px wide.

<div id="sidebarabout">
    <h3>About Elliott Davidson</h3>
    <img src="http://placehold.it/100x100"><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam rhoncus luctus odio, sed sagittis dolor volutpat ut. Pellentesque efficitur orci at nunc fermentum, nec feugiat erat gravida. <a href="about.html">Continue reading</a></p>
</div>

Answer №2

Check out this example

<p><img src="http://placeholder.com/200x200"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi ornare vehicula sem, id egestas felis mollis vel. Duis ac ex eget ligula rhoncus convallis et id risus. <a href="details.html">Read more</a></p>

Answer №3

img{float:right;margin:0 15px 0 0}

If you want to give your image some space, it's better to use margin instead of padding. Padding can make your image appear squeezed.

Answer №4

To make the img and p elements display inline or inline-block, adjust their CSS styles accordingly.

Additionally, consider setting a width depending on how you want your text to "float".

https://jsfiddle.net/xoL510og/ << Example

<div id="sidebarabout">
    <h3>About Elliott Davidson</h3>
    <img style='display:inline-block;' src="http://placehold.it/100x100"><p style='display:inline-block; width:400px;'>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam rhoncus luctus odio, sed sagittis dolor volutpat ut. Pellentesque efficitur orci at nunc fermentum, nec feugiat erat gravida. <a href="about.html">Continue reading</a></p>
</div>

Answer №5

To achieve the desired layout, you can apply the CSS property display: table;

*{
    padding: 0;
    margin: 0;
    box-sizing: border-box;
}



.item-inner{
    display: table;
    width: 100%;    
    padding: 20px;
}
.item{
    display: table-cell;
    vertical-align: top;
}
.item-text{
    padding-left: 15px;
}
   
    <div class="item-inner">
        <div class="item">
            <img src="http://placehold.it/100x100" />
        </div>
        <div class="item item-text">
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam rhoncus luctus odio, sed sagittis dolor volutpat ut. Pellentesque efficitur orci at nunc fermentum, nec feugiat erat gravida. <a href="about.html">Continue reading</a></p>
        </div>        
       

Answer №6

To ensure that the img markup only applies to the sidebarabout div, it is important to define the div before adding the img styling. For example:

#sidebarabout img{
  float: left;
  padding: 0 15px 0 0;
}

If you have set a width of 200px for your paragraph element, make sure to adjust it to 185px to accommodate the 15px padding. Failing to do so may result in a webpage layout issue as the div width will exceed 315px.

While some suggest using margin, this could shift the image outside the div by 15px to the left. This should be avoided.

Furthermore, don't forget to include attributes such as width, height, and alt when defining your img element correctly to ensure it passes the W3C validator. Use the following format:

<img src="http://placehold.it/100x100" width="100" height="100" alt="Description">

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

Creating a persistent sticky element that remains at the forefront in Electron or JavaScript development

Currently, I have a remote desktop control application built on Electron that functions as a desktop app on Mac and Windows, and as a web app on Chrome and Firefox. My next goal is to create a sticky component that remains on top at all times, regardless ...

Adjust the Subnav <li> element to take up 100% width and reposition the dropdown menu

My current challenge involves customizing a navigation bar using CSS. I am struggling with making the sub-menus expand to 100% width as they currently appear fixed in size. Additionally, I am trying to find a solution to display the submenu options on eith ...

How to write a more advanced Path for Selenium Chrome WebDriver using C#

I am struggling to find the correct path to click on a specific button on my webpage. To help illustrate, I have provided a sample page for testing purposes. All you need to do is download the HTML file and open it in your browser.Code of HTML page What ar ...

Denied running the inline script in Redocly

Experimenting with redoc-cli (https://github.com/Redocly/redoc/) in order to generate static documentation. Unfortunately, encountered some stumbling blocks due to errors related to the Content-Security-Policy, which seems to be blocking JavaScript code ex ...

Adjust the parent element to match the width of the child by utilizing the "display: grid" property

edit: I'm uncertain if this is a duplicate issue. My concern is not about the background color extending beyond the container width, but rather about expanding the container to match the size of its child with display: grid. I have updated the example ...

Creating collapsible column functionality for the <td> element within a <tr> in mobile view using React

Ensuring my websites have a consistent look across various devices is a top priority for me. I currently have a table that is displayed like this: https://i.sstatic.net/a91LL.png Is there a way to modify this table so that when viewed on mobile, the < ...

Is there a way to modify the commandlink image when the mouse hovers over it in PrimeFaces?

After implementing this code snippet, my commandlink seemed to vanish into thin air. Upon inspecting it with firebug, I discovered that it was present but had a size of 0 x 0 px. .myNewButton { width: 50px !important; height: 50px !important; background ...

Unraveling HTML in TypeScript with Angular 2

After receiving encoded HTML from the back-end, I am struggling to decode it. I have attempted to use decodeURIComponent and decodeURI. The server sent me: "<table style="background-color: white;"> <tbody> <tr> ...

Unable to submit form when using image type buttons

Here is a basic example to demonstrate my code : if(isset($_POST['login'])) { do this } <form method='POST' action="index.php"> Username : <input type='text' name='username'/> Password : <input ty ...

Executing a PHP script to initiate a ping transmission

I have a project to complete for my university involving the development of a simple application. However, I lack experience in this area and am unsure how to proceed. The objective is straightforward: I want to send ping requests to 50 IP addresses at t ...

Is there a common issue in web browsers? Neglecting the position relative attribute on 'tbody', 'tr', and 'td' elements?

Trying to absolutely position elements within tbody, tr, and td may not work in certain browsers. While it behaves as expected in Firefox, it does not work properly in IE, Edge, and Chrome. If you apply position: relative to tbody, tr, or td, it will be i ...

Mastering CSS States: Hover and Active

Imagine we have a link with the class "myClass1". Using JavaScript, we are able to manipulate the classes for this link - for instance, adding "myClass2" to it. My query is whether we can also manage states through JavaScript, such as removing the hover s ...

Tips for preventing route changes when clicking on a hash tag in AngularJS

I have a link in a small carousel on the page, and I am utilizing AngularJS routing to create a Single Page App. The tiny carousel uses anchor tags as buttons to navigate between images. <div class="carousel-controls-mini"> <a href=" ...

Tips for preserving a string in angularJS or Java (and implementing it in an iframe)

My plan involves utilizing a Java web service to fetch the HTML content from a specific URL using Jsoup, and then returning it as a string to the requesting party, which could be an Angular or JS script. I am keen on preserving this content somewhere and l ...

Inconsistent Responsiveness of CSS Search Bar

I'm currently in the process of developing a mobile-friendly GitHub Homepage Clone using HTML and CSS, with plans to integrate JS at a later stage. My main focus right now is on refining the search bar functionality. However, I've encountered an ...

Steps for triggering a click event on a div with a button role within a class containing multiple elements

Can anyone help me figure out how to auto-click every button in Instagram's "hide story from" settings using console? I tried the following code: for (let i = 0; i < 300; i++) { document.getElementsByClassName('wbloks_1')[i] ...

Disabling the onClick event on HTML tags with personalized WooCommerce messages

I have customized the notices/success.php template file by modifying the content as shown below: <?php foreach ( $messages as $message ) : ?> <div class="woocommerce-message" role="alert"> <span> <?php ...

Utilizing the Material Design card div element, however the elements inside the card are not properly aligned in the center

I'm attempting to include the align="center" attribute to this Material Design Lite card: <main class="mdl-layout__content" align="center"> <div class="mdl-cell mdl-card mdl-shadow--4dp portfolio-card"> <div class="mdl-card__title"&g ...

The presentation changes when styling is added through the <link> element

I've encountered a strange issue. Here's the HTML I'm currently using: <!DOCTYPE HTML> <html> <head> <style type='text/css'> <title>Site name</title> *{ ...

CSS: inner division layering on top of each other

Looking to create a unique "label" within a container div? The label should be positioned in the top left corner and resemble this example: here. One common approach is to slightly shift the inner div so that it overlaps with the container. However, this m ...