In: Computer Science
Question
Block elements v.s. inline elements.
<!DOCTYPE html>
<html>
<head>
<style>
body{
text-align: center;
}
.textdiv {
background-color: LightSlateGrey;
width: 50%;
}
.imgdiv {
background-color: PapayaWhip;
width: 30%;
}
</style>
</head>
<body>
<h2>Center Me</h2>
<p>"Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit..."</p>
<div class="textdiv">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
</div>
<img src="imgs/one.png" alt="">
<div class="imgdiv">
<img src="imgs/two.png" alt="">
</div>
</body>
</html>
Step 1. Review the difference between block element and inline element from the reference (Links to an external site.).
Step 2. The CSS text-align property is used for aligning the inner content of a block element.
To do: Uncomment line 6 in center-ex1.html. Check if all the Lorem Ipsum text on page is centered. Can you explain why some text is not centered. Fix this problem and center all text by adding a single CSS rule in the CSS class selector textdiv.
Step 3.
To center align an <img> element, which is an inline element, you can use different approaches.
Approach 1: Use text-align: center; to center the image within its parent container.
Approach 2: Turn the <img> into a block element by changing its display property from the default inline value to block: display: block; and then use margin: auto; so the image is centered within its parent container.
To do: Add a single rule in the CSS class selector. imgdiv to center the "number two" image on the page.
Step 1:
Every HTML element has a default display value depending on what type of element it is. The default display value fo rmost elements in block or inline.
Inline Element: it is a element which does not start on a new line and only takes up as much width as necessary. the inline element following tags include:
Block Element: it is a element which always starts on a new line and takes up the full width available. the block level element following tags include:
let us identify block element and inline element from the given html code:
<h2>Center Me</h2>
<p>"<big>N</big>eque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit..."</p>
<div class="textdiv">
Lorem ipsum dolor sit amet, <span>consectetur</span> adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
</div>
<img src="imgs/one.png" >
<div class="imgdiv">
<img src="imgs/two.png" >
</div>
From the code, <img>, <span> & <big> are inline element, where as <p>, <div> are block element.
Step 2: The class textdiv on the center alignment but in the css for texdiv the width is assign 50% for this reason text is in center but align on left on the page, here the code of css:
.textdiv {
background-color: LightSlateGrey;
width: 50%;
}
to need to be center alignment, remove the width from css textdiv then display will be at center of the page. here the final code of css
.textdiv {
background-color: LightSlateGrey;
}
Step 3: the img tag which is inline block display at center of page because in CSS, the center alignment is defined but the image two is not display at center because the image two is define in block element for that need to be define css for center alignment, here the css which was define:
.imgdiv {
background-color: PapayaWhip;
width: 30%;
}
for block element to be center element then need to add css margin to auto & width: 50%,
.imgdiv {
background-color: PapayaWhip;
margin: auto;
width: 50%;
}
The final html code with all changes is:
<!DOCTYPE html>
<html>
<head>
<style>
body{
text-align: center;
}
.textdiv {
background-color: LightSlateGrey;
}
.imgdiv {
background-color: PapayaWhip;
margin: auto;
width: 50%;
}
</style>
</head>
<body>
<h2>Center Me</h2>
<p>"Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit..."</p>
<div class="textdiv">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
</div>
<img src="imgs/one.png" >
<div class="imgdiv">
<img src="imgs/two.png" >
</div>
</body>
</html>