anchor won't center vertically inside list item - css

I am trying to center an anchor horizontally AND vertically inside a list.
#menu li a {
display: inline-block;
width: 100%;
height: 40px;
padding: 12px 0 12px 0;
text-decoration: none;
color: #AAAAAA;
font-size: 40px;
text-shadow:
-2px -2px 0 #000,
2px -2px 0 #000,
-2px 2px 0 #000,
2px 2px 0 #000;
}
http://jsfiddle.net/9FEDC/2/
All I want is an anchor that:
is horizontally and vertically centered
stretches accross the entire parent
EDIT: text-align is working fine but not even vertical-align: middle is working.
EDIT 2: As suggested, I'm now using a line-height equal to the anchor's height. This does indeed get the anchor closer to its vertical center, but for some reason it is still a few pixels off its true vertical center. Here's an updated fiddle...
http://jsfiddle.net/zp5GM/1/
EDIT 3: just out of curiosity I defined a 1px solid border for the anchor element and saw that the text INSIDE the anchor itself is not centered vertically. So things like vertical-align or even position changes to the anchor do not affect anything in this regard.
Can somebody please explain this weird behavior?

As has been mentioned, your markup is invalid. Once that is fixed, you can center the anchor tag by doing something similar to this:
Set the width and text alignment to the anchor's parent instead of the anchor itself.
li {
text-align: center;
width: 100%;
}
To vertically align and element, you can take different approaches. The easiest is probably to give the element the same height and line-height. That way it will vertically center the text.
<div class="parent-element">
<div class="child-element">
<p>This is some text.</p>
</div>
</div>
.parent-element {
width: 100%;
}
.child-element {
height: 100px;
line-height: 100px;
}
You can also position it 50% from the top and then subtract half the height of the child element.
<div class="parent-element">
<div class="child-element">
<p>This is some text.</p>
</div>
</div>
.parent-element { position: relative; }
.child-element {
height: 100px;
margin-top: -50px;
position: absolute;
top: 50%;
}

text-align:center;
That should do it.

Related

responsive text wrap alignment for transparent background and solid text on centred div

Aim: to have a centred div with fluid width that has a semi translucent background with solid text and remains centred on smaller screens after text wrapping.
Issue: When the screen becomes small enough for the text to wrap the solid text shifts right instead of remaining centred.
Info: I have managed to combine two answers to try and get the desired outcome (took half a day to find two compatible answers and merge them, the woes of being a beginner):
opacity of background only not text
responsive div centred with fluid width
<div id="divboxtable">
<div id="divboxcell">
<div id="divbox">
<div class="divtxtbck">
<h1>WHAT AM I DOING WITH MY LIFE!</h1>
</div>
<div class="divtxtfor">
<h1>WHAT AM I DOING WITH MY LIFE!</h1>
</div>
</div>
</div>
</div>
#divboxtable {
display: table;
text-align: center;
width: 100%;
height: 100%;
}
#divboxcell {
display: table-cell;
}
#divbox {
display: inline-block;
zoom: 1;
position: relative;
-moz-border-radius: 6px 6px 6px 6px;
border-radius: 6px 6px 6px 6px;
}
.divtxtbck {
background-color: #fffffa;
padding: 0 10px 0 10px;
-moz-border-radius: 6px 6px 6px 6px;
border-radius: 6px 6px 6px 6px;
filter: alpha(opacity=50);
filter: progid: DXImageTransform.Microsoft.Alpha(opacity=50);
-moz-opacity: 0.50;
opacity: 0.5;
}
.divtxtbck h1 {
// visibility: hidden;
color: red;
}
.divtxtfor {
position: absolute;
top: 0;
left: 10px;
}
I have commented out the hidden nature of the background text so you can see how the alignment differs. The issue seems to be because of the absolute positioning - but how do you overcome this left align issue when the text wraps?
Fiddle You MUST make the output window narrow until the text wraps - then you will see the solid text is not centred the same as the transparent text.
try to use media queries for responsive behaviour expected, and Also take a look on that position:absolute; Also display:block; for The id= divboxtable. Also why are you id instead classes?. Hope that helps you.

Moving Text Freely Inside Its parent

