Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

CSS worries

Featured Replies

I have two small CSS problems.

 

The first is that I'm trying to center an image within an <a> by giving it a width and doing the 0 auto thing:

 

a {
margin: 0 auto;
}

 

Do I have to make a div to do this? I'm trying to keep my code as clean as possible.

 

The second is positioning this little zoom image that is actually a CSS pseudo elelement (on hover). I'm trying to get it either to the lower right corner or perhaps the middle of the image but it doesn't like to go far and I'm not too sure where its static position is supposed to be.

 

Here is the link: My link

 

Thanks in advance.

If you are giving a link an image, you shouldn't be wrapping an anchor tag around the side.

Instead try this:

 

HTML

<a class="imageLink" href=""><span>image</span></a>

 

CSS

a span { display: none; }
a.imageLink {
display: block;
margin: 0 auto;
background: url("LINK");
width: 500px;
height: 500px;
}

 

You can't use margin: 0 auto; without setting a width to the element. And you shouldn't really do this with an anchor element anyway.

Edited by MatthehCarter

  • Author

I see what you mean but I can't really do that as the image isn't just background. They are the content of the site so semantically it wouldn't be a good idea. I did set a width for the anchor element and it won't work. Why isn't it a good idea to give a width to an anchor element?

Edited by mantis

I have two small CSS problems.

 

The first is that I'm trying to center an image within an <a> by giving it a width and doing the 0 auto thing:

 

a {
margin: 0 auto;
}

 

Do I have to make a div to do this? I'm trying to keep my code as clean as possible.

 

The second is positioning this little zoom image that is actually a CSS pseudo elelement (on hover). I'm trying to get it either to the lower right corner or perhaps the middle of the image but it doesn't like to go far and I'm not too sure where its static position is supposed to be.

 

Here is the link: My link

 

Thanks in advance.

 

 

What i'd try first is like you suggested and wrap it in a <div>.

 


<div class="imageLink">

<a href="image" url="target_blank" />

</div>

 

Bearing in mind you want as clean coding as possible, I'm still learning so I wouldn't be able to help you there.

 

As for the CSS I find sometimes this may work, though for reasons I don't know sometimes it doesn't.

 


.imageLink {

position: center;
width: 500px;
height: 500px;

}

 

Might be a load of crap, I don't know. Always worth a shot though.

 

 

 

Have you tried absolutely positioning it or positioning it using margins?

 

As been as it is the "content" of your site it wouldn't seem wrong to use DIVs like Alex suggested.

  • Author

Thanks for all the suggestions. Its not working properly with the div either which leads me to beleive that its a problem with the max-width and max-height properties. I think I'm just going to have to resize my images individulaly. (I was being lazy)

 

Maybe thats also why I can't position my pseudo element correctly?

 

Have you tried absolutely positioning it or positioning it using margins?

 

I wanted to to centre them all at the same time and since they are different sizes... Well, maybe absolute positioning would work? Is it taken from the centre of the image? I'll have to try it.

Edited by mantis

Don't use CSS to size and proportion your images. It's bad practise and, well, also there are the problems you're having.

 

Try timthumb instead, and have the images resized dynamically on the servers side. Then they will all be the exact same width and proportion.

If you are trying to center and image within an a tag, you need to set the a to

 

a img {
display: block;
margin: 0 auto;
}

 

Unless I am getting what you are trying to do wrong.

Don't use CSS to size and proportion your images. It's bad practise and, well, also there are the problems you're having.

 

Try timthumb instead, and have the images resized dynamically on the servers side. Then they will all be the exact same width and proportion.

 

What is timthumb exactly?

  • Author

@brightonmike, thanks for the tip. I wish I had known about that before I made thumbs for the entire site lol. I took out the max-size and just resized them manually and I'm still having the same problem centring the images.

 

