Is there any possibility of creating two divs side by side in html without using css?
I could not find any solution and somehow believe, that this is not possible with pure html.
I have no chance to use css, therefore the only any solution with only html is required.
I think you misunderstand what it means to "do it without CSS". Remember that "CSS" effectively refers to the layout system used by every web-page in a modern web-browser (and by "modern" I mean: made since 2001).
So a web-page with no explicit style="" attributes, no <style> elements and no <link rel="stylesheet" /> elements is still using CSS: it still has the built-in user-agent stylesheet. If you use the now-obsolete presentational elements and attributes like <font> and border="" then those are converted into CSS rules by the browser. My point is that it's impossible to not use CSS.
...so I assume by "without CSS" you really mean "without an external stylesheet" or "without a separate <style> element", but in your comment reply you said that using <style> is acceptable, so I think you're confused about what your restrictions are:
Thank you for your reply Dai. Inline <style> works for me. How could it leads to solve the place two divs side by side? I do not want to do it without css, but i have to. It is all about to create an html file with plotly.js
Anyway, if you can use <style>, then just do something like this:
<style>
#parent {
display: flex;
}
#parent > div {
flex-basis: 50%;
flex-grow: 1;
flex-shrink: 1;
}
#left {
border: 1px solid red;
}
#right {
border: 1px solid blue;
}
</style>
<div id="parent">
<div id="left">
</div>
<div id="right">
</div>
</div>
And if you cannot use <style> but you can use inline style="" attributes, then you can still do this:
<div id="parent" style="display: flex;">
<div id="left" style="flex-basis: 50%; flex-grow: 1; flex-shrink: 1; border: 1px solid red;">
</div>
<div id="right" style="flex-basis: 50%; flex-grow: 1; flex-shrink: 1; border: 1px solid blue;">
</div>
</div>
If you cannot add your own <style> elements nor style="" attributes then your only remaining option for laying out elements is by abusing <table> - but I won't post an example using <table> because I don't want to encourage others to do so - and because I've never encountered a system that allowed arbitrary elements but did not allow at least for custom style="" attributes.
Related
Im sure this is crazy simple so I apologize for the silly question but I can't get a black vertical line to show up on my site. Here is the code I have:
'''html:
<body>
<img src="icon-01.png" class="logo">
<p class="textlogo">Rob Reyes</p>
<div id="vl"></div>
</body>'''
'''css:
.vl {
margin-left: 100px;
width::10px;
height:100px;
background-color: black;
}'''
stylesheet is linked and all other styling seems to be working. Whats going on here?
There are two colons after the width. There has to be one colon. So you have to remove the extra colon to work it properly.
And you are selecting class instead of id. Check your CSS selector.
Change in your CSS.
#vl {
background: black;
height: 100px;
width: 10px;
margin-left: 100px;
}
Or, change the HTML attribute.
<div class="vl"></div>
I got a table using flexbox and noticed one interesting feature: the br element can not do anything inside the flexbox.
Example:
.flexbox {
display: flex;
flex-wrap: wrap;
border: 2px solid red;
padding: 2px;
}
.item {
width: 50px;
height: 50px;
margin: 2px;
border: 2px solid blue;
}
.new-row, br {
display: block;
width: 100%;
height: 0px;
}
<p>Line break using div:</p>
<div class="flexbox">
<div class="item"></div>
<div class="item"></div>
<div class="new-row"></div>
<div class="item"></div>
<div class="item"></div>
</div>
<p>Line break using br:</p>
<div class="flexbox">
<div class="item"></div>
<div class="item"></div>
<br>
<div class="item"></div>
<div class="item"></div>
</div>
In the example, div and br share the same properties, but div transfers elements to the new line, and br does not.
Why?
The implementation of the br element in CSS is very well known to be quite the mystery. Different browsers support a different set of properties on the element, to varying degrees of effect (although all of them do support setting display: none to remove it from the layout at least). CSS itself acknowledges this oddity as early as CSS1, having dedicated an entire subsection to it, and even now in CSS3 it's still severely underspecified.
This peculiarity involving flexbox is not new; it has been known since 2014. Basically, in current implementations br does not generate a principal box, but is instead treated as part of a contiguous run of text as described in section 4 of the Flexbox spec, generating an anonymous flex item that cannot be styled (because it's anonymous). This is similar to an orphaned display: table-cell element causing an anonymous table box to be created around it, except you can at least still style the display: table-cell element — in the case of the br element, the style properties you apply have no effect, and the anonymous flex item is laid out with the defaults.
In this case, since br itself is (mostly) empty and it is not being accompanied by any other bare text within the flex container, this results in an anonymous flex item with no dimensions, making it seem as though the br element has vanished completely.
Back in 2014 the CSSWG resolved this "mystery" not by changing the Flexbox spec, but simply to add a special definition for the br element to css-display-3 to account for the behavior seen here. But no such definition exists in the current version of that spec, nor the FPWD (which was published after the resolution!), nor the HTML spec, nor anywhere else. Nevertheless, the definition looks like this in terms of the current css-display-3 spec (which does not define any new properties, just changes the definition of display):
br {
content: '\A';
display: contents;
white-space: pre;
}
... which means that the br element doesn't generate a principal box, but simply an anonymous inline box containing a single newline.
Because this definition is still missing from css-display-3, I wouldn't immediately consider it canon just yet. Even so, the behavior seen here is extremely unlikely to change considering this is how the br element has been for so long.
One other option when using <br> in your code if you just have a single column of elements is to use flex-direction: column.
.flexbox {
display: flex;
flex-wrap: wrap;
border: 2px solid red;
padding: 2px;
flex-direction: column;
}
.item {
width: 50px;
height: 50px;
margin: 2px;
border: 2px solid blue;
}
<p>Line break using br:</p>
<div class="flexbox">
<div class="item"></div>
<br>
<div class="item"></div>
</div>
Unlike Chrome, you must add whitespace ( ) after a <br> in order for it to work in Firefox.
By doing so, you will have the same result in both browsers.
Conlusion:
Works only in Chrome: <br><br><br>
Works in both Chrome and Firefox: <br> <br> <br>
I have 3 div-s inside a div, I want to put a picture in red area which have a 500px height, I want to fix red height area to 500px and want to stretch other div-s to fill the pages, I don't know hot fit that, also tests put 50% to each blue div-s but don't worked.
<html>
<head></head>
<body>
<div>
<div style="background-color: blue; height:100%"></div>
<div style="background-color: red;height:760px"></div>
<div style="background-color: blue;height:100%"></div>
</div>
</body>
</html>
My solution:
<style>
*, *:before, *:after {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
zoom: 1;
}
.main{
background: yellow;
height: 100%;
display: table;
min-width: 100%;
}
.box{
background: blue;
width: 100%;
height: 100%;
display: table;
}
.image{
background: red;
width: 100%;
height: 100px;
display: table-row;
}
</style>
<div class="main">
<div class="box">#1</div>
<div class="image">#2</div>
<div class="box">#1</div>
</div>
Easiest solution: Use a table (they are valid again for layout thanks to html5 and it's presentational role attribute)
Works in "modern" browsers (IE 8 at least): use css display table + display: table-cell etc to use table rendering on other elements
Works when you have javascript: Use javascript. This will lag behind most of the time and might file when the JS engine encounters an error, so it should be your last resort (or actually past that). Example (with explanation) can be found here: http://nicholasbarger.com/2011/08/04/jquery-makes-100-height-so-much-easier/
If it's only about a visual effect: Use a container div for the blue color and 100% height and then center the red one in it (again, multiple ways to achieve that, e.g. tables, table display + css vertical align, ...)
Regarding a correct HTML5 solution (using role attribute marks table as presentational according to W3C spec)
<table style="width:100%;height:100%;border-collapse:collapse" role="presentation">
<tr>
<td style="background-color: blue;"></td>
</tr>
<tr>
<td style="background-color: red;height:760px"></td>
</tr>
<tr>
<td style="background-color: blue;"></td>
</tr>
</table>
Complete working fiddle here (css part is also important for it to work correctly): http://jsfiddle.net/8jFan/
Additional info regarding HTML5 + Tables:
http://www.w3.org/TR/2011/WD-html5-20110525/tabular-data.html#table-layout-techniques
On one hand tables are disencouraged to fix layouting ("Tables should not be used as layout aids."), on the other hand the spec goes on to describe how to use tables for layouting. As tables are - in contrast to css alternatives - backwards compatible really far (they are used for most newsletters because of that), they still seem to be the better choice to me than css "display attribute hacks".
This question already has answers here:
Is there a CSS parent selector?
(33 answers)
Closed 2 years ago.
The community reviewed whether to reopen this question 5 months ago and left it closed:
Original close reason(s) were not resolved
Is it possible to define a CSS style for an element, that is only applied if the matching element contains a specific element (as the direct child item)?
I think this is best explained using an example.
Note: I'm trying to style the parent element, depending on what child elements it contains.
<style>
/* note this is invalid syntax. I'm using the non-existing
":containing" pseudo-class to show what I want to achieve. */
div:containing div.a { border: solid 3px red; }
div:containing div.b { border: solid 3px blue; }
</style>
<!-- the following div should have a red border because
if contains a div with class="a" -->
<div>
<div class="a"></div>
</div>
<!-- the following div should have a blue border -->
<div>
<div class="b"></div>
</div>
Note 2: I know I can achieve this using javascript, but I just wondered whether this is possible using some unknown (to me) CSS features.
The syntax for that is:
div:has(div.a) { border: solid 3px red; }
div:has(div.b) { border: solid 3px blue; }
As far as I'm aware, styling a parent element based on the child element is not an available feature of CSS. You'll likely need scripting for this.
It'd be wonderful if you could do something like div[div.a] or div:containing[div.a] as you said, but this isn't possible.
You may want to consider looking at jQuery. Its selectors work very well with 'containing' types. You can select the div, based on its child contents and then apply a CSS class to the parent all in one line.
If you use jQuery, something along the lines of this would may work (untested but the theory is there):
$('div:has(div.a)').css('border', '1px solid red');
or
$('div:has(div.a)').addClass('redBorder');
combined with a CSS class:
.redBorder
{
border: 1px solid red;
}
Here's the documentation for the jQuery "has" selector.
Basically, no. The following would be what you were after in theory:
div.a < div { border: solid 3px red; }
Unfortunately it doesn't exist.
There are a few write-ups along the lines of "why the hell not". A well fleshed out one by Shaun Inman is pretty good:
http://www.shauninman.com/archive/2008/05/05/css_qualified_selectors
Update. Many years later, :has has arrived, browser support is increasing.
https://developer.mozilla.org/en-US/docs/Web/CSS/:has
12 years is a long time.
On top of #kp's answer:
I'm dealing with this and in my case, I have to show a child element and correct the height of the parent object accordingly (auto-sizing is not working in a bootstrap header for some reason I don't have time to debug).
But instead of using javascript to modify the parent, I think I'll dynamically add a CSS class to the parent and CSS-selectively show the children accordingly. This will maintain the decisions in the logic and not based on a CSS state.
tl;dr; apply the a and b styles to the parent <div>, not the child (of course, not everyone will be able to do this. i.e. Angular components making decisions of their own).
<style>
.parent { height: 50px; }
.parent div { display: none; }
.with-children { height: 100px; }
.with-children div { display: block; }
</style>
<div class="parent">
<div>child</div>
</div>
<script>
// to show the children
$('.parent').addClass('with-children');
</script>
In my case, I had to change the cell padding of an element that contained an input checkbox for a table that's being dynamically rendered with DataTables:
<td class="dt-center">
<input class="a" name="constCheck" type="checkbox" checked="">
</td>
After implementing the following line code within the initComplete function I was able to produce the correct padding, which fixed the rows from being displayed with an abnormally large height
$('tbody td:has(input.a)').css('padding', '0px');
Now, you can see that the correct styles are being applied to the parent element:
<td class=" dt-center" style="padding: 0px;">
<input class="a" name="constCheck" type="checkbox" checked="">
</td>
Essentially, this answer is an extension of #KP's answer, but the more collaboration of implementing this the better. In summation, I hope this helps someone else because it works! Lastly, thank you so much #KP for leading me in the right direction!
I'd like to integrate a theme tag to my elements so they appear in diffrent colours. But since the css selectors have the same css specificity the latest overrides the earlier defined rule.
this is an example that shows my problem:
<div class="red">
<div class="box">This should be red</div>
<div class="yellow">
...
<div class="box">This should be yellow (nested in x levels under the div.yellow)</div>
...
</div>
and here my css
.box { width: 100px; height: 100px; }
.yellow { background-color: yellow; }
.red { background-color: red; }
the box should be listed somewhere, but as soon as it is a sub child of another color definition it should been overwritten.
thanks for any help!
You shouldn't really be doing things this way -- if your theme changes, then suddenly things with class yellow may actually be blue, for example. I would suggest finding a common way of naming things (even if it's just colour1, colour2, colour-highlight...) and then specifying those styles. You can then look into the way your pages are designed and make the rules more specific as necessary (either by using !important or by making the rule more specific, e.g. .colour1 becoming .box .colour1 or div.colour1).
Try:
.box { background-color: inherit; }
See:
http://jsbin.com/imube/edit
I don’t quite see the problem. Here’s what I get with that code:
alt text http://www.pauldwaite.co.uk/images/so/1905834.png
You probably need to use CSS's !important keyword eg:
.yellow { background-color: yellow !important;}