I'm New to CSS.
Is there a way to move text within the following span like a non-repeated background-image?
for example: 50px from left, 40 px from left? without causing extra height and width!
 
 
div{
width: 300px;
height: 300px;
border: 1px solid black;
}
<body>
<div>
<span>Hello World</span>
</div>
</body>
Can I ask more question? how many space does a 16px character occupies? I mean what does 16px mean? 16pixel wide? 16 pixel height? when we select a character with mouse, there is a blue box around the selected character, which is bigger than that character. is this relevant to this question?
Firstly I think font-size always relates to the height of the letters and the reason the "blue box" is slightly larger is because it is highlighting the line specified by line-height.
To position the text inside your box you have a couple of options:
1) You can absolutely position the span inside the div like so:
div {
width: 300px;
height: 300px;
border: 1px solid black;
position: relative;
}
span {
position: absolute;
top: 50px;
left: 50px;
}
http://jsfiddle.net/5xukp/
2) You can set the span's display to block or inline-block and then apply margin or padding to position the span like so:
span {
display: inline-block;
margin-top: 50px;
margin-left: 50px;
}
http://jsfiddle.net/m79as/
EDIT - In response to your comment, there is no property like background-position where you can set it to be center center or center left however you can use vertical-align and text-align to position the text. In order to vertically align the span correctly you will need to set the display to table-cell
div {
width: 300px;
height: 300px;
border: 1px solid black;
display: table-cell;
text-align: center; /* left|right|center|justify */
vertical-align: middle; /* top|text-top|middle|bottom|text-bottom */
}
http://jsfiddle.net/6ExB2/

padding an <a> element inside a <div> to achieve a clickable box

On this website: http://www.bestcastleintown.co.uk/pg/
I am trying to get the coloured area of each <div class="awards_square_home"> to be a click-able link by nesting an <a> tag inside the <div> and adding padding. However I am already using padding on the <div> to position the <a>. With the padding in this rule:
.awards_square_home {
border: 10px solid rgb(195, 195, 195);
height: 90px;
margin-bottom: 20px;
padding: 90px 20px 20px;
}
Now when I add padding to the element it is not spreading the padding in the way that I desire, which is to to make the entire coloured area of the div click-able.
.awards_square_home a {
padding: 120px 0 20px;
}
I am trying to make the from the <a> fill this area of the <div>:
If I did not misunderstand you, you want your div all to be clickable rather than the text only.
<a href="#" class="text220 centertext capitalise_text home_feature_turqoise NG">
<div class="awards_square_home turqoise podium">
<span>Best</span>
<span>University</span>
<span>Campaign</span>
</div>
</a>
Fiddle here
I believe I have fixed the issue by amending the the CSS as follows:
.awards_square_home {
border: 10px solid #c3c3c3;
height: 200px;
margin-bottom: 20px;
}
.awards_square_home a {
display:block;
padding: 90px 0px 20px 0px;
}
Instead of setting background / padding / height on the parent <div>, you can just set the <a> element to behave like a div and set them there instead:
a {
display: block;
background-image: url("../images/icons/globe_v3.gif");
background-repeat: no-repeat;
background-position: 50% 15%;
padding: 95px 20px 20px 20px;
height: 90px;
}
Don't forget to remove these properties from where they currently exist.
and why you can't add <div> into <a> tag?
a {
display: block;
}
and add your content into <a> tag.
Or remove padding from div and set padding only for <a> tag.
if you want the a to fill the div,why do you use padding on the div,and margin on tha a, which the sole purpose is to prevent the a from filling the div?
delete the padding on the a,it should work

Can't put margin on image

I am doing a website in wordpress. And under the navigation I have container(id="cover_photo") for image(id="cover_photo_image).I center it with margin but I want to move it down, and center it in the container, but the container follow if I put margin on it.
HTML
<div id="cover_photo">
<p id="cover_photo_image">
</p>
</div>
CSS
#cover_photo {
width: 100%;
height: 278px;
background-color: #6b0c0b;
box-shadow: inset 0px 0px 3px 0px #888, 0px 0px -3px 0px #888;
}
p#cover_photo_image {
width: 821px;
height: 172px;
display: block;
margin: 0 auto;
margin-top: 6px;
background-image: url(images/cover_photo.png);
}
Plopped your code into a fiddle and saw what you mean. The problem is that you haven't set positioning for #cover_photo or #cover_photo_image. The outer element needs to be relative, the inner needs to be an absolute.
#cover_photo {
position: relative;
}
p#cover_photo_image {
position: absolute;
}​
I changed the sizes so it would fit into the preview, and if you try adjusting the margin values it should move around and not move the #cover_photo container.
http://jsfiddle.net/ESCNm/
Just in case you're are looking to automatically vertically and horizontally align an element within an element, there are a LOT more methods that manual positioning. Manual positioning is such a hassle, and I try to avoid absolutes whenever possible.
Here an article about it: http://www.vanseodesign.com/css/vertical-centering/
About vertical and horizontal centering.
You can use Flex.
Please add the following code
#cover_photo {
display: flex;
justify-content: center;
align-items:center;
}
p#cover_photo_image {
border: 1px solid black;
background-repeat: no-repeat;
background-image: url(https://cdn.sstatic.net/Img/teams/teams-illo-free-
sidebar-promo.svg?v=47faa659a05e);
background-size: contain;
background-position: center;
}
This should be the effect you want.
enter image description here

