How to make an entire div clickable (table style divs) - css

Yes, I know, this question have been asked many times and a possible solution is to add style="display:block;" to the link.
For some reason this solution does not work with table style DIVs:
https://jsfiddle.net/exyv8jmw/1/
HTML:
<div class="table">
<div class="tablerow">
<div class="left">
<a href="/something.html" style="display:block">
This is a link</a>
</div>
<div class="right">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</div>
</div>
</div>
CSS:
.table{
width:500px;
display:table;
}
.tablerow{
display:table-row;
}
.left{
width:50%;
background:green;
display:table-cell;
padding:5px;
}
.right{
width:50%;
display:table-cell;
background:red;
padding:5px;
}
As you can see, the empty green space is clickable only horizontally, but not vertically. I also tried:
<div class="left">This is a link</div>
but it does not help.

You need to add height: 100%; to the link, .left, and .tablerow elements.
.table{
width:500px;
display:table;
}
.tablerow{
display:table-row;
height: 100%;
}
.left{
width:50%;
background:green;
display:table-cell;
padding:5px;
height: 100%;
}
.right{
width:50%;
display:table-cell;
background:red;
padding:5px;
}
<div class="table">
<div class="tablerow">
<div class="left">
<a href="/something.html" style="display:block;height:100%;">
This is a link</a>
</div>
<div class="right">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</div>
</div>
</div>

First set .left position to relative, Then set a tag position to absolute and width and height to 100%.
.table{
width:500px;
display:table;
}
.tablerow{
display:table-row;
}
.left{
width:50%;
background:green;
display:table-cell;
padding:5px;
position: relative;
}
.left a {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.right{
width:50%;
display:table-cell;
background:red;
padding:5px;
}
<div class="table">
<div class="tablerow">
<div class="left">
<a href="/something.html" style="display:block">
This is a link</a>
</div>
<div class="right">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</div>
</div>
</div>

Can't you put the div inside the 'a' element?
<a href="/something.html" >
<div class="left">
This is a link </div>
</a>

Apply the 'left' class to the a element directly, i.e.:
this is a link
Although this does not allow for additional content, it does make the anchor fill the available space.
Part of the problem is that the anchor tag is, by default, a "span" type tag, which only fills a space as big as its content, disregarding internal divs.
You have to make the anchor tag act like a "block" style element, not its surrounding element, or an internal element.

You could add listener to the div id.
<div class="table" id="clik">
document.getElementById("clik").addEventListener("click", function(){
// document.location = "/something.html";
alert("hello");
});

Related

Set background width / height within list tag whilst using sprites

Ok, I have a situation where I need to display a custom image for the list bullets; obviously you can do this via list-style-image; however I want to use a sprite.
So another method I came across was to just put the background on the li itself, however without being able to restrict the width/height of the background more of the sprite shows.
I read this solution but..
It didn't seem to work for me for some reason - no background image
showed at all
I would like to support IE8 (extra styles ok though)
I also tried doing something like this and floating the divs inside:
<ul>
<li>
<div class="icon"></div>
<div class="text"></div>
</li>
<li>
<div class="icon"></div>
<div class="text"></div>
</li>
</ul>
... but if the text wrapped to the next line it would go under the icon as the icon height didn't extend high enough.
Is there anything I can do here?
You can use pseudo :before element for styling list
ul {
list-style:none;
padding: 0 0 0 40px;
}
ul li {
position:relative;
margin:5px 0;
}
ul li:before {
content:'';
background: url('http://blog.fogcreek.com/wp-content/uploads/2013/01/sprite.png') no-repeat;
width: 20px;
height: 20px;
display: inline-block;
border: 1px solid black;
vertical-align: middle;
background-position: -197px 0px;
position: absolute;
left: -34px;
background-size: 492px auto;
}
<ul>
<li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. amet, consectetuer adipiscing elit.</li>
<li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
<li>Lorem ipsum dolor sit amet, consectetuer adipiscing amet, consectetuer adipiscing elit. elit.</li>
<li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
</ul>

Right div on float:right appears a line below the left one

Here's my problem.
I have a wrapper div(width: 800px and height: 250px) which contains two divs occupying all the space in height and dividing their width in half.
I set up my css, float the right div to float: right and this one appears where it should but "below" the other one, exceeding the wrapper div space(which shouldn't even be possibile).
I'm posting both the jdfiddle and the code.
JS Fiddle http://jsfiddle.net/FV9yC/
HTML
<div id="wrapper">
<!-- left div -->
<div id="leftDiv">
<h1>This is my heading</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
<!-- right div -->
<div id="rightDiv">
<h1>This is my heading</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
</div>
CSS
#wrapper {
background-color: grey;
height: 200px;
width: 500px; }
#leftDiv {
background-color: purple;
height: 200px;
width: 250px; }
#rightDiv {
background-color: green;
float: right;
height: 250px;
width: 250px; }
Just shift the div with ID rightDiv above the div with ID leftDiv. That's it.
Here is the WORKING SOLUTION
The Code:
<div id="wrapper">
<!-- right div -->
<div id="rightDiv">
<h1>This is my heading</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
<!-- left div -->
<div id="leftDiv">
<h1>This is my heading</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
</div>
http://jsfiddle.net/FV9yC/1/
You should add float: left to your left div.
Add float: left to the other div. Also you can use float: left for both; unless you have other reasons than just positioning there to use it.
Used to this code
#leftDiv{float:left;}
#wrapper:after{
content:"";
clear:both;
display:table;
}
#wrapper {
height:200px; // remove this line
}
Demo
try this
http://jsfiddle.net/FV9yC/5/
#wrapper {
background-color: grey;
height: 200px;
width: 500px;
}
#leftDiv {
background-color: purple;
height: 200px;
width: 250px;
float:left;
}
#rightDiv {
background-color: green;
float: right;
height: 250px;
width: 250px; }
You don't need to float your div to the right — you just need to align each block beside the other, and you can do this using float: left;.
I made a consistent solution for you. See below:
Using a class to remove the DRY of your code, I grouped your blocks into a common class with common behaviors.
See your new CSS:
#wrapper {
background-color: grey;
height: 200px;
width: 500px; }
.block {
float: left;
width: 250px;
}
#leftDiv {
background-color: purple;
height: 200px; }
#rightDiv {
background-color: green;
height: 250px; }
And your new HTML:
<div id="wrapper">
<!-- left div -->
<div class="block" id="leftDiv">
<h1>This is my heading</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
<!-- right div -->
<div class="block" id="rightDiv">
<h1>This is my heading</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
</div>
Using classes to execute common behaviors it is a good practice to avoid redundancy and future problems.
To see your code working on jsFiddle, just click here.

