How do I add an image over background colour? - html

How do I add an image over background colour? I want the image the to at the bottom of the div. I tried it with the code below but it doesn't seem to work for me. See code below:
.teal {
text-align: center;
padding: 40px;
background-color: #2dd5c4;
background-image: url("https://www.w3schools.com/html/pic_trulli.jpg");
background-repeat: no-repeat;
background-position: bottom;
}
<div class="teal">
<h3>Lorem ipsum dolor sit amet</h3>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo l. igula eget dolor. Aenean massa. <br />
<button> Call to action</button></p>
</div>

Maybe try add the !important tag at the end of the background-image
background-image: url(https://www.w3schools.com/html/pic_trulli.jpg) !important;

you can put the image in a different div and put it over your teal class using z-index or having it as a child component to teal class
z-index
<div class='teal' >
<div class='image' > </div>
<div>

You can use this:
background-image: url();

Related

How to make a child div take up all the horizontal space when the parent has overflow: scroll

I want to have a child div with text on one line, with a background color that covers the whole line, inside a parent div with a max-width and be able to scroll along the x-axis.
The background color must be on the child element because the children(there are multiple children) will have different colors. All child elements must have the same width.
I only want to use CSS and no hard coded numbers on the child element.
My problem is that the child element doesn't cover the whole line, you can see the problem when you scroll horizontally.
To reproduce my issue:
.parent {
width: 400px;
max-width: 400px;
overflow: scroll;
}
.child {
white-space: nowrap;
background: skyblue;
}
<div class="parent">
<div class="child">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.
Aenean massa.
</div>
<div class="child">second child</div>
<div class="child">third child</div>
</div>
or https://codepen.io/alawiii521/pen/rNObXrm
You can achieve this by wrapping you text in a span like this and adding the color to span.
.parent {
width: 400px;
max-width: 400px;
overflow: scroll;
}
.child {
white-space: nowrap;
display:table-row;
background: skyblue;
}
<div class="parent">
<div class="child">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.
</div>
<div class="child">second child</div>
<div class="child">third child</div>
</div>
EDIT:
Just add display:table to child class, it will work

How to place two img next to each other with text below responsive

I'm making my second test HTML file from PSD file.
In this picture you may see my issue.
Could you please guide me how to sit two images next to each other which have text below?
Also I want it be responsive.
For example in large screens, the two images sit next each other. In small screens each image in one separate line.
Thanks a bunch
first have the image and text in a box like this:
<div class="contentBox">
<img>
<h3>some title</h3>
<p>some text</p>
</div>
then float those boxes.
.contentBox{
float:left;
}
I made a quick snippet to show you how you could use it:
#boxes{
text-align:center;
}
.contentBox {
display: inline-block;
width: 200px;
border: 1px solid black;
margin: 20px;
}
.contentBox img {
width: 100%;
}
.contentBox h3 {
margin: 5px;
}
.contentBox p {
text-align: justify;
margin: 5px;
}
<div id="boxes">
<div class="contentBox">
<img src="http://via.placeholder.com/350x250">
<h3>some title</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin facilisis mauris sem, in elementum tortor eleifend vel.</p>
</div>
<div class="contentBox">
<img src="http://via.placeholder.com/350x250">
<h3>some title</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin facilisis mauris sem, in elementum tortor eleifend vel.</p>
</div>
</div>

Repeating background not displaying

Ok, this is by far one of the strangest problems I have encountered in my css experience. I am trying to use some subtle patterns that repeat for a div on my webpage. Strange enough, whenever i try to set an pattern that is very white, the browser refuses to load the image. When I try to use a darker image, it works just fine. Now I know that the colour is obviously not the problem, but what am I missing. Here is the HTML/CSS:
#section1{
background-image: url("../images/as.png");
}
<div id="section1">
<div class="content98">
<h1 id="h1d">De ce matest.ro?</h1>
<div id="border">
<img class="show" id="show1" src="images/untitled-2.png">
<h2 id="dix">Obiective</h2>
<hr>
<div id="hided1"><p class="hiding">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer tellus lorem, feugiat ut condimentum ac, ultricies vitae nibh. Duis sed lacinia magna.>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>
<br><br>
</div>
First check your body background color white or change and see. Then Check in inspect element of browser in style box that your property is strikeout or not.
#section1{
background-image: url("../images/as.png");
}
And then add background-repeat:repeat; to your image like this.
#section1{
background-image: url("../images/as.png");
background-repeat:repeat;
}
even like this you can write
#section1{
background:url("../images/as.png") repeat;
}