Keeping/scaling DIV Ratio with percentages

At the moment I have a layout that pulls a number of thumbnails into a grid - each is defined by a style that keeps them a fixed ratio, (roughly 16:9) which is defined by pixel dimensions (389px x 230px) but they are looking a bit small on high-res screens.
The images are actually pulled into the DIV as a background that covers 100% width and height of the DIV and then the DIV's obviously control the aspect and size.
What I am looking to do is have these DIV's dynamically resize based on the page size of the device but to keep the ratio of the DIV's.
Is this possible?
My thoughts would be to set the width based on the percentage of the page but then I'm not sure how I would set the height and keep the correct aspect ratio (due to different resolutions etc.)
What would be the best way to do this?
EDIT - Thanks for all your ideas so far, thought maybe I should show you how I'm pulling in the data at the moment.
In my HTML I've got the following code which generated the grid
<a class="griditem" href="../video.php?video=13" style="background-image:url(../video/Relentless/Relentless.jpg); background-size:100% 100%;">
<div class="titles">
<h5>Relentless Short Stories</h5>
<h6>Frank Turner: The Road</h6>
</div>
This is styled with the following CSS
.griditem {
position: relative;
float: left;
margin-right: 17px;
margin-bottom: 17px;
background-color: #777;
-webkit-box-shadow: 0 1px 5px rgba(0, 0, 0, 0.5);
-moz-box-shadow: 0 1px 5px rgba(0, 0, 0, 0.5);
box-shadow: 0 1px 5px rgba(0, 0, 0, 0.5);
-webkit-border-radius: 2px;
-moz-border-radius: 2px;
border-radius: 2px;
width: 389px;
height: 230px;
text-align: left;
}
.titles {
padding: 5px;
position: absolute;
bottom: 10px;
left: -1px;
right: -1px;
background: transparent url(../images/layout/white80.png) top left;
-moz-border-radius: 1px 1px 0 0;
border-radius: 1px 1px 0 0;
text-align: left;
}
The reason I'm implementing it this way is so that the Div can float over the bottom of the image.
Just a quick idea which might be useful for you.
It is based on the fact that vertical padding/margin use the WIDTH of the parent box when it is set to percentages, so it is possible to resize a div relative its parent box
http://jsfiddle.net/xExuQ/2/
body,html { height:100%; }
.fixed-ratio-resize {
width: 50%; /* child width = parent width * percent */
padding-bottom: 50%; /* child height = parent width * percent */
height: 0; /* well, it is not perfect :) */
}
​If you want to put some (non-background) content into this nicely resized box, then put an absolutely positioned div inside it.
Reference:
http://www.w3.org/TR/CSS2/box.html#margin-properties and
http://www.w3.org/TR/CSS2/box.html#padding-properties says:
Margins: "The percentage is calculated with respect to the width of the generated box's containing block. Note that this is true for 'margin-top' and 'margin-bottom' as well. If the containing block's width depends on this element, then the resulting layout is undefined in CSS 2.1."
Paddings:"The percentage is calculated with respect to the width of the generated box's containing block, even for 'padding-top' and 'padding-bottom'. If the containing block's width depends on this element, then the resulting layout is undefined in CSS 2.1."
EDIT
http://jsfiddle.net/mszBF/6/
HTML:
<a class="griditem" href="#" style="background-image: url(http://pic.jpg);">
<span class="titles">
<span class="name">Unicomp Studios</span>
<span class="title">Springs Buckling (2012)</span>
</span>
</a>
CSS:
.griditem {
float: left;
margin-right: 17px;
margin-bottom: 17px;
min-width: 100px; /* extremely narrow blocks ==> crap looking */
width: 30%;
background: blue no-repeat;
background-size: contain; /* from IE9 only: https://developer.mozilla.org/en/CSS/background-size */
border: 1px solid transparent; /* prevent .titles:margin-top's margin collapse */
}
.titles {
/* <a> elements must only have inline elements like img, span.
divs, headers, etc are forbidden, because some browsers will display a big mess (safari) */
display: block; /* so display those inline elements as blocks */
padding: 5px;
margin: 0 auto;
margin-top: 105%;
background: yellow;
}
.titles > span {
display: block;
}​
I know this might not be the best solution, but
<html>
<style type="text/css">
#cool{
width:40%;
background:blue;
padding-bottom:10%;
}
</style>
<div id="cool" >
</div>
</html>
Here Ive used padding-bottom, to maintain its height relative to its width. U can set padding-bottom as a percentage. Hope this helped.

Resources