I have a list of n items.
I'm trying to align the a element having child img, next to sibling p element.
I've tried setting both to display:inline-block, but I don't see any change. My code looks like this:
<li>
<h3>Header 3</h3>
<img src="example.img" alt="">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc vulputate interdum lacus in hendrerit. Donec libero metus, maximus sit amet.</p>
</li>
My goal is to create a sort of panel where the a element is to the left with the text next to it, and the h3 centered in that same panel.
<li>
<h3>Header 3</h3>
<a href="#">
<img src="example.img" alt="">
</a>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc vulputate interdum lacus in hendrerit. Donec libero metus, maximus sit amet.</p>
</li>
I didn't understand what your question goal. But, tell me if my answer was wrong :)
li{
list-style: none;
}
h3{
text-align: center;
}
img{
float: left;
}
p{
text-align: right;
}
<li>
<h3>Header 3</h3>
<img src="example.img" alt="">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc vulputate interdum lacus in hendrerit. Donec libero metus, maximus sit amet.</p>
</li>
use display:inline-block in a and p and set some width, use text-align:center in h3
body,
h3,
p {
margin: 0;
font-size:16px /* restore font-size inline-block fix*/
}
h3 {text-align: center}
ul {
margin: 0;
padding: 0;
font-size: 0
/*inline-block fix gap*/
}
li {
background: red;
}
a,
li,
p {
display: inline-block;
vertical-align: top;
width: 50%
}
img {
width: 100%;
display: block
}
<ul>
<li>
<h3>Header 3</h3>
<a href="#">
<img src="//lorempixel.com/100/100" alt="">
</a>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc vulputate interdum lacus in hendrerit. Donec libero metus, maximus sit amet.</p>
</li>
<li>
<h3>Header 3</h3>
<a href="#">
<img src="//lorempixel.com/100/100" alt="">
</a>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc vulputate interdum lacus in hendrerit. Donec libero metus, maximus sit amet.</p>
</li>
</ul>
Related
I have three columns inside a section tag in HTML but they sit on top of each other instead of standing next to eachother.
<section class="sec-top">
<div style="background-color:#f4f5f9">
<div class="column">
<h2 style="padding-left: 100px; padding-top:40px; color:#3d7cc9;">Lorem ipsum dolor sit amet.</h2>
<p style="padding-left:100px;">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla ac pharetra turpis. Sed vitae orci.
</p>
<ul class="checkmark">
<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</li>
<li>Duis eu lorem ut risus sollicitudin bibendum.</li>
<li>uspendisse malesuada augue sit amet ipsum laoreet, eget consequat purus consequat.</li>
</ul>
</div>
<div class="column">
<img class="gify" src="../../assets/apps.gif" />
</div>
<div class="column">
<div class="backgroundBlue">
<h2 class="ptop">Lorem Ipsum</h2>
<h3>Lorem ipsum dolor sit amet.s</h3>
<iframe width="90%" frameborder="0"></iframe>
</div>
</div>
</div>
</section>
I am not sure what I am doing wrong or why this is happening. Any help is appreciated. Thanks!
A div is a block-level element. That means that even if you reduce it's width it will still cause a new line to be created:
A block-level element always starts on a new line and takes up the
full width available (stretches out to the left and right as far as it
can). https://developer.mozilla.org/en-US/docs/Web/HTML/Block-level_elements
See also: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flow_Layout/Block_and_Inline_Layout_in_Normal_Flow
You need to either change the display value of .column (or its parent) or remove it from the normal flow with a float.
See examples below.
In the first example, I added float:left to the column class. This removes each column from the normal flow and they are stacked to the left of the screen.
In the second example, I changed the container div to the flexbox model and added flex: 1 1 33% to each column. This will cause each column to always take up 33% of the width of the parent. Here is a great resource on flexbox: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
.column {
width: 33%;
float: left;
}
.clearfix::after {
content: "";
display: block;
clear: both;
}
.gify {
max-width: 100%;
}
.flex-parent {
display: flex;
flex-direction:row;
}
.flex-parent>.column {
flex: 1 1 33%;
float: none;
}
<section class="sec-top">
<strong>Using Float</strong>
<div style="background-color:#f4f5f9;" class="clearfix">
<div class="column">
<h2 style="padding-left: 100px; padding-top:40px; color:#3d7cc9;">Lorem ipsum dolor sit amet.</h2>
<p style="padding-left:100px;">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla ac pharetra turpis. Sed vitae orci.
</p>
<ul class="checkmark">
<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</li>
<li>Duis eu lorem ut risus sollicitudin bibendum.</li>
<li>uspendisse malesuada augue sit amet ipsum laoreet, eget consequat purus consequat.</li>
</ul>
</div>
<div class="column">
<img class="gify" src="https://via.placeholder.com/450" />
</div>
<div class="column">
<div class="backgroundBlue">
<h2 class="ptop">Lorem Ipsum</h2>
<h3>Lorem ipsum dolor sit amet.s</h3>
<iframe width="90%" frameborder="0"></iframe>
</div>
</div>
</div>
</section>
<section class="sec-top" style="margin-top:30px">
<strong>Using FlexBox</strong>
<div style="background-color:#f4f5f9;" class="flex-parent">
<div class="column">
<h2 style="padding-left: 100px; padding-top:40px; color:#3d7cc9;">Lorem ipsum dolor sit amet.</h2>
<p style="padding-left:100px;">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla ac pharetra turpis. Sed vitae orci.
</p>
<ul class="checkmark">
<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</li>
<li>Duis eu lorem ut risus sollicitudin bibendum.</li>
<li>uspendisse malesuada augue sit amet ipsum laoreet, eget consequat purus consequat.</li>
</ul>
</div>
<div class="column">
<img class="gify" src="https://via.placeholder.com/450" />
</div>
<div class="column">
<div class="backgroundBlue">
<h2 class="ptop">Lorem Ipsum</h2>
<h3>Lorem ipsum dolor sit amet.s</h3>
<iframe width="90%" frameborder="0"></iframe>
</div>
</div>
</div>
</section>
I have to display span tag content in one line inside li tag. Would you help me in this?
I am getting output like this.
I need output like this.
ul {
list-style: none;
}
ul li {
display: inline;
}
ul li h2 {
display: inline-block;
}
<ul>
<li>
<h2>Lorem ipsum dolor sit</h2>
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam tellus nisl, molestie vitae nibh nec, dictum dignissim dui.Lorem </span>
</li>
<li>
<h2>Lorem ipsum dolor sit</h2>
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam tellus nisl, molestie vitae nibh nec, dictum dignissim dui.Lorem </span>
</li>
</ul>
You have to provide display: inline-block to your li, h2 and span.
Give your span appropriate width so that it gets accommodated within the space between both the li.
To view in full screen, check fiddle: https://jsfiddle.net/nashcheez/s4tqwcmr/
Refer code:
ul {
list-style: none;
}
ul li {
display: inline-block;
width: 49%;
}
ul li h2 {
display: inline-block;
margin: 0;
height: 100%;
vertical-align: top;
}
span {
display: inline-block;
width: 110px;
}
<ul>
<li>
<h2>Lorem ipsum dolor sit</h2>
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam tellus nisl, molestie vitae nibh nec, dictum dignissim dui.Lorem </span>
</li>
<li>
<h2>Lorem ipsum dolor sit</h2>
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam tellus nisl, molestie vitae nibh nec, dictum dignissim dui.Lorem </span>
</li>
</ul>
Add inline-block to your li this will let them have a specific width and not acting like an inline element...
ul
{
list-style: none;
}
ul li{
display: inline-block;
width: 40%;
margin: 10px;
}
ul li h2{
display: inline-block;
width:40%;
vertical-align: top;
}
ul li span{
display:inline-block;
width:39%;
vertical-align: top;
}
ul li button{
display: inline-block;
width: 19%;
min-width: 60px;
vertical-align: top;
}
<ul>
<li>
<h2>Lorem ipsum dolor sit</h2>
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam tellus nisl, molestie vitae nibh nec, dictum dignissim dui.Lorem </span>
<button>button</button>
</li>
<li>
<h2>Lorem ipsum dolor sit</h2>
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam tellus nisl, molestie vitae nibh nec, dictum dignissim dui.Lorem </span>
<button>button</button>
</li>
</ul>
ul
{
list-style: none;
}
ul li{
width: 40%;
display: -webkit-inline-box;
}
ul li h2{
display: inline-block;
}
<ul>
<li>
<h2>Lorem ipsum dolor sit</h2>
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam tellus nisl, molestie vitae nibh nec, dictum dignissim dui.Lorem </span>
</li>
<li>
<h2>Lorem ipsum dolor sit</h2>
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam tellus nisl, molestie vitae nibh nec, dictum dignissim dui.Lorem </span>
</li>
</ul>
I want to position a text and image like this:
This is what I have now
http://jsfiddle.net/vzovk7xd/
<section class="feature">
<div class="container">
<article class="feature-item design">
<h2>Web Design</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse rhoncus, lacus ac commodo volutpat, felis odio pulvinar enim, non dapibus mauris nulla sed ipsum.</p>
READ MORE
</article>
<article class="feature-item media">
<h2>Social Media</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse rhoncus, lacus ac commodo volutpat, felis odio pulvinar enim, non dapibus mauris nulla sed ipsum.</p>
READ MORE
</article>
<article class="feature-item apps">
<h2>Mobile Apps</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse rhoncus, lacus ac commodo volutpat, felis odio pulvinar enim, non dapibus mauris nulla sed ipsum.</p>
READ MORE
</article>
</div>
</section>
CSS
.feature {
margin-top: 100px;
text-align: center;
line-height: 25px;
}
.feature-item {
width: 33.3%;
float: left;
}
.design {
background: url(https://31.media.tumblr.com/avatar_b0a905e3eb32_128.png) no-repeat center center;
}
Don't set background image to your .design div, put another div between the heading and paragraph, give it width and height, and set background image to that div.
<article class="feature-item design">
<h2>Web Design</h2>
<div class="image"></div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse rhoncus, lacus ac commodo volutpat, felis odio pulvinar enim, non dapibus mauris nulla sed ipsum.</p>
READ MORE
</article>
css
.image { width: 100%; height: 140px; background: url(https://31.media.tumblr.com/avatar_b0a905e3eb32_128.png) no-repeat center center; }
https://jsfiddle.net/vzovk7xd/1/
add this css:
.design, .media, .apps {
background: url(https://31.media.tumblr.com/avatar_b0a905e3eb32_128.png) no-repeat top center;
background-position:75px 90px;
}
.feature-item p {margin-top:200px}
http://jsfiddle.net/vzovk7xd/6/
Don't put it as background (if you want to do that, you need to set width and height for .design box).. instead, add another div element between h2 and p elmenets:
<h2>Web Design</h2>
<div class="design"><img src="https://31.media.tumblr.com/avatar_b0a905e3eb32_128.png"/></div>
<p>Lorem ipsum dolor sit amet</p>
Working fiddle: http://jsfiddle.net/vzovk7xd/7/
You try to put an inline (in the elments flow order) element with the background style (out of the elements flow)... Better use an inline element to present it like an inline element.
Solution A: (modify CSS, remove background)
.feature-item h2:after {
content: url('https://31.media.tumblr.com/avatar_b0a905e3eb32_128.png');
margin: 24px 0 0 0;
display: inline-block;
}
http://jsfiddle.net/vzovk7xd/9/
Solution B: (modify HTML)
<article class="feature-item design">
<h2>Web Design</h2>
<figure></figure>
figure {
background: url(https://31.media.tumblr.com/avatar_b0a905e3eb32_128.png)
no-repeat center center;
width: 100px;
height: 100px;
}
http://jsfiddle.net/vzovk7xd/3/
I have tried coding the HTML layout in the image below, but as a result does not seem to be working correctly. Any help with the error would be appreciated.
HTML:
<header>
<h1>Header</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam pharetra rutrum tem <br> por. Curabitur at rhoncus orci. Donec ante velit, scelerisque vitae tincidunt sit amet.</p>
</header>
<section>
<h2>Heading</h2>
<p>Lorem ipsum dolor sit amet, consectetur. <br> Curabitur at rhoncus orci. Lorem ipsumm.</p>
<h2>Heading</h2>
<p>Lorem ipsum dolor sit amet, consectetur. <br> Curabitur at rhoncus orci. Lorem ipsumm.</p>
</section>
CSS:
body {
font-family: 'open sans';
text-align: center;
color: #333;
}
section {
display: block;
}
h1, h2 {
font-weight: 300;
}
Desired Layout:
You should split your section into two, by headings, so that you can float them separately:
<header>
<h1>Header</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam pharetra rutrum tem <br> por. Curabitur at rhoncus orci. Donec ante velit, scelerisque vitae tincidunt sit amet.</p>
</header>
<section>
<article>
<h2>Heading</h2>
<p>Lorem ipsum dolor sit amet, consectetur. <br> Curabitur at rhoncus orci. Lorem ipsumm.</p>
</article>
<article>
<h2>Heading</h2>
<p>Lorem ipsum dolor sit amet, consectetur. <br> Curabitur at rhoncus orci. Lorem ipsumm.</p>
</article>
</section>
And for your CSS:
body {
font-family: 'open sans';
text-align: center;
color: #333;
}
section {
display: block;
overflow: hidden;
}
section > article {
box-sizing: border-box;
float: left;
width: 50%;
}
h1, h2 {
font-weight: 300;
}
I recommend using box-sizing: border-box because it will not break the layout when you add padding to the <article> element.
See fiddle here - http://jsfiddle.net/teddyrised/XPGb7/
Wrap your content in left & right containers as:
<header>
<h1>Header</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam pharetra rutrum tem <br> por. Curabitur at rhoncus orci. Donec ante velit, scelerisque vitae tincidunt sit amet.</p>
</header>
<section>
<div class="left">
<h2>Heading</h2>
<p>Lorem ipsum dolor sit amet, consectetur. <br> Curabitur at rhoncus orci. Lorem ipsumm.</p>
</div>
<div class="right">
<h2>Heading</h2>
<p>Lorem ipsum dolor sit amet, consectetur. <br> Curabitur at rhoncus orci. Lorem ipsumm.</p>
</div>
</section>
And apply the following style
body {
font-family: 'open sans';
text-align: center;
color: #333;
}
section {
display: block;
}
h1, h2 {
font-weight: 300;
}
.left, .right{
width: 50%;
}
.left{
float: left;
}
.right {
float: right;
}
Working example:
http://jsbin.com/aragem/1/
You will need to wrap your section into two div blocks:
<section>
<div>
<h2>Heading</h2>
<p>Lorem ipsum dolor sit amet, consectetur.
<br>Curabitur at rhoncus orci. Lorem ipsumm.</p>
</div>
<div>
<h2>Heading</h2>
<p>Lorem ipsum dolor sit amet, consectetur.
<br>Curabitur at rhoncus orci. Lorem ipsumm.</p>
</div>
</section>
So you can apply the following style:
section > div {
display: inline-block;
width: 45%;
}
http://jsfiddle.net/samliew/8XJ7F/3/
this will work :
html :
<header>
<h1>Header</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam pharetra rutrum tem <br> por. Curabitur at rhoncus orci. Donec ante velit, scelerisque vitae tincidunt sit amet.</p>
</header>
<section>
<div class="left">
<h2>Heading</h2>
<p>Lorem ipsum dolor sit amet, consectetur. <br> Curabitur at rhoncus orci. Lorem ipsumm.</p>
</div>
<div class="right">
<h2>Heading</h2>
<p>Lorem ipsum dolor sit amet, consectetur. <br> Curabitur at rhoncus orci. Lorem ipsumm.</p>
</div>
</section>
css :
body {
font-family: 'open sans';
text-align: center;
color: #333;
}
section {
display: block;
}
h1, h2 {
font-weight: 300;
}
.left, .right{
width: 50%;
}
.left{
float: left;
}
.right {
float: right;
}
I have an ordered list which is actually a display of products on a page. Within each list item (li) there is some content followed by a div containing some more content, before closing the list item.
I need for the div within each list item to expand (its width) beyond its parent list item and actually fill the width of the ordered list (ol). Each div also needs to sit directly below its parent list item and push any following list items down.
I know the probably doesn't make sense, it's not very easy to explain.
Here's the HTML I have so far:
<ol class="products group">
<li>
<a href="#">
<img src="assets/img/ind-aerospace.jpg" align="" />
<h4>Product Title</h4>
</a>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum at auctor justo. Vivamus non elit velit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum porttitor blandit lacus in sodales.</p>
</li>
<li>
<a href="#">
<img src="assets/img/ind-automotive.jpg" align="" />
<h4>Product Title</h4>
</a>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum at auctor justo. Vivamus non elit velit. Vestibulum porttitor blandit lacus in sodales.</p>
<!-- Expand this -->
<div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum at auctor justo. Vivamus non elit velit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum porttitor blandit lacus in sodales.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum at auctor justo. Vivamus non elit velit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum porttitor blandit lacus in sodales.</p>
</div>
</li>
<li>
<a href="#">
<img src="assets/img/ind-power.jpg" align="" />
<h4>Product Title</h4>
</a>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum at auctor justo. Vivamus non elit velit. Vestibulum porttitor blandit lacus in sodales.</p>
</li>
<li>
<a href="#">
<img src="assets/img/ind-power.jpg" align="" />
<h4>Product Title</h4>
</a>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum at auctor justo. Vivamus non elit velit. Vestibulum porttitor blandit lacus in sodales.</p>
</li>
<li>
<a href="#">
<img src="assets/img/ind-power.jpg" align="" />
<h4>Product Title</h4>
</a>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum at auctor justo. Vivamus non elit velit. Vestibulum porttitor blandit lacus in sodales.</p>
</li>
<li>
<a href="#">
<img src="assets/img/ind-power.jpg" align="" />
<h4>Product Title</h4>
</a>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum at auctor justo. Vivamus non elit velit. Vestibulum porttitor blandit lacus in sodales.</p>
</li>
Here's my CSS:
ol.products {
position: relative;
}
ol.products li {
list-style: none;
float: left;
width: 30%;
margin: 0 3% 1.5em 0;
border-bottom: dotted 1px #ed2124;
border-bottom: dotted 1px rgba(237,33,36,.5);
}
ol.products li p {
margin: .5em 0;
min-height: 140px;
line-height: 1.2em;
}
ol.products li div {
position: relative;
width: 100%;
border: solid 1px red;
}
ol.products li div p {
min-height: 0;
}
Here's a wireframe of the list that might help it make a little sense:
You should use jQuery to accomplish this. You can add a "expandable" class to each of the info boxes and in the css have it display:none; then when you click on the parent info button use jQuery to position the box as you need it to (and return the display to visible, add animation, fade, etc). You will need to have jQuery traverse your html and place the info box in the correct area.
I tried something similar with css hover states, you can see my results here:
CSS Popouts
It's similar because I have a nested div that's set to display:none by default and is set to display:block by a hover anywhere on the list item.
The div floats overtop the other content though, rather than pushing it out of the way like your wireframes seem to indicate. You might have to get some JS going for you like #JohnP and #Collin White mentioned.