Whenever I float an image to the side of text and try to make any part of it a link it pushes the image downward, it happens when the whole block (text and image) is a link and even when only the text just above is also, can anyone please help me resolve this?
Its hard to see from that snippet what you are attempting but if I can explain how floats work it might help:)
When you float an element then any content in the html that follows after the float in the html will wrap around the float (assuming there is room of course and that the element or the float are not 100% wide).
Content before a float in the html is unaffected by the float. The float will not impact html that is higher in the html source. If it did do this then it would eventually float through the top of the monitor.
Therefore it is only content that follows a float will wrap around the float assuming as I said there is room to do so. It makes no difference if the element is text or a link or another image they will basically behave the same.
In your snippet above the only link is in the h2 which is above the float so will not be affected by the float.
Remember floats are used to allow text to wrap around them but if you are trying to make a columnar layout then you should be using flex or grid instead.
I’d need a more fleshed out version of your code to see what you were doing as the snippet above doesn’t really tell me much other than your are floating stuff left and right.
It’s only being used for a preview section of an article, not layout. I’m using css grid for that.
Here is the html for the full page, the reason navigation isn’t in the header area is because when I tried that it wouldn’t show its grid area in my browser. I realise it’s not related, only mentioning it cos it likely looks unusual.
Ok I’ve put that code into a codepen (and removed some of the typos) so can you explain which elements you are having a problem with? You say its related to the float but I can’t see anything unexpected but I may be looking at the wrong thing.
PS: Always validate your css regularly as you have some errors creeping in already.
Okay, I was trying to replace my original text for something generic in the html, if the text is only a few words it’s not an issue, as it isn’t with your Codepen. When I have it at about a sentence or 2 is when the issue happens.
I’ve changed the text to something longer to show what I mean, should have done that to begin with, sorry. I don’t have a Codepen account, so this is the changed html:
<section class="layout">
<a href="#main" class="skip">
go to main content</a>
<header class="header intro">
<p class="sitename">site name </p>
</header>
<nav class="navigation">
<ul id="menu">
<li><a href="filea.html">text</a></li>
<li><a href="fileb.html">text</a></li>
</ul>
</nav>
<main class="main content" id="about">
<h1 tabindex="-1" id="main">Title</h1>
<article>
<h2><a href="file.html">text in h2</a></h2>
<section>
<p class="articlespace">Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p>
<img src="https://picsum.photos/id/237/200/300" class="preview" id="preview" alt="" />
</section>
</article>
</main>
<footer class="footer websiteinfo">
<p>text in footer</p>
</footer>
</section>
Thank you for reminding me about my css, I’ve changed it so it’s valid. Going to look into replacing clip because it’s depreciated.
Then you have floated the image to the right here.
#preview {
float: right;
}
Floats are shrink to fit elements and when no width is provided they will expand to take up all the available horizontal space. That means when you add more text to .articlespace then it will stretch to the full width of the page. The right floated image must move down out of the way because it follows after the articlespace float and there is no room left on that line for it.
What you should have done is float the image to the right but have the image above the .articlespace text in the html and then there’s no need to float the articlespace element at all.
Codepen is free for basic use and a great way to set up demos especially for debugging. You only need a paid account for uploading assets and other extras.