Div - "fill the rest of the line" [duplicate]

My requirement is simple: 2 columns where the right one has a fixed size. Unfortunately I couldn't find a working solution, neither on stackoverflow nor in Google. Each solution described there fails if I implement in my own context. The current solution is:
div.container {
position: fixed;
float: left;
top: 100px;
width: 100%;
clear: both;
}
#content {
margin-right: 265px;
}
#right {
float: right;
width: 225px;
margin-left: -225px;
}
#right, #content {
height: 1%; /* fixed for IE, although doesn't seem to work */
padding: 20px;
}
<div class="container">
<div id="content">
fooburg content
</div>
<div id="right">
test right
</div>
</div>
I get the following with above code:
|----------------------- -------|
| fooburg content | |
|-------------------------------|
| | test right |
|----------------------- -------|
Please advise. Many thanks!
Remove the float on the left column.
At the HTML code, the right column needs to come before the left one.
If the right has a float (and a width), and if the left column doesn't have a width and no float, it will be flexible :)
Also apply an overflow: hidden and some height (can be auto) to the outer div, so that it surrounds both inner divs.
Finally, at the left column, add a width: auto and overflow: hidden, this makes the left column independent from the right one (for example, if you resized the browser window, and the right column touched the left one, without these properties, the left column would run arround the right one, with this properties it remains in its space).
Example HTML:
<div class="container">
<div class="right">
right content fixed width
</div>
<div class="left">
left content flexible width
</div>
</div>
CSS:
.container {
height: auto;
overflow: hidden;
}
.right {
width: 180px;
float: right;
background: #aafed6;
}
.left {
float: none; /* not needed, just for clarification */
background: #e8f6fe;
/* the next props are meant to keep this block independent from the other floated one */
width: auto;
overflow: hidden;
}​​
Example here: http://jsfiddle.net/jackJoe/fxWg7/
See http://www.alistapart.com/articles/negativemargins/ , this is exactly what you need (example 4 there).
<div id="container">
<div id="content">
<h1>content</h1>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Phasellus varius eleifend tellus. Suspendisse potenti. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Nulla facilisi. Sed wisi lectus, placerat nec, mollis quis, posuere eget, arcu.</p>
<p class="last">Donec euismod. Praesent mauris mi, adipiscing non, mollis eget, adipiscing ac, erat. Integer nonummy mauris sit amet metus. In adipiscing, ligula ultrices dictum vehicula, eros turpis lacinia libero, sed aliquet urna diam sed tellus. Etiam semper sapien eget metus.</p>
</div>
</div>
<div id="sidebar">
<h1>sidebar</h1>
<ul>
<li>link one</li>
<li>link two</li>
</ul>
</div>
#container {
width: 100%;
background: #f1f2ea url(background.gif) repeat-y right;
float: left;
margin-right: -200px;
}
#content {
background: #f1f2ea;
margin-right: 200px;
}
#sidebar {
width: 200px;
float: right;
Best to avoid placing the right column before the left, simply use a negative right-margin.
And be "responsive" by including an #media setting so the right column falls under the left on narrow screens.
<div style="background: #f1f2ea;">
<div id="container">
<div id="content">
<strong>Column 1 - content</strong>
</div>
</div>
<div id="sidebar">
<strong>Column 2 - sidebar</strong>
</div>
<div style="clear:both"></div>
<style type="text/css">
#container {
margin-right: -300px;
float:left;
width:100%;
}
#content {
margin-right: 320px; /* 20px added for center margin */
}
#sidebar {
width:300px;
float:left
}
#media (max-width: 480px) {
#container {
margin-right:0px;
margin-bottom:20px;
}
#content {
margin-right:0px;
width:100%;
}
#sidebar {
clear:left;
}
}
</style>
Simplest and most flexible solution so far it to use table display:
HTML, left div comes first, right div comes second ... we read and write left to right, so it won't make any sense to place the divs right to left
<div class="container">
<div class="left">
left content flexible width
</div>
<div class="right">
right content fixed width
</div>
</div>
CSS:
.container {
display: table;
width: 100%;
}
.left {
display: table-cell;
width: (whatever you want: 100%, 150px, auto)
}​​
.right {
display: table-cell;
width: (whatever you want: 100%, 150px, auto)
}
Cases examples:
// One div is 150px fixed width ; the other takes the rest of the width
.left {width: 150px} .right {width: 100%}
// One div is auto to its inner width ; the other takes the rest of the width
.left {width: 100%} .right {width: auto}
I'd like to suggest a yet-unmentioned solution: use CSS3's calc() to mix % and px units. calc() has excellent support nowadays, and it allows for fast construction of quite complex layouts.
Here's a JSFiddle link for the code below.
HTML:
<div class="sidebar">
sidebar fixed width
</div>
<div class="content">
content flexible width
</div>
CSS:
.sidebar {
width: 180px;
float: right;
background: green;
}
.content {
width: calc(100% - 180px);
background: orange;
}
And here's another JSFiddle demonstrating this concept applied to a more complex layout. I used SCSS here since its variables allow for flexible and self-descriptive code, but the layout can be easily re-created in pure CSS if having "hard-coded" values is not an issue.
This is a generic, HTML source ordered solution where:
The first column in source order is fluid
The second column in source order is fixed
This column can be floated left or right using CSS
Fixed/Second Column on Right
#wrapper {
margin-right: 200px;
}
#content {
float: left;
width: 100%;
background-color: powderblue;
}
#sidebar {
float: right;
width: 200px;
margin-right: -200px;
background-color: palevioletred;
}
#cleared {
clear: both;
}
<div id="wrapper">
<div id="content">Column 1 (fluid)</div>
<div id="sidebar">Column 2 (fixed)</div>
<div id="cleared"></div>
</div>
Fixed/Second Column on Left
#wrapper {
margin-left: 200px;
}
#content {
float: right;
width: 100%;
background-color: powderblue;
}
#sidebar {
float: left;
width: 200px;
margin-left: -200px;
background-color: palevioletred;
}
#cleared {
clear: both;
}
<div id="wrapper">
<div id="content">Column 1 (fluid)</div>
<div id="sidebar">Column 2 (fixed)</div>
<div id="cleared"></div>
</div>
Alternate solution is to use display: table-cell; which results in equal height columns.
Hey, What you can do is apply a fixed width to both the containers and then use another div class where clear:both, like
div#left {
width: 600px;
float: left;
}
div#right {
width: 240px;
float: right;
}
div.clear {
clear:both;
}
place a the clear div under left and right container.
I have simplified it : I have edited jackjoe's answer. The height auto etc not required I think.
CSS:
#container {
position: relative;
margin:0 auto;
width: 1000px;
background: #C63;
padding: 10px;
}
#leftCol {
background: #e8f6fe;
width: auto;
}
#rightCol {
float:right;
width:30%;
background: #aafed6;
}
.box {
position:relative;
clear:both;
background:#F39;
}
</style>
HTML:
<div id="container">
<div id="rightCol">
<p>Lorem ipsum dolor sit amet,consectetuer adipiscing elit. Phasellus varius eleifend. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.Phasellus varius eleifend.</p>
<p>Lorem ipsum dolor sit amet,consectetuer adipiscing elit. Phasellus varius eleifend. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.Phasellus varius eleifend.</p>
</div>
<div id="leftCol">
<p>Lorem ipsum dolor sit amet,consectetuer adipiscing elit. Phasellus varius eleifend. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.Phasellus varius eleifend.</p>
<p>Lorem ipsum dolor sit amet,consectetuer adipiscing elit. Phasellus varius eleifend. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.Phasellus varius eleifend.</p>
<p>Lorem ipsum dolor sit amet,consectetuer adipiscing elit. Phasellus varius eleifend. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.Phasellus varius eleifend.</p>
Lorem ipsum dolor sit amet,consectetuer adipiscing elit. Phasellus varius eleifend. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.Phasellus varius eleifend.
</div>
</div>
<div class="box">
<p>Lorem ipsum dolor sit amet,consectetuer adipiscing elit. Phasellus varius eleifend. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.Phasellus varius eleifend.</p>
<p>Lorem ipsum dolor sit amet,consectetuer adipiscing elit. Phasellus varius eleifend. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.Phasellus varius eleifend.</p>
<p>Lorem ipsum dolor sit amet,consectetuer adipiscing elit. Phasellus varius eleifend. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.Phasellus varius eleifend.</p>
</div>

