I have a grid created in CSS (of which only one of the items is shown in the code below)
CSS:
/* Grid 1 START*/
.grid-1 {
display: grid;
grid-template-columns: 33.33% 33.33% 33.33%;
grid-template-rows: auto quto auto;
grid-gap: 4px;
}
/* Grid 1 END*/
Html:
<div class="item-1"><img class="alignleft wp-image-5497 size-full" src="image#1.png" alt="" width="300" height="200" /></div>
If on hovering the mouse over the image#1, I need to replace the image#1 (on hover only) to image#2 what would the code be to add into the lines above?
Any help would be appreciated. There are other "items" in the grid but I have shown only one line of code to keep it simple.
This isn't really a css-grid question, as most of the solutions would be completely independent of the layout.
But #Paulie_D is right - CSS can't change the html code. You'd either need the image already on the page, or use javascript to swap in the src of the new image. Or use background-image properties on the a element instead of using img elements.
But just adding the 'on-hover' img to the grid (immediately alongside the 'non-hover' img) and hiding / unhiding with css is probably the easiest, fastest solution:
<a href="/">
<img src="https://via.placeholder.com/300x200/b4c833/000000?text=Image+1" class="img1"/>
<img src="https://via.placeholder.com/300x200/000000/b4c833?text=Image+2" class="img2"/>
</a>
<style>
img {
display: block;
}
img + img {
display: none;
}
a:hover img {
display: none;
}
a:hover img + img {
display: block;
}
</style>
I'm using broad selectors here... You'd probably want to be more specific. Assuming the same code structure from the example you provided, this should be sufficient:
.grid-1 img {
display: block;
}
.grid-1 img + img {
display: none;
}
.grid-1 a:hover img {
display: none;
}
.grid-1 a:hover img + img {
display: block;
}
Also, you have a typo in your question that's also present in your example code:
grid-template-rows: auto quto auto; should be grid-template-rows: auto auto auto; (note the second auto)
Related
I am trying to get two columns of content the same height using the CSS tables method. However, for some reason, the first column has extra padding at the bottom, the second column has extra padding at the top.
I am using the same code I usually do and cannot find the source of the problem when inspecting the code. I have double checked my code and look at other examples but cannot find the cause of this problem.
The code I am using is:
.archive-post{
display:table;
vertical-align: top;
padding:20px 0px;}
.archive-post .left-column{
display:table-cell;
width:60%;}
.archive-post .right-column{
display:table-cell;
width:40%;
padding-left:20px;}
Or you can see a live link here.
Use vertical align
.archive-post .left-column,
.archive-post .right-column {
vertical-align: top;
}
This should to the trick.
Just a small idea.. have you tried flexbox, for that? It's really a simple and easiest way to do that. Plus you can use position:absolute; inside the columns (display:table and display:table-cell do not allow that).
* {
box-sizing: border-box;
line-height: 2;
}
main {
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
padding: 1.25em 0em;
}
section {
background-color: #ccc;
-webkit-flex: 1;
-ms-flex: 1;
flex: 1;
}
aside {
background-color: #ccc;
margin-left: 20px;
width: 40%;
}
<main>
<section>
left column.<br>higher then the other
</section>
<aside>
right column
</aside>
</main>
I have a problem in sorting the div(s), i have two types a, b
a - should always be at the front (all a types)
b - should be following all a types.
HTML:
<div class="" style="">
<div class="a">a</div>
<div class="a">a</div>
<div class="b">b</div>
<div class="a">a</div>
</div>
CSS:
.a, .b {
display:inline-block;
width:50px;
height:50px;
padding:15px;
margin:5px;
}
.a {
float:left;
background-color: blue;
}
.b { background-color: red; }
This seems to work fine in a line:
But breaks as a grid:
Desired result (number of boxes is irrelevant):
JsFiddle: http://jsfiddle.net/kQkn9/
How would i go about fixing this problem?
If you're looking for a pure CSS solution, your only option is to use Flexbox.
http://jsfiddle.net/kQkn9/2/
.container { /* parent element */
display: -webkit-flexbox;
display: -ms-flexbox;
display: -webkit-flex;
-webkit-flex-wrap: wrap;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
}
#supports (flex-wrap: wrap) {
.container {
display: flex;
}
}
.a, .b {
display: inline-block;
width: 50px;
height: 50px;
padding: 15px;
margin: 5px;
}
.a {
background-color: blue;
}
.b {
-webkit-flex-order: 1;
-ms-flex-order: 1;
-webkit-order: 1;
order: 1;
background-color: red;
}
Browser support: Chrome, Opera, IE10. http://caniuse.com/#feat=flexbox
don't believe this is possible with CSS and HTML alone. My recommendation would be to sort the a's and b's, without changing your css as you have it then re-insert them into the DOM in their new, sorted order.
something to this effect: (in JQ)
var listOfAs = $('.a').clone();
var listOfBs = $('.b').clone();
var parent = $('.a').first().parent('div');
$('.a, .b').remove();
parent.append(listOfAs);
parent.append(listOfBs);
I know this is a touch cumbersome and not super 'responsive' but as i said, don't think its possible with CSS alone...this is just a quick-and-dirty implementation to get you started.
PS: updated your fiddle http://jsfiddle.net/kQkn9/6/
EDIT: clearly this IS possible in newer browsers (thanks to #cimmanon). If you need to support older browsers, you'll have to do something like this (which is definitely less cool)
When you resize vBulletin you can see the 3 columns rescaling nicely (Board Name, Threads / Posts, Last Post).
How could you achieve something like that in CSS? When I try to do so it always overlaps each other. The .board-icon width needs to stay the same because there's an image inside.
This is my CSS
.board-icon {
float: left;
width: 55px;
}
.board-title {
float: left;
background: red;
width: 50%;
}
.board-info {
float: left;
background: green;
width: 120px;
}
.board-lastpost {
float: left;
background: orange;
width: 240px;
}
Here are some other examples using the same flexible layout:
http://punbb.informer.com/forums/
http://www.simplemachines.org/community/index.php
http://community.invisionpower.com/
The CSS behind it isn't that complex for modern browsers. Though some of these techniques will not work in earlier versions of ie without some modifications.
Using this HTML
<body>
<div id="canvas">
...
</div>
</body>
And this CSS
body {
margin:0;
padding:0;
position:relative;
}
#canvas, .main-width {
min-width:960px;
width: 80%;
margin:0 auto;
}
.clear, #canvas:after {
clear: both;
height: 0;
visibility: hidden;
#canvas:after {
content: ".";
display: block;
}
Start working with % widths, not px widths as you have in your example. The only time to use fixed width values should be on a min-width delcaration.
You could create two fluid columns inside your canvas div for example
<div class="col1>
...
</div>
<div class="col2">
...
</div>
<br class="clear" />
And the CSS (no need for min-widths now as 34% of 960px is just as good)
.col1,.col2 {
float:left;
}
.col1 {
width:34%;
}
.col2 {
width:66%;
}
Notice the .main-width declaration above? That applies the #canvas style to any container in the #canvas div, giving it exactly the same width and fluidity.
<div class="main-width"> ... </div>
That should set you on the right tracks, just remember that you need to work in % not px. If you need to use borders on elements, make sure they are display:block to maintain positioning with % widths.
I'm looking an elegant way to position two divs one besides the other without line wrapping. The first div is an icon the second a text of unknown size.
They should not break in two lines but hide if not enough place. I'm trying with this example, but it doesn't work.
There is a similar question, but's it's not the same scenario as size is unknown.
Help is appreciated
Write like this:
.container {
white-space: nowrap;
}
.d1,
.d2{
display: inline-block;
*display:inline;/*for IE 7 */
*zoom:1;/*for IE 7 */
vertical-align:top;
}
.d1 {
background-color:#ff0;
}
.d2 {
background-color:red;
}
Check this http://jsfiddle.net/xcSXA/5/
float: left does not give you, what you need.
Try display: inline
http://jsfiddle.net/xcSXA/3/
Instead of floating your divs, display them as inline-block so they don't wrap. Also, set the container's "white-space" style to "nowrap" to also prevent line wrapping.
HTML
<div class="container">
<div class="d1">icon</div>
<div class="d2">This can be very very very very large.</div>
</div>
CSS
.container {
white-space:nowrap;
overflow:hidden;
width: 100px;
}
.d1 {
display: inline-block;
background-color:#ff0;
}
.d2 {
display: inline-block;
background-color:red;
}
Working Example: http://jsfiddle.net/C4Wfa/
.d1 and .d2 you have to give a certain width, but you gotta make sure that the width of both .d1 and .d2 together (+ margins and paddings) isn't bigger then the the container class, else they won't be able to be set next to each other.
I think, the following CSS is, what you need.
.container {
display:inline-block;
overflow:hidden;
white-space: nowrap;
}
.d1 {
display: inline-block;
background-color:#ff0;
}
.d2 {
white-space: nowrap;
display: inline-block;
background-color:red;
}
You can try it with
float: left;
and create an outer div with this style:
height: 1%; overflow: hidden;
See here: http://www.google.de/imgres?imgurl=http://www.mikepadgett.com/legacy/images/client_images/float_problem.gif&imgrefurl=http://www.mikepadgett.com/technology/technical/alternative-to-the-pie-clearfix-hack/&usg=__NW1NVgWIKW-rBh0Cp60ouDdIGvg=&h=300&w=412&sz=6&hl=en&start=0&sig2=4nJ8a7o2JcYBdlBaPaL3VA&zoom=1&tbnid=raa9wIX8T8PbWM:&tbnh=103&tbnw=141&ei=uGlLT9j4MsWEhQfl7eGYBw&prev=/search%3Fq%3Dfloat%2Bleft%26um%3D1%26hl%3Den%26sa%3DN%26biw%3D1920%26bih%3D1075%26tbm%3Disch&um=1&itbs=1&iact=rc&dur=152&sig=110912085308513740608&page=1&ndsp=57&ved=1t:429,r:1,s:0&tx=64&ty=50
I want to set vertical alignment of image inside a div. I use img { vertical-align:middle}
but it is not working.
Using the line-height property will solve the problem:
<style>
.someclass {
width: 300px;
height: 300px;
text-align: center;
line-height: 300px;
border: dotted;
}
.someclass img {
margin: auto;
vertical-align: middle;
}
</style>
<div class="someclass">
<img src="someimg.jpg" border="0" alt="">
</div>
This is a solution that doesn't require JavaScript (as my previous solution did).
You can achieve what you want by assigning display: table-cell to the containing div. Here's an example: http://jsbin.com/evuqo5/2/edit
I feel I must warn you that you will need to test this in every browser you intend to support. Support for the table-cell value is fairly new, particularly in Firefox. I know it works in Firefox 4, but I don't know about any of the 3.x iterations. You'll also want to test in IE (I've only tested in Chrome 10 and Firefox 4).
The CSS:
div#container {
width: 700px;
height: 400px;
position: relative;
border: 1px solid #000;
display: table-cell;
vertical-align: middle;
}
div#container img {
margin: 0 auto;
display: block;
}
You won't need the div#container img styles if you don't also want to horizontally align the image.
If you're trying to do what I think, vertical align isn't going to work; you'll need to use positioning.
In general, position the container relative, and then position the image absolute, with top and left set to 50%, and then move the image back to the center by setting negative margins equal to half the width / height.
Here's a working example: http://jsbin.com/evuqo5/edit
Basic CSS is this:
#container { position: relative; }
#container img {
position: absolute;
left: 50%;
top: 50%;
margin-top: /* -1/2 the height of the image */
margin-left: /* -1/2 the width of the image */
}
See this awser: How to vertical align image inside div
If you want to align horizontally also, add the right and left, like this:
div {
position:relative;
}
img {
position:absolute;
top:0;
bottom:0;
left:0;
right:0;
margin:auto;
}
The following post has some useful references:
Text Alignment w/ IE9 in Standards-Mode
Also, depending on which version of IE you are testing against, you may end up needing some browser-specific hacks or some jQuery/JavaScript code.
If you have to, use a one-row-one-cell table and take advantage of the vertical-align property. This is brute-force, not overly semantic, but it works.
If you set the div display attribute to table-cell then vertical-align: middle; will work.
The vertical-align rule only affects table cells or elements with display: table-cell.
See this article from SitePoint for a detailed explanation.
<style>
/* change body to .someClasses's parent */
body {
width: 100%;
height: 100%;
display: table;
}
body > .someclass {
width: 300px;
height: 300px;
text-align: center;
border:dotted;
margin: 0 auto
display: table-cell;
vertical-align: middle;
}
</style>
<body>
<div class="someclass">
<img src="someimg.jpg" border="0" alt="">
</div>
</body>