lets say i have this html:
<div class="wrapper">
<p>testOne</p>
<p>testTwo</p>
<p>testThree</p>
</div>
If i want to apply to the p tag design, i can go to CSS and use:
1)
.wrapper > * {
color: red;
}
OR
2)
.wrapper {
color: red;
}
Both of them work just fine, so, what is the difference?
I have heard once that the the first example apply the design only to the direct childs of the "wrapper", so then i did:
<div class="wrapper">
<p>testOne</p>
<div class="container">
<p>testTwo</p>
</div>
<p>testThree</p>
</div>
so testTwo is not a direct child..but he still got the color red!
so testTwo is not a direct child..but he still got the color red!
testTwo's parent <div class="container"> has the color red, though, so all of its children inherit that style. It's the same fundamental behavior as setting your body color to red and that reflecting on the whole document.
I have heard once that the the first example apply the design only to the direct childs of the "wrapper"
That's right.
Maybe border will better illustrate the difference between the selectors, since children don't inherit it:
.wrapper > * {
border: 1px solid red;
}
.wrapper {
border: 1px solid blue;
}
<div class="wrapper">
<p>testOne</p>
<div class="container">
<p>testTwoA</p>
<p>testTwoB</p>
</div>
<p>testThree</p>
</div>
Although you didn't ask about it, for context consider also .wrapper *, which selects all children regardless of depth, further illustrating >:
.wrapper * {
border: 1px solid green;
}
.wrapper > * {
border: 1px solid red;
}
.wrapper {
border: 1px solid blue;
}
<div class="wrapper">
<p>testOne</p>
<div class="container">
<p>testTwoA</p>
<p>testTwoB</p>
</div>
<p>testThree</p>
</div>
Note that order matters in the above example since .wrapper * and .wrapper > * are no longer disjoint as .wrapper and .wrapper > * are.
Related
I am trying to target all top level .dokan-form-group elements with nth-child.
As you can see, there is a wrapper in the middle called dokan-child-wrapper which has some dokan-form-group elements inside.
I don't want to target these. Only the upper most top level classes which match dokan-form-group, which is a direct descendant of .wrapper.
You can see it's targeting 4 items, but the 3rd and 4th are targeting the child elements which I don't want.
Code example:
https://codepen.io/jordanc26/pen/NWKOXKQ
HTML / SCSS Code:
<div class="wrapper">
<div class="dokan-form-group">parent-item</div>
<div class="dokan-form-group">parent-item</div>
<div class="dokan-child-wrapper">
<div class="dokan-form-group">child-item</div>
<div class="dokan-form-group">child-item</div>
<div class="dokan-form-group">child-item</div>
</div>
<div class="dokan-form-group">parent-item</div>
<div class="dokan-form-group">parent-item</div>
</div>
.
.wrapper {
.dokan-form-group {
background-color: blue;
&:nth-child(0),
&:nth-child(1),
&:nth-child(2),
&:nth-child(3) {
background-color: red;
}
}
}
Change scss to:
.wrapper {
>.dokan-form-group {
background-color: blue;
&:nth-child(1),
&:nth-child(2),
&:nth-child(3),
&:nth-child(4) {
background-color: red;
}
}
}
live demo
I want to select the first and the last child with CSS but it does not work. Please take a look at my Fiddle and help me:
.area {
height: 100px;
width: 100px;
}
.area:first-child {
background-color: red;
}
.area:last-child {
background-color: green;
}
<div class="area">1</div>
<div class="area">2</div>
<div class="area">3</div>
<div class="area">4</div>
https://jsfiddle.net/rbw8dpsb/1/
I advise you to add a container as in your code they are childs of body BUT you don't know the last-child or the first-child of body as you may have other elements like script tags or other tags dynamically added (like in the snippet here or with jsfiddle or any other online coding tools).
.area {
height: 100px;
width: 100px;
}
.area:first-child {
background-color: red;
}
.area:last-child {
background-color: green;
}
<div>
<div class="area">1</div>
<div class="area">2</div>
<div class="area">3</div>
<div class="area">4</div>
</div>
Here is a screenshot to show what is inside your body when you run the snippet:
As you may clearly notice, there is a div added at the end which is the last-child of the body. Adding a container will avoid you dealing with random settings and hidden elements added.
If you don't want to let all that divs in another structure you should use first-of-type and last-of-type instead of first-child and last-child
.area {
height: 100px;
width: 100px;
}
.area:first-of-type {
background-color: red;
}
.area:last-of-type {
background-color: green;
}
<div class="area">1</div>
<div class="area">2</div>
<div class="area">3</div>
<div class="area">4</div>
As Temani Afif pointed, this solution is arbitrary and may not work in all the situations. As shown, it is not properly working on the code snippet but it does on JSFiddle for example. I.E. https://jsfiddle.net/vm1scerv/
I understand that I can change another element's style when hovering on a different element like this:
.element-one:hover .element-two {
opacity: 0.8;
}
But how can I change the style of all the elements in the page except element-two when I hover on element-one?
You can use .element-one:hover :not(.element-two).
Here is an example:
.element-one:hover :not(.element-two) {
opacity: 0.8;
}
.element-one {
background: black;
margin: 10px;
}
.element-one div {
background: green;
border: 1px solid blue;
margin: 10px;
padding: 10px;
}
<div class="element-one">
<div class="element-two">
element-two
</div>
<div class="element-three">
element-three
</div>
<div class="element-four">
element-four
</div>
</div>
However - note that it will work only for elements inside element-one and not for all the elements in the page.
You can do this with body for example, but the problem there is that .element-two is probably also inside some other element that exists inside body, and in such case the .element-two will get the opacity from it's containing element.
Problem:
I am trying to create a multi-column CSS layout with borders that look something in line with this picture:
Code:
<div style="border-radius:4px; border: 1px solid #ddd;">
<div style="display:block;float:left;width:50%;">
<div><b>Författare:</b> '.$authors.'<br></div>
<b>Handledare:</b> '.$row['Supervisor'].'<br>
<b>Examinator:</b> '.$row['Examiner'].'<br>
<b>Design av studie:</b> '.$design.'
</div>
<div style="display:block;float:left;width:50%;">
<b>Examinationsdatum:</b> '.$row['ExaminationDate'].'<br>
<b>Nivå:</b> '.$level.' ('.$credits.')<br>
<b>Kommentar:</b> '.$row['Comments'].'<br>
<b>Övrigt:</b> '.$row['Participants'].' deltagare, '.$row['Reference'].' referenser
</div>
</div>
The above-mentioned code will produce the following:
Question:
What needs to be modified so I can get the horizontal and vertical lines to the box?
You can modify the CSS to look like this:
.row {
border: 1px solid #ddd;
margin-bottom: -1px;
}
.left {
display: inline-block;
width: 50%;
border-right: 1px solid #ddd;
}
.right {
display: inline-block;
width: 49%;
border-left: 1px solid #ddd;
margin-left: -1px;
}
.top {
border-top-right-radius: 4px;
border-top-left-radius: 4px;
}
.bottom {
border-bottom-right-radius: 4px;
border-bottom-left-radius: 4px;
}
.outerBox {
margin: 10px;
}
Then have your markup look like this:
<div class="outerBox">
<div class="row top">
<span class="left"><b>Författare:</b> '.$authors.'</span><span class="right"><b>Examinationsdatum:</b> '.$row['ExaminationDate'].'</span>
</div>
<div class="row">
<span class="left"><b>Handledare:</b> '.$row['Supervisor'].'</span><span class="right"><b>Nivå:</b> '.$level.' ('.$credits.')</span>
</div>
<div class="row">
<span class="left"><b>Examinator:</b> '.$row['Examiner'].'</span><span class="right"><b>Kommentar:</b> '.$row['Comments'].'</span>
</div>
<div class="row bottom">
<span class="left"><b>Design av studie:</b> '.$design.'</span><span class="right"><b>Övrigt:</b> '.$row['Participants'].' deltagare, '.$row['Reference'].' referenser</span>
</div>
</div>
CAVEAT: The formatting will break if you put a space between the spans on an individual line, so don't break them; otherwise, take this solution and work out something that doesn't break =)
You can see a working example at http://jsfiddle.net/saluce/XhnBE/
EDIT: It seems that mPDF doesn't like inline-block, so change this part of your CSS:
.left {
display: block;
float: left;
width: 50%;
border-right: 1px solid #ddd;
}
http://jsfiddle.net/saluce/XhnBE/1/
You could use two lists side by side:
<div style="border-radius:4px; border: 1px solid #ddd;">
<ul style="display:block;float:left;width:50%;">
<li><b>Författare:</b> '.$authors.'</li>
<li><b>Handledare:</b> '.$row['Supervisor'].'</li>
<li><b>Examinator:</b> '.$row['Examiner'].'</li>
<li><b>Design av studie:</b> '.$design.'</li>
</ul>
<ul style="display:block;float:left;width:50%;">
<li><b>Examinationsdatum:</b> '.$row['ExaminationDate'].'</li>
<li><b>Nivå:</b> '.$level.' ('.$credits.')</li>
<li><b>Kommentar:</b> '.$row['Comments'].'</li>
<li><b>Övrigt:</b> '.$row['Participants'].' deltagare, '.$row['Reference'].' referenser</li>
</ul>
</div>
You'll need to add some styles to get rid of the default styles for lists and add you borders to the top, bottom and sides of your lis .
A down side of this is that you'll have to give your lis fixed heights so the borders line up.
You have to change your marke-up. You need a table or more boxes to do this. I think tables would be easier to manage... and somehow this is a case tables are for.
Or you take a bg-image... but I won't recommend this
Check this: http://jsfiddle.net/eNEzs/
It's enough to correct your css a little bit and wrapp items in div's.
<div style="border-radius:4px; border: 1px solid #ddd; margin: 20px;">
<div class="block">
<div><b>Författare:</b> '.$authors.'</div>
<div><b>Handledare:</b> '.$row['Supervisor'].'</div>
<div><b>Examinator:</b> '.$row['Examiner'].'</div>
<div><b>Design av studie:</b> '.$design.'</div>
</div>
<div class="block">
<div><b>Examinationsdatum:</b> '.$row['ExaminationDate'].'</div>
<div><b>Nivå:</b> '.$level.' ('.$credits.')</div>
<div><b>Kommentar:</b> '.$row['Comments'].'</div>
<div><b>Övrigt:</b> '.$row['Participants'].</div>
</div>
<div style="clear:both"></div>
</div>
CSS
.block {
display:block;
float:left;
width:50%;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
.block:first-child {
border-right: 1px solid #ddd;
}
.block div {
border-bottom: 1px solid #ddd;
}
.block div:last-child {
border-bottom: 0px;
}
But I would recommend you to simply change elements to a table or list (ul or ol). Above solution is simply for this particular exmaple.
Wrap each cell in a div instead of using br, and add border-left and border-bottom on the appropriate element. If you still want to use 50% width, then use box-sizing: border-box; to keep it from wrapping.
See this jsfiddle for one way to do it, though you should use classes to properly select which elements to apply the different styles to.
I have 2 nested divs inside outer one, which has width:100%. Both nested divs should be in one line and first should get it size from it's contents:
<div id="#outer" style="width:100%; border:1px">
<div id="#inner1" style="border:1px; display:inline">
inner div 1. Some text...
</div>
<div id="#inner2" style="width:100%????; border:1px; display:inline">
inner div 2...
</div>
</div>
Question is how to make #inner2 div to get rest of the horizontal space if width of the #inner1 div is not specified and depends on what it is inside?
P.S. All styles are in separate classes in my case, here I putted CSS into style attributes just for simplification.
I want result to work in IE7+ and FF 3.6
In more details for me it looks like this:
<style type="text/css">
.captionText
{
float:left;
}
.captionLine
{
height: 1px;
background-color:black;
margin: 0px;
margin-left: 5px;
margin-top: 5px;
border: 0px;
padding: 0px;
padding-top: 1px;
}
</style>
<table style="width:300px;">
<caption width="100%">
<div class="captionText">Some text</div>
<div class="captionLine"> </div>
</caption>
<tr>
<td>something</td>
</tr>
</table>
Here is the image of what I want:
The mysterious overflow: hidden; is your friend here. It stops elements adjacent to floats from extending behind the float — I think that’s the layout you’re looking for.
Here’s some slightly edited HTML: I don’t think you can have # characters in your ids:
<div id="outer">
<div id="inner1">
inner div 1. Some text...
</div>
<div id="inner2">
inner div 2...
</div>
</div>
And here’s the CSS to achieve the layout you want.
(I put in additional CSS for IE 6 with HTML conditional comments. I just noticed you didn’t actually need it to work in IE 6 too, but if you fancy being nice to the IE 6 users out there...)
<style type="text/css">
#outer {
overflow: hidden;/* Makes #outer contain its floated children */
width: 100%;
/* Colours and borders for illustration purposes */
border: solid 3px #666;
background: #ddd;
}
#inner1 {
float: left;/* Make this div as wide as its contents */
/* Colours and borders for illustration purposes */
border: solid 3px #c00;
background: #fdd;
}
#inner2 {
overflow: hidden;/* Make this div take up the rest of the horizontal space, and no more */
/* Colours and borders for illustration purposes */
border: solid 3px #00c;
background: #ddf;
}
</style>
<!--[if lte IE 6]>
<style type="text/css">
#inner2 {
zoom: 1;/* Make this div take up the rest of the horizontal space, and no more, in IE 6 */
}
#inner1 {
margin-right: -3px;/* Fix the 3-pixel gap that the previous rule introduces. (Shit like this is why web developers hate IE 6.) */
}
</style>
<![endif]-->
Tested and working in IE 6, 7, and 8; Firefox 3.5; and Chrome 4.
If you're reading this now you can probably use calc, so be thankful.
HTML
<div class="universe">
<div class="somewidth">
</div>
<div class="everythingelse">
</div>
</div>
CSS
.universe {
width: 100%;
height: 100%;
}
.somewidth {
width: 200px;
height: 100%;
}
.everythingelse {
width: 800px; /* fallback for emergencies */
width: calc(100% - 200px);
width: -moz-calc(100% - 200px);
width: -webkit-calc(100% - 200px);
height: 100%;
}
See the working example on JSFiddle.
You would need to float the inner1 div to the left, like so:
<div id="#outer" ....>
<div id='#inner1" style="float:left; border: 1px solid #000;">
blabla
</div>
<div id="#inner2" style="... DON'T USE WIDTH AND DISPLAY HERE! ...">
gnihihi
</div>
</div>
This should do the trick. Check it out!
bye
You do not need to use div for nested element, just use SPAN like this
<div>
<span style="display:inline-block;width: auto;border: solid 1px black;">
hey you
</span>
<span style="display:inline-block;marging: 0px 2px;border: solid 1px black;">
always use proper tools.
</span>
</div>
Expanding on #Nasser Hajloo's answer, this works for me (even in IE6)
<div style="width: 400px; border: solid 1px red;">
<span style="float:left;width: auto;border: solid 1px black;">
hey you
</span>
<div style="display:inline-block;margin: 0px 2px;border: solid 1px black;">always use proper tools.</div>
</div>
Try it with the main div smaller than 400px to see how it adjusts. (It also works with divs rather than spans - the key is the width: auto in the first div/span.)
Try this: nest inner1 inside inner2, and remove the display:inline from inner2, like this:
<div id="#outer" style="width:100%; border:1px solid red">
<div id="#inner2" style="width:100%; border:1px solid black;">
<div id="#inner1" style="border:1px solid blue; display:inline">
inner div 1. Some text...
</div>
inner div 2...
</div>
</div>
You can see it working here: http://jsbin.com/adiwi
From your code it looks like you are trying to get a horizontal line to fill the empty space in your div. If I'm correct your looking to create a visual effect with markup. Correct me if I'm wrong.
(Would be nice to see an image of what you want)
Example:
Title ---------------------------
or
Title: Caption ------------------
This is not best practice. You should try to get this effect with CSS.
Try making your code more semantic first:
<div id="#outer" style="width:100%; border:1px">
<h3 style="border:1px; display:inline">
Caption
</h3>
</div>
To get the line:
create an image with the color you
want
make its height the same that you
want the line to be in px
position it with the background
property
.
#outer h3 {
display: inline;
background-color: #000;
color: #FFF;
}
#outer {
width: 100%; /* is the default of block element but just for celerity */
background: #000 url('image path') center left; /* position the image */
}
Your first problem is that you are prefixing your ids with a '#'. The # is only used in CSS to refer to the element with that id, e.g. the CSS rule #outer{width:100%} refers to your element:
<div id="outer"></div>
Also you don't need to use width's on div's (or any other block elements) that aren't floated, as they already automatically take up 100% of the available width.
If you want to the 2 DIVs to appear on the same line you have to float the first one to the left. The adjacent DIV will then appear on the side, again you don't need to sepecify widthd for the second element. Here is your complete example including a different coloured border for each div.
I've made the borders bigger so you can see clearer whats going on.
<html><body>
<style type="text/css">
#outer {
border: solid 5px #c00;
}
#inner1 {
border: solid 5px #0c0;
float: left;
width: 200px;
height: 300px;
}
#inner2 {
border: solid 5px #00c;
height: 300px;
margin-left: 210px; /* 200px left width + 2 x 5px borders */
}
</style>
<div id="outer">
<div id="inner1">
inner div 1. Some text...
</div>
<div id="inner2">
inner div 2...
</div>
</div>
</body></html>
Another solution is to run a javascript which resizes the captionLine class when document has loaded like this.
Took some time to get it working under IE8, have not tried IE7 but should work.
2 things to note.
IE does not support getElementsByClassName, therefor this function is rewritten.
IE handles margins differently when objects are resized and moved with style.marginLeft, somehow IE seems to keep the margin in the class declaration and adds this to the new style.margin.
<body onload="resizeCaptionLine()">
<style>
caption {
border: 1px solid blue;
padding: 0px;
}
.captionText {
border: 1px solid red;
float: left;
}
.captionLine {
background-color:black;
margin: 0px;
margin: 5px 0px 0px 5px;
border: 0px;
padding: 0px;
padding-top: 1px;
}
</style>
<table style="width:300px;">
<caption width="100%" name="caption1">
<div class="captionText">Some text</div>
<div class="captionLine"> </div>
</caption>
<tr>
<td>something</td>
</tr>
</table>
<table style="width:300px;">
<caption width="100%" name="caption2">
<div class="captionText">Some text</div>
<div class="captionLine"> </div>
</caption>
<tr>
<td>something</td>
</tr>
</table>
<script type="text/javascript">
function getElementsByClassName(node, class_name) {
elems = node.all || node.getElementsByTagName('*');
var arr = new Array();
for(j = 0; j < elems.length; j++)
{
if (elems[j].className == class_name)
arr[arr.length] = elems[j];
}
return arr;
}
function resizeCaptionLine()
{
var elems = getElementsByClassName(document, 'captionLine');
for(i = 0; i < elems.length ; i++)
{
var parent = elems[i].parentNode;
var sibling = getElementsByClassName(parent, 'captionText');
var width = parent.offsetWidth - sibling[0].offsetWidth;
if(elems[i].currentStyle)
{
var currentMargin = elems[i].currentStyle.marginLeft;
var margin = parseInt(currentMargin.substr(0,currentMargin.length-2));
elems[i].style.marginLeft = (sibling[0].offsetWidth) + "px";
}
else if (document.defaultView && document.defaultView.getComputedStyle)
{
var currentStyle = document.defaultView.getComputedStyle(elems[i], '');
var currentMargin = currentStyle.marginLeft;
var margin = parseInt(currentMargin.substr(0,currentMargin.length-2));
elems[i].style.marginLeft = (sibling[0].offsetWidth + margin) + "px";
}
else
{
var currentMargin = elems[i].style.marginLeft;
var margin = parseInt(currentMargin.substr(0,currentMargin.length-2));
elems[i].style.marginLeft = (sibling[0].offsetWidth) + "px";
}
elems[i].style.width = (width - margin)+"px";
}
}
</script>
</body>
Answer is really simple! If you have fixed div (menu) on the left side, then give fixed div float: left and your right flexible div margin-left that is bigger then width of first fixed div.