Displaying elements as block or inline elements
Over the last few chapters our focus was directly associated with items as we assumed that each item has pre-built characteristics. In this lecture, we will learn how to change the default layout rules of elements. By the end of this lesson, you will understand the differences between inline and block elements and know how to use this logic to define CSS layouts.
Video Transcript
00:00As we said, this lecture is about changing
00:03the way elements interact with their
00:05bounding areas of the document.
00:08In the last lecture, we saw how to manipulate an element
00:11and how its relationship to the next item would be.
00:14Is it gonna flow to the right?
00:15Will it flow to the left?
00:17Will it clear?
00:18In this lecture, we take it one step deeper
00:23where we ask, does this element,
00:25what type of rendering does this element have by default?
00:29But then this lecture, you'll understand what I mean,
00:30but a property such as a paragraph
00:33automatically breaks a line by default.
00:35It is a box, it is boxed.
00:38Contrary to that, a span tag or a B tag
00:42or a U tag or an A reference
00:45are all tags that don't actually change
00:48the boxing mechanisms of the page.
00:51Now, if you've already worked a little bit with HTML,
00:53if you've put a link inside of a paragraph,
00:56that link does not break the page.
00:58It stays within that paragraph.
01:00In other words, its natural layout is flowed
01:04part of the application.
01:06In this lecture, we're gonna learn how to change
01:08those defaults using CSS.
01:10So let's jump right into it.
01:11It's amazing and very powerful.
01:13Let's do it.
01:14Over the last few lectures,
01:16we already started working on layout in general
01:19and we wanna continue with that.
01:21And to do that, we're gonna talk about layouts
01:23that are black layouts versus inline elements.
01:27You know them already.
01:28As you know, HTML, such as an H2, is a black element.
01:33A paragraph is a black element.
01:35A div is a black element.
01:37But elements such as a span,
01:39so if I go ahead here and I just add here somewhere,
01:41let's say in the middle here,
01:42I'm just gonna add here a span
01:45and close that span,
01:47are considered inline elements.
01:50And there's a few of them, such as the B tag
01:52and many other tags that are considered content within
01:56that do not affect the layout at all
01:58or they do not affect that the way the page itself
02:00is rendered beyond maybe some visual changing of elements.
02:04And in those situations, in our case with a span,
02:07you wouldn't even see it exists.
02:09Now, the reason is as we said,
02:11some items are black elements,
02:14some lines are inline elements,
02:15some lines are even list items,
02:17which are their own unique characteristic.
02:21We could change those defaults anytime we want to.
02:24And literally, I could go ahead into my article,
02:26and, excuse me again, got a bit of a cough,
02:30and I could go to all my spans inside of my paragraph.
02:33And I'm just gonna go ahead and highlight that my spans
02:36will be easy for us to notice it.
02:38So I'm gonna go ahead and create a background color,
02:40set it to be yellow.
02:42And I'm gonna also set a color for my font,
02:44so it'll be easy for me to see the copy.
02:46I'm gonna set it to be black.
02:49And I'm gonna go ahead and also set a height to my span.
02:56Now, notice something very interesting.
02:57When I set my span to be 100 pixels,
03:00let's see what happens to our rendering.
03:02One is that even though I set my height to be 100 pixels,
03:05it did not have any meaning.
03:07The reason is is because it is an inline element.
03:10It does not control the layout of the remainder of the page.
03:13So even though I asked for it to be 100 pixels,
03:16it basically ignored my request.
03:19Now, if I go back into my application,
03:21and now go ahead and just say to my item,
03:23by the way, your display, from now on,
03:26instead of being inline, which is the default,
03:29I'm gonna go ahead, and for this item,
03:31by the way, for a span, for a paragraph,
03:32the default, as we said, is a block.
03:34So I'm gonna go ahead and say,
03:35hey, by the way, right now you're a block.
03:37You're not inline anymore.
03:41Once I convert it into a block,
03:42notice how now it breaks the line
03:44both on the top and the bottom,
03:45because it is a block.
03:47And it's taking and appreciating that 100 pixels in height
03:51that I requested it to be.
03:53Now, there's one more interesting layout option,
03:56which is not block and not inline,
04:00and it is called inline block.
04:02Now, an inline block contrary to a block
04:05is basically remains inside of the line that it's in,
04:10but respects the height and width that we define to it.
04:13So in this case, if it's an inline block,
04:15it's basically gonna lay itself out, define that size,
04:19but then the next line won't be able to render
04:21because this line now takes 100 pixels
04:23because of this item.
04:25So those are the three options really that we have
04:28when we're talking about really normal display objects
04:32between something that is inline blocked
04:34or an inline block.
04:36Now, I wanna talk about just one more display item option,
04:40which is list item.
04:42Now, there's a lot of other display items
04:45and I'm just gonna put here all the ones that we know about
04:48and we're gonna be talking about,
04:49as we said, about tables in a later chapter,
04:53and actually in a later title,
04:55and when we talk about tables,
04:57we're gonna go back to display and discover
04:59a lot more table related CSS driven tables.
05:04Now, in the display, there's also one more item,
05:06which is called list item.
05:07I'm just gonna show it to you for a second
05:09and then revert back, and really all it really does
05:12is converts that item, not only to be a display item,
05:15but it's now also one of those marker display items,
05:19but we are going to move on from that topic.
05:22We're not gonna stick to there,
05:23and before we wrap up this video,
05:26I really wanna show you another,
05:27some interesting things regarding layout.
05:30So, I'm gonna go ahead and I already pre-configured
05:33two images that are inside of my library,
05:36and I wanna put them right inside of my paragraph,
05:38and really what I want to happen is that
05:40for the copy to wrap itself around that image,
05:43so the image itself, as a default,
05:46that image will take itself its own block element,
05:50because it is a block element,
05:52and as a block element, it's going out,
05:54well, it's actually an inline block element, as we can see,
05:56and as an inline block element,
05:59it's going to basically have my image
06:01and then let the copy wrap around it.
06:03It's not gonna force the content around it to break a line.
06:06If I wanted to force it completely,
06:08I could go ahead there inside, create a CSS rule,
06:12I could go and create here another CSS rule for images
06:17that are inside of our paragraph,
06:18that are inside of our article,
06:20and in those cases, what I wanna do,
06:22let's say I wanna change my display to be a block,
06:28and when I set my display to be a block element,
06:31notice what would happen in the rendering.
06:33The copy will then be pushed out,
06:35it won't be part of the line,
06:36it won't be that inline,
06:37which was the default of our image,
06:40but if we go ahead and change it and say,
06:42I want you to be inline,
06:44then notice what would happen.
06:45If I set it to be inline,
06:46the copy would then return back,
06:48it would then appreciate our content.
06:50Now, the only problem that we have here with our inline
06:53is that we really want our copy to start all the way
06:56in the top of our image,
06:58and we can't really do that using just display options.
07:01We tried all three, all four really,
07:04and they just don't work,
07:06but we already learned how to do it
07:07because we know how to use the float command,
07:10and with the float command,
07:11I could say, hey, I want all the content
07:13that is inside, around this image,
07:15that the way this image relates the content out of it
07:18to float to its left,
07:19and by setting it to float to its left,
07:21I could literally have my image float with my copy,
07:25and by the way, I could auto reverse it,
07:27I could have it float to the right if I wanted to,
07:30and if I set the content to float to the right,
07:31then my content would right float with my image,
07:35but really what I want is to float to the left,
07:38and if I'm already added,
07:39I would love to put some padding here,
07:41so I would go ahead and put a padding of one size
07:44of our font, which would basically be that global font,
07:48that font size that we're in when we're in this area,
07:50which is, in this case, 14 pixels,
07:53but if we ever change it,
07:54we would change also the spacing,
07:56so it's gonna be correlated directly to that level
07:59of engagement with that specific paragraph,
08:02so if I go ahead here,
08:03it's gonna work really wonderfully,
08:05and now we have our images sitting there with our float,
08:07and I'll just go ahead to my item here
08:09and just make sure that it's also not,
08:11it's just an inline item, which is the default,
08:14just so for the sake of the visuals
08:16as we wrap up this item as well.
08:19So there we go, we just created and worked with
08:21and learned how to work with
08:23all the different critical display elements
08:25and how to change elements based on what we need them to be,
08:28and we also took a look at how
08:29we could leverage the float again
08:31to have it work for our advantage
08:33when we wanna create a beautiful image text combination.
