I've found HTML/CSS combinations that do one or multiple of these things but none that do all of them together. Basically I want:
a tile-style menu with six tiles
each tile to be a fixed height and width (maybe 10em - square)
wrapped text without creating a bigger tile (line-height has this problem)
background colour for the tiles but whitespace (margins) to the right and below
all text centred vertically and horizontally
wrapped tiles for smaller displays
Basically I'm looking for something like the collapse by rows option here but with centred text (and WordPress doesn't seem to recognise the #breakpoint rule, so it doesn't work for me). I've tried various methods with display:table/table-cell, display:inline-block, position:absolute/relative and different element types (div, a, p, tables...) but can't get anything to fulfil all the criteria listed above. Anyone got a solution (ideally with just standard HTML and CSS to make it easy to implement in WordPress)?
Below are examples of one of the methods I've tried. Example one shows how I'd like it to look (with a nicer colour scheme and proper link styling), except with smaller tiles and wrapped text. Example two has wrapped text, but because of using line-height the tiles with wrapped text double in height.
.big a {
display:inline-block; width:15em; line-height:15em; text-align:center; background-color:#FFC107; padding:10px; margin-bottom:10px; margin-right:10px;
}
.small a {
display:inline-block; width:10em; line-height:10em; text-align:center; background-color:#FFC107; padding:10px; margin-bottom:10px; margin-right:10px;
}
<p><strong>Example one</strong></p>
<div class="big"><a><strong>Language change</strong></a><a><strong>Language diversity</strong></a><a><strong>Language and the media</strong></a><a><strong>Language and gender</strong></a><a><strong>Child language acquisition</strong></a><a><strong>Discoures and attitudes</strong></a></div>
<p><strong>Example two</strong></p>
<div class="small"><a><strong>Language change</strong></a><a><strong>Language diversity</strong></a><a><strong>Language and the media</strong></a><a><strong>Language and gender</strong></a><a><strong>Child language acquisition</strong></a><a><strong>Discoures and attitudes</strong></a></div>
How about something like this... using display:flex and align-items: center;
NB: may not be compatible with some older web browsers.
.small{
display:flex;
flex-wrap:wrap;
}
.small a {
display:flex; align-items: center; height:10em; width:10em; text-align:center; background-color:#FFC107; margin-bottom:10px; margin-right:10px; padding:30px; box-sizing:border-box;
}
<p><strong>Example two</strong></p>
<div class="small">
<a><strong>Language change</strong></a>
<a><strong>Language diversity</strong></a>
<a><strong>Language and the media</strong></a>
<a><strong>Language and gender</strong></a>
<a><strong>Child language acquisition</strong></a>
<a><strong>Discoures and attitudes</strong></a>
</div>
a tile-style menu with six tiles
I would suggest a <div> with six display:inline-block <div> children.
each tile to be a fixed height and width (maybe 10em - square)
So of course we give your <div> children width:10em; height:10em;
wrapped text without creating a bigger tile (line-height has this
problem)
First we give each <div> child an overflow:hidden; so even if the content is larger than the tile, it will be cut. Then, since we know that the child is 10em high, if you can fix the number of lines of the text, you can define line-height as, say, 2em or even 20% which will be 20% of 10em.
background colour for the tiles
That's easy-peasy: background-color:#00FF00;
but whitespace (margins) to the right and below all text centred
I suggest you use border instead: border-width:0px 0px 0.5em 0.5em;. If you do it well, nobody will be able to tell it's border and not margin.
vertically and horizontally wrapped tiles for smaller displays
Inline block will do this for you. Since each <div> child is 10em wide, you can give the container <div> max-width:30em and it will shrink on small screens making the inline-block children skip to the next line.
Hope this gives you some useful ideas at least.
Related
I put a header which contains three divs. One has an image and the other two contain text.I then tried putting an image under it which has the same width of the header. But when I first put it, it was over the header div( I thought it should go under it). I then tried pushing it down by increasing the top margin and it worked. But as I increase the width of it the text in the header moves although it is not touching it!
This is the html code:
<div id="header">
<img id="logo" src="...."> <!---the logo at the top right-->
<div id="name">JANE DOETTE<div> <!---the text that moves - top left -->
<div id="job">Front-End Ninja</div> <!--under the text that moves but doesn't move--->
</div>
<img id="image" src="...."> <!---the image-->
This is the css code:
#header {
height: 6em;
width:80%;
background-color: white;
margin-left:10%;
margin-right:10%;
border-bottom:2px solid #BCBBBB;
margin-bottom:10px;
}
#image{
margin-left:10%;
margin-right:10%;
height:10em;
width:80%;
}
#logo {
height:88px;
width:89px;
}
#name {
color: #BCBBBB;
text-align:left;
float:right;
font-size:2.7em;
font-family:sans-serif;
height:50%;
}
#job {
color: #BCBBBB;
text-align:left;
float:right;
font-size:0.5em;
font-family:sans-serif;
font-weight:bold;
margin-top:0.2em;
}
Those are my questions:
Why doesn't the image automatically go under the header div?
Why does the text move?
Why is the top text the one that moved although the one at the bottom is nearer to the image?
What should I do to get the image under the heading div?
I adjusted the width of the image to 80%. But it seems to be just 20%. Why?
Has it got anything to do with position or display?
***Sorry for not adding an image of it but I don't have a reputation of more than 10 ( I am not allowed to).
***Sorry for the long questions.( I am still a beginner).
***Your answers would be much appreciated.
Your question isn't all that clear (please clarify), I will try to answer regardless, but I might misrepresent your question.
1 / 6 . The biggest problem you have I think is that you don't tell the browser how to 'order' the divs. Should they be under eachother or next to eachother? Use the "display" property for this. Use "display: block" to make sure that page-elements like divs or images are stacked under eachother. Depending on the margin the browser uses the remaining space to stack elements next or above eachother.
2 / 3. Because it floats. A float is relative to other elements on the page. If you make it float right, but the content within it align to the left the box goes left while the content within it stays as far to the left as it can keeping with the contraints of the div container. Therefore it seems to move. Lose the float and use "display: block" to make the div be the full width of the header div.
#name {
color: #BCBBBB;
text-align:left;
font-size:2.7em;
font-family:sans-serif;
height:50%;
display: block;
padding-left: 10px;
}
4 / 5 . Lose the "height" property of the image. Because the image has a relative 'height' property next to a relative 'width' property it distorts the image scaling. Use only the width as a percentage and it will scale proportionally.
You are missing a slash. Instead of
<div id="name">JANE DOETTE<div>
it should be:
<div id="name">JANE DOETTE</div>
After adding the slash it appears fine to me in Chrome and Firefox (except for the missing images obviously). See fiddle. Does that solve all of your questions?
I am working on a wordpress site.
I have these decorative titles that are constructed like this:
(the following is not the real html structure, it is just as example purpose)
<div class="decoration-left"> <div class="title"> <div class="decoration-right">
.title has a h1 title inside.
.decoration-left, and .decoration-right, are empty divs, with a decorative background.
I need the title to be centered all the time.
I first tried to give all the three divs a 33.3% width, which worked nice on big screens, but as i reduce the window the title breaks into two lines, and it looks ugly. I need the title to have a constant width therefore. I dont want the text to be smaller.
Right now, i have the .title div with "width:auto" which works fine. however i need the left and right decorative divs to take each one, half of the remaining space in a responsive/fluid way.
attaching picture for better understunding.!
My guess is: put your DECORATIVE divs inside the TITLE div, and your TITLE div inside a WRAP div. Make the TITLE div with 'position: relative; display:inline-block; background:#fff;' the DECORATIVE ones with 'position:absolute; left:-50px;' and 'position:absolute; right:-50px;', and the WRAP with 'text-align:center; background:url(LINE IMAGE LINK) center repeat-x;'.
I know it's hard to understand, but I am mostly sure it will work that way. I'll try making it look better by coding some of it:
HTML:
<div class="title-warpper">
<div class="title">
<div class="decoration-left"></div>
<span>YOUR TITLE HERE</span>
<div class="decoration-right"></div>
</div>
</div>
CSS:
div.title-wrapper {
background:url(LINE IMAGE LINK) center repeat-x;
text-align: center;
}
div.title {
position:relative;
background:#fff;
display:inline-block;
}
div.decoration-right{
position:absolute;
right:-25px;
background:url(DECORATION BG LINK);
width:25px;
height:25px;
}
div.decoration-left{
position:absolute;
left:-25px;
background:url(DECORATION BG LINK);
width:25px;
height:25px;
}
I set the decoration divs for 25px, but use what you need. Just remember to adjust them and the left and right properties to fit your needs.
if you don't mind the title overlapping the decorative divs on some screen sizes then you can absolute position the decorative divs with
position:absolute;
left:0px; (OR right:0px;)
give them whatever width and height you like.
I've looked at W3Schools and several threads here, and no matter what I do, these image links will not center inside of their elements. I can't just make the smaller, because some of the images further down in the table just barely fit in the their cells. I would like to both center horizontally, and vertically the images and everywhere I look, the following is how it is said to do it (Well. The horizontal. I figured I'd tackle the vertical centering once I got this straightened out.)
margin-left:auto;
margin-right:auto;
width: (some pixellage amount)
For some reason, this is not working in my html/css. Here is one of the links I'm trying to center, and the css codes referencing it.
HTML
<table class = "displayTable"><tr>
<td class = "photodisplaytd chana">
<img src="chanasImages/animeApron.jpg" onmouseover="this.src='chanasImages/animeApronBack.jpg'" onmouseout="this.src='chanasImages/animeApron.jpg'" alt = "Anime styled white apron, front with back image on mouse rollover."/>
</td>
CSS
.chana
{
margin-left: auto;
margin-right:auto;
width:50px;
}
.displayTable
{
alignment: right;
margin-left:auto;
margin-right:auto;
}
.photodisplaytd
{
border:4px solid slategrey;
background-color:#FFFFFF;
}
Here's a jsfiddle where it works with the old style text-align: centre; tech, on the photodisplaytd: http://jsfiddle.net/C6Zux/2/
.photodisplaytd
{
border:4px solid slategrey;
background-color:#FFFFFF;
text-align: center;
}
I also tried to do it with #cincodenada's suggestion but probably made some mistake and failed so far - that declaration is commented out in the css there now.
You have the auto margins on everything except where you need them. margin:auto gives the targeted object automatic margins to center it in its parent.
As a result, you need to apply margin:auto to your links themselves (.chana a, for instance), not to the table cell or table. It's not a property of the table cell (like the old align attributes would be), but of the thing being centered.
Additionally, since a elements are inline elements by default, you have to make them block elements in order to have auto margins. You can do this by adding a display: block to the styles as well.
I put together a quick demo fiddle showing everything working as expected.
I am trying to build a simple div with a span of text inside of it.
<div id="bottom-text">
<span>ONE STOP</span>
</div>
And here is the simple CSS styling I have in effect for "#bottom-text":
#bottom-text{
font-weight:700;
font-size:50px;
text-align:center;
margin:auto;
position:relative;
padding-top:25px;
height:65px;
width:auto;
}
For some reason, the text "ONE STOP" displays partially outside of #bottom-text. (only the top portion of all the letters...) I've tried using padding to fix it but the text then overflows partially into the padding region!
Can anyone help me figure out why this text is displaying outside the div it is supposed to be contained within? (I've been testing Chrome and Firefox)
Thanks all.
.largefont {
color: #0066FF;
font-family:arial;
font-size: 6px;
display: inline;
}
<span class="largefont">block level span</span>
Assign a class to the span and play with that.
look at your code, the #bottom-text is 65px height, the font-size is 50px, and padding-top is 25px
65-(50+25) = -10
So you will see only the top 10 pixel of your text.
Set padding-top to a lesser amount, and play with just so it is correct
Check your line-height. Only thing I can think of is you might have some styles elsewhere that are adding some in. Try adding "line-height: 1;" to your existing #bottom-text CSS so that your text is actually 50px high. Or, if you want the text to vertically center in #bottom-text make your line-height match the height of #bottom-text (65px).
I have a simple HTML button which contains text and an image:
HTML: (Already on http://jsfiddle.net/EFwgN)
<span class="Button">
<img src="http://www.connectedtext.com/Image/ok.gif" />
Small Icon
</span>
CSS:
span.Button {display:inline-block; margin:2px 4px;padding:3px 6px;
background-color:#ddd; height:24px; line-height:24px;
vertical-align:middle;}
span.Button img {vertical-align:middle;}
I can't come up with a combination that would fit these requirements:
The image and text need to be vertically at the middle of the div, with the text in the middle of the image. It should be neat.
Horizontally - the image may be in any width, and the button should grow to show it.
Vertically - the image may be in any height, smaller or larger than the button. When the image is larger, I don't mind if the extra pixels are displayed or cropped, as long as it is centered.
The button is in a fixed height. Currently I use line-height to center the text.
The button should sit nicely in line with other buttons and text.
The solution needs to work on all latest versions of major browsers, and Internet Explorer 8.
Here's a jsfiddle with my current code: http://jsfiddle.net/EFwgN
(note the small icon is slightly below the center of the button)
A simple solution, if you need no less than IE8 (in Standards mode): http://jsfiddle.net/kizu/EFwgN/31/
Just add margin: -100px 0 to img, so the negative margin would eat any large height:
span.Button img {vertical-align:middle; margin:-100px 0;
position:relative; top:-2px;}
I've added position: relative; top:-2px; just to look it better :)
But if you'll need support for compatibility mode or IE lt 8 (for some reason), the thing with margin won't work. So you'll need an extra markup: http://jsfiddle.net/kizu/EFwgN/28/, but it's somewhat hacky and works only with the fixed button's height (like in your example).
Use table-based display. Requires shrinking of image due to conflict between display:table-cell; and height:24px. Very similar to your aborted attempt from the comments, but includes the required display:block; on the image: http://jsfiddle.net/shanethehat/5ck3s/
There you go, using jQuery:
$(".button img").load(function()
{
$(this).each(function()
{
sh = $(this).outerHeight();
if (sh > 24){
alert(sh);
$(this).css("margin-top", - (sh - 24) / 2 + 'px');
}
});
});
Edit: Just saw that you wanted it pure CSS, well, here's to the code gulfers out there! :)
Why not make the image shrink in the case where it is indeed taller than the button?
span.Button img {
vertical-align:middle;
max-height: 100%;
}
I probably missed some of the requirements, as setting span.Button's height to auto did the trick for me.
If what you wanted is button growing only horizontally, with vertical overflow cropped, than maybe I'd do it like this:
span.Button {display:inline-block; margin:2px 4px;padding:3px 6px;
background-color:#ddd; width:auto; height:24px; line-height:24px;overflow:hidden;
vertical-align:middle;
}
using a bit of javascript to determine img height, and then center it accordingly.
How about... this?
http://jsfiddle.net/92K8J/
Added display:inline-block to the images, and removed the fixed heightof the container.