Two inline blocks: place text next to an image

I have problem with 2 inline blocks. I want create something like this:
<img>
<span>some long text next to the img</span>
I have following structure (which I have to use):
<div class="mainContainer">
<div class="additional">
<div class="description">
<img></img>
</div>
<div class="description">
Lorem ipsum dolor sit amet, consectetur adipisicing elit...
</div>
</div>
</div>
Styles:
.mainContainer {
height: 15px; } //doesn't matter in this case
.additional {
line-height: 15px; } //doesn't matter in this case
.description {
float: left;
display: inline;
}
The problem is when I want add long text, then image is above the text, but it should be next to it.
It should be something like this (I add on code but it has to be removed):
http://jsfiddle.net/476fm/
Any suggestions?
EDIT:
Ok actually the text above was only example and to be more specific what I want achieve:
I have one main container which contains 2 inline elements:
- first element have image
- second have image too and the text
and what I wanna do is: when the text is long and need to be in second line it shouldn't be underneath the first image
http://jsfiddle.net/z2t7b/ - it should be fixed
(I hope that somebody understood what I wanna do )
Is this what you are trying to accomplish?
<div class="mainContainer">
<div class="additional">
<div class="description">
<img></img> <div class="description">Lorem ipsum dolor sit amet, consectetur adipisicing elit,</br> sed do eiusmod tempor incididunt ut labore</div>
<div class="breaker"></div>
<img></img><div class="description">Lorem ipsum dolor sit amet, consectetur adipisicing elit,</br> sed do eiusmod tempor incididunt ut labore</div>
</div>
</div>
</div>
.mainContainer {
height: 23px;
}
.additional{
line-height: 23px;
}
.description {
float:left;
display: inline;
}
.breaker {clear: both;}
img {
background-color:#FFF;
width:20px;
height:20px;
display:inline-block;
border:solid black 1px;
float:left;
}
Here is the fiddle
Well, for INLINE elements, just remove the 'float:left' .. Natural inline flow will apply, THEN, your text will pass under normally.
OK, now i understook your quest (i hobe)
just make your container behave has a block, float it left and add overflow: hidden;
http://jsfiddle.net/NgfSF/
.description {
padding-right:10px;
float:left;
display: inline;
}
.description1 {
display: block;
overflow: hidden;
}