background image with overlapping content depending on mobile or desktop view

Running into a layout issue between mobile/tablet and desktop.
For mobile: I need the hero image to be first, then the content to be beneath it. (sample here)
For tablet/desktop: I need the content to lay on top of the hero image. (sample here)
I've tried using a background image but found that adjusting the layout of the page effects the background image size and proportions
I've tried absolutely position one on top of the other but run into a scenario where I end up fiddling with the layout between views (using bootstrap) more than what should probably be manageable.
Would love any thoughts on how to produce this result.
Will try to provide code samples of what I've done but not sure it's relevant since none of it seems to produce the desired results.
Here's a quick example that I've created for you, please check:
#container {
background: red;
text-align: center;
padding: 20px 0px;
}
#image, #content {
display: inline-block;
width: 45%;
vertical-align: middle;
}
#image img {
width: 100%;
}
#media (max-width: 500px) {
#image, #content {
display: block;
width: 100%;
}
}
<div id="container"><div id="image"><img src="http://placehold.it/350x150" /></div><div id="content">Lorem ipsum dolor ismet. Lorem ipsum dolor ismet. Lorem ipsum dolor ismet. Lorem ipsum dolor ismet. Lorem ipsum dolor ismet. Lorem ipsum dolor ismet. Lorem ipsum dolor ismet. Lorem ipsum dolor ismet. Lorem ipsum dolor ismet. Lorem ipsum dolor ismet. Lorem ipsum dolor ismet. Lorem ipsum dolor ismet. Lorem ipsum dolor ismet. </div>
Click on Run Code Snippet and view the above snippet in Full Page and then reduce your browser window to 500px or smaller, you'll see that the content will automatically get under the image. Initially, both the <div> containing the image and the content are being displayed as inline-block but for screens smaller than 500px, we use #media-queries to change their display property to block which is why the content goes underneath the image.
Here's a CodePen example I set up to accomplish what you're asking: http://codepen.io/trevanhetzel/pen/pyCnf
Essentially, if you use an inline image as compared to a background image, you can set it's position to absolute at a certain media query breakpoint. With it's container positioned relative and the image's sibling (.hero-content) positioned relative, you can just float it right at that same breakpoint, change the width to however wide you want it, and it sits right on top of the image.
HTML
<div class="hero-contain">
<img src="http://lorempixel.com/1000/300/" class="hero-img">
<div class="hero-content">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In a enim eu risus accumsan venenatis. Donec venenatis nunc ac tellus tempor, ac rutrum lorem semper. Suspendisse pulvinar elit sed luctus pulvinar. Donec id ultricies dui. Pellentesque suscipit nulla maximus, mollis urna laoreet, tempor tortor.</p>
</div>
</div>
CSS
.hero-contain {
position: relative;
margin: 0 auto;
max-width: 1000px;
}
.hero-img {
max-width: 100%;
}
.hero-content {
position: relative;
background: rgba(255, 255, 255, .5);
padding: 20px;
}
#media (min-width: 800px) {
.hero-img {
position: absolute;
}
.hero-content {
float: right;
width: 45%;
margin: 20px;
}
}
Use media queries.
When width of window will be less, than your tablet/desktop content, switch media query and just display your divs block. For displaying them in row use display:inline-block;
Example. Try to resize result window.