@cibgraphics, Thats what I did. (and what I'm trying to do) but it doesn't seem to be working. Maybe there is some sort of specificity problem? I can't think why it won't work. I even made one of the anchor images into a div (second example). What am I missing here?

Hmm, maybe the problem lies in a parent style or another part of your content conflicting with it.

What specific image on your website are you attempting to center?

Could you please URL to the image and/or page that the image lies on?

I had more in mind making the anchor tag a block and setting the text align to center in it an displaying the image as an inline element. Seems more natural than making the img a block somehow. That kind of thing. Can't remember whether you'd have to put a margin:auto on the img just to make sure, hopefully you wouldn't have to as it's an inline element within a block that has text-align:center set.

You'd have to put a width on the anchor tag I would have thought as well.

 

Are we understanding what you're trying to do here?

Push comes to shove you could always bosh it together with ;

 

position: absolute;
left: 50%;
margin-left: -XXpx;

 

Where XX = half the width of the image in question. Horrible way of doing it but sometimes you just gotta get it looking right and sod the validation.

  • Author

Hmm, maybe the problem lies in a parent style or another part of your content conflicting with it.

What specific image on your website are you attempting to center?

Could you please URL to the image and/or page that the image lies on?

 

My link

 

I'm trying to center the images within the space to the left of the price information (.priceinfo). I put the second image with the green border in a div but I can't manage to centre that one either.

  • Author

I had more in mind making the anchor tag a block and setting the text align to center in it an displaying the image as an inline element. Seems more natural than making the img a block somehow. That kind of thing. Can't remember whether you'd have to put a margin:auto on the img just to make sure, hopefully you wouldn't have to as it's an inline element within a block that has text-align:center set.

You'd have to put a width on the anchor tag I would have thought as well.

 

Are we understanding what you're trying to do here?

 

I thought I did that but I checked my CSS and apparently not...its all a muddle now. Yes, I think you all understand very well. I'll have to try that when I get home from work.

 

@Glowbridge, good to know. I think I'll try for the validation now but its always good to have a few desperate measures up one's sleeve. Thanks.

Edited by mantis

My link

 

I'm trying to center the images within the space to the left of the price information (.priceinfo). I put the second image with the green border in a div but I can't manage to centre that one either.

 

Ok firstly, why does your "minirat" have a DIV class "imagebox" whilst your other do not?

Also like Glow said, try applying a margin-left: -xx to your img class or li class; not the best practice but it will get it looking good.

  • Author
Ok firstly, why does your "minirat" have a DIV class "imagebox" whilst your other do not?

 

I applied a div to one of them to see if the problem was in giving a width to the anchor element (it was not). I'll try the XX thing but I really would like it to validate and I don't understand why it isn't working as it always has in the past.

you might have already tried this but trying it out with firebug i just adjusted the css to:

 

.miniprintbox {    border: thin dashed #EE0000;    float: left;    height: 230px;    position: relative;    width: 380px;}
.miniprintbox a.single_image {    float: left;    height: 230px;    width: 200px;}
.miniprintbox a.single_image img {    display: block;    margin: 0 auto;}
a.single_image:hover:after {    content: url("../images/zoom.png");    height: 25px;    position: absolute;    width: 25px;    z-index: 999;    top: 25%;    left: 25%;}

 

Then just play around with the top and left on the hover effect to put it in the right place! The images were centered and the the hover image is now positioned to the parent li instead of further up the dom tree

 

I thought I did that but I checked my CSS and apparently not...its all a muddle now. Yes, I think you all understand very well. I'll have to try that when I get home from work.

 

@Glowbridge, good to know. I think I'll try for the validation now but its always good to have a few desperate measures up one's sleeve. Thanks.

 

I'd imagine the space to the left as another container (obviously) either use a div or a span and set it to center text align. Give it dimensions, which isn't a problem because they're all the same size. If it's a span which is inherently an inline element you'll have to set it to display:block; - no need to do this is if it's a div because div is inherently a block element.

Float it left. So kind of similar to what you've already done with the floated right <ul> which you have.

I think it may help if you define the width of the floated right <ul> which contains the details as well.

Also - I don't think there's a need to put the dimensions round the anchor tag as it will wrap itself round the image naturally.

 

<li class="miniprintbox">
<div class="whatever (floated left with dimensions - looks like about 205px on your site and text-align:center;)">
   <a class="single_image" href="images/miniprints/mini2007.jpg">
       <img alt="mini 2007" src="images/miniprints/mini2007thumb.jpg">
   </a>
</div>
<ul class="priceinfo" (add the width dimensions to this - looks like about 165px on your site)>
<li>
<li>Edition de 50, 2006</li>
<li>8,5 x 6cm sur papier de 14 x 10,5cm</li>
<li class="price">15,00€</li>
<li>button</li>
</ul>
</li>

Edited by Gibson

  • Author

@ysaran: Yes! It worked! I'd give you more than plus 1 if I could. I tried to fix it using firebug but got frustrated with it. All that from floating the wrong element? Or maybe it was something else. I thought I'd tried everything!

 

@Gibson, thanks for you help. I actually did put one of them in a div at one point. I'm not sure what I did wrong but I certainly took some work to fix. I wanted to use the anchor tag as a block because I like the idea of not having a lot of divs, just a personal preference I suppose but I forgot that the a tag is inherently inline.

 

Thanks again to all of you :hi:

Glad I could help

 

Yeah it was a case of floating the parent element and displaying the child element as a block element.

 

The problem with the pointer was you were absolute positioning it which will position itself in relation to the last positioned parent element in the dom tree. If it doesnt find one then its positioned in relation to the browser window I think. So setting the li with position:relative it relates to that element and will be bound within in it.

 

Hope that makes sense :D

  • Author

yeah, seems I had it backwards. I didn't play around as much with the pseudo element but that makes sense that it would follow the normal rules of positioning.

 

Thanks again B)

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.