Vertical lines with CSS

I have a #comments element which contains .comment elements. I would like to have 5 vertical lines from left to right, each 1px in width, 100% height (till the end of the #comments element), with 20px between them and without images. I tried to do that myself, but my CSS-fu isn't that high. Any help would be much appreciated.
HTML:
<div id="comments">
<div class="comment level1">Lorem ipsum dolor sit amet</div>
<div class="comment level2">Lorem ipsum dolor sit amet</div>
<div class="comment level3">Lorem ipsum dolor sit amet</div>
</div>
CSS:
#comments {
width: 400px;
border: 1px solid black;
}
.comment {
margin: 10px 0;
}
.level1 {}
.level2 { margin-left: 20px; }
.level3 { margin-left: 40px; }
Demo.
Here's how I imagine it:
|[comment ]
| |[comment ]
| |[comment ]
| | |[comment]
Is there some reason you need to have all the divs as direct children of the outer parent div? If you nest the divs you can accomplish this very easily:
css:
div div {
border-left: 1px solid black;
padding-left:20px;
}
nested html
<div id="comments">
<div>Lorem ipsum dolor sit amet</div>
<div>Lorem ipsum dolor sit amet
<br/>
<div>Lorem ipsum dolor sit amet
<br/>
<div>Lorem ipsum dolor sit amet
<br/>
<div>Lorem ipsum dolor sit amet</div>
<div>Lorem ipsum dolor sit amet
<br/>
<div>Lorem ipsum dolor sit amet</div>
</div>
</div>
</div>
</div>
</div>
updated fiddle showing how it would look here nested down to 5 levels:
http://jsfiddle.net/webchemist/tuZB6/4/

Is it possible to vertically center a SPAN within a DIV?

Here's what I got:
<div class="slideshow">
<span style="font-size:12px; color:#333333; font-family:Lucida Grande,Lucida Sans Unicode,Calibri,Arial,sans-serif;">Lorum ipsum delore sit amet. Lorum ipsum delore sit amet. Lorum ipsum delore sit amet. Lorum ipsum delore sit amet. Lorum ipsum delore sit amet. Lorum ipsum delore sit amet. Lorum ipsum delore sit amet. Lorum ipsum delore sit amet. Lorum ipsum delore sit amet. Lorum ipsum delore sit amet.</span>
<span style="font-size:12px; color:#333333; font-family:Lucida Grande,Lucida Sans Unicode,Calibri,Arial,sans-serif;">Goodbye</span>
</div><br />
And the CSS:
/* slideshow */
.slideshow {
width:940px;
height:64px;
text-align:center;
background-image:url(../images/quotes.png);
position:relative;
}
.slideshow span {
display:block;
width:940px;
height:64px;
}
The <span>s are currently centered horizontally, but they should also be centered vertically. Is this possible?
The whole idea is to have testimonials on top the background image (quotes on the left and right side), but it doesn't quite look right without being centered both horizontally and vertically.
I'm sure I could somewhat get the desired effect using padding, but since each testimonial will be a different length I don't think that'd be a good approach.
if you know that the content of your span will never exceed one line of text, just set
.slideshow span {
line-height: 64px /* = the height of the containing div */
}
Or, if you know the height of the span:
.slideshow span {
display: block;
position: absolute;
height: 64px;
top: 50%;
margin-top: -32px; /* = height/2 */
}
Last option would be to use a table formed by a single cell in place of the div, and use the vertical-align property.
CSS:
<style>
div {
width:300px; height:300px;
text-align:center;
display:table-cell; vertical-align:middle;
}
</style>
HTML:
<div>
<span>The Span</span>
</div>
yes it is possible.
Use the CSS attribute vertical-align:middle.
If you want to align verticaly below or above the horizontal center position then Use like this :
vertical-align:5;
or
vertical-align:-5